* Configure undici's global dispatcher so Node's built-in `fetch()` and any undici-based * clients route through the proxy.
(config: ProxyConfig)
| 260 | * clients route through the proxy. |
| 261 | */ |
| 262 | async function configureUndiciProxy(config: ProxyConfig): Promise<void> { |
| 263 | if (!config.enabled || !config.serverUrl) { |
| 264 | return |
| 265 | } |
| 266 | |
| 267 | if (undiciProxyInitialized) { |
| 268 | log(`undici global dispatcher already configured; restart VS Code to change proxy safely`) |
| 269 | return |
| 270 | } |
| 271 | |
| 272 | try { |
| 273 | const { |
| 274 | ProxyAgent, |
| 275 | setGlobalDispatcher, |
| 276 | fetch: undiciFetch, |
| 277 | } = (await import("undici")) as typeof import("undici") |
| 278 | |
| 279 | const proxyAgent = new ProxyAgent({ |
| 280 | uri: config.serverUrl, |
| 281 | // If the user enabled TLS insecure mode (debug only), apply it to undici. |
| 282 | requestTls: config.tlsInsecure |
| 283 | ? ({ rejectUnauthorized: false } satisfies import("tls").ConnectionOptions) // lgtm[js/disabling-certificate-validation] |
| 284 | : undefined, |
| 285 | proxyTls: config.tlsInsecure |
| 286 | ? ({ rejectUnauthorized: false } satisfies import("tls").ConnectionOptions) // lgtm[js/disabling-certificate-validation] |
| 287 | : undefined, |
| 288 | }) |
| 289 | setGlobalDispatcher(proxyAgent) |
| 290 | undiciProxyInitialized = true |
| 291 | log(`undici global dispatcher configured for proxy: ${redactProxyUrl(config.serverUrl)}`) |
| 292 | |
| 293 | // Node's built-in `fetch()` (Node 18+) is powered by an internal undici copy. |
| 294 | // Setting a dispatcher on our `undici` dependency does NOT affect that internal fetch. |
| 295 | // To ensure Roo Code's `fetch()` calls are proxied, patch global fetch in debug mode. |
| 296 | // This patch is scoped to the extension lifecycle (restored on deactivate) and can be restored |
| 297 | // immediately if the proxy is disabled. |
| 298 | if (!fetchPatched) { |
| 299 | if (typeof globalThis.fetch === "function") { |
| 300 | originalFetch = globalThis.fetch |
| 301 | } |
| 302 | |
| 303 | globalThis.fetch = undiciFetch as unknown as typeof fetch |
| 304 | fetchPatched = true |
| 305 | log(`globalThis.fetch patched to undici.fetch (debug proxy mode)`) |
| 306 | |
| 307 | if (extensionContext) { |
| 308 | extensionContext.subscriptions.push({ |
| 309 | dispose: () => restoreGlobalFetchPatch(), |
| 310 | }) |
| 311 | } |
| 312 | } |
| 313 | } catch (error) { |
| 314 | log(`Failed to configure undici proxy dispatcher: ${error instanceof Error ? error.message : String(error)}`) |
| 315 | } |
| 316 | } |
| 317 | /** |
| 318 | * Update environment variables for proxy configuration. |
| 319 | * global-agent reads from GLOBAL_AGENT_* environment variables. |
no test coverage detected