Skip to content

Commit c8236ff

Browse files
committed
fix(core): add timeouts and trimming to proxy agents
1 parent ca01320 commit c8236ff

2 files changed

Lines changed: 36 additions & 12 deletions

File tree

packages/core/src/utils/fetch.test.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const {
3333
isAddressPrivate,
3434
fetchWithTimeout,
3535
setGlobalProxy,
36+
createSafeProxyAgent,
3637
} = await import('./fetch.js');
3738
interface ErrorWithCode extends Error {
3839
code?: string;
@@ -55,6 +56,7 @@ describe('fetch utils', () => {
5556
return [{ address: '93.184.216.34', family: 4 }];
5657
});
5758
vi.unstubAllEnvs();
59+
updateGlobalFetchTimeouts(60000);
5860
});
5961

6062
afterEach(() => {
@@ -239,17 +241,17 @@ describe('fetch utils', () => {
239241

240242
describe('setGlobalProxy', () => {
241243
it('should configure EnvHttpProxyAgent with experiment flag timeout and noProxy', () => {
242-
const proxyUrl = 'http://proxy.example.com';
243-
const noProxyValue = 'localhost,127.0.0.1';
244+
const proxyUrl = ' http://proxy.example.com ';
245+
const noProxyValue = ' localhost,127.0.0.1 ';
244246
vi.stubEnv('NO_PROXY', noProxyValue);
245247

246248
updateGlobalFetchTimeouts(45773134);
247249
setGlobalProxy(proxyUrl);
248250

249251
expect(EnvHttpProxyAgent).toHaveBeenCalledWith({
250-
httpProxy: proxyUrl,
251-
httpsProxy: proxyUrl,
252-
noProxy: noProxyValue,
252+
httpProxy: 'http://proxy.example.com',
253+
httpsProxy: 'http://proxy.example.com',
254+
noProxy: 'localhost,127.0.0.1',
253255
headersTimeout: 45773134,
254256
bodyTimeout: 300000,
255257
});
@@ -270,4 +272,22 @@ describe('fetch utils', () => {
270272
);
271273
});
272274
});
275+
276+
describe('createSafeProxyAgent', () => {
277+
it('should create an EnvHttpProxyAgent with trimmed values and default timeouts', () => {
278+
const proxyUrl = ' http://proxy.example.com ';
279+
const noProxyValue = ' localhost,127.0.0.1 ';
280+
vi.stubEnv('NO_PROXY', noProxyValue);
281+
282+
createSafeProxyAgent(proxyUrl);
283+
284+
expect(EnvHttpProxyAgent).toHaveBeenCalledWith({
285+
httpProxy: 'http://proxy.example.com',
286+
httpsProxy: 'http://proxy.example.com',
287+
noProxy: 'localhost,127.0.0.1',
288+
headersTimeout: 60000,
289+
bodyTimeout: 300000,
290+
});
291+
});
292+
});
273293
});

packages/core/src/utils/fetch.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,14 @@ export async function isPrivateIpAsync(url: string): Promise<boolean> {
172172
* Creates an undici EnvHttpProxyAgent that incorporates safe DNS lookup.
173173
*/
174174
export function createSafeProxyAgent(proxyUrl: string): EnvHttpProxyAgent {
175-
const noProxy = process.env['NO_PROXY'] || process.env['no_proxy'];
175+
const trimmedProxy = proxyUrl.trim();
176+
const noProxy = (process.env['NO_PROXY'] || process.env['no_proxy'])?.trim();
176177
return new EnvHttpProxyAgent({
177-
httpProxy: proxyUrl,
178-
httpsProxy: proxyUrl,
178+
httpProxy: trimmedProxy,
179+
httpsProxy: trimmedProxy,
179180
noProxy,
181+
headersTimeout: defaultHeadersTimeout,
182+
bodyTimeout: defaultBodyTimeout,
180183
});
181184
}
182185

@@ -223,12 +226,13 @@ export async function fetchWithTimeout(
223226
}
224227

225228
export function setGlobalProxy(proxy: string) {
226-
currentProxy = proxy;
227-
const noProxy = process.env['NO_PROXY'] || process.env['no_proxy'];
229+
const trimmedProxy = proxy.trim();
230+
currentProxy = trimmedProxy;
231+
const noProxy = (process.env['NO_PROXY'] || process.env['no_proxy'])?.trim();
228232
setGlobalDispatcher(
229233
new EnvHttpProxyAgent({
230-
httpProxy: proxy,
231-
httpsProxy: proxy,
234+
httpProxy: trimmedProxy,
235+
httpsProxy: trimmedProxy,
232236
noProxy,
233237
headersTimeout: defaultHeadersTimeout,
234238
bodyTimeout: defaultBodyTimeout,

0 commit comments

Comments
 (0)