Skip to content

Commit

Permalink
feat: Support no proxy yarn (#682)
Browse files Browse the repository at this point in the history
Co-authored-by: Alfie Wan <[email protected]>
  • Loading branch information
kwasniew and Alfie Wan authored Dec 6, 2024
1 parent a30b59f commit 88d7bd4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"ip-address": "^9.0.5",
"make-fetch-happen": "^13.0.1",
"murmurhash3js": "^3.0.1",
"proxy-from-env": "^1.1.0",
"semver": "^7.6.2"
},
"engines": {
Expand All @@ -55,6 +56,7 @@
"@types/murmurhash3js": "^3.0.3",
"@types/nock": "^11.1.0",
"@types/node": "^20.2.5",
"@types/proxy-from-env": "^1.0.4",
"@types/semver": "^7.5.0",
"@types/sinon": "^17.0.0",
"@typescript-eslint/eslint-plugin": "^6.0.0",
Expand Down
25 changes: 15 additions & 10 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { HttpsProxyAgent } from 'https-proxy-agent';
import * as http from 'http';
import * as https from 'https';
import { URL } from 'url';
import { getProxyForUrl } from 'proxy-from-env';
import { CustomHeaders } from './headers';
import { HttpOptions } from './http-options';

Expand Down Expand Up @@ -32,24 +33,28 @@ export interface PostRequestOptions extends RequestOptions {
httpOptions?: HttpOptions;
}

const httpProxy = process.env.HTTP_PROXY || process.env.http_proxy;
const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy;

const httpAgentOptions: http.AgentOptions = {
keepAlive: true,
keepAliveMsecs: 30 * 1000,
timeout: 10 * 1000,
};

const httpAgent = httpProxy
? new HttpProxyAgent(httpProxy, httpAgentOptions)
: new http.Agent(httpAgentOptions);
const httpNoProxyAgent = new http.Agent(httpAgentOptions);
const httpsNoProxyAgent = new https.Agent(httpAgentOptions);

export const getDefaultAgent = (url: URL) => {
const proxy = getProxyForUrl(url.href);
const isHttps = url.protocol === 'https:';

const httpsAgent = httpsProxy
? new HttpsProxyAgent(httpsProxy, httpAgentOptions)
: new https.Agent(httpAgentOptions);
if (!proxy || proxy === '') {
return isHttps ? httpsNoProxyAgent : httpNoProxyAgent;
}

return isHttps
? new HttpsProxyAgent(proxy, httpAgentOptions)
: new HttpProxyAgent(proxy, httpAgentOptions);
};

export const getDefaultAgent = (url: URL) => (url.protocol === 'https:' ? httpsAgent : httpAgent);
export const buildHeaders = (
appName?: string,
instanceId?: string,
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,13 @@
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.4.tgz#56e2cc26c397c038fab0e3a917a12d5c5909e901"
integrity sha512-37i+OaWTh9qeK4LSHPsyRC7NahnGotNuZvjLSgcPzblpHB3rrCJxAOgI5gCdKm7coonsaX1Of0ILiTcnZjbfxA==

"@types/proxy-from-env@^1.0.4":
version "1.0.4"
resolved "https://registry.yarnpkg.com/@types/proxy-from-env/-/proxy-from-env-1.0.4.tgz#0a0545768f2d6c16b81a84ffefb53b423807907c"
integrity sha512-TPR9/bCZAr3V1eHN4G3LD3OLicdJjqX1QRXWuNcCYgE66f/K8jO2ZRtHxI2D9MbnuUP6+qiKSS8eUHp6TFHGCw==
dependencies:
"@types/node" "*"

"@types/qs@*":
version "6.9.10"
resolved "https://registry.yarnpkg.com/@types/qs/-/qs-6.9.10.tgz#0af26845b5067e1c9a622658a51f60a3934d51e8"
Expand Down Expand Up @@ -4233,6 +4240,11 @@ propagate@^2.0.0:
resolved "https://registry.yarnpkg.com/propagate/-/propagate-2.0.1.tgz#40cdedab18085c792334e64f0ac17256d38f9a45"
integrity sha512-vGrhOavPSTz4QVNuBNdcNXePNdNMaO1xj9yBeH1ScQPjk/rhg9sSlCXPhMkFuaNNW/syTvYqsnbIJxMBfRbbag==

proxy-from-env@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==

psl@^1.1.28:
version "1.8.0"
resolved "https://registry.npmjs.org/psl/-/psl-1.8.0.tgz"
Expand Down

0 comments on commit 88d7bd4

Please sign in to comment.