2017-05-15 03:05:41 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 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.
|
|
|
|
*/
|
|
|
|
|
2017-08-02 19:06:47 +00:00
|
|
|
const os = require('os');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const extract = require('extract-zip');
|
|
|
|
const util = require('util');
|
|
|
|
const URL = require('url');
|
|
|
|
const removeRecursive = require('rimraf');
|
2017-10-10 05:31:40 +00:00
|
|
|
// @ts-ignore
|
2017-08-23 15:33:29 +00:00
|
|
|
const ProxyAgent = require('https-proxy-agent');
|
2017-10-10 05:31:40 +00:00
|
|
|
// @ts-ignore
|
2017-08-23 15:33:29 +00:00
|
|
|
const getProxyForUrl = require('proxy-from-env').getProxyForUrl;
|
2017-05-15 03:05:41 +00:00
|
|
|
|
2017-10-22 01:22:13 +00:00
|
|
|
const DEFAULT_DOWNLOAD_HOST = 'https://storage.googleapis.com';
|
2017-08-02 19:06:47 +00:00
|
|
|
const downloadURLs = {
|
2017-10-22 01:22:13 +00:00
|
|
|
linux: '%s/chromium-browser-snapshots/Linux_x64/%d/chrome-linux.zip',
|
|
|
|
mac: '%s/chromium-browser-snapshots/Mac/%d/chrome-mac.zip',
|
|
|
|
win32: '%s/chromium-browser-snapshots/Win/%d/chrome-win32.zip',
|
|
|
|
win64: '%s/chromium-browser-snapshots/Win_x64/%d/chrome-win32.zip',
|
2017-05-15 03:05:41 +00:00
|
|
|
};
|
|
|
|
|
2017-12-08 23:14:28 +00:00
|
|
|
// Project root will be different for node6-transpiled code.
|
|
|
|
const PROJECT_ROOT = fs.existsSync(path.join(__dirname, '..', 'package.json')) ? path.join(__dirname, '..') : path.join(__dirname, '..', '..');
|
2017-12-08 21:39:13 +00:00
|
|
|
|
|
|
|
class Downloader {
|
|
|
|
/**
|
|
|
|
* @param {string} downloadsFolder
|
|
|
|
*/
|
|
|
|
constructor(downloadsFolder) {
|
|
|
|
this._downloadsFolder = downloadsFolder;
|
|
|
|
this._downloadHost = DEFAULT_DOWNLOAD_HOST;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
static defaultRevision() {
|
|
|
|
return require(path.join(PROJECT_ROOT, 'package.json')).puppeteer.chromium_revision;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Downloader}
|
|
|
|
*/
|
|
|
|
static createDefault() {
|
|
|
|
const downloadsFolder = path.join(PROJECT_ROOT, '.local-chromium');
|
|
|
|
return new Downloader(downloadsFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} downloadHost
|
|
|
|
*/
|
|
|
|
setDownloadHost(downloadHost) {
|
2018-01-09 02:08:27 +00:00
|
|
|
this._downloadHost = downloadHost.replace(/\/+$/, '');
|
2017-12-08 21:39:13 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-07-12 21:46:41 +00:00
|
|
|
* @return {!Array<string>}
|
|
|
|
*/
|
2017-12-08 21:39:13 +00:00
|
|
|
supportedPlatforms() {
|
2017-06-21 20:51:06 +00:00
|
|
|
return Object.keys(downloadURLs);
|
2017-12-08 21:39:13 +00:00
|
|
|
}
|
2017-05-15 03:05:41 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-07-12 21:46:41 +00:00
|
|
|
* @return {string}
|
|
|
|
*/
|
2017-12-08 21:39:13 +00:00
|
|
|
currentPlatform() {
|
2017-08-21 23:39:04 +00:00
|
|
|
const platform = os.platform();
|
2017-06-21 20:51:06 +00:00
|
|
|
if (platform === 'darwin')
|
|
|
|
return 'mac';
|
|
|
|
if (platform === 'linux')
|
|
|
|
return 'linux';
|
|
|
|
if (platform === 'win32')
|
|
|
|
return os.arch() === 'x64' ? 'win64' : 'win32';
|
|
|
|
return '';
|
2017-12-08 21:39:13 +00:00
|
|
|
}
|
2017-05-15 03:05:41 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-07-12 21:46:41 +00:00
|
|
|
* @param {string} platform
|
|
|
|
* @param {string} revision
|
|
|
|
* @return {!Promise<boolean>}
|
|
|
|
*/
|
2017-12-08 21:39:13 +00:00
|
|
|
canDownloadRevision(platform, revision) {
|
2017-06-21 20:51:06 +00:00
|
|
|
console.assert(downloadURLs[platform], 'Unknown platform: ' + platform);
|
2017-08-23 15:33:29 +00:00
|
|
|
|
2017-12-08 21:39:13 +00:00
|
|
|
const url = util.format(downloadURLs[platform], this._downloadHost, revision);
|
2017-08-23 15:33:29 +00:00
|
|
|
|
2017-06-22 20:38:10 +00:00
|
|
|
let resolve;
|
2017-08-21 23:39:04 +00:00
|
|
|
const promise = new Promise(x => resolve = x);
|
2017-12-04 21:45:21 +00:00
|
|
|
const request = httpRequest(url, 'HEAD', response => {
|
2017-06-21 20:51:06 +00:00
|
|
|
resolve(response.statusCode === 200);
|
|
|
|
});
|
|
|
|
request.on('error', error => {
|
|
|
|
console.error(error);
|
|
|
|
resolve(false);
|
|
|
|
});
|
|
|
|
return promise;
|
2017-12-08 21:39:13 +00:00
|
|
|
}
|
2017-05-15 03:05:41 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-07-12 21:46:41 +00:00
|
|
|
* @param {string} platform
|
|
|
|
* @param {string} revision
|
|
|
|
* @param {?function(number, number)} progressCallback
|
|
|
|
* @return {!Promise}
|
|
|
|
*/
|
2017-12-08 21:39:13 +00:00
|
|
|
downloadRevision(platform, revision, progressCallback) {
|
2017-06-22 20:38:10 +00:00
|
|
|
let url = downloadURLs[platform];
|
2017-06-21 20:51:06 +00:00
|
|
|
console.assert(url, `Unsupported platform: ${platform}`);
|
2017-12-08 21:39:13 +00:00
|
|
|
url = util.format(url, this._downloadHost, revision);
|
|
|
|
const zipPath = path.join(this._downloadsFolder, `download-${platform}-${revision}.zip`);
|
|
|
|
const folderPath = this._getFolderPath(platform, revision);
|
2017-06-21 20:51:06 +00:00
|
|
|
if (fs.existsSync(folderPath))
|
|
|
|
return;
|
2017-12-08 21:39:13 +00:00
|
|
|
if (!fs.existsSync(this._downloadsFolder))
|
|
|
|
fs.mkdirSync(this._downloadsFolder);
|
2017-08-24 19:20:05 +00:00
|
|
|
return downloadFile(url, zipPath, progressCallback)
|
|
|
|
.then(() => extractZip(zipPath, folderPath))
|
|
|
|
.catch(err => err)
|
2017-09-01 16:47:57 +00:00
|
|
|
.then(err => {
|
2017-08-24 19:20:05 +00:00
|
|
|
if (fs.existsSync(zipPath))
|
|
|
|
fs.unlinkSync(zipPath);
|
2017-09-01 16:47:57 +00:00
|
|
|
if (err)
|
|
|
|
throw err;
|
2017-08-24 19:20:05 +00:00
|
|
|
});
|
2017-12-08 21:39:13 +00:00
|
|
|
}
|
2017-05-15 03:05:41 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-07-12 21:46:41 +00:00
|
|
|
* @return {!Array<!{platform:string, revision: string}>}
|
|
|
|
*/
|
2017-12-08 21:39:13 +00:00
|
|
|
downloadedRevisions() {
|
|
|
|
if (!fs.existsSync(this._downloadsFolder))
|
2017-07-12 21:54:48 +00:00
|
|
|
return [];
|
2017-12-08 21:39:13 +00:00
|
|
|
const fileNames = fs.readdirSync(this._downloadsFolder);
|
2017-07-12 21:46:41 +00:00
|
|
|
return fileNames.map(fileName => parseFolderPath(fileName)).filter(revision => !!revision);
|
2017-12-08 21:39:13 +00:00
|
|
|
}
|
2017-07-12 21:46:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} platform
|
|
|
|
* @param {string} revision
|
2017-08-24 19:20:05 +00:00
|
|
|
* @return {!Promise}
|
2017-07-12 21:46:41 +00:00
|
|
|
*/
|
2017-12-08 21:39:13 +00:00
|
|
|
removeRevision(platform, revision) {
|
2017-07-12 21:46:41 +00:00
|
|
|
console.assert(downloadURLs[platform], `Unsupported platform: ${platform}`);
|
2017-12-08 21:39:13 +00:00
|
|
|
const folderPath = this._getFolderPath(platform, revision);
|
2017-07-12 21:46:41 +00:00
|
|
|
console.assert(fs.existsSync(folderPath));
|
2017-08-24 19:20:05 +00:00
|
|
|
return new Promise(fulfill => removeRecursive(folderPath, fulfill));
|
2017-12-08 21:39:13 +00:00
|
|
|
}
|
2017-07-12 21:46:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} platform
|
|
|
|
* @param {string} revision
|
2017-12-08 21:39:13 +00:00
|
|
|
* @return {!{revision: string, folderPath: string, executablePath: string, downloaded: boolean}}
|
2017-07-12 21:46:41 +00:00
|
|
|
*/
|
2017-12-08 21:39:13 +00:00
|
|
|
revisionInfo(platform, revision) {
|
2017-07-12 21:46:41 +00:00
|
|
|
console.assert(downloadURLs[platform], `Unsupported platform: ${platform}`);
|
2017-12-08 21:39:13 +00:00
|
|
|
const folderPath = this._getFolderPath(platform, revision);
|
2017-06-22 20:38:10 +00:00
|
|
|
let executablePath = '';
|
2017-06-21 20:51:06 +00:00
|
|
|
if (platform === 'mac')
|
|
|
|
executablePath = path.join(folderPath, 'chrome-mac', 'Chromium.app', 'Contents', 'MacOS', 'Chromium');
|
|
|
|
else if (platform === 'linux')
|
|
|
|
executablePath = path.join(folderPath, 'chrome-linux', 'chrome');
|
|
|
|
else if (platform === 'win32' || platform === 'win64')
|
|
|
|
executablePath = path.join(folderPath, 'chrome-win32', 'chrome.exe');
|
|
|
|
else
|
2017-10-18 04:33:16 +00:00
|
|
|
throw 'Unsupported platform: ' + platform;
|
2017-06-21 20:51:06 +00:00
|
|
|
return {
|
2017-12-08 21:39:13 +00:00
|
|
|
revision,
|
2017-08-21 20:34:10 +00:00
|
|
|
executablePath,
|
|
|
|
folderPath,
|
2017-10-22 01:22:13 +00:00
|
|
|
downloaded: fs.existsSync(folderPath)
|
2017-06-21 20:51:06 +00:00
|
|
|
};
|
2017-12-08 21:39:13 +00:00
|
|
|
}
|
2017-05-15 03:05:41 +00:00
|
|
|
|
2017-12-08 21:39:13 +00:00
|
|
|
/**
|
|
|
|
* @param {string} platform
|
|
|
|
* @param {string} revision
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
_getFolderPath(platform, revision) {
|
|
|
|
return path.join(this._downloadsFolder, platform + '-' + revision);
|
|
|
|
}
|
2017-07-12 21:46:41 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 21:39:13 +00:00
|
|
|
module.exports = Downloader;
|
|
|
|
|
2017-07-12 21:46:41 +00:00
|
|
|
/**
|
|
|
|
* @param {string} folderPath
|
|
|
|
* @return {?{platform: string, revision: string}}
|
|
|
|
*/
|
|
|
|
function parseFolderPath(folderPath) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const name = path.basename(folderPath);
|
|
|
|
const splits = name.split('-');
|
2017-07-12 21:46:41 +00:00
|
|
|
if (splits.length !== 2)
|
|
|
|
return null;
|
2017-08-21 23:39:04 +00:00
|
|
|
const [platform, revision] = splits;
|
2017-07-12 21:46:41 +00:00
|
|
|
if (!downloadURLs[platform])
|
|
|
|
return null;
|
|
|
|
return {platform, revision};
|
2017-05-15 03:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} url
|
|
|
|
* @param {string} destinationPath
|
|
|
|
* @param {?function(number, number)} progressCallback
|
|
|
|
* @return {!Promise}
|
|
|
|
*/
|
|
|
|
function downloadFile(url, destinationPath, progressCallback) {
|
2017-06-22 20:38:10 +00:00
|
|
|
let fulfill, reject;
|
2017-08-23 15:33:29 +00:00
|
|
|
|
2017-08-21 23:39:04 +00:00
|
|
|
const promise = new Promise((x, y) => { fulfill = x; reject = y; });
|
2017-08-23 15:33:29 +00:00
|
|
|
|
2017-12-04 21:45:21 +00:00
|
|
|
const request = httpRequest(url, 'GET', response => {
|
2017-06-21 20:51:06 +00:00
|
|
|
if (response.statusCode !== 200) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const error = new Error(`Download failed: server returned code ${response.statusCode}. URL: ${url}`);
|
2017-06-21 20:51:06 +00:00
|
|
|
// consume response data to free up memory
|
|
|
|
response.resume();
|
|
|
|
reject(error);
|
|
|
|
return;
|
2017-06-21 20:36:04 +00:00
|
|
|
}
|
2017-08-21 23:39:04 +00:00
|
|
|
const file = fs.createWriteStream(destinationPath);
|
2017-06-21 20:51:06 +00:00
|
|
|
file.on('finish', () => fulfill());
|
|
|
|
file.on('error', error => reject(error));
|
|
|
|
response.pipe(file);
|
2017-10-10 05:31:40 +00:00
|
|
|
const totalBytes = parseInt(/** @type {string} */ (response.headers['content-length']), 10);
|
2017-06-21 20:51:06 +00:00
|
|
|
if (progressCallback)
|
|
|
|
response.on('data', onData.bind(null, totalBytes));
|
|
|
|
});
|
|
|
|
request.on('error', error => reject(error));
|
|
|
|
return promise;
|
|
|
|
|
|
|
|
function onData(totalBytes, chunk) {
|
|
|
|
progressCallback(totalBytes, chunk.length);
|
|
|
|
}
|
2017-05-15 03:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} zipPath
|
|
|
|
* @param {string} folderPath
|
|
|
|
* @return {!Promise<?Error>}
|
|
|
|
*/
|
|
|
|
function extractZip(zipPath, folderPath) {
|
2017-06-21 20:51:06 +00:00
|
|
|
return new Promise(fulfill => extract(zipPath, {dir: folderPath}, fulfill));
|
2017-05-15 03:05:41 +00:00
|
|
|
}
|
2017-08-23 15:33:29 +00:00
|
|
|
|
2017-12-04 21:45:21 +00:00
|
|
|
function httpRequest(url, method, response) {
|
2017-10-10 05:31:40 +00:00
|
|
|
/** @type {Object} */
|
2017-12-04 21:45:21 +00:00
|
|
|
const options = URL.parse(url);
|
|
|
|
options.method = method;
|
2017-08-23 15:33:29 +00:00
|
|
|
|
|
|
|
const proxyURL = getProxyForUrl(url);
|
|
|
|
if (proxyURL) {
|
2017-10-10 05:31:40 +00:00
|
|
|
/** @type {Object} */
|
2017-08-23 15:33:29 +00:00
|
|
|
const parsedProxyURL = URL.parse(proxyURL);
|
|
|
|
parsedProxyURL.secureProxy = parsedProxyURL.protocol === 'https:';
|
2017-12-04 21:45:21 +00:00
|
|
|
|
|
|
|
options.agent = new ProxyAgent(parsedProxyURL);
|
|
|
|
options.rejectUnauthorized = false;
|
2017-08-23 15:33:29 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 21:45:21 +00:00
|
|
|
const driver = options.protocol === 'https:' ? 'https' : 'http';
|
|
|
|
const request = require(driver).request(options, response);
|
|
|
|
request.end();
|
|
|
|
return request;
|
2017-09-01 16:47:57 +00:00
|
|
|
}
|