Lazy import proxy-agent

This commit is contained in:
Fedor Indutny 2024-03-20 11:05:10 -07:00 committed by GitHub
parent 83e8f4b59d
commit 091b50c414
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 60 additions and 39 deletions

View file

@ -1,8 +1,8 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { ProxyAgent } from 'proxy-agent';
import net from 'net';
import type { ProxyAgent } from 'proxy-agent';
import { URL } from 'url';
import type { LookupOptions, LookupAddress } from 'dns';
import { lookup } from 'dns/promises';
@ -25,7 +25,9 @@ const SOCKS_PROTOCOLS = new Set([
'socks5h:',
]);
export function createProxyAgent(proxyUrl: string): ProxyAgent {
export type { ProxyAgent };
export async function createProxyAgent(proxyUrl: string): Promise<ProxyAgent> {
const { port: portStr, hostname: proxyHost, protocol } = new URL(proxyUrl);
let defaultPort: number | undefined;
if (protocol === 'http:') {
@ -96,6 +98,8 @@ export function createProxyAgent(proxyUrl: string): ProxyAgent {
}
}
const { ProxyAgent } = await import('proxy-agent');
return new ProxyAgent({
lookup:
port !== undefined