From 69714590898713c90cf0f818697c75ed637b91a6 Mon Sep 17 00:00:00 2001 From: xream Date: Wed, 22 Jul 2026 15:34:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=20undici,=20fetch-so?= =?UTF-8?q?cks;=20=E4=BF=AE=E6=AD=A3=20throwOnMaxRedirect;=20=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=8F=82=E6=95=B0=E6=94=AF=E6=8C=81=E4=BC=A0=E5=85=A5?= =?UTF-8?q?=20proxyTunnel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/main.yml | 3 ++ backend/package.json | 6 ++-- backend/pnpm-lock.yaml | 16 ++++----- backend/src/test/utils/download.spec.js | 42 +++++++++++++++++++++++- backend/src/test/vendor/open-api.spec.js | 36 ++++++++++++++++++++ backend/src/utils/download.js | 2 +- backend/src/vendor/open-api.js | 4 ++- 7 files changed, 95 insertions(+), 14 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a824e548..c3a788aa 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -8,6 +8,9 @@ on: - master paths: - "backend/package.json" +concurrency: + group: build + cancel-in-progress: true jobs: build: runs-on: ubuntu-latest diff --git a/backend/package.json b/backend/package.json index 432b0ad1..32136277 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.36.17", + "version": "2.36.18", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.", "main": "src/main.js", "packageManager": "pnpm@11.0.9", @@ -34,7 +34,7 @@ "dotenv": "^16.4.7", "express": "^4.17.1", "fastestsmallesttextencoderdecoder": "^1.0.22", - "fetch-socks": "^1.3.2", + "fetch-socks": "^1.3.3", "http-proxy-middleware": "^3.0.3", "ip-address": "^9.0.5", "js-base64": "^3.7.2", @@ -45,7 +45,7 @@ "ms": "^2.1.3", "nanoid": "^3.3.3", "semver": "^7.6.3", - "undici": "^7.4.0", + "undici": "^8.8.0", "yaml": "^2.9.0" }, "devDependencies": { diff --git a/backend/pnpm-lock.yaml b/backend/pnpm-lock.yaml index 18c866c0..23e2f475 100644 --- a/backend/pnpm-lock.yaml +++ b/backend/pnpm-lock.yaml @@ -45,7 +45,7 @@ importers: specifier: ^1.0.22 version: 1.0.22 fetch-socks: - specifier: ^1.3.2 + specifier: ^1.3.3 version: 1.3.3 http-proxy-middleware: specifier: ^3.0.3 @@ -78,8 +78,8 @@ importers: specifier: ^7.6.3 version: 7.8.0 undici: - specifier: ^7.4.0 - version: 7.25.0 + specifier: ^8.8.0 + version: 8.8.0 yaml: specifier: ^2.9.0 version: 2.9.0 @@ -2592,9 +2592,9 @@ packages: undici-types@7.19.2: resolution: {integrity: sha512-qYVnV5OEm2AW8cJMCpdV20CDyaN3g0AjDlOGf1OW4iaDEx8MwdtChUp4zu4H0VP3nDRF/8RKWH+IPp9uW0YGZg==} - undici@7.25.0: - resolution: {integrity: sha512-xXnp4kTyor2Zq+J1FfPI6Eq3ew5h6Vl0F/8d9XU5zZQf1tX9s2Su1/3PiMmUANFULpmksxkClamIZcaUqryHsQ==} - engines: {node: '>=20.18.1'} + undici@8.8.0: + resolution: {integrity: sha512-ubshXMXwF3MQIMF1y/WxZdNBnjEKeSg2wF5mcGUtU55YTw34tnVVpKRlLf7ruDXZ5344KokPVX4RBx1wJm64Bw==} + engines: {node: '>=22.19.0'} unicode-canonical-property-names-ecmascript@2.0.1: resolution: {integrity: sha512-dA8WbNeb2a6oQzAQ55YlT5vQAWGV9WXOsi3SskE3bcCdM0P4SDd+24zS/OCacdRq5BkdsRj9q3Pg6YyQoxIGqg==} @@ -4277,7 +4277,7 @@ snapshots: fetch-socks@1.3.3: dependencies: socks: 2.8.9 - undici: 7.25.0 + undici: 8.8.0 file-entry-cache@6.0.1: dependencies: @@ -5500,7 +5500,7 @@ snapshots: undici-types@7.19.2: {} - undici@7.25.0: {} + undici@8.8.0: {} unicode-canonical-property-names-ecmascript@2.0.1: {} diff --git a/backend/src/test/utils/download.spec.js b/backend/src/test/utils/download.spec.js index 6fe6e1d6..2a288c84 100644 --- a/backend/src/test/utils/download.spec.js +++ b/backend/src/test/utils/download.spec.js @@ -13,6 +13,7 @@ import { let $; let openApi; let download; +let downloadFile; let resourceCache; let headersResourceCache; let originalRead; @@ -49,7 +50,7 @@ describe('download github proxy regex', function () { ({ default: headersResourceCache, } = require('@/utils/headers-resource-cache')); - ({ default: download } = require('@/utils/download')); + ({ default: download, downloadFile } = require('@/utils/download')); ageUtils = require('@/utils/age'); originalRead = $.read.bind($); @@ -321,4 +322,43 @@ describe('download github proxy regex', function () { wrongPair['age-secret-key'], ); }); + + it('uses the Undici option that throws at the file redirect limit', async function () { + const undici = eval("require('undici')"); + const originalAgent = undici.Agent; + const originalRedirect = undici.interceptors.redirect; + const originalRequest = undici.request; + let redirectOptions; + let error; + + undici.Agent = class { + compose() { + return this; + } + }; + undici.interceptors.redirect = (options) => { + redirectOptions = options; + return () => {}; + }; + undici.request = async () => ({ statusCode: 500 }); + + try { + await downloadFile( + 'https://example.com/redirecting-file', + path.join(tempDir, 'redirecting-file'), + ); + } catch (e) { + error = e; + } finally { + undici.Agent = originalAgent; + undici.interceptors.redirect = originalRedirect; + undici.request = originalRequest; + } + + expect(error).to.be.instanceOf(Error); + expect(redirectOptions).to.deep.equal({ + maxRedirections: 3, + throwOnMaxRedirect: true, + }); + }); }); diff --git a/backend/src/test/vendor/open-api.spec.js b/backend/src/test/vendor/open-api.spec.js index 5201d902..7e4f08cc 100644 --- a/backend/src/test/vendor/open-api.spec.js +++ b/backend/src/test/vendor/open-api.spec.js @@ -19,8 +19,10 @@ describe('open-api HTTP adapter', function () { let originalEnv; let originalEnvHttpProxyAgent; let originalProxyAgent; + let originalRedirect; let originalRequest; let agentOptions; + let redirectOptions; let requestOptions; beforeEach(function () { @@ -33,8 +35,10 @@ describe('open-api HTTP adapter', function () { originalEnvHttpProxyAgent = undici.EnvHttpProxyAgent; originalProxyAgent = undici.ProxyAgent; + originalRedirect = undici.interceptors.redirect; originalRequest = undici.request; agentOptions = []; + redirectOptions = undefined; requestOptions = undefined; class CapturingAgent { @@ -49,6 +53,10 @@ describe('open-api HTTP adapter', function () { undici.EnvHttpProxyAgent = CapturingAgent; undici.ProxyAgent = CapturingAgent; + undici.interceptors.redirect = (options) => { + redirectOptions = options; + return () => {}; + }; undici.request = async (_url, options) => { requestOptions = options; return { @@ -65,6 +73,7 @@ describe('open-api HTTP adapter', function () { afterEach(function () { undici.EnvHttpProxyAgent = originalEnvHttpProxyAgent; undici.ProxyAgent = originalProxyAgent; + undici.interceptors.redirect = originalRedirect; undici.request = originalRequest; proxyEnvKeys.forEach((key) => { if (originalEnv[key] == null) { @@ -96,6 +105,33 @@ describe('open-api HTTP adapter', function () { expect(agentOptions[0].requestTls.allowH2).to.equal(false); }); + it('passes proxyTunnel to undici proxy agents', async function () { + await HTTP().get({ + url: 'http://example.com/subscription', + proxy: 'http://127.0.0.1:8080', + proxyTunnel: true, + }); + await HTTP().get({ + url: 'http://example.com/subscription', + proxyTunnel: true, + }); + + expect(agentOptions).to.have.length(2); + expect(agentOptions.map(({ proxyTunnel }) => proxyTunnel)).to.deep.equal([ + true, + true, + ]); + }); + + it('uses the Undici option that throws at the redirect limit', async function () { + await HTTP().get('https://example.com/subscription'); + + expect(redirectOptions).to.deep.equal({ + maxRedirections: 3, + throwOnMaxRedirect: true, + }); + }); + 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 3c1a01f2..2bc6b5ae 100644 --- a/backend/src/utils/download.js +++ b/backend/src/utils/download.js @@ -521,7 +521,7 @@ export async function downloadFile(url, file) { dispatcher: new Agent().compose( interceptors.redirect({ maxRedirections: 3, - throwOnRedirect: true, + throwOnMaxRedirect: true, }), ), }); diff --git a/backend/src/vendor/open-api.js b/backend/src/vendor/open-api.js index 64f8fbf3..d9526670 100644 --- a/backend/src/vendor/open-api.js +++ b/backend/src/vendor/open-api.js @@ -570,12 +570,14 @@ export function HTTP(defaultOptions = { baseURL: '' }) { ...agentOpts, uri: opts.proxy, requestTls: tlsOptions, + proxyTunnel: opts.proxyTunnel, }); } } else { dispatcher = new EnvHttpProxyAgent({ ...agentOpts, requestTls: tlsOptions, + proxyTunnel: opts.proxyTunnel, }); } const response = await request(opts.url, { @@ -584,7 +586,7 @@ export function HTTP(defaultOptions = { baseURL: '' }) { dispatcher: dispatcher.compose( interceptors.redirect({ maxRedirections: 3, - throwOnMaxRedirects: true, + throwOnMaxRedirect: true, }), ), });