fix: 预览时脚本下载报错导致的崩溃

This commit is contained in:
xream 2023-09-07 23:17:36 +08:00
parent 07b38cf971
commit 59bca5670d
No known key found for this signature in database
GPG Key ID: 1D2C5225471789F9
2 changed files with 108 additions and 84 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "sub-store", "name": "sub-store",
"version": "2.14.49", "version": "2.14.50",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js", "main": "src/main.js",
"scripts": { "scripts": {

View File

@ -12,98 +12,122 @@ export default function register($app) {
} }
async function compareSub(req, res) { async function compareSub(req, res) {
const sub = req.body; try {
const target = req.query.target || 'JSON'; const sub = req.body;
let content; const target = req.query.target || 'JSON';
if (sub.source === 'local') { let content;
content = sub.content; if (sub.source === 'local') {
} else { content = sub.content;
try { } else {
content = await download(sub.url, sub.ua);
} catch (err) {
failed(
res,
new NetworkError(
'FAILED_TO_DOWNLOAD_RESOURCE',
'无法下载远程资源',
`Reason: ${err}`,
),
);
return;
}
}
// parse proxies
const original = ProxyUtils.parse(content);
// add id
original.forEach((proxy, i) => {
proxy.id = i;
});
// apply processors
const processed = await ProxyUtils.process(
original,
sub.process || [],
target,
);
// produce
success(res, { original, processed });
}
async function compareCollection(req, res) {
const allSubs = $.read(SUBS_KEY);
const collection = req.body;
const subnames = collection.subscriptions;
const results = {};
await Promise.all(
subnames.map(async (name) => {
const sub = findByName(allSubs, name);
try { try {
let raw; content = await download(sub.url, sub.ua);
if (sub.source === 'local') {
raw = sub.content;
} else {
raw = await download(sub.url, sub.ua);
}
// parse proxies
let currentProxies = ProxyUtils.parse(raw);
// apply processors
currentProxies = await ProxyUtils.process(
currentProxies,
sub.process || [],
'JSON',
);
results[name] = currentProxies;
} catch (err) { } catch (err) {
failed( failed(
res, res,
new InternalServerError( new NetworkError(
'PROCESS_FAILED', 'FAILED_TO_DOWNLOAD_RESOURCE',
`处理子订阅 ${name} 失败`, '无法下载远程资源',
`Reason: ${err}`, `Reason: ${err}`,
), ),
); );
return;
} }
}), }
); // parse proxies
const original = ProxyUtils.parse(content);
// merge proxies with the original order // add id
const original = Array.prototype.concat.apply( original.forEach((proxy, i) => {
[], proxy.id = i;
subnames.map((name) => results[name] || []), });
);
original.forEach((proxy, i) => { // apply processors
proxy.id = i; const processed = await ProxyUtils.process(
}); original,
sub.process || [],
target,
);
const processed = await ProxyUtils.process( // produce
original, success(res, { original, processed });
collection.process || [], } catch (err) {
'JSON', $.error(err.message ?? err);
); failed(
res,
success(res, { original, processed }); new InternalServerError(
`INTERNAL_SERVER_ERROR`,
`Failed to preview subscription`,
`Reason: ${err.message ?? err}`,
),
);
}
}
async function compareCollection(req, res) {
try {
const allSubs = $.read(SUBS_KEY);
const collection = req.body;
const subnames = collection.subscriptions;
const results = {};
await Promise.all(
subnames.map(async (name) => {
const sub = findByName(allSubs, name);
try {
let raw;
if (sub.source === 'local') {
raw = sub.content;
} else {
raw = await download(sub.url, sub.ua);
}
// parse proxies
let currentProxies = ProxyUtils.parse(raw);
// apply processors
currentProxies = await ProxyUtils.process(
currentProxies,
sub.process || [],
'JSON',
);
results[name] = currentProxies;
} catch (err) {
failed(
res,
new InternalServerError(
'PROCESS_FAILED',
`处理子订阅 ${name} 失败`,
`Reason: ${err}`,
),
);
}
}),
);
// merge proxies with the original order
const original = Array.prototype.concat.apply(
[],
subnames.map((name) => results[name] || []),
);
original.forEach((proxy, i) => {
proxy.id = i;
});
const processed = await ProxyUtils.process(
original,
collection.process || [],
'JSON',
);
success(res, { original, processed });
} catch (err) {
$.error(err.message ?? err);
failed(
res,
new InternalServerError(
`INTERNAL_SERVER_ERROR`,
`Failed to preview collection`,
`Reason: ${err.message ?? err}`,
),
);
}
} }