Skip to content

Commit 533fffb

Browse files
committed
feat: 通过链接参数传入 $options 时, 保留默认的 _req 请求数据; 订阅输出也支持使用 $options._res.status/$options._res.headers 设置响应状态码/响应头
1 parent 6087d8c commit 533fffb

4 files changed

Lines changed: 56 additions & 29 deletions

File tree

backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sub-store",
3-
"version": "2.20.68",
3+
"version": "2.20.69",
44
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
55
"main": "src/main.js",
66
"scripts": {

backend/src/restful/download.js

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,23 @@ async function downloadSubscription(req, res) {
121121
},
122122
};
123123
if (req.query.$options) {
124+
let options = {};
124125
try {
125126
// 支持 `#${encodeURIComponent(JSON.stringify({arg1: "1"}))}`
126-
$options = JSON.parse(decodeURIComponent(req.query.$options));
127+
options = JSON.parse(decodeURIComponent(req.query.$options));
127128
} catch (e) {
128129
for (const pair of req.query.$options.split('&')) {
129130
const key = pair.split('=')[0];
130131
const value = pair.split('=')[1];
131132
// 部分兼容之前的逻辑 const value = pair.split('=')[1] || true;
132-
$options[key] =
133+
options[key] =
133134
value == null || value === ''
134135
? true
135136
: decodeURIComponent(value);
136137
}
137138
}
138-
$.info(`传入 $options: ${JSON.stringify($options)}`);
139+
$.info(`传入 $options: ${JSON.stringify(options)}`);
140+
Object.assign($options, options);
139141
}
140142
if (url) {
141143
$.info(`指定远程订阅 URL: ${url}`);
@@ -315,12 +317,21 @@ async function downloadSubscription(req, res) {
315317
req.query,
316318
);
317319
}
318-
res.set('Content-Type', 'application/json;charset=utf-8').send(
319-
output,
320-
);
320+
res.set('Content-Type', 'application/json;charset=utf-8');
321321
} else {
322-
res.send(output);
322+
res.set('Content-Type', 'text/plain; charset=utf-8');
323+
}
324+
if ($options?._res?.headers) {
325+
Object.entries($options._res.headers).forEach(
326+
([key, value]) => {
327+
res.set(key, value);
328+
},
329+
);
330+
}
331+
if ($options?._res?.status) {
332+
res.status($options._res.status);
323333
}
334+
res.send(output);
324335
} catch (err) {
325336
$.notify(
326337
`🌍 Sub-Store 下载订阅失败`,
@@ -386,21 +397,23 @@ async function downloadCollection(req, res) {
386397
},
387398
};
388399
if (req.query.$options) {
400+
let options = {};
389401
try {
390402
// 支持 `#${encodeURIComponent(JSON.stringify({arg1: "1"}))}`
391-
$options = JSON.parse(decodeURIComponent(req.query.$options));
403+
options = JSON.parse(decodeURIComponent(req.query.$options));
392404
} catch (e) {
393405
for (const pair of req.query.$options.split('&')) {
394406
const key = pair.split('=')[0];
395407
const value = pair.split('=')[1];
396408
// 部分兼容之前的逻辑 const value = pair.split('=')[1] || true;
397-
$options[key] =
409+
options[key] =
398410
value == null || value === ''
399411
? true
400412
: decodeURIComponent(value);
401413
}
402414
}
403-
$.info(`传入 $options: ${JSON.stringify($options)}`);
415+
$.info(`传入 $options: ${JSON.stringify(options)}`);
416+
Object.assign($options, options);
404417
}
405418

406419
if (proxy) {
@@ -578,12 +591,21 @@ async function downloadCollection(req, res) {
578591
req.query,
579592
);
580593
}
581-
res.set('Content-Type', 'application/json;charset=utf-8').send(
582-
output,
583-
);
594+
res.set('Content-Type', 'application/json;charset=utf-8');
584595
} else {
585-
res.send(output);
596+
res.set('Content-Type', 'text/plain; charset=utf-8');
597+
}
598+
if ($options?._res?.headers) {
599+
Object.entries($options._res.headers).forEach(
600+
([key, value]) => {
601+
res.set(key, value);
602+
},
603+
);
604+
}
605+
if ($options?._res?.status) {
606+
res.status($options._res.status);
586607
}
608+
res.send(output);
587609
} catch (err) {
588610
$.notify(
589611
`🌍 Sub-Store 下载组合订阅失败`,

backend/src/restful/file.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,23 @@ async function getFile(req, res) {
7777
},
7878
};
7979
if (req.query.$options) {
80+
let options = {};
8081
try {
8182
// 支持 `#${encodeURIComponent(JSON.stringify({arg1: "1"}))}`
82-
$options = JSON.parse(decodeURIComponent(req.query.$options));
83+
options = JSON.parse(decodeURIComponent(req.query.$options));
8384
} catch (e) {
8485
for (const pair of req.query.$options.split('&')) {
8586
const key = pair.split('=')[0];
8687
const value = pair.split('=')[1];
8788
// 部分兼容之前的逻辑 const value = pair.split('=')[1] || true;
88-
$options[key] =
89+
options[key] =
8990
value == null || value === ''
9091
? true
9192
: decodeURIComponent(value);
9293
}
9394
}
94-
$.info(`传入 $options: ${JSON.stringify($options)}`);
95+
$.info(`传入 $options: ${JSON.stringify(options)}`);
96+
Object.assign($options, options);
9597
}
9698
if (url) {
9799
$.info(`指定远程文件 URL: ${url}`);

scripts/demo.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ function operator(proxies = [], targetPlatform, context) {
5050
// 先这样处理 encodeURIComponent('arg1=a&arg2=b')
5151
// /api/file/foo?$options=arg1%3Da%26arg2%3Db
5252

53+
// 注意, 编辑页面左下角那个即可预览只是获取数据 并不是一个真实的请求, 故此时无法使用 $options
5354
// 默认会带上 _req 字段, 结构为
5455
// {
5556
// method,
@@ -63,27 +64,29 @@ function operator(proxies = [], targetPlatform, context) {
6364
// console.log($options)
6465

6566
// 若设置 $options._res.headers
66-
// 则会在输出文件时设置响应头, 例如:
67-
68-
// $options._res = {
69-
// headers: {
70-
// 'X-Custom': '1'
67+
// 则会在输出时设置响应头, 例如:
68+
// if ($options) {
69+
// $options._res = {
70+
// headers: {
71+
// 'X-Custom': '1'
72+
// }
7173
// }
7274
// }
7375

7476
// 若设置 $options._res.status
75-
// 则会在输出文件时设置响应状态码, 例如:
76-
77-
// $options._res = {
78-
// status: 404
77+
// 则会在输出时设置响应状态码, 例如:
78+
// if ($options) {
79+
// $options._res = {
80+
// status: 404
81+
// }
7982
// }
8083

8184
// 一个示例: 请求来自分享且 ua 不符合时, 返回自定义状态码和响应内容
8285

83-
// const { headers, url, path } = $options._req || {}
86+
// const { headers, url, path } = $options?._req || {}
8487
// const ua = headers?.['user-agent'] || headers?.['User-Agent']
8588

86-
// if (/^\/share\//.test(url) && !/surge/i.test(ua)) {
89+
// if ($options && /^\/share\//.test(url) && !/surge/i.test(ua)) {
8790
// $options._res = {
8891
// status: 418
8992
// }

0 commit comments

Comments
 (0)