Adding proxy support for ChromiumDownloader (#455)

This patch teaches puppeteer to download chromium over the
HTTP proxy, if any.

Fixes #448.
This commit is contained in:
Paul Hawxby 2017-08-23 16:33:29 +01:00 committed by Andrey Lushnikov
parent 151d512ae2
commit a00ba6a3d9
3 changed files with 54 additions and 8 deletions

View File

@ -23,8 +23,10 @@
"dependencies": {
"debug": "^2.6.8",
"extract-zip": "^1.6.5",
"https-proxy-agent": "^2.1.0",
"mime": "^1.3.4",
"progress": "^2.0.0",
"proxy-from-env": "^1.0.0",
"rimraf": "^2.6.1",
"ws": "^3.0.0"
},

View File

@ -22,6 +22,8 @@ const extract = require('extract-zip');
const util = require('util');
const URL = require('url');
const removeRecursive = require('rimraf');
const ProxyAgent = require('https-proxy-agent');
const getProxyForUrl = require('proxy-from-env').getProxyForUrl;
const DOWNLOADS_FOLDER = path.join(__dirname, '..', '.local-chromium');
@ -61,12 +63,9 @@ module.exports = {
*/
canDownloadRevision: function(platform, revision) {
console.assert(downloadURLs[platform], 'Unknown platform: ' + platform);
const url = URL.parse(util.format(downloadURLs[platform], revision));
const options = {
method: 'HEAD',
host: url.host,
path: url.pathname,
};
const options = requestOptions(util.format(downloadURLs[platform], revision), 'HEAD');
let resolve;
const promise = new Promise(x => resolve = x);
const request = https.request(options, response => {
@ -184,8 +183,11 @@ function parseFolderPath(folderPath) {
*/
function downloadFile(url, destinationPath, progressCallback) {
let fulfill, reject;
const promise = new Promise((x, y) => { fulfill = x; reject = y; });
const request = https.get(url, response => {
const options = requestOptions(url);
const request = https.get(options, response => {
if (response.statusCode !== 200) {
const error = new Error(`Download failed: server returned code ${response.statusCode}. URL: ${url}`);
// consume response data to free up memory
@ -217,3 +219,18 @@ function downloadFile(url, destinationPath, progressCallback) {
function extractZip(zipPath, folderPath) {
return new Promise(fulfill => extract(zipPath, {dir: folderPath}, fulfill));
}
function requestOptions(url, method = 'GET') {
const result = URL.parse(url);
result.method = method;
const proxyURL = getProxyForUrl(url);
if (proxyURL) {
const parsedProxyURL = URL.parse(proxyURL);
parsedProxyURL.secureProxy = parsedProxyURL.protocol === 'https:';
result.agent = new ProxyAgent(parsedProxyURL);
}
return result;
}

View File

@ -16,6 +16,12 @@ acorn@^5.0.1:
version "5.0.3"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.0.3.tgz#c460df08491463f028ccb82eab3730bf01087b3d"
agent-base@^4.1.0:
version "4.1.1"
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.1.1.tgz#92d8a4fc2524a3b09b3666a33b6c97960f23d6a4"
dependencies:
es6-promisify "^5.0.0"
ajv-keywords@^1.0.0:
version "1.5.1"
resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-1.5.1.tgz#314dd0a4b3368fad3dfcdc54ede6171b886daf3c"
@ -196,7 +202,7 @@ debug@2.2.0:
dependencies:
ms "0.7.1"
debug@^2.6.8:
debug@^2.4.1, debug@^2.6.8:
version "2.6.8"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.8.tgz#e731531ca2ede27d188222427da17821d68ff4fc"
dependencies:
@ -237,6 +243,16 @@ emojis-list@^2.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/entities/-/entities-1.1.1.tgz#6e5c2d0a5621b5dadaecef80b90edfb5cd7772f0"
es6-promise@^4.0.3:
version "4.1.1"
resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-4.1.1.tgz#8811e90915d9a0dba36274f0b242dbda78f9c92a"
es6-promisify@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/es6-promisify/-/es6-promisify-5.0.0.tgz#5109d62f3e56ea967c4b63505aef08291c8a5203"
dependencies:
es6-promise "^4.0.3"
escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
@ -464,6 +480,13 @@ has-ansi@^2.0.0:
dependencies:
ansi-regex "^2.0.0"
https-proxy-agent@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/https-proxy-agent/-/https-proxy-agent-2.1.0.tgz#1391bee7fd66aeabc0df2a1fa90f58954f43e443"
dependencies:
agent-base "^4.1.0"
debug "^2.4.1"
iconv-lite@^0.4.17:
version "0.4.18"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.18.tgz#23d8656b16aae6742ac29732ea8f0336a4789cf2"
@ -864,6 +887,10 @@ progress@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.0.tgz#8a1be366bf8fc23db2bd23f10c6fe920b4389d1f"
proxy-from-env@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.0.0.tgz#33c50398f70ea7eb96d21f7b817630a55791c7ee"
randomatic@^1.1.3:
version "1.1.7"
resolved "https://registry.yarnpkg.com/randomatic/-/randomatic-1.1.7.tgz#c7abe9cc8b87c0baa876b19fde83fd464797e38c"