diff --git a/backend/package.json b/backend/package.json index f5e538c5..4a248994 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.36.19", + "version": "2.36.20", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.", "main": "src/main.js", "packageManager": "pnpm@11.0.9", diff --git a/backend/src/main.js b/backend/src/main.js index 594f6ec0..fef8c8af 100644 --- a/backend/src/main.js +++ b/backend/src/main.js @@ -22,5 +22,21 @@ console.log( import migrate from '@/utils/migration'; import serve from '@/restful'; +if ($.env.isNode) { + if (typeof Promise.withResolvers !== 'function') { + eval("require('core-js/actual/promise/with-resolvers')"); + } + + const workerThreads = eval("require('node:worker_threads')"); + if (typeof workerThreads.markAsUncloneable !== 'function') { + // ponytail: Node < 22 cannot mark web objects uncloneable; remove this fallback when the Android runtime reaches Node 22. + workerThreads.markAsUncloneable = () => {}; + } + + eval('process').on('uncaughtExceptionMonitor', (error, origin) => { + console.error(`[FATAL] ${origin}: ${error?.stack ?? error}`); + }); +} + migrate(); serve(); diff --git a/backend/src/test/utils/download.spec.js b/backend/src/test/utils/download.spec.js index 2a288c84..fcc7e9a3 100644 --- a/backend/src/test/utils/download.spec.js +++ b/backend/src/test/utils/download.spec.js @@ -208,6 +208,27 @@ describe('download github proxy regex', function () { expect(maxActiveRequests).to.equal(1); }); + it('logs download failures before rejecting', async function () { + openApi.HTTP = () => ({ + get: async () => { + throw new Error('request setup failed'); + }, + }); + + let error; + try { + await download('https://example.com/failing.txt'); + } catch (e) { + error = e; + } + + expect(error).to.be.instanceOf(Error); + expect(error.message).to.equal( + '无法下载 URL https://example.com/failing.txt: request setup failed', + ); + expect(errorLogs).to.deep.equal([error.message]); + }); + it('returns unpreprocessed raw content when requested', async function () { responseBody = [ 'proxies:', diff --git a/backend/src/test/vendor/open-api.spec.js b/backend/src/test/vendor/open-api.spec.js index 7e4f08cc..4e9961be 100644 --- a/backend/src/test/vendor/open-api.spec.js +++ b/backend/src/test/vendor/open-api.spec.js @@ -132,6 +132,31 @@ describe('open-api HTTP adapter', function () { }); }); + it('rejects request setup errors', async function () { + let timeoutCalls = 0; + const options = { + url: 'https://example.com/subscription', + timeout: 5, + events: { + onTimeout() { + timeoutCalls += 1; + }, + }, + }; + options.circular = options; + + let error; + try { + await HTTP().get(options); + } catch (e) { + error = e; + } + + expect(error).to.be.instanceOf(TypeError); + await new Promise((resolve) => setTimeout(resolve, 10)); + expect(timeoutCalls).to.equal(0); + }); + it('normalizes Node.js request header names to lowercase', async function () { await HTTP({ headers: { diff --git a/backend/src/utils/download.js b/backend/src/utils/download.js index 2bc6b5ae..cbdef52d 100644 --- a/backend/src/utils/download.js +++ b/backend/src/utils/download.js @@ -484,7 +484,9 @@ export default async function download( ); } } - throw new Error(`无法下载 URL ${safeUrl}: ${e.message ?? e}`); + const message = `无法下载 URL ${safeUrl}: ${e.message ?? e}`; + $.error(message); + throw new Error(message); } } diff --git a/backend/src/vendor/open-api.js b/backend/src/vendor/open-api.js index d9526670..6254d423 100644 --- a/backend/src/vendor/open-api.js +++ b/backend/src/vendor/open-api.js @@ -467,7 +467,7 @@ export function HTTP(defaultOptions = { baseURL: '' }) { opts: options.opts, }); } else if (isLoon || isSurge || isNode) { - worker = new Promise(async (resolve, reject) => { + const run = async (resolve, reject) => { const body = options.body; const opts = JSON.parse(JSON.stringify(options)); opts.body = body; @@ -627,6 +627,9 @@ export function HTTP(defaultOptions = { baseURL: '' }) { }, ); } + }; + worker = new Promise((resolve, reject) => { + run(resolve, reject).catch(reject); }); } else if (isGUIforCores) { worker = new Promise(async (resolve, reject) => { @@ -669,16 +672,14 @@ export function HTTP(defaultOptions = { baseURL: '' }) { }) : null; - return ( - timer - ? Promise.race([timer, worker]).then((res) => { - if (typeof clearTimeout !== 'undefined') { - clearTimeout(timeoutid); - } - return res; - }) - : worker - ).then((resp) => events.onResponse(resp)); + const request = timer ? Promise.race([timer, worker]) : worker; + return request + .finally(() => { + if (timer && typeof clearTimeout !== 'undefined') { + clearTimeout(timeoutid); + } + }) + .then((resp) => events.onResponse(resp)); } const http = {