2026-01-30 02:21:37 -05:00
|
|
|
import { ProxyAgent, fetch as undiciFetch } from "undici";
|
2025-12-07 22:46:02 +01:00
|
|
|
|
|
|
|
|
export function makeProxyFetch(proxyUrl: string): typeof fetch {
|
|
|
|
|
const agent = new ProxyAgent(proxyUrl);
|
2026-02-04 10:09:28 +00:00
|
|
|
// undici's fetch is runtime-compatible with global fetch but the types diverge
|
|
|
|
|
// on stream/body internals. Single cast at the boundary keeps the rest type-safe.
|
2026-02-04 04:09:53 -08:00
|
|
|
const fetcher = ((input: RequestInfo | URL, init?: RequestInit) =>
|
2026-02-04 10:09:28 +00:00
|
|
|
undiciFetch(input as string | URL, {
|
|
|
|
|
...(init as Record<string, unknown>),
|
|
|
|
|
dispatcher: agent,
|
2026-02-04 04:09:53 -08:00
|
|
|
}) as unknown as Promise<Response>) as typeof fetch;
|
2026-02-16 08:24:31 -05:00
|
|
|
// Return raw proxy fetch; call sites that need AbortSignal normalization
|
|
|
|
|
// should opt into resolveFetch/wrapFetchWithAbortSignal once at the edge.
|
|
|
|
|
return fetcher;
|
2025-12-07 22:46:02 +01:00
|
|
|
}
|