Skip to content

Commit

Permalink
lazy load non-direct proxies within pac-proxy (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys authored Dec 6, 2024
1 parent 5fd2546 commit 38760db
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-rocks-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'pac-proxy-agent': minor
---

Lazily load agents inside pac-proxy-agent
13 changes: 10 additions & 3 deletions packages/pac-proxy-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import createDebug from 'debug';
import { Readable } from 'stream';
import { URL } from 'url';
import { Agent, AgentConnectOpts, toBuffer } from 'agent-base';
import { HttpProxyAgent, HttpProxyAgentOptions } from 'http-proxy-agent';
import { HttpsProxyAgent, HttpsProxyAgentOptions } from 'https-proxy-agent';
import { SocksProxyAgent, SocksProxyAgentOptions } from 'socks-proxy-agent';
import type { HttpProxyAgentOptions } from 'http-proxy-agent';
import type { HttpsProxyAgentOptions } from 'https-proxy-agent';
import type { SocksProxyAgentOptions } from 'socks-proxy-agent';
import {
getUri,
protocols as gProtocols,
Expand Down Expand Up @@ -41,6 +41,7 @@ const setServernameFromNonIpHost = <
}
return options;
};

type Protocols = keyof typeof gProtocols;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -245,9 +246,11 @@ export class PacProxyAgent<Uri extends string> extends Agent {
}
} else if (type === 'SOCKS' || type === 'SOCKS5') {
// Use a SOCKSv5h proxy
const { SocksProxyAgent } = await import('socks-proxy-agent');
agent = new SocksProxyAgent(`socks://${target}`, this.opts);
} else if (type === 'SOCKS4') {
// Use a SOCKSv4a proxy
const { SocksProxyAgent } = await import('socks-proxy-agent');
agent = new SocksProxyAgent(`socks4a://${target}`, this.opts);
} else if (
type === 'PROXY' ||
Expand All @@ -260,8 +263,12 @@ export class PacProxyAgent<Uri extends string> extends Agent {
type === 'HTTPS' ? 'https' : 'http'
}://${target}`;
if (secureEndpoint || isWebSocket) {
const { HttpsProxyAgent } = await import(
'https-proxy-agent'
);
agent = new HttpsProxyAgent(proxyURL, this.opts);
} else {
const { HttpProxyAgent } = await import('http-proxy-agent');
agent = new HttpProxyAgent(proxyURL, this.opts);
}
}
Expand Down

0 comments on commit 38760db

Please sign in to comment.