Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ Sub-Store 后端的 Cloudflare Workers 移植版
- **完整功能**:复用原始后端全部业务逻辑(订阅管理、格式转换、下载、预览等)
- **预编译解析器**:peggy 文法在构建时编译,避免运行时 eval()

## 相较原仓库的新增功能

- **脚本过滤 / 操作支持**:通过 QuickJS WASM 沙箱在 Workers 运行时执行用户脚本,支持 Script Filter 和 Script Operator(含快捷脚本与函数式脚本),弥补了原仓库因 Workers 禁止 `eval` / `new Function` 而无法使用脚本操作的限制
- **脚本引擎可切换**:`wrangler.toml` 中通过 `[vars] SCRIPT_ENGINE` 控制脚本引擎,设为 `"quickjs"` 启用,未设置则禁用并返回引导提示
- **OpenAPI 适配层补全**:修复 Workers 版 `OpenAPI` 缺失 `warn` 方法导致解析订阅时 `I.warn is not a function` 报错的问题

## 目录

- [部署](#部署)(核心流程,建议从这里开始)
Expand Down Expand Up @@ -586,6 +592,7 @@ https://raw.githubusercontent.com/Yu9191/sub-store-workers/main/surge/SubStorePa
## 致谢

基于 [Sub-Store](https://github.com/sub-store-org/Sub-Store) 项目,感谢原作者及所有贡献者。
反馈请在 [LINUX DO](https://linux.do) 站内反馈

## 许可证

Expand Down
12 changes: 11 additions & 1 deletion esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ const evalRewritePlugin = {
contents = contents.replace(
/function createDynamicFunction\(name, script, \$arguments, \$options\) \{[\s\S]*?\n\}/,
`function createDynamicFunction(name, script, $arguments, $options) {
throw new Error('Script Operator is not supported in Cloudflare Workers because dynamic code execution through eval/new Function is disabled. Use built-in filters/operators, mihomo YAML patch, or an external trusted execution service.');
const engine = globalThis.__workerEnv?.SCRIPT_ENGINE;
if (engine === 'quickjs') {
const { createScriptFunction } = require('@/vendor/quickjs-executor');
return createScriptFunction(script, name, $arguments, $options);
}
throw new Error('Script Operator is not supported. Set SCRIPT_ENGINE="quickjs" in wrangler.toml [vars] to enable. Alternative: use built-in filters/operators, mihomo YAML patch, or an external trusted execution service.');
}`,
);
}
Expand Down Expand Up @@ -231,6 +236,7 @@ const nodeStubPlugin = {
'https',
'os',
'crypto',
'dgram',
];

for (const mod of stubs) {
Expand Down Expand Up @@ -274,6 +280,10 @@ const nodeStubPlugin = {
target: 'es2022',
outfile: path.join(__dirname, 'dist', 'worker.js'),
plugins: [aliasPlugin, peggyPrecompilePlugin, evalRewritePlugin, nodeStubPlugin],
loader: {
'.wasm': 'copy',
},
assetNames: '[name]',
define: {
'process.env.NODE_ENV': '"production"',
},
Expand Down
89 changes: 88 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"prepush": "node -e \"let f=require('fs'),t=f.readFileSync('wrangler.toml','utf8');if(!t.includes('你的KV命名空间ID')){console.error('\\x1b[31m请先清除 wrangler.toml 中的 KV ID!\\x1b[0m');process.exit(1)}\""
},
"dependencies": {
"@jitl/quickjs-wasmfile-release-sync": "^0.32.0",
"age-encryption": "^0.3.0",
"buffer": "^6.0.3",
"ip-address": "^9.0.5",
Expand All @@ -23,8 +24,10 @@
"ms": "^2.1.3",
"nanoid": "^3.3.3",
"peggy": "^2.0.1",
"quickjs-emscripten": "^0.32.0",
"semver": "^7.6.3",
"static-js-yaml": "^1.0.0"
"static-js-yaml": "^1.0.0",
"yaml": "^2.9.0"
},
"devDependencies": {
"esbuild": "^0.19.8",
Expand Down
7 changes: 5 additions & 2 deletions src/restful/miscs.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,11 @@ function getWorkerStatus(req, res) {
},
capabilities: {
scriptOperator: {
supported: false,
reason: 'Cloudflare Workers does not allow dynamic code execution through eval/new Function.',
supported: workerEnv.SCRIPT_ENGINE === 'quickjs',
engine: workerEnv.SCRIPT_ENGINE || 'none',
reason: workerEnv.SCRIPT_ENGINE === 'quickjs'
? 'Enabled via QuickJS WASM sandbox. Set SCRIPT_ENGINE="quickjs" in wrangler.toml [vars] to keep enabled.'
: 'Disabled. Set SCRIPT_ENGINE="quickjs" in wrangler.toml [vars] to enable.',
alternatives: [
'Use built-in filters/operators',
'Use mihomo YAML patch scripts',
Expand Down
4 changes: 4 additions & 0 deletions src/vendor/open-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ export class OpenAPI {
console.log(`[${this.name}] INFO: ${msg}`);
}

warn(msg) {
console.log(`[${this.name}] WARN: ${msg}`);
}

error(msg) {
console.log(`[${this.name}] ERROR: ${msg}`);
}
Expand Down
Loading