Skip to content

Commit 88d7bd4

Browse files
kwasniewAlfie Wan
andauthored
feat: Support no proxy yarn (#682)
Co-authored-by: Alfie Wan <[email protected]>
1 parent a30b59f commit 88d7bd4

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"ip-address": "^9.0.5",
3636
"make-fetch-happen": "^13.0.1",
3737
"murmurhash3js": "^3.0.1",
38+
"proxy-from-env": "^1.1.0",
3839
"semver": "^7.6.2"
3940
},
4041
"engines": {
@@ -55,6 +56,7 @@
5556
"@types/murmurhash3js": "^3.0.3",
5657
"@types/nock": "^11.1.0",
5758
"@types/node": "^20.2.5",
59+
"@types/proxy-from-env": "^1.0.4",
5860
"@types/semver": "^7.5.0",
5961
"@types/sinon": "^17.0.0",
6062
"@typescript-eslint/eslint-plugin": "^6.0.0",

src/request.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { HttpsProxyAgent } from 'https-proxy-agent';
44
import * as http from 'http';
55
import * as https from 'https';
66
import { URL } from 'url';
7+
import { getProxyForUrl } from 'proxy-from-env';
78
import { CustomHeaders } from './headers';
89
import { HttpOptions } from './http-options';
910

@@ -32,24 +33,28 @@ export interface PostRequestOptions extends RequestOptions {
3233
httpOptions?: HttpOptions;
3334
}
3435

35-
const httpProxy = process.env.HTTP_PROXY || process.env.http_proxy;
36-
const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy;
37-
3836
const httpAgentOptions: http.AgentOptions = {
3937
keepAlive: true,
4038
keepAliveMsecs: 30 * 1000,
4139
timeout: 10 * 1000,
4240
};
4341

44-
const httpAgent = httpProxy
45-
? new HttpProxyAgent(httpProxy, httpAgentOptions)
46-
: new http.Agent(httpAgentOptions);
42+
const httpNoProxyAgent = new http.Agent(httpAgentOptions);
43+
const httpsNoProxyAgent = new https.Agent(httpAgentOptions);
44+
45+
export const getDefaultAgent = (url: URL) => {
46+
const proxy = getProxyForUrl(url.href);
47+
const isHttps = url.protocol === 'https:';
4748

48-
const httpsAgent = httpsProxy
49-
? new HttpsProxyAgent(httpsProxy, httpAgentOptions)
50-
: new https.Agent(httpAgentOptions);
49+
if (!proxy || proxy === '') {
50+
return isHttps ? httpsNoProxyAgent : httpNoProxyAgent;
51+
}
52+
53+
return isHttps
54+
? new HttpsProxyAgent(proxy, httpAgentOptions)
55+
: new HttpProxyAgent(proxy, httpAgentOptions);
56+
};
5157

52-
export const getDefaultAgent = (url: URL) => (url.protocol === 'https:' ? httpsAgent : httpAgent);
5358
export const buildHeaders = (
5459
appName?: string,
5560
instanceId?: string,

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,13 @@
736736
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.4.tgz#56e2cc26c397c038fab0e3a917a12d5c5909e901"
737737
integrity sha512-37i+OaWTh9qeK4LSHPsyRC7NahnGotNuZvjLSgcPzblpHB3rrCJxAOgI5gCdKm7coonsaX1Of0ILiTcnZjbfxA==
738738

739+
"@types/proxy-from-env@^1.0.4":
740+
version "1.0.4"
741+
resolved "https://registry.yarnpkg.com/@types/proxy-from-env/-/proxy-from-env-1.0.4.tgz#0a0545768f2d6c16b81a84ffefb53b423807907c"
742+
integrity sha512-TPR9/bCZAr3V1eHN4G3LD3OLicdJjqX1QRXWuNcCYgE66f/K8jO2ZRtHxI2D9MbnuUP6+qiKSS8eUHp6TFHGCw==
743+
dependencies:
744+
"@types/node" "*"
745+
739746
"@types/qs@*":
740747
version "6.9.10"
741748
resolved "https://registry.yarnpkg.com/@types/qs/-/qs-6.9.10.tgz#0af26845b5067e1c9a622658a51f60a3934d51e8"
@@ -4233,6 +4240,11 @@ propagate@^2.0.0:
42334240
resolved "https://registry.yarnpkg.com/propagate/-/propagate-2.0.1.tgz#40cdedab18085c792334e64f0ac17256d38f9a45"
42344241
integrity sha512-vGrhOavPSTz4QVNuBNdcNXePNdNMaO1xj9yBeH1ScQPjk/rhg9sSlCXPhMkFuaNNW/syTvYqsnbIJxMBfRbbag==
42354242

4243+
proxy-from-env@^1.1.0:
4244+
version "1.1.0"
4245+
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
4246+
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==
4247+
42364248
psl@^1.1.28:
42374249
version "1.8.0"
42384250
resolved "https://registry.npmjs.org/psl/-/psl-1.8.0.tgz"

0 commit comments

Comments
 (0)