From a7c91357076e0678eac0d7fa989d1ea212824459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20Mar=C3=A9chal?= Date: Tue, 5 May 2020 10:15:16 -0400 Subject: [PATCH] cli: use proxy settings when downloading plugins MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit request/requestretry used to support some set of proxy environment variables, while node-fetch doesn't by default. This commit add support for proxy settings when downloading plugins. Signed-off-by: Paul Maréchal --- dev-packages/cli/package.json | 5 ++++ dev-packages/cli/src/download-plugins.ts | 18 +++++++++++++-- yarn.lock | 29 +++++++++++++++++++++++- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/dev-packages/cli/package.json b/dev-packages/cli/package.json index dce702d0bb1cc..9b4ee99b1f8c4 100644 --- a/dev-packages/cli/package.json +++ b/dev-packages/cli/package.json @@ -40,13 +40,18 @@ "@types/tar": "^4.0.3", "chai": "^4.2.0", "colors": "^1.4.0", + "https-proxy-agent": "^5.0.0", "mkdirp": "^0.5.0", "mocha": "^7.0.0", "node-fetch": "^2.6.0", + "proxy-from-env": "^1.1.0", "puppeteer": "^2.0.0", "puppeteer-to-istanbul": "^1.2.2", "tar": "^4.0.0", "unzip-stream": "^0.3.0", "yargs": "^11.1.0" + }, + "devDependencies": { + "@types/proxy-from-env": "^1.0.1" } } diff --git a/dev-packages/cli/src/download-plugins.ts b/dev-packages/cli/src/download-plugins.ts index dbdb0a7baab0f..8f3e0c751eb63 100644 --- a/dev-packages/cli/src/download-plugins.ts +++ b/dev-packages/cli/src/download-plugins.ts @@ -16,7 +16,9 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ -import fetch, { Response } from 'node-fetch'; +import fetch, { Response, RequestInit } from 'node-fetch'; +import { HttpsProxyAgent } from 'https-proxy-agent'; +import { getProxyForUrl } from 'proxy-from-env'; import * as fs from 'fs'; import * as mkdirp from 'mkdirp'; import * as path from 'path'; @@ -94,7 +96,7 @@ export default async function downloadPlugins(options: DownloadPluginsOptions = } lastError = undefined; try { - response = await fetch(pluginUrl); + response = await xfetch(pluginUrl); } catch (error) { lastError = error; continue; @@ -152,3 +154,15 @@ export default async function downloadPlugins(options: DownloadPluginsOptions = function isDownloaded(filePath: string): boolean { return fs.existsSync(filePath); } + +/** + * Follow HTTP(S)_PROXY, ALL_PROXY and NO_PROXY environment variables. + */ +export function xfetch(url: string, options?: RequestInit): Promise { + const proxiedOptions: RequestInit = { ...options }; + const proxy = getProxyForUrl(url); + if (!proxiedOptions.agent && proxy !== '') { + proxiedOptions.agent = new HttpsProxyAgent(proxy); + } + return fetch(url, proxiedOptions); +} diff --git a/yarn.lock b/yarn.lock index b64dbcdd08e00..3a533c91eb656 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1313,6 +1313,13 @@ resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.3.tgz#2ab0d5da2e5815f94b0b9d4b95d1e5f243ab2ca7" integrity sha512-KfRL3PuHmqQLOG+2tGpRO26Ctg+Cq1E01D2DMriKEATHgWLfeNDmq9e29Q9WIky0dQ3NPkd1mzYH8Lm936Z9qw== +"@types/proxy-from-env@^1.0.1": + version "1.0.1" + resolved "https://registry.yarnpkg.com/@types/proxy-from-env/-/proxy-from-env-1.0.1.tgz#b5f3e99230ca4518af196c18267055fc51f892b7" + integrity sha512-luG++TFHyS61eKcfkR1CVV6a1GMNXDjtqEQIIfaSHax75xp0HU3SlezjOi1yqubJwrG8e9DeW59n6wTblIDwFg== + dependencies: + "@types/node" "*" + "@types/ps-tree@^1.1.0": version "1.1.0" resolved "https://registry.yarnpkg.com/@types/ps-tree/-/ps-tree-1.1.0.tgz#7e2034e8ccdc16f6b0ced7a88529ebcb3b1dc424" @@ -1800,6 +1807,13 @@ agent-base@4, agent-base@^4.3.0: dependencies: es6-promisify "^5.0.0" +agent-base@6: + version "6.0.0" + resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.0.tgz#5d0101f19bbfaed39980b22ae866de153b93f09a" + integrity sha512-j1Q7cSCqN+AwrmDd+pzgqc0/NpC655x2bUf5ZjRIO77DcNBFmh+OgRNzF6OKdCC9RSCb19fGd99+bhXFdkRNqw== + dependencies: + debug "4" + aggregate-error@^3.0.0: version "3.0.1" resolved "https://registry.yarnpkg.com/aggregate-error/-/aggregate-error-3.0.1.tgz#db2fe7246e536f40d9b5442a39e117d7dd6a24e0" @@ -4524,7 +4538,7 @@ debug@3.2.6, debug@^3.0.0, debug@^3.1.0: dependencies: ms "^2.1.1" -debug@^4.0.1, debug@^4.1.0, debug@^4.1.1: +debug@4, debug@^4.0.1, debug@^4.1.0, debug@^4.1.1: version "4.1.1" resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791" integrity sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw== @@ -6805,6 +6819,14 @@ https-proxy-agent@^3.0.0: agent-base "^4.3.0" debug "^3.1.0" +https-proxy-agent@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/https-proxy-agent/-/https-proxy-agent-5.0.0.tgz#e2a90542abb68a762e0a0850f6c9edadfd8506b2" + integrity sha512-EkYm5BcKUGiduxzSt3Eppko+PiNWNEpa4ySk9vTC6wDsQJW9rHSa+UhGNJoRYp7bz6Ht1eaRIa6QaJqO5rCFbA== + dependencies: + agent-base "6" + debug "4" + iconv-lite@0.4.23: version "0.4.23" resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.23.tgz#297871f63be507adcfbfca715d0cd0eed84e9a63" @@ -10253,6 +10275,11 @@ proxy-from-env@^1.0.0: resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.0.0.tgz#33c50398f70ea7eb96d21f7b817630a55791c7ee" integrity sha1-M8UDmPcOp+uW0h97gXYwpVeRx+4= +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== + prr@~1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/prr/-/prr-1.0.1.tgz#d3fc114ba06995a45ec6893f484ceb1d78f5f476"