Files
setup-uv/src/utils/fetch.ts
eifinger-bot 8dc20b2aca fix: add timeout to fetch to prevent silent hangs (#883)
Add `AbortSignal.timeout(5s)` to fetch requests to ensure they fail fast
instead of hanging indefinitely when network issues occur.
2026-05-31 09:37:59 +02:00

31 lines
850 B
TypeScript

import { ProxyAgent, type RequestInit, fetch as undiciFetch } from "undici";
export function getProxyAgent() {
const httpProxy = process.env.HTTP_PROXY || process.env.http_proxy;
if (httpProxy) {
return new ProxyAgent(httpProxy);
}
const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy;
if (httpsProxy) {
return new ProxyAgent(httpsProxy);
}
return undefined;
}
export const fetch = async (url: string, opts: RequestInit) => {
// Merge timeout signal with any existing signal from opts
const timeoutSignal = AbortSignal.timeout(5_000);
const existingSignal = opts.signal;
const mergedSignal = existingSignal
? AbortSignal.any([timeoutSignal, existingSignal])
: timeoutSignal;
return await undiciFetch(url, {
dispatcher: getProxyAgent(),
...opts,
signal: mergedSignal,
});
};