/** * Create a fetch function that routes requests through the given HTTP proxy. * Uses undici's ProxyAgent under the hood.
*/
export function makeProxyFetch(proxyUrl: string): typeof fetch {
let agent: ProxyAgent | null = null; const resolveAgent = (): ProxyAgent => { if (!agent) {
agent = new ProxyAgent(proxyUrl);
} return agent;
}; // 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. const proxyFetch = ((input: RequestInfo | URL, init?: RequestInit) =>
undiciFetch(input as string | URL, {
...(init as Record<string, unknown>),
dispatcher: resolveAgent(),
}) as unknown as Promise<Response>) as ProxyFetchWithMetadata;
Object.defineProperty(proxyFetch, PROXY_FETCH_PROXY_URL, {
value: proxyUrl,
enumerable: false,
configurable: false,
writable: false,
}); return proxyFetch;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.