feat(install): add environment variable to overwrite host part of url (#958)

This patch adds PUPPETEER_DOWNLOAD_HOST env variable that allows to specify a download host. Useful for downloading from mirrors.

Fixes #951
This commit is contained in:
r3dDoX 2017-10-22 03:22:13 +02:00 committed by Andrey Lushnikov
parent c9e1a2de4e
commit 945a826a0b
3 changed files with 14 additions and 11 deletions

View File

@ -210,6 +210,7 @@ Puppeteer looks for certain [environment variables](https://en.wikipedia.org/wik
- `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 Chromium.
- `PUPPETEER_SKIP_CHROMIUM_DOWNLOAD` - do not download bundled Chromium during installation step. - `PUPPETEER_SKIP_CHROMIUM_DOWNLOAD` - do not download bundled Chromium during installation step.
- `PUPPETEER_DOWNLOAD_HOST` - overwrite host part of URL that is used to download Chromium
### class: Puppeteer ### class: Puppeteer

View File

@ -42,7 +42,8 @@ if (NPM_HTTP_PROXY)
process.env.HTTP_PROXY = NPM_HTTP_PROXY; process.env.HTTP_PROXY = NPM_HTTP_PROXY;
const allRevisions = Downloader.downloadedRevisions(); const allRevisions = Downloader.downloadedRevisions();
Downloader.downloadRevision(platform, revision, onProgress) const DOWNLOAD_HOST = process.env.PUPPETEER_DOWNLOAD_HOST || process.env.npm_config_puppeteer_download_host;
Downloader.downloadRevision(platform, revision, DOWNLOAD_HOST, onProgress)
.then(onSuccess) .then(onSuccess)
.catch(onError); .catch(onError);

View File

@ -28,12 +28,13 @@ const ProxyAgent = require('https-proxy-agent');
const getProxyForUrl = require('proxy-from-env').getProxyForUrl; const getProxyForUrl = require('proxy-from-env').getProxyForUrl;
const DOWNLOADS_FOLDER = path.join(__dirname, '..', '.local-chromium'); const DOWNLOADS_FOLDER = path.join(__dirname, '..', '.local-chromium');
const DEFAULT_DOWNLOAD_HOST = 'https://storage.googleapis.com';
const downloadURLs = { const downloadURLs = {
linux: 'https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/%d/chrome-linux.zip', linux: '%s/chromium-browser-snapshots/Linux_x64/%d/chrome-linux.zip',
mac: 'https://storage.googleapis.com/chromium-browser-snapshots/Mac/%d/chrome-mac.zip', mac: '%s/chromium-browser-snapshots/Mac/%d/chrome-mac.zip',
win32: 'https://storage.googleapis.com/chromium-browser-snapshots/Win/%d/chrome-win32.zip', win32: '%s/chromium-browser-snapshots/Win/%d/chrome-win32.zip',
win64: 'https://storage.googleapis.com/chromium-browser-snapshots/Win_x64/%d/chrome-win32.zip', win64: '%s/chromium-browser-snapshots/Win_x64/%d/chrome-win32.zip',
}; };
module.exports = { module.exports = {
@ -66,7 +67,7 @@ module.exports = {
canDownloadRevision: function(platform, revision) { canDownloadRevision: function(platform, revision) {
console.assert(downloadURLs[platform], 'Unknown platform: ' + platform); console.assert(downloadURLs[platform], 'Unknown platform: ' + platform);
const options = requestOptions(util.format(downloadURLs[platform], revision), 'HEAD'); const options = requestOptions(util.format(downloadURLs[platform], DEFAULT_DOWNLOAD_HOST, revision), 'HEAD');
let resolve; let resolve;
const promise = new Promise(x => resolve = x); const promise = new Promise(x => resolve = x);
@ -84,13 +85,14 @@ module.exports = {
/** /**
* @param {string} platform * @param {string} platform
* @param {string} revision * @param {string} revision
* @param {string} [downloadHost=DEFAULT_DOWNLOAD_HOST]
* @param {?function(number, number)} progressCallback * @param {?function(number, number)} progressCallback
* @return {!Promise} * @return {!Promise}
*/ */
downloadRevision: function(platform, revision, progressCallback) { downloadRevision: function(platform, revision, downloadHost = DEFAULT_DOWNLOAD_HOST, progressCallback) {
let url = downloadURLs[platform]; let url = downloadURLs[platform];
console.assert(url, `Unsupported platform: ${platform}`); console.assert(url, `Unsupported platform: ${platform}`);
url = util.format(url, revision); url = util.format(url, downloadHost, revision);
const zipPath = path.join(DOWNLOADS_FOLDER, `download-${platform}-${revision}.zip`); const zipPath = path.join(DOWNLOADS_FOLDER, `download-${platform}-${revision}.zip`);
const folderPath = getFolderPath(platform, revision); const folderPath = getFolderPath(platform, revision);
if (fs.existsSync(folderPath)) if (fs.existsSync(folderPath))
@ -133,7 +135,7 @@ module.exports = {
/** /**
* @param {string} platform * @param {string} platform
* @param {string} revision * @param {string} revision
* @return {!{folderPath: string, executablePath: string, downloaded: boolean, url: string}} * @return {!{folderPath: string, executablePath: string, downloaded: boolean}}
*/ */
revisionInfo: function(platform, revision) { revisionInfo: function(platform, revision) {
console.assert(downloadURLs[platform], `Unsupported platform: ${platform}`); console.assert(downloadURLs[platform], `Unsupported platform: ${platform}`);
@ -150,8 +152,7 @@ module.exports = {
return { return {
executablePath, executablePath,
folderPath, folderPath,
downloaded: fs.existsSync(folderPath), downloaded: fs.existsSync(folderPath)
url: util.format(downloadURLs[platform], revision)
}; };
}, },
}; };