fix(core): respect NO_PROXY in global fetch dispatcher (#27216)

This commit is contained in:
Coco Sheng
2026-05-19 13:56:59 -04:00
committed by GitHub
parent fcc8c62b8b
commit 37f3a4c90a
5 changed files with 162 additions and 17 deletions

View File

@@ -6,7 +6,7 @@
import { getErrorMessage, isAbortError } from './errors.js';
import { URL } from 'node:url';
import { Agent, ProxyAgent, setGlobalDispatcher } from 'undici';
import { Agent, EnvHttpProxyAgent, setGlobalDispatcher } from 'undici';
import ipaddr from 'ipaddr.js';
import { lookup } from 'node:dns/promises';
@@ -169,11 +169,21 @@ export async function isPrivateIpAsync(url: string): Promise<boolean> {
}
/**
* Creates an undici ProxyAgent that incorporates safe DNS lookup.
* Creates an undici EnvHttpProxyAgent that incorporates safe DNS lookup.
*/
export function createSafeProxyAgent(proxyUrl: string): ProxyAgent {
return new ProxyAgent({
uri: proxyUrl,
export function createSafeProxyAgent(proxyUrl: string): EnvHttpProxyAgent {
const trimmedProxy = proxyUrl.trim();
const noProxy = (
process.env['NO_PROXY'] ??
process.env['no_proxy'] ??
''
)?.trim();
return new EnvHttpProxyAgent({
httpProxy: trimmedProxy,
httpsProxy: trimmedProxy,
noProxy,
headersTimeout: defaultHeadersTimeout,
bodyTimeout: defaultBodyTimeout,
});
}
@@ -220,10 +230,18 @@ export async function fetchWithTimeout(
}
export function setGlobalProxy(proxy: string) {
currentProxy = proxy;
const trimmedProxy = proxy.trim();
currentProxy = trimmedProxy;
const noProxy = (
process.env['NO_PROXY'] ??
process.env['no_proxy'] ??
''
)?.trim();
setGlobalDispatcher(
new ProxyAgent({
uri: proxy,
new EnvHttpProxyAgent({
httpProxy: trimmedProxy,
httpsProxy: trimmedProxy,
noProxy,
headersTimeout: defaultHeadersTimeout,
bodyTimeout: defaultBodyTimeout,
}),