Files
openclaw/src/telegram/proxy.ts
Vincent Koc e6049345db fix(telegram): preserve HTTP proxy env in global dispatcher workaround (#29940)
* fix(telegram): preserve HTTP proxy env in global dispatcher workaround

* telegram: document request-scoped proxy dispatcher constraint

* telegram: assert proxy path never mutates global dispatcher

* changelog: credit telegram proxy env regression fix

---------

Co-authored-by: Rylen Anil <rylen.anil@gmail.com>
2026-03-01 13:21:01 -08:00

18 lines
898 B
TypeScript

import { ProxyAgent, fetch as undiciFetch } from "undici";
export function makeProxyFetch(proxyUrl: string): typeof fetch {
const agent = new ProxyAgent(proxyUrl);
// 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.
// Keep proxy dispatching request-scoped. Replacing the global dispatcher breaks
// env-driven HTTP(S)_PROXY behavior for unrelated outbound requests.
const fetcher = ((input: RequestInfo | URL, init?: RequestInit) =>
undiciFetch(input as string | URL, {
...(init as Record<string, unknown>),
dispatcher: agent,
}) as unknown as Promise<Response>) as typeof fetch;
// Return raw proxy fetch; call sites that need AbortSignal normalization
// should opt into resolveFetch/wrapFetchWithAbortSignal once at the edge.
return fetcher;
}