From afe5809a7ec2b98f0ce49f63c72fed737e234d5e Mon Sep 17 00:00:00 2001 From: Nick Randall Date: Tue, 16 Nov 2021 12:51:52 -0700 Subject: [PATCH 1/2] feat(cli): API requests will respect HTTP_PROXY env variable --- packages/cli/package.json | 1 + packages/cli/src/fetch.js | 23 +++++++++++++++++++++++ packages/cli/src/upload/upload.js | 4 ++-- packages/cli/src/wizard/wizard.js | 2 ++ yarn.lock | 22 ++++++++++++++++++++++ 5 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 packages/cli/src/fetch.js diff --git a/packages/cli/package.json b/packages/cli/package.json index 33e9081c2..ad1f76ff3 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -18,6 +18,7 @@ "compression": "^1.7.4", "debug": "^4.3.1", "express": "^4.17.1", + "https-proxy-agent": "^5.0.0", "inquirer": "^6.3.1", "isomorphic-fetch": "^3.0.0", "lighthouse": "8.0.0", diff --git a/packages/cli/src/fetch.js b/packages/cli/src/fetch.js new file mode 100644 index 000000000..220782958 --- /dev/null +++ b/packages/cli/src/fetch.js @@ -0,0 +1,23 @@ +/** + * @license Copyright 2019 Google Inc. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + */ +'use strict'; + +const fetch = require('isomorphic-fetch'); +const {HttpsProxyAgent} = require('https-proxy-agent'); + +/** @type import('isomorphic-fetch') */ +module.exports = (url, options) => { + /** @type {Parameters[1] & { agent?: import('https-proxy-agent').HttpsProxyAgent }} */ + const instanceOptions = { + ...options, + }; + + if (!instanceOptions.agent && process.env.HTTP_PROXY) { + instanceOptions.agent = new HttpsProxyAgent(process.env.HTTP_PROXY); + } + + return fetch(url, instanceOptions); +}; diff --git a/packages/cli/src/upload/upload.js b/packages/cli/src/upload/upload.js index 7aa13977a..e7e10d51b 100644 --- a/packages/cli/src/upload/upload.js +++ b/packages/cli/src/upload/upload.js @@ -8,7 +8,6 @@ const fs = require('fs'); const path = require('path'); const URL = require('url').URL; -const fetch = require('isomorphic-fetch'); const _ = require('@lhci/utils/src/lodash.js'); const ApiClient = require('@lhci/utils/src/api-client.js'); const {writeUrlMapToFile} = require('@lhci/utils/src/saved-reports.js'); @@ -31,6 +30,7 @@ const { getCurrentBranchSafe, getAncestorHash, } = require('@lhci/utils/src/build-context.js'); +const fetch = require('../fetch.js'); /** @param {string} message */ const print = message => { @@ -393,7 +393,7 @@ function buildTemporaryStorageLink(compareUrl, urlAudited, previousUrlMap) { async function runLHCITarget(options) { if (!options.token) throw new Error('Must provide token for LHCI target'); - const api = new ApiClient({...options, rootURL: options.serverBaseUrl}); + const api = new ApiClient({fetch, ...options, rootURL: options.serverBaseUrl}); api.setBuildToken(options.token); const project = await api.findProjectByToken(options.token); diff --git a/packages/cli/src/wizard/wizard.js b/packages/cli/src/wizard/wizard.js index 12e2aacb9..dd286e02d 100644 --- a/packages/cli/src/wizard/wizard.js +++ b/packages/cli/src/wizard/wizard.js @@ -12,6 +12,7 @@ const ApiClient = require('@lhci/utils/src/api-client.js'); const _ = require('@lhci/utils/src/lodash.js'); const log = require('lighthouse-logger'); const {assertOptionalDependency} = require('../utils.js'); +const fetch = require('../fetch.js'); /** * @param {import('yargs').Argv} yargs @@ -58,6 +59,7 @@ async function runNewProjectWizard(options) { ]); const api = new ApiClient({ + fetch, ...options, rootURL: responses.serverBaseUrl || options.serverBaseUrl, }); diff --git a/yarn.lock b/yarn.lock index 74c3211b2..dedd5970a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4655,6 +4655,13 @@ agent-base@4, agent-base@^4.1.0: dependencies: es6-promisify "^5.0.0" +agent-base@6: + version "6.0.2" + resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77" + integrity sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ== + dependencies: + debug "4" + agent-base@~4.2.1: version "4.2.1" resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.2.1.tgz#d89e5999f797875674c07d87f260fc41e83e8ca9" @@ -8033,6 +8040,13 @@ debug@3.1.0, debug@=3.1.0: dependencies: ms "2.0.0" +debug@4: + version "4.3.2" + resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.2.tgz#f0a49c18ac8779e31d4a0c6029dfb76873c7428b" + integrity sha512-mOp8wKcvj7XxC78zLgw/ZA+6TSgkoE2C/ienthhRD298T7UNwAg9diBpLRxC0mOezLl4B0xV7M0cCO6P/O0Xhw== + dependencies: + ms "2.1.2" + debug@^3.0.0, debug@^3.0.1, debug@^3.1.0, debug@^3.2.5: version "3.2.6" resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.6.tgz#e83d17de16d8a7efb7717edbe5fb10135eee629b" @@ -11268,6 +11282,14 @@ https-proxy-agent@^2.2.1: agent-base "^4.1.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" + human-signals@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-1.1.1.tgz#c5b1cd14f50aeae09ab6c59fe63ba3395fe4dfa3" From 913c89c473f312151030c2b89ce8af63cbe0671c Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Mon, 18 Jul 2022 11:04:28 -0700 Subject: [PATCH 2/2] Apply suggestions from code review --- packages/cli/src/fetch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/fetch.js b/packages/cli/src/fetch.js index 220782958..44824cd68 100644 --- a/packages/cli/src/fetch.js +++ b/packages/cli/src/fetch.js @@ -1,5 +1,5 @@ /** - * @license Copyright 2019 Google Inc. All Rights Reserved. + * @license Copyright 2021 Google Inc. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */