Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/cloud-api/proxy-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import fetch, { RequestInit, Response } from "node-fetch";
import { fetch as UndiciFetch, ProxyAgent } from "undici";
import { HttpsProxyAgent } from "https-proxy-agent";
import { SocksProxyAgent } from "socks-proxy-agent";
import { Agent } from "http";
import dotenv from "dotenv";

dotenv.config();

/**
* Automatically creates a proxy-enabled version of node-fetch
* based on system environment variables (HTTP_PROXY, HTTPS_PROXY, ALL_PROXY).
* Uses Node.js native fetch (available in Node 18+).
* For proxy support, we use undici's ProxyAgent.
*/
function createProxyFetch() {
const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy;
const httpProxy = process.env.HTTP_PROXY || process.env.http_proxy;
const allProxy = process.env.ALL_PROXY || process.env.all_proxy;

let agent: Agent | undefined;

const proxy = httpsProxy || httpProxy || allProxy;

if (proxy) {
if (proxy.startsWith("socks")) {
agent = new SocksProxyAgent(proxy);
} else {
agent = new HttpsProxyAgent(proxy);
}
// Use undici fetch with proxy
const dispatcher = new ProxyAgent(proxy);
return async function proxyFetch(
url: string | URL | Request,
options: RequestInit = {}
): Promise<Response> {
return UndiciFetch(url as string, { dispatcher, ...options } as any) as unknown as Promise<Response>;
};
}

// No proxy - use native fetch
return async function proxyFetch(
url: string,
url: string | URL | Request,
options: RequestInit = {}
): Promise<Response> {
return fetch(url, { agent, ...options });
return fetch(url, options);
};
}

Expand Down