mirror of
https://github.com/sub-store-org/Sub-Store.git
synced 2026-11-28 07:24:12 +00:00
feat: 脚本操作可通过 context.process 按 customName 控制后续操作(前端 >= 2.17.16)
This commit is contained in:
4 files changed
+241
-1
No files matched your search
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "sub-store",
|
||||
"version": "2.23.20",
|
||||
"version": "2.23.21",
|
||||
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
|
||||
"main": "src/main.js",
|
||||
"packageManager": "pnpm@11.0.9",
|
||||
|
||||
@@ -109,6 +109,21 @@ function parse(raw) {
|
||||
});
|
||||
}
|
||||
|
||||
function shouldSkipByContext(item, context) {
|
||||
const control = context.process;
|
||||
if (!control || typeof control !== 'object') return false;
|
||||
|
||||
const { type, customNames } = control;
|
||||
if (!Array.isArray(customNames)) return false;
|
||||
|
||||
const matched =
|
||||
item.customName != null && customNames.includes(item.customName);
|
||||
|
||||
if (type === 'disable') return matched;
|
||||
if (type === 'enable') return !matched;
|
||||
return false;
|
||||
}
|
||||
|
||||
async function processFn(
|
||||
proxies,
|
||||
operators = [],
|
||||
@@ -128,6 +143,14 @@ async function processFn(
|
||||
);
|
||||
continue;
|
||||
}
|
||||
if (shouldSkipByContext(item, context)) {
|
||||
$.info(
|
||||
`Skipping context-controlled operator: "${
|
||||
item.type
|
||||
}" with customName "${item.customName || ''}"`,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
if (item.disabled) {
|
||||
$.log(
|
||||
`Skipping disabled operator: "${
|
||||
@@ -184,6 +207,14 @@ async function processResponseFn(
|
||||
let output = normalizeResponse(response);
|
||||
for (const item of operators) {
|
||||
if (!isResponseTransformerType(item.type)) continue;
|
||||
if (shouldSkipByContext(item, context)) {
|
||||
$.info(
|
||||
`Skipping context-controlled response transformer: "${
|
||||
item.type
|
||||
}" with customName "${item.customName || ''}"`,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
if (item.disabled) {
|
||||
$.log(
|
||||
`Skipping disabled response transformer: "${
|
||||
|
||||
@@ -0,0 +1,192 @@
|
||||
import { expect } from 'chai';
|
||||
import { describe, it } from 'mocha';
|
||||
|
||||
import { ProxyUtils } from '@/core/proxy-utils';
|
||||
|
||||
describe('Process context control', function () {
|
||||
it('disables later actions by customName from shared context', async function () {
|
||||
const operators = [
|
||||
{
|
||||
type: 'Script Operator',
|
||||
args: {
|
||||
mode: 'script',
|
||||
content: `function operator(proxies, targetPlatform, context) {
|
||||
context.process = {
|
||||
type: 'disable',
|
||||
customNames: ['branch-b'],
|
||||
};
|
||||
return proxies;
|
||||
}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'Script Operator',
|
||||
customName: 'branch-b',
|
||||
args: {
|
||||
mode: 'script',
|
||||
content: `function operator(proxies) {
|
||||
return proxies.map((proxy) => ({
|
||||
...proxy,
|
||||
name: 'B-' + proxy.name,
|
||||
}));
|
||||
}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'Script Operator',
|
||||
customName: 'branch-c',
|
||||
args: {
|
||||
mode: 'script',
|
||||
content: `function operator(proxies) {
|
||||
return proxies.map((proxy) => ({
|
||||
...proxy,
|
||||
name: proxy.name + '-C',
|
||||
}));
|
||||
}`,
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const output = await ProxyUtils.process([{ name: 'A' }], operators);
|
||||
|
||||
expect(output.map((proxy) => proxy.name)).to.deep.equal(['A-C']);
|
||||
});
|
||||
|
||||
it('enables only listed later actions by customName from shared context', async function () {
|
||||
const operators = [
|
||||
{
|
||||
type: 'Script Operator',
|
||||
args: {
|
||||
mode: 'script',
|
||||
content: `function operator(proxies, targetPlatform, context) {
|
||||
context.process = {
|
||||
type: 'enable',
|
||||
customNames: ['branch-c'],
|
||||
};
|
||||
return proxies;
|
||||
}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'Script Operator',
|
||||
customName: 'branch-b',
|
||||
args: {
|
||||
mode: 'script',
|
||||
content: `function operator(proxies) {
|
||||
return proxies.map((proxy) => ({
|
||||
...proxy,
|
||||
name: 'B-' + proxy.name,
|
||||
}));
|
||||
}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'Script Operator',
|
||||
customName: 'branch-c',
|
||||
args: {
|
||||
mode: 'script',
|
||||
content: `function operator(proxies) {
|
||||
return proxies.map((proxy) => ({
|
||||
...proxy,
|
||||
name: proxy.name + '-C',
|
||||
}));
|
||||
}`,
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const output = await ProxyUtils.process([{ name: 'A' }], operators);
|
||||
|
||||
expect(output.map((proxy) => proxy.name)).to.deep.equal(['A-C']);
|
||||
});
|
||||
|
||||
it('disables later response transformers by customName from shared context', async function () {
|
||||
const operators = [
|
||||
{
|
||||
type: 'Response Transformer',
|
||||
args: {
|
||||
mode: 'script',
|
||||
content: `function transformFunction(res, context) {
|
||||
context.process = {
|
||||
type: 'disable',
|
||||
customNames: ['branch-b'],
|
||||
};
|
||||
res.body += 'A';
|
||||
return res;
|
||||
}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'Response Transformer',
|
||||
customName: 'branch-b',
|
||||
args: {
|
||||
mode: 'script',
|
||||
content: `function transformFunction(res) {
|
||||
res.body += 'B';
|
||||
return res;
|
||||
}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'Response Transformer',
|
||||
customName: 'branch-c',
|
||||
args: {
|
||||
mode: 'script',
|
||||
content: `function transformFunction(res) {
|
||||
res.body += 'C';
|
||||
return res;
|
||||
}`,
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const output = await ProxyUtils.processResponse({ body: '' }, operators);
|
||||
|
||||
expect(output.body).to.equal('AC');
|
||||
});
|
||||
|
||||
it('enables only listed later response transformers by customName from shared context', async function () {
|
||||
const operators = [
|
||||
{
|
||||
type: 'Response Transformer',
|
||||
args: {
|
||||
mode: 'script',
|
||||
content: `function transformFunction(res, context) {
|
||||
context.process = {
|
||||
type: 'enable',
|
||||
customNames: ['branch-c'],
|
||||
};
|
||||
res.body += 'A';
|
||||
return res;
|
||||
}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'Response Transformer',
|
||||
customName: 'branch-b',
|
||||
args: {
|
||||
mode: 'script',
|
||||
content: `function transformFunction(res) {
|
||||
res.body += 'B';
|
||||
return res;
|
||||
}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'Response Transformer',
|
||||
customName: 'branch-c',
|
||||
args: {
|
||||
mode: 'script',
|
||||
content: `function transformFunction(res) {
|
||||
res.body += 'C';
|
||||
return res;
|
||||
}`,
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const output = await ProxyUtils.processResponse({ body: '' }, operators);
|
||||
|
||||
expect(output.body).to.equal('AC');
|
||||
});
|
||||
});
|
||||
@@ -319,6 +319,23 @@ function operator(proxies = [], targetPlatform, context) {
|
||||
|
||||
// context 为传入的上下文, 可在多个脚本中共享使用
|
||||
// 其中 env 为 环境信息, 包含运行版本和其他后端信息
|
||||
// 可通过 context.process 动态控制本次处理中后续 action 是否执行
|
||||
// 只按 customName 匹配, 只影响当前脚本之后的 action
|
||||
// type 为 disable 时, 跳过 customNames 中的后续 action
|
||||
// type 为 enable 时, 只执行 customNames 中的后续 action
|
||||
// 示例: 准备 A/B/C 三个脚本操作, A 根据请求 UA 决定后续使用 B 还是 C
|
||||
// if ($options?._req) {
|
||||
// const { headers, url, path } = $options?._req || {};
|
||||
// // 获取 user-agent
|
||||
// const ua = headers?.["user-agent"] || headers?.["User-Agent"];
|
||||
// console.log(`User-Agent: ${ua}`);
|
||||
// context.process = /Surge/i.test(ua)
|
||||
// ? { type: 'enable', customNames: ['branch-b'] }
|
||||
// : { type: 'enable', customNames: ['branch-c'] };
|
||||
// }
|
||||
// 脚本操作和修改响应(Response Transformer)各自有独立的 context.process 控制, 两者不互相跨
|
||||
// 脚本操作只控制后续非修改响应操作
|
||||
// 修改响应中设置 context.process 只控制后续修改响应操作
|
||||
|
||||
// 其中 source 为 订阅和组合订阅的数据, 有三种情况, 按需判断 (若只需要取订阅/组合订阅名称 直接用 `_subName` `_subDisplayName` `_collectionName` `_collectionDisplayName` 即可)
|
||||
|
||||
|
||||
Reference in new issue
Block a user