diff --git a/cli/package.json b/cli/package.json index 4b6beea3d5..d08352d9ed 100644 --- a/cli/package.json +++ b/cli/package.json @@ -59,7 +59,8 @@ "open": "^9.1.0", "ora": "^8.0.1", "pathe": "^1.1.1", - "picocolors": "^1.0.0" + "picocolors": "^1.0.0", + "undici": "^6.19.8" }, "devDependencies": { "@types/cli-progress": "^3.11.5", diff --git a/cli/src/commands/index.ts b/cli/src/commands/index.ts index 32fe52b881..354da05551 100644 --- a/cli/src/commands/index.ts +++ b/cli/src/commands/index.ts @@ -14,9 +14,23 @@ import ContractCommands from './contract/index.js'; import FeatureGraphCommands from './feature-subgraph/index.js'; import FeatureFlagCommands from './feature-flag/index.js'; +const proxyUrl = process.env.HTTPS_PROXY || process.env.HTTP_PROXY; + +if (proxyUrl) { + // Lazy load undici only when needed + const { setGlobalDispatcher, ProxyAgent } = await import('undici'); + + // Set the global dispatcher for undici to route through the proxy + const dispatcher = new ProxyAgent({ + uri: new URL(proxyUrl).toString(), + }); + setGlobalDispatcher(dispatcher); +} + const client = CreateClient({ baseUrl: config.baseURL, apiKey: config.apiKey, + proxyUrl, }); const program = new Command(); diff --git a/cli/src/core/client/client.ts b/cli/src/core/client/client.ts index d55d82e323..b148a635b9 100644 --- a/cli/src/core/client/client.ts +++ b/cli/src/core/client/client.ts @@ -2,10 +2,12 @@ import { compressionBrotli, compressionGzip, createConnectTransport } from '@con import { createPromiseClient, PromiseClient } from '@connectrpc/connect'; import { PlatformService } from '@wundergraph/cosmo-connect/dist/platform/v1/platform_connect'; import { NodeService } from '@wundergraph/cosmo-connect/dist/node/v1/node_connect'; +import { HttpsProxyAgent } from 'https-proxy-agent'; export interface ClientOptions { baseUrl: string; apiKey?: string; + proxyUrl?: string; } export interface Client { @@ -20,7 +22,9 @@ export const CreateClient = (opts: ClientOptions): Client => { // You have to tell the Node.js http API which HTTP version to use. httpVersion: '1.1', - + nodeOptions: { + agent: opts.proxyUrl ? new HttpsProxyAgent(opts.proxyUrl) : undefined, + }, // Avoid compression for small requests compressMinBytes: 1024,