|
1 | 1 | import { HttpsProxyAgent } from 'https-proxy-agent'; |
2 | 2 | import { SocksProxyAgent } from 'socks-proxy-agent'; |
3 | | - |
4 | | -import { ProxyAgent } from 'undici' |
| 3 | +import { ProxyAgent } from 'undici'; |
5 | 4 |
|
6 | 5 | type Proxy = { |
7 | 6 | host: string; |
@@ -46,38 +45,38 @@ export function makeProxyAgent(proxy: Proxy | string): HttpsProxyAgent<string> | |
46 | 45 | } |
47 | 46 |
|
48 | 47 | export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent { |
49 | | - let proxyUrl: string |
50 | | - let protocol: string |
| 48 | + let proxyUrl: string; |
| 49 | + let protocol: string; |
51 | 50 |
|
52 | 51 | 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; |
56 | 55 | } 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(':', ''); |
59 | 58 |
|
60 | 59 | if (protocol === 'socks') { |
61 | | - protocol = 'socks5' |
| 60 | + protocol = 'socks5'; |
62 | 61 | } |
63 | 62 |
|
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}`; |
66 | 65 | } |
67 | 66 |
|
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'; |
72 | 71 |
|
73 | 72 | switch (protocol) { |
74 | 73 | case PROXY_HTTP_PROTOCOL: |
75 | 74 | case PROXY_HTTPS_PROTOCOL: |
76 | 75 | case PROXY_SOCKS4_PROTOCOL: |
77 | 76 | case PROXY_SOCKS5_PROTOCOL: |
78 | | - return new ProxyAgent(proxyUrl) |
| 77 | + return new ProxyAgent(proxyUrl); |
79 | 78 |
|
80 | 79 | default: |
81 | | - throw new Error(`Unsupported proxy protocol: ${protocol}`) |
| 80 | + throw new Error(`Unsupported proxy protocol: ${protocol}`); |
82 | 81 | } |
83 | 82 | } |
0 commit comments