From a84faaa575cf7661760dd61a24df91b69ccad4cb Mon Sep 17 00:00:00 2001 From: ricael Date: Thu, 30 Oct 2025 10:55:47 -0300 Subject: [PATCH] refactor(utils): improve makeProxyAgent for Undici compatibility --- src/utils/makeProxyAgent.ts | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/utils/makeProxyAgent.ts b/src/utils/makeProxyAgent.ts index e882876e..ac64b9de 100644 --- a/src/utils/makeProxyAgent.ts +++ b/src/utils/makeProxyAgent.ts @@ -1,7 +1,6 @@ import { HttpsProxyAgent } from 'https-proxy-agent'; import { SocksProxyAgent } from 'socks-proxy-agent'; - -import { ProxyAgent } from 'undici' +import { ProxyAgent } from 'undici'; type Proxy = { host: string; @@ -46,38 +45,38 @@ export function makeProxyAgent(proxy: Proxy | string): HttpsProxyAgent | } export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent { - let proxyUrl: string - let protocol: string + let proxyUrl: string; + let protocol: string; if (typeof proxy === 'string') { - const url = new URL(proxy) - protocol = url.protocol.replace(':', '') - proxyUrl = proxy + const url = new URL(proxy); + protocol = url.protocol.replace(':', ''); + proxyUrl = proxy; } else { - const { host, password, port, protocol: proto, username } = proxy - protocol = (proto || 'http').replace(':', '') + const { host, password, port, protocol: proto, username } = proxy; + protocol = (proto || 'http').replace(':', ''); if (protocol === 'socks') { - protocol = 'socks5' + protocol = 'socks5'; } - const auth = username && password ? `${username}:${password}@` : '' - proxyUrl = `${protocol}://${auth}${host}:${port}` + const auth = username && password ? `${username}:${password}@` : ''; + proxyUrl = `${protocol}://${auth}${host}:${port}`; } - const PROXY_HTTP_PROTOCOL = 'http' - const PROXY_HTTPS_PROTOCOL = 'https' - const PROXY_SOCKS4_PROTOCOL = 'socks4' - const PROXY_SOCKS5_PROTOCOL = 'socks5' + const PROXY_HTTP_PROTOCOL = 'http'; + const PROXY_HTTPS_PROTOCOL = 'https'; + const PROXY_SOCKS4_PROTOCOL = 'socks4'; + const PROXY_SOCKS5_PROTOCOL = 'socks5'; switch (protocol) { case PROXY_HTTP_PROTOCOL: case PROXY_HTTPS_PROTOCOL: case PROXY_SOCKS4_PROTOCOL: case PROXY_SOCKS5_PROTOCOL: - return new ProxyAgent(proxyUrl) + return new ProxyAgent(proxyUrl); default: - throw new Error(`Unsupported proxy protocol: ${protocol}`) + throw new Error(`Unsupported proxy protocol: ${protocol}`); } }