diff --git a/backend/package.json b/backend/package.json index 11983801..0182be35 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.34.0", + "version": "2.35.0", "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/test/vendor/open-api.spec.js b/backend/src/test/vendor/open-api.spec.js index 26aca74c..8b89ca8b 100644 --- a/backend/src/test/vendor/open-api.spec.js +++ b/backend/src/test/vendor/open-api.spec.js @@ -21,6 +21,7 @@ describe('open-api HTTP adapter', function () { let originalProxyAgent; let originalRequest; let agentOptions; + let requestOptions; beforeEach(function () { originalEnv = Object.fromEntries( @@ -34,6 +35,7 @@ describe('open-api HTTP adapter', function () { originalProxyAgent = undici.ProxyAgent; originalRequest = undici.request; agentOptions = []; + requestOptions = undefined; class CapturingAgent { constructor(options) { @@ -47,14 +49,17 @@ describe('open-api HTTP adapter', function () { undici.EnvHttpProxyAgent = CapturingAgent; undici.ProxyAgent = CapturingAgent; - undici.request = async () => ({ - statusCode: 200, - headers: {}, - body: { - text: async () => '', - arrayBuffer: async () => new ArrayBuffer(0), - }, - }); + undici.request = async (_url, options) => { + requestOptions = options; + return { + statusCode: 200, + headers: {}, + body: { + text: async () => '', + arrayBuffer: async () => new ArrayBuffer(0), + }, + }; + }; }); afterEach(function () { @@ -90,4 +95,42 @@ describe('open-api HTTP adapter', function () { expect(agentOptions[0].connect.allowH2).to.equal(false); expect(agentOptions[0].requestTls.allowH2).to.equal(false); }); + + it('adds a curl-like Accept header by default in Node.js', async function () { + await HTTP({ + headers: { + 'User-Agent': 'Surge', + }, + }).get('https://example.com/subscription'); + + expect(requestOptions.headers).to.include({ + 'User-Agent': 'Surge', + Accept: '*/*', + }); + }); + + it('keeps an explicit Accept header in Node.js', async function () { + await HTTP({ + headers: { + accept: 'application/json', + }, + }).get('https://example.com/subscription'); + + expect(requestOptions.headers).to.deep.equal({ + accept: 'application/json', + }); + }); + + it('omits Accept when callers set it to null in Node.js', async function () { + await HTTP({ + headers: { + 'User-Agent': 'Surge', + Accept: null, + }, + }).get('https://example.com/subscription'); + + expect(requestOptions.headers).to.deep.equal({ + 'User-Agent': 'Surge', + }); + }); }); diff --git a/backend/src/vendor/open-api.js b/backend/src/vendor/open-api.js index 1d14133a..101f0ac4 100644 --- a/backend/src/vendor/open-api.js +++ b/backend/src/vendor/open-api.js @@ -42,6 +42,24 @@ function parseSocks5Uri(uri) { password: password != null ? decodeURIComponent(password) : undefined, }; } + +function normalizeNodeRequestHeaders(headers) { + const normalized = { ...(headers || {}) }; + let hasAccept = false; + + for (const key of Object.keys(normalized)) { + if (key.toLowerCase() !== 'accept') continue; + hasAccept = true; + if (normalized[key] == null) delete normalized[key]; + } + + if (!hasAccept) { + normalized.Accept = '*/*'; + } + + return normalized; +} + export class OpenAPI { constructor(name = 'untitled', debug = false) { this.name = name; @@ -523,6 +541,9 @@ export function HTTP(defaultOptions = { baseURL: '' }) { ).toString('base64')}`, }; } + opts.headers = normalizeNodeRequestHeaders( + opts.headers, + ); let dispatcher; if (!opts.proxy) { const allProxy =