diff --git a/packages/cli/package.json b/packages/cli/package.json index 3ca8e455a..f82800a5b 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": "9.3.0", diff --git a/packages/cli/src/fetch.js b/packages/cli/src/fetch.js new file mode 100644 index 000000000..44824cd68 --- /dev/null +++ b/packages/cli/src/fetch.js @@ -0,0 +1,23 @@ +/** + * @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. + */ +'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 7aeeae659..ed6a7db8d 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, });