From 88d7bd42bdaf31af884066900d4b3d3a90b66606 Mon Sep 17 00:00:00 2001 From: Mateusz Kwasniewski Date: Fri, 6 Dec 2024 13:15:40 +0100 Subject: [PATCH] feat: Support no proxy yarn (#682) Co-authored-by: Alfie Wan --- package.json | 2 ++ src/request.ts | 25 +++++++++++++++---------- yarn.lock | 12 ++++++++++++ 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 537e64a1..983cb85a 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", diff --git a/src/request.ts b/src/request.ts index dd609767..15186ca5 100644 --- a/src/request.ts +++ b/src/request.ts @@ -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'; @@ -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, diff --git a/yarn.lock b/yarn.lock index fba4be7f..1230a211 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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"