Skip to content

Commit 38760db

Browse files
authored
lazy load non-direct proxies within pac-proxy (#356)
1 parent 5fd2546 commit 38760db

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

.changeset/bright-rocks-dance.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'pac-proxy-agent': minor
3+
---
4+
5+
Lazily load agents inside pac-proxy-agent

packages/pac-proxy-agent/src/index.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import createDebug from 'debug';
77
import { Readable } from 'stream';
88
import { URL } from 'url';
99
import { Agent, AgentConnectOpts, toBuffer } from 'agent-base';
10-
import { HttpProxyAgent, HttpProxyAgentOptions } from 'http-proxy-agent';
11-
import { HttpsProxyAgent, HttpsProxyAgentOptions } from 'https-proxy-agent';
12-
import { SocksProxyAgent, SocksProxyAgentOptions } from 'socks-proxy-agent';
10+
import type { HttpProxyAgentOptions } from 'http-proxy-agent';
11+
import type { HttpsProxyAgentOptions } from 'https-proxy-agent';
12+
import type { SocksProxyAgentOptions } from 'socks-proxy-agent';
1313
import {
1414
getUri,
1515
protocols as gProtocols,
@@ -41,6 +41,7 @@ const setServernameFromNonIpHost = <
4141
}
4242
return options;
4343
};
44+
4445
type Protocols = keyof typeof gProtocols;
4546

4647
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -245,9 +246,11 @@ export class PacProxyAgent<Uri extends string> extends Agent {
245246
}
246247
} else if (type === 'SOCKS' || type === 'SOCKS5') {
247248
// Use a SOCKSv5h proxy
249+
const { SocksProxyAgent } = await import('socks-proxy-agent');
248250
agent = new SocksProxyAgent(`socks://${target}`, this.opts);
249251
} else if (type === 'SOCKS4') {
250252
// Use a SOCKSv4a proxy
253+
const { SocksProxyAgent } = await import('socks-proxy-agent');
251254
agent = new SocksProxyAgent(`socks4a://${target}`, this.opts);
252255
} else if (
253256
type === 'PROXY' ||
@@ -260,8 +263,12 @@ export class PacProxyAgent<Uri extends string> extends Agent {
260263
type === 'HTTPS' ? 'https' : 'http'
261264
}://${target}`;
262265
if (secureEndpoint || isWebSocket) {
266+
const { HttpsProxyAgent } = await import(
267+
'https-proxy-agent'
268+
);
263269
agent = new HttpsProxyAgent(proxyURL, this.opts);
264270
} else {
271+
const { HttpProxyAgent } = await import('http-proxy-agent');
265272
agent = new HttpProxyAgent(proxyURL, this.opts);
266273
}
267274
}

0 commit comments

Comments
 (0)