|
| 1 | +import type { Dispatcher } from "undici"; |
| 2 | + |
| 3 | +export type FetchFn = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>; |
| 4 | +export type ProxyRuntime = "bun" | "node"; |
| 5 | + |
| 6 | +export type UndiciProxyModule = { |
| 7 | + readonly EnvHttpProxyAgent: new () => Dispatcher; |
| 8 | +}; |
| 9 | + |
| 10 | +export interface ProxyAwareFetchOptions { |
| 11 | + readonly runtime?: ProxyRuntime; |
| 12 | + readonly loadUndici?: () => Promise<UndiciProxyModule>; |
| 13 | + readonly createUnsupportedProxyError?: (message: string) => Error; |
| 14 | +} |
| 15 | + |
| 16 | +interface ProxyableRequestInit extends RequestInit { |
| 17 | + readonly dispatcher?: Dispatcher; |
| 18 | +} |
| 19 | + |
| 20 | +const LOOPBACK_HOSTS = new Set(["localhost", "127.0.0.1", "::1", "[::1]"]); |
| 21 | + |
| 22 | +export const BUN_PROXY_UNSUPPORTED_MESSAGE = |
| 23 | + "Proxy environment variables are set, but proxy dispatchers require Node.js for now. Unset proxy env vars or run DevAgent with Node.js."; |
| 24 | + |
| 25 | +let cachedProxySignature: string | null = null; |
| 26 | +let cachedProxyDispatcher: Dispatcher | null = null; |
| 27 | + |
| 28 | +function getProxyEnvValue(name: string): string | undefined { |
| 29 | + return process.env[name] ?? process.env[name.toLowerCase()]; |
| 30 | +} |
| 31 | + |
| 32 | +function getProxySignature(): string | null { |
| 33 | + const httpProxy = getProxyEnvValue("HTTP_PROXY"); |
| 34 | + const httpsProxy = getProxyEnvValue("HTTPS_PROXY"); |
| 35 | + if (!httpProxy && !httpsProxy) return null; |
| 36 | + |
| 37 | + return JSON.stringify({ |
| 38 | + HTTP_PROXY: httpProxy ?? null, |
| 39 | + HTTPS_PROXY: httpsProxy ?? null, |
| 40 | + NO_PROXY: getProxyEnvValue("NO_PROXY") ?? null, |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +function getProxyRuntime(): ProxyRuntime { |
| 45 | + return typeof Bun === "undefined" ? "node" : "bun"; |
| 46 | +} |
| 47 | + |
| 48 | +async function loadUndiciProxyModule(): Promise<UndiciProxyModule> { |
| 49 | + return await import("undici") as UndiciProxyModule; |
| 50 | +} |
| 51 | + |
| 52 | +async function getProxyDispatcher(options?: ProxyAwareFetchOptions): Promise<Dispatcher | null> { |
| 53 | + const signature = getProxySignature(); |
| 54 | + if (!signature) return null; |
| 55 | + const runtime = options?.runtime ?? getProxyRuntime(); |
| 56 | + if (runtime === "bun") { |
| 57 | + throw options?.createUnsupportedProxyError?.(BUN_PROXY_UNSUPPORTED_MESSAGE) |
| 58 | + ?? new Error(BUN_PROXY_UNSUPPORTED_MESSAGE); |
| 59 | + } |
| 60 | + if (signature === cachedProxySignature && cachedProxyDispatcher) { |
| 61 | + return cachedProxyDispatcher; |
| 62 | + } |
| 63 | + |
| 64 | + const { EnvHttpProxyAgent } = await (options?.loadUndici ?? loadUndiciProxyModule)(); |
| 65 | + cachedProxySignature = signature; |
| 66 | + cachedProxyDispatcher = new EnvHttpProxyAgent(); |
| 67 | + return cachedProxyDispatcher; |
| 68 | +} |
| 69 | + |
| 70 | +function resolveRequestUrl(input: RequestInfo | URL): URL | null { |
| 71 | + try { |
| 72 | + if (typeof input === "string") return new URL(input); |
| 73 | + if (input instanceof URL) return input; |
| 74 | + if (typeof Request !== "undefined" && input instanceof Request) { |
| 75 | + return new URL(input.url); |
| 76 | + } |
| 77 | + if (typeof input === "object" && input !== null && "url" in input) { |
| 78 | + const url = (input as { url?: unknown }).url; |
| 79 | + return typeof url === "string" ? new URL(url) : null; |
| 80 | + } |
| 81 | + } catch { |
| 82 | + return null; |
| 83 | + } |
| 84 | + return null; |
| 85 | +} |
| 86 | + |
| 87 | +export function hasProxyEnv(): boolean { |
| 88 | + return getProxySignature() !== null; |
| 89 | +} |
| 90 | + |
| 91 | +export function shouldBypassProxy(input: RequestInfo | URL): boolean { |
| 92 | + const url = resolveRequestUrl(input); |
| 93 | + if (!url) return false; |
| 94 | + return LOOPBACK_HOSTS.has(url.hostname.toLowerCase()); |
| 95 | +} |
| 96 | + |
| 97 | +export function createProxyAwareFetch( |
| 98 | + baseFetch: FetchFn = globalThis.fetch.bind(globalThis), |
| 99 | + options?: ProxyAwareFetchOptions, |
| 100 | +): FetchFn { |
| 101 | + return async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => { |
| 102 | + if (shouldBypassProxy(input)) { |
| 103 | + return baseFetch(input, init); |
| 104 | + } |
| 105 | + const dispatcher = await getProxyDispatcher(options); |
| 106 | + if (!dispatcher) { |
| 107 | + return baseFetch(input, init); |
| 108 | + } |
| 109 | + |
| 110 | + return baseFetch(input, { |
| 111 | + ...(init ?? {}), |
| 112 | + dispatcher, |
| 113 | + } as ProxyableRequestInit); |
| 114 | + }; |
| 115 | +} |
0 commit comments