Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lazy load non-direct proxies within pac-proxy #356

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading