diff --git a/backend/package.json b/backend/package.json index a610d4b..bb33ea4 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.14.145", + "version": "2.14.146", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.", "main": "src/main.js", "scripts": { diff --git a/backend/src/core/proxy-utils/processors/index.js b/backend/src/core/proxy-utils/processors/index.js index 26cc161..e1c0b6e 100644 --- a/backend/src/core/proxy-utils/processors/index.js +++ b/backend/src/core/proxy-utils/processors/index.js @@ -316,11 +316,20 @@ function ScriptOperator(script, targetPlatform, $arguments, source) { await (async function () { const operator = createDynamicFunction( 'operator', - `async function operator(proxies = []) { - return proxies.map(($server = {}) => { - ${script} - return $server - }) + `async function operator(input = []) { + let proxies + if (Array.isArray(input)) { + proxies = input + return proxies.map(($server = {}) => { + ${script} + return $server + }) + } else { + let $content = input + ${script} + return $content + } + }`, $arguments, ); @@ -689,7 +698,10 @@ async function ApplyOperator(operator, objs) { ); let funcErr = ''; let funcErrMsg = `${err.message ?? err}`; - if (funcErrMsg.includes('$server is not defined')) { + if ( + funcErrMsg.includes('$server is not defined') || + funcErrMsg.includes('$content is not defined') + ) { funcErr = ''; } else { funcErr = `执行 function operator 失败 ${funcErrMsg}; `; diff --git a/backend/src/restful/file.js b/backend/src/restful/file.js index bf2db86..29a3738 100644 --- a/backend/src/restful/file.js +++ b/backend/src/restful/file.js @@ -3,6 +3,7 @@ import { FILES_KEY } from '@/constants'; import { failed, success } from '@/restful/response'; import $ from '@/core/app'; import { RequestInvalidError, ResourceNotFoundError } from '@/restful/errors'; +import { ProxyUtils } from '@/core/proxy-utils'; export default function register($app) { if (!$.read(FILES_KEY)) $.write([], FILES_KEY); @@ -40,13 +41,17 @@ function createFile(req, res) { success(res, file, 201); } -function getFile(req, res) { +async function getFile(req, res) { let { name } = req.params; name = decodeURIComponent(name); const allFiles = $.read(FILES_KEY); const file = findByName(allFiles, name); if (file) { - res.set('Content-Type', 'text/plain; charset=utf-8').send(file.content); + let content = file.content ?? ''; + content = await ProxyUtils.process(content, file.process || []); + res.set('Content-Type', 'text/plain; charset=utf-8').send( + content ?? '', + ); } else { failed( res, diff --git a/backend/src/restful/preview.js b/backend/src/restful/preview.js index 54e9329..04be134 100644 --- a/backend/src/restful/preview.js +++ b/backend/src/restful/preview.js @@ -9,6 +9,28 @@ import $ from '@/core/app'; export default function register($app) { $app.post('/api/preview/sub', compareSub); $app.post('/api/preview/collection', compareCollection); + $app.post('/api/preview/file', previewFile); +} + +async function previewFile(req, res) { + try { + let { content = '', process = [] } = req.body; + + const processed = await ProxyUtils.process(content, process || []); + + // produce + success(res, { original: content, processed }); + } catch (err) { + $.error(err.message ?? err); + failed( + res, + new InternalServerError( + `INTERNAL_SERVER_ERROR`, + `Failed to preview file`, + `Reason: ${err.message ?? err}`, + ), + ); + } } async function compareSub(req, res) { diff --git a/backend/src/restful/sync.js b/backend/src/restful/sync.js index 05a14c5..e1f137d 100644 --- a/backend/src/restful/sync.js +++ b/backend/src/restful/sync.js @@ -331,7 +331,10 @@ async function produceArtifact({ } else if (type === 'file') { const allFiles = $.read(FILES_KEY); const file = findByName(allFiles, name); - return file?.content ?? ''; + if (!file) throw new Error(`找不到文件 ${name}`); + let content = file.content ?? ''; + content = await ProxyUtils.process(content, file.process || []); + return content ?? ''; } }