From 9399c9786fba4e45e1c5485ddbb197d2d4f1735f Mon Sep 17 00:00:00 2001 From: Ben Elliott <4996462+benelliott@users.noreply.github.com> Date: Wed, 15 Sep 2021 20:41:03 +0100 Subject: [PATCH] =?UTF-8?q?fix(install):=20respect=20environment=20proxy?= =?UTF-8?q?=20config=20when=20downloading=20Firef=E2=80=A6=20(#6577)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issues: #6573 --- docs/api.md | 2 +- src/node/install.ts | 29 +++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/docs/api.md b/docs/api.md index cd56decf2fbd0..9edfecd6b67ce 100644 --- a/docs/api.md +++ b/docs/api.md @@ -463,7 +463,7 @@ You will then need to call [`puppeteer.connect([options])`](#puppeteerconnectopt Puppeteer looks for certain [environment variables](https://en.wikipedia.org/wiki/Environment_variable) to aid its operations. If Puppeteer doesn't find them in the environment during the installation step, a lowercased variant of these variables will be used from the [npm config](https://docs.npmjs.com/cli/config). -- `HTTP_PROXY`, `HTTPS_PROXY`, `NO_PROXY` - defines HTTP proxy settings that are used to download and run Chromium. +- `HTTP_PROXY`, `HTTPS_PROXY`, `NO_PROXY` - defines HTTP proxy settings that are used to download and run the browser. - `PUPPETEER_SKIP_CHROMIUM_DOWNLOAD` - do not download bundled Chromium during installation step. - `PUPPETEER_DOWNLOAD_HOST` - overwrite URL prefix that is used to download Chromium. Note: this includes protocol and might even include path prefix. Defaults to `https://storage.googleapis.com`. - `PUPPETEER_DOWNLOAD_PATH` - overwrite the path for the downloads folder. Defaults to `/.local-chromium`, where `` is Puppeteer's package root. diff --git a/src/node/install.ts b/src/node/install.ts index d7e751f44ef87..856b13ae9ce3c 100644 --- a/src/node/install.ts +++ b/src/node/install.ts @@ -15,11 +15,16 @@ */ import os from 'os'; -import https from 'https'; +import https, { RequestOptions } from 'https'; import ProgressBar from 'progress'; +import URL from 'url'; import puppeteer from '../node.js'; import { PUPPETEER_REVISIONS } from '../revisions.js'; import { PuppeteerNode } from './Puppeteer.js'; +import createHttpsProxyAgent, { + HttpsProxyAgentOptions, +} from 'https-proxy-agent'; +import { getProxyForUrl } from 'proxy-from-env'; const supportedProducts = { chrome: 'Chromium', @@ -148,16 +153,32 @@ export async function downloadBrowser(): Promise { } function getFirefoxNightlyVersion() { - const firefoxVersions = + const firefoxVersionsUrl = 'https://product-details.mozilla.org/1.0/firefox_versions.json'; + const proxyURL = getProxyForUrl(firefoxVersionsUrl); + + const requestOptions: RequestOptions = {}; + + if (proxyURL) { + const parsedProxyURL = URL.parse(proxyURL); + + const proxyOptions = { + ...parsedProxyURL, + secureProxy: parsedProxyURL.protocol === 'https:', + } as HttpsProxyAgentOptions; + + requestOptions.agent = createHttpsProxyAgent(proxyOptions); + requestOptions.rejectUnauthorized = false; + } + const promise = new Promise((resolve, reject) => { let data = ''; logPolitely( - `Requesting latest Firefox Nightly version from ${firefoxVersions}` + `Requesting latest Firefox Nightly version from ${firefoxVersionsUrl}` ); https - .get(firefoxVersions, (r) => { + .get(firefoxVersionsUrl, requestOptions, (r) => { if (r.statusCode >= 400) return reject(new Error(`Got status code ${r.statusCode}`)); r.on('data', (chunk) => {