Skip to content

Commit a84faaa

Browse files
author
ricael
committed
refactor(utils): improve makeProxyAgent for Undici compatibility
1 parent 503cbfb commit a84faaa

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

src/utils/makeProxyAgent.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { HttpsProxyAgent } from 'https-proxy-agent';
22
import { SocksProxyAgent } from 'socks-proxy-agent';
3-
4-
import { ProxyAgent } from 'undici'
3+
import { ProxyAgent } from 'undici';
54

65
type Proxy = {
76
host: string;
@@ -46,38 +45,38 @@ export function makeProxyAgent(proxy: Proxy | string): HttpsProxyAgent<string> |
4645
}
4746

4847
export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent {
49-
let proxyUrl: string
50-
let protocol: string
48+
let proxyUrl: string;
49+
let protocol: string;
5150

5251
if (typeof proxy === 'string') {
53-
const url = new URL(proxy)
54-
protocol = url.protocol.replace(':', '')
55-
proxyUrl = proxy
52+
const url = new URL(proxy);
53+
protocol = url.protocol.replace(':', '');
54+
proxyUrl = proxy;
5655
} else {
57-
const { host, password, port, protocol: proto, username } = proxy
58-
protocol = (proto || 'http').replace(':', '')
56+
const { host, password, port, protocol: proto, username } = proxy;
57+
protocol = (proto || 'http').replace(':', '');
5958

6059
if (protocol === 'socks') {
61-
protocol = 'socks5'
60+
protocol = 'socks5';
6261
}
6362

64-
const auth = username && password ? `${username}:${password}@` : ''
65-
proxyUrl = `${protocol}://${auth}${host}:${port}`
63+
const auth = username && password ? `${username}:${password}@` : '';
64+
proxyUrl = `${protocol}://${auth}${host}:${port}`;
6665
}
6766

68-
const PROXY_HTTP_PROTOCOL = 'http'
69-
const PROXY_HTTPS_PROTOCOL = 'https'
70-
const PROXY_SOCKS4_PROTOCOL = 'socks4'
71-
const PROXY_SOCKS5_PROTOCOL = 'socks5'
67+
const PROXY_HTTP_PROTOCOL = 'http';
68+
const PROXY_HTTPS_PROTOCOL = 'https';
69+
const PROXY_SOCKS4_PROTOCOL = 'socks4';
70+
const PROXY_SOCKS5_PROTOCOL = 'socks5';
7271

7372
switch (protocol) {
7473
case PROXY_HTTP_PROTOCOL:
7574
case PROXY_HTTPS_PROTOCOL:
7675
case PROXY_SOCKS4_PROTOCOL:
7776
case PROXY_SOCKS5_PROTOCOL:
78-
return new ProxyAgent(proxyUrl)
77+
return new ProxyAgent(proxyUrl);
7978

8079
default:
81-
throw new Error(`Unsupported proxy protocol: ${protocol}`)
80+
throw new Error(`Unsupported proxy protocol: ${protocol}`);
8281
}
8382
}

0 commit comments

Comments
 (0)