Skip to content
40 changes: 21 additions & 19 deletions src/utils/makeProxyAgent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpsProxyAgent } from 'https-proxy-agent';

Check failure on line 1 in src/utils/makeProxyAgent.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Run autofix to sort these imports!
import { SocksProxyAgent } from 'socks-proxy-agent';

import { ProxyAgent } from 'undici'

Check failure on line 4 in src/utils/makeProxyAgent.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Insert `;`
Expand All @@ -19,11 +19,13 @@
// the end so, we add the protocol constants without the `:` to avoid confusion.
const PROXY_HTTP_PROTOCOL = 'http:';
const PROXY_SOCKS_PROTOCOL = 'socks:';
const PROXY_SOCKS5_PROTOCOL = 'socks5:';

switch (url.protocol) {
case PROXY_HTTP_PROTOCOL:
return new HttpsProxyAgent(url);
case PROXY_SOCKS_PROTOCOL:
case PROXY_SOCKS5_PROTOCOL:
return new SocksProxyAgent(url);
default:
throw new Error(`Unsupported proxy protocol: ${url.protocol}`);
Expand All @@ -45,39 +47,39 @@
return selectProxyAgent(proxyUrl);
}

export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent {
let proxyUrl: string
let protocol: string
export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent|SocksProxyAgent {

Check failure on line 50 in src/utils/makeProxyAgent.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Replace `|` with `·|·`
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}`;
}

Check failure on line 68 in src/utils/makeProxyAgent.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Delete `⏎;`

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:
return new ProxyAgent(proxyUrl);
case PROXY_SOCKS4_PROTOCOL:
case PROXY_SOCKS5_PROTOCOL:
return new ProxyAgent(proxyUrl)

return new SocksProxyAgent(proxyUrl);
default:
throw new Error(`Unsupported proxy protocol: ${protocol}`)
throw new Error(`Unsupported proxy protocol: ${protocol}`);
}
}
Loading