* Configure global-agent to route all HTTP/HTTPS traffic through the proxy.
(config: ProxyConfig)
| 218 | * Configure global-agent to route all HTTP/HTTPS traffic through the proxy. |
| 219 | */ |
| 220 | async function configureGlobalProxy(config: ProxyConfig): Promise<void> { |
| 221 | if (proxyInitialized) { |
| 222 | // global-agent can only be bootstrapped once |
| 223 | // Update environment variables for any new connections |
| 224 | log(`Proxy already initialized, updating env vars only`) |
| 225 | updateProxyEnvVars(config) |
| 226 | return |
| 227 | } |
| 228 | |
| 229 | // Set up environment variables before bootstrapping |
| 230 | log(`Setting proxy environment variables before bootstrap (values redacted)...`) |
| 231 | updateProxyEnvVars(config) |
| 232 | |
| 233 | let bootstrap: (() => void) | undefined |
| 234 | try { |
| 235 | const mod = (await import("global-agent")) as typeof import("global-agent") |
| 236 | bootstrap = mod.bootstrap |
| 237 | } catch (error) { |
| 238 | log( |
| 239 | `Failed to load global-agent (proxy support is only available in debug/dev builds): ${error instanceof Error ? error.message : String(error)}`, |
| 240 | ) |
| 241 | return |
| 242 | } |
| 243 | |
| 244 | // Bootstrap global-agent to intercept all HTTP/HTTPS requests |
| 245 | log(`Calling global-agent bootstrap()...`) |
| 246 | try { |
| 247 | bootstrap() |
| 248 | proxyInitialized = true |
| 249 | log(`global-agent bootstrap() completed successfully`) |
| 250 | } catch (error) { |
| 251 | log(`global-agent bootstrap() FAILED: ${error instanceof Error ? error.message : String(error)}`) |
| 252 | return |
| 253 | } |
| 254 | |
| 255 | log(`Network proxy configured: ${redactProxyUrl(config.serverUrl)}`) |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Configure undici's global dispatcher so Node's built-in `fetch()` and any undici-based |
no test coverage detected