mirror of
https://github.com/sub-store-org/Sub-Store.git
synced 2026-11-30 00:10:30 +00:00
feat: 优化 Node.js 下请求头 Accept 逻辑
This commit is contained in:
3 files changed
+73
-9
No files matched your search
@@ -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",
|
||||
|
||||
+51
-8
@@ -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',
|
||||
});
|
||||
});
|
||||
});
|
||||
Vendored
+21
@@ -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 =
|
||||
|
||||
Reference in new issue
Block a user