Spaces:
Running
Running
| import consola from "consola" | |
| import { getProxyForUrl } from "proxy-from-env" | |
| import { Agent, ProxyAgent, setGlobalDispatcher, type Dispatcher } from "undici" | |
| export function initProxyFromEnv(): void { | |
| if (typeof Bun !== "undefined") return | |
| try { | |
| const direct = new Agent() | |
| const proxies = new Map<string, ProxyAgent>() | |
| // We only need a minimal dispatcher that implements `dispatch` at runtime. | |
| // Typing the object as `Dispatcher` forces TypeScript to require many | |
| // additional methods. Instead, keep a plain object and cast when passing | |
| // to `setGlobalDispatcher`. | |
| const dispatcher = { | |
| dispatch( | |
| options: Dispatcher.DispatchOptions, | |
| handler: Dispatcher.DispatchHandler, | |
| ) { | |
| try { | |
| const origin = | |
| typeof options.origin === "string" ? | |
| new URL(options.origin) | |
| : (options.origin as URL) | |
| const get = getProxyForUrl as unknown as ( | |
| u: string, | |
| ) => string | undefined | |
| const raw = get(origin.toString()) | |
| const proxyUrl = raw && raw.length > 0 ? raw : undefined | |
| if (!proxyUrl) { | |
| consola.debug(`HTTP proxy bypass: ${origin.hostname}`) | |
| return (direct as unknown as Dispatcher).dispatch(options, handler) | |
| } | |
| let agent = proxies.get(proxyUrl) | |
| if (!agent) { | |
| agent = new ProxyAgent(proxyUrl) | |
| proxies.set(proxyUrl, agent) | |
| } | |
| let label = proxyUrl | |
| try { | |
| const u = new URL(proxyUrl) | |
| label = `${u.protocol}//${u.host}` | |
| } catch { | |
| /* noop */ | |
| } | |
| consola.debug(`HTTP proxy route: ${origin.hostname} via ${label}`) | |
| return (agent as unknown as Dispatcher).dispatch(options, handler) | |
| } catch { | |
| return (direct as unknown as Dispatcher).dispatch(options, handler) | |
| } | |
| }, | |
| close() { | |
| return direct.close() | |
| }, | |
| destroy() { | |
| return direct.destroy() | |
| }, | |
| } | |
| setGlobalDispatcher(dispatcher as unknown as Dispatcher) | |
| consola.debug("HTTP proxy configured from environment (per-URL)") | |
| } catch (err) { | |
| consola.debug("Proxy setup skipped:", err) | |
| } | |
| } | |