fix: align proxy routing across fetch and downloader

This commit is contained in:
Tam Nhu Tran
2026-03-16 07:10:39 -04:00
parent f50c9625de
commit fd5d16f3f3
5 changed files with 236 additions and 21 deletions
+118 -9
View File
@@ -1,7 +1,17 @@
import { afterEach, beforeEach, describe, expect, it } from 'bun:test';
import * as http from 'node:http';
import { fetch, getGlobalDispatcher, setGlobalDispatcher } from 'undici';
import { applyGlobalFetchProxy } from '../../../src/utils/fetch-proxy-setup';
import {
Agent,
Dispatcher,
ProxyAgent,
fetch,
getGlobalDispatcher,
setGlobalDispatcher,
} from 'undici';
import {
applyGlobalFetchProxy,
createGlobalFetchProxyDispatcher,
} from '../../../src/utils/fetch-proxy-setup';
const PROXY_ENV_KEYS = [
'http_proxy',
@@ -38,6 +48,52 @@ describe('global fetch proxy setup', () => {
}
});
async function captureDispatchRouting(
origin: string
): Promise<{ directCalls: number; proxyCalls: number }> {
const dispatcher = createGlobalFetchProxyDispatcher();
if (!dispatcher) {
throw new Error('Expected proxy dispatcher to be configured');
}
let directCalls = 0;
let proxyCalls = 0;
const originalAgentDispatch = Agent.prototype.dispatch;
const originalProxyDispatch = ProxyAgent.prototype.dispatch;
Agent.prototype.dispatch = function mockAgentDispatch(
_options: Dispatcher.DispatchOptions,
_handler: Dispatcher.DispatchHandlers
): boolean {
directCalls += 1;
return true;
};
ProxyAgent.prototype.dispatch = function mockProxyDispatch(
_options: Dispatcher.DispatchOptions,
_handler: Dispatcher.DispatchHandlers
): boolean {
proxyCalls += 1;
return true;
};
try {
dispatcher.dispatch(
{
origin,
method: 'GET',
path: '/',
} as Dispatcher.DispatchOptions,
{} as Dispatcher.DispatchHandlers
);
} finally {
Agent.prototype.dispatch = originalAgentDispatch;
ProxyAgent.prototype.dispatch = originalProxyDispatch;
}
return { directCalls, proxyCalls };
}
it('does not fail when proxy configuration is invalid', () => {
process.env.HTTP_PROXY = 'not-a-valid-url';
@@ -47,6 +103,27 @@ describe('global fetch proxy setup', () => {
});
});
it('rejects unsupported proxy protocols for global fetch setup', () => {
process.env.ALL_PROXY = 'socks5://proxy:1080';
expect(applyGlobalFetchProxy()).toEqual({
enabled: false,
error: 'Unsupported proxy protocol: socks5:',
});
});
it('rebinds globalThis.fetch to undici fetch when proxying is enabled', () => {
const originalFetch = globalThis.fetch;
process.env.HTTP_PROXY = 'http://proxy.example:8080';
try {
expect(applyGlobalFetchProxy()).toEqual({ enabled: true });
expect(globalThis.fetch).toBe(fetch);
} finally {
globalThis.fetch = originalFetch;
}
});
it('bypasses loopback fetches even when HTTP_PROXY is set', async () => {
process.env.HTTP_PROXY = 'http://127.0.0.1:9';
@@ -74,16 +151,48 @@ describe('global fetch proxy setup', () => {
}
});
it('supports ALL_PROXY as a fetch proxy fallback', () => {
process.env.ALL_PROXY = 'http://127.0.0.1:8080';
it('routes non-loopback HTTP requests through HTTP_PROXY', async () => {
process.env.HTTP_PROXY = 'http://proxy.example:8080';
expect(applyGlobalFetchProxy()).toEqual({ enabled: true });
const result = await captureDispatchRouting('http://example.com/quota');
expect(result).toEqual({ directCalls: 0, proxyCalls: 1 });
});
it('falls back to ALL_PROXY when HTTP_PROXY is invalid', () => {
process.env.HTTP_PROXY = 'not-a-valid-url';
process.env.ALL_PROXY = 'http://127.0.0.1:8080';
it('routes HTTPS requests through HTTPS_PROXY', async () => {
process.env.HTTPS_PROXY = 'http://proxy.example:8443';
expect(applyGlobalFetchProxy()).toEqual({ enabled: true });
const result = await captureDispatchRouting('https://example.com/secure');
expect(result).toEqual({ directCalls: 0, proxyCalls: 1 });
});
it('supports ALL_PROXY as a fetch proxy fallback', async () => {
process.env.ALL_PROXY = 'http://proxy.example:8080';
const result = await captureDispatchRouting('http://example.com/all-proxy');
expect(result).toEqual({ directCalls: 0, proxyCalls: 1 });
});
it('falls back to ALL_PROXY when HTTP_PROXY is invalid', async () => {
process.env.HTTP_PROXY = 'not-a-valid-url';
process.env.ALL_PROXY = 'http://proxy.example:8080';
const result = await captureDispatchRouting('http://example.com/fallback');
expect(result).toEqual({ directCalls: 0, proxyCalls: 1 });
});
it('falls back from invalid HTTPS_PROXY to HTTP_PROXY for HTTPS requests', async () => {
process.env.HTTPS_PROXY = 'not-a-valid-url';
process.env.HTTP_PROXY = 'http://proxy.example:8080';
const result = await captureDispatchRouting('https://example.com/fallback-secure');
expect(result).toEqual({ directCalls: 0, proxyCalls: 1 });
});
it('honors NO_PROXY on the fetch routing path', async () => {
process.env.HTTP_PROXY = 'http://proxy.example:8080';
process.env.NO_PROXY = 'example.com';
const result = await captureDispatchRouting('http://example.com/direct');
expect(result).toEqual({ directCalls: 1, proxyCalls: 0 });
});
});