Skip to content

Commit

Permalink
[proxy-agent] Add support for httpAgent and httpsAgent options
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed May 4, 2023
1 parent 4d75e6c commit 9a90063
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-clocks-join.md
@@ -0,0 +1,5 @@
---
'proxy-agent': minor
---

Add support for `httpAgent` and `httpsAgent` options
22 changes: 20 additions & 2 deletions packages/proxy-agent/src/index.ts
Expand Up @@ -46,7 +46,20 @@ function isValidProtocol(v: string): v is ValidProtocol {
export type ProxyAgentOptions = HttpProxyAgentOptions<''> &
HttpsProxyAgentOptions<''> &
SocksProxyAgentOptions &
PacProxyAgentOptions<''>;
PacProxyAgentOptions<''> & {
/**
* Default `http.Agent` instance to use when no proxy is
* configured for a request. Defaults to a new `http.Agent()`
* instance with the proxy agent options passed in.
*/
httpAgent?: http.Agent;
/**
* Default `http.Agent` instance to use when no proxy is
* configured for a request. Defaults to a new `https.Agent()`
* instance with the proxy agent options passed in.
*/
httpsAgent?: http.Agent;
};

/**
* Uses the appropriate `Agent` subclass based off of the "proxy"
Expand All @@ -62,11 +75,16 @@ export class ProxyAgent extends Agent {
cache = new LRUCache<string, Agent>({ max: 20 });

connectOpts?: ProxyAgentOptions;
httpAgent: http.Agent;
httpsAgent: http.Agent;

constructor(opts?: ProxyAgentOptions) {
super(opts);
debug('Creating new ProxyAgent instance: %o', opts);
this.connectOpts = opts;
this.httpAgent = opts?.httpAgent || new http.Agent(opts);
this.httpsAgent =
opts?.httpsAgent || new https.Agent(opts as https.AgentOptions);
}

async connect(
Expand All @@ -80,7 +98,7 @@ export class ProxyAgent extends Agent {

if (!proxy) {
debug('Proxy not enabled for URL: %o', url);
return opts.secureEndpoint ? https.globalAgent : http.globalAgent;
return opts.secureEndpoint ? this.httpsAgent : this.httpAgent;
}

debug('Request URL: %o', url);
Expand Down

1 comment on commit 9a90063

@vercel
Copy link

@vercel vercel bot commented on 9a90063 May 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

proxy-agents – ./

proxy-agents-tootallnate.vercel.app
proxy-agents-git-main-tootallnate.vercel.app
proxy-agents.vercel.app

Please sign in to comment.