Skip to content

Commit

Permalink
refactor: Do not override user-provided agent
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbankhead committed Apr 5, 2024
1 parent fb8002b commit 81d551b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -163,8 +163,10 @@ interface GaxiosOptions = {
/**
* An optional proxy to use for requests.
* Available via `process.env.HTTP_PROXY` and `process.env.HTTPS_PROXY` as well - with a preference for the this config option when multiple are available.
* The `agent` option overrides this.
*
* @see {@link GaxiosOptions.noProxy}
* @see {@link GaxiosOptions.agent}
*/
proxy?: string | URL;
/**
Expand Down
2 changes: 2 additions & 0 deletions src/common.ts
Expand Up @@ -220,8 +220,10 @@ export interface GaxiosOptions {
/**
* An optional proxy to use for requests.
* Available via `process.env.HTTP_PROXY` and `process.env.HTTPS_PROXY` as well - with a preference for the this config option when multiple are available.
* The {@link GaxiosOptions.agent `agent`} option overrides this.
*
* @see {@link GaxiosOptions.noProxy}
* @see {@link GaxiosOptions.agent}
*/
proxy?: string | URL;
/**
Expand Down
15 changes: 8 additions & 7 deletions src/gaxios.ts
Expand Up @@ -318,19 +318,20 @@ export class Gaxios {
}
opts.method = opts.method || 'GET';

const shouldUseProxy = this.#shouldUseProxyForURLIfAvailable(
opts.url,
opts.noProxy
);

const proxy =
opts.proxy ||
process?.env?.HTTPS_PROXY ||
process?.env?.https_proxy ||
process?.env?.HTTP_PROXY ||
process?.env?.http_proxy;
const urlMayUseProxy = this.#shouldUseProxyForURLIfAvailable(
opts.url,
opts.noProxy
);

if (shouldUseProxy && proxy) {
if (opts.agent) {
// don't do any of the following options - use the user-provided agent.
} else if (proxy && urlMayUseProxy) {
const HttpsProxyAgent = await Gaxios.#getProxyAgent();

if (this.agentCache.has(proxy)) {
Expand All @@ -344,7 +345,7 @@ export class Gaxios {
this.agentCache.set(proxy, opts.agent);
}
} else if (opts.cert && opts.key) {
// Configure client for mTLS:
// Configure client for mTLS
if (this.agentCache.has(opts.key)) {
opts.agent = this.agentCache.get(opts.key);
} else {
Expand Down

0 comments on commit 81d551b

Please sign in to comment.