Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): API requests will respect HTTP_PROXY env variable #727

Merged
merged 5 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 23 additions & 0 deletions packages/cli/src/fetch.js
Original file line number Diff line number Diff line change
@@ -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<import('isomorphic-fetch')>[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);
};
4 changes: 2 additions & 2 deletions packages/cli/src/upload/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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 => {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/wizard/wizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -58,6 +59,7 @@ async function runNewProjectWizard(options) {
]);

const api = new ApiClient({
fetch,
...options,
rootURL: responses.serverBaseUrl || options.serverBaseUrl,
});
Expand Down