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');
|
2018-02-07 17:31:53 +00:00
|
|
|
const {helper} = require('./helper');
|
2017-08-02 19:06:47 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2018-02-07 17:31:53 +00:00
|
|
|
const readdirAsync = helper.promisify(fs.readdir.bind(fs));
|
|
|
|
const mkdirAsync = helper.promisify(fs.mkdir.bind(fs));
|
|
|
|
const unlinkAsync = helper.promisify(fs.unlink.bind(fs));
|
2018-04-10 21:11:59 +00:00
|
|
|
const chmodAsync = helper.promisify(fs.chmod.bind(fs));
|
2017-12-08 21:39:13 +00:00
|
|
|
|
2018-02-07 17:31:53 +00:00
|
|
|
function existsAsync(filePath) {
|
|
|
|
let fulfill = null;
|
|
|
|
const promise = new Promise(x => fulfill = x);
|
|
|
|
fs.access(filePath, err => fulfill(!err));
|
|
|
|
return promise;
|
|
|
|
}
|
2017-12-08 21:39:13 +00:00
|
|
|
|
2018-02-07 17:31:53 +00:00
|
|
|
class BrowserFetcher {
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2018-02-07 17:31:53 +00:00
|
|
|
* @param {!BrowserFetcher.Options=} options
|
2017-07-12 21:46:41 +00:00
|
|
|
*/
|
2018-02-07 17:31:53 +00:00
|
|
|
constructor(options = {}) {
|
|
|
|
this._downloadsFolder = options.path || path.join(helper.projectRoot(), '.local-chromium');
|
|
|
|
this._downloadHost = options.host || DEFAULT_DOWNLOAD_HOST;
|
|
|
|
this._platform = options.platform || '';
|
|
|
|
if (!this._platform) {
|
|
|
|
const platform = os.platform();
|
|
|
|
if (platform === 'darwin')
|
|
|
|
this._platform = 'mac';
|
|
|
|
else if (platform === 'linux')
|
|
|
|
this._platform = 'linux';
|
|
|
|
else if (platform === 'win32')
|
|
|
|
this._platform = os.arch() === 'x64' ? 'win64' : 'win32';
|
|
|
|
console.assert(this._platform, 'Unsupported platform: ' + os.platform());
|
|
|
|
}
|
|
|
|
const supportedPlatforms = ['mac', 'linux', 'win32', 'win64'];
|
|
|
|
console.assert(supportedPlatforms.includes(this._platform), 'Unsupported platform: ' + this._platform);
|
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}
|
|
|
|
*/
|
2018-02-07 17:31:53 +00:00
|
|
|
platform() {
|
|
|
|
return this._platform;
|
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} revision
|
|
|
|
* @return {!Promise<boolean>}
|
|
|
|
*/
|
2018-02-07 17:31:53 +00:00
|
|
|
canDownload(revision) {
|
|
|
|
const url = util.format(downloadURLs[this._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} revision
|
|
|
|
* @param {?function(number, number)} progressCallback
|
2018-02-07 17:31:53 +00:00
|
|
|
* @return {!Promise<!BrowserFetcher.RevisionInfo>}
|
2017-07-12 21:46:41 +00:00
|
|
|
*/
|
2018-02-07 17:31:53 +00:00
|
|
|
async download(revision, progressCallback) {
|
|
|
|
let url = downloadURLs[this._platform];
|
2017-12-08 21:39:13 +00:00
|
|
|
url = util.format(url, this._downloadHost, revision);
|
2018-02-07 17:31:53 +00:00
|
|
|
const zipPath = path.join(this._downloadsFolder, `download-${this._platform}-${revision}.zip`);
|
|
|
|
const folderPath = this._getFolderPath(revision);
|
|
|
|
if (await existsAsync(folderPath))
|
|
|
|
return this.revisionInfo(revision);
|
|
|
|
if (!(await existsAsync(this._downloadsFolder)))
|
|
|
|
await mkdirAsync(this._downloadsFolder);
|
|
|
|
try {
|
|
|
|
await downloadFile(url, zipPath, progressCallback);
|
|
|
|
await extractZip(zipPath, folderPath);
|
|
|
|
} finally {
|
|
|
|
if (await existsAsync(zipPath))
|
|
|
|
await unlinkAsync(zipPath);
|
|
|
|
}
|
2018-04-10 21:11:59 +00:00
|
|
|
const revisionInfo = this.revisionInfo(revision);
|
|
|
|
if (revisionInfo)
|
|
|
|
await chmodAsync(revisionInfo.executablePath, 0o755);
|
|
|
|
return revisionInfo;
|
2017-12-08 21:39:13 +00:00
|
|
|
}
|
2017-05-15 03:05:41 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2018-02-07 17:31:53 +00:00
|
|
|
* @return {!Promise<!Array<string>>}
|
2017-07-12 21:46:41 +00:00
|
|
|
*/
|
2018-02-07 17:31:53 +00:00
|
|
|
async localRevisions() {
|
|
|
|
if (!await existsAsync(this._downloadsFolder))
|
2017-07-12 21:54:48 +00:00
|
|
|
return [];
|
2018-02-07 17:31:53 +00:00
|
|
|
const fileNames = await readdirAsync(this._downloadsFolder);
|
|
|
|
return fileNames.map(fileName => parseFolderPath(fileName)).filter(entry => entry && entry.platform === this._platform).map(entry => entry.revision);
|
2017-12-08 21:39:13 +00:00
|
|
|
}
|
2017-07-12 21:46:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} revision
|
2017-08-24 19:20:05 +00:00
|
|
|
* @return {!Promise}
|
2017-07-12 21:46:41 +00:00
|
|
|
*/
|
2018-02-07 17:31:53 +00:00
|
|
|
async remove(revision) {
|
|
|
|
const folderPath = this._getFolderPath(revision);
|
|
|
|
console.assert(await existsAsync(folderPath), `Failed to remove: revision ${revision} is not downloaded`);
|
|
|
|
await new Promise(fulfill => removeRecursive(folderPath, fulfill));
|
2017-12-08 21:39:13 +00:00
|
|
|
}
|
2017-07-12 21:46:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} revision
|
2018-02-07 17:31:53 +00:00
|
|
|
* @return {!BrowserFetcher.RevisionInfo}
|
2017-07-12 21:46:41 +00:00
|
|
|
*/
|
2018-02-07 17:31:53 +00:00
|
|
|
revisionInfo(revision) {
|
|
|
|
const folderPath = this._getFolderPath(revision);
|
2017-06-22 20:38:10 +00:00
|
|
|
let executablePath = '';
|
2018-02-07 17:31:53 +00:00
|
|
|
if (this._platform === 'mac')
|
2017-06-21 20:51:06 +00:00
|
|
|
executablePath = path.join(folderPath, 'chrome-mac', 'Chromium.app', 'Contents', 'MacOS', 'Chromium');
|
2018-02-07 17:31:53 +00:00
|
|
|
else if (this._platform === 'linux')
|
2017-06-21 20:51:06 +00:00
|
|
|
executablePath = path.join(folderPath, 'chrome-linux', 'chrome');
|
2018-02-07 17:31:53 +00:00
|
|
|
else if (this._platform === 'win32' || this._platform === 'win64')
|
2017-06-21 20:51:06 +00:00
|
|
|
executablePath = path.join(folderPath, 'chrome-win32', 'chrome.exe');
|
|
|
|
else
|
2018-02-07 17:31:53 +00:00
|
|
|
throw 'Unsupported platform: ' + this._platform;
|
|
|
|
let url = downloadURLs[this._platform];
|
|
|
|
url = util.format(url, this._downloadHost, revision);
|
|
|
|
const local = fs.existsSync(folderPath);
|
|
|
|
return {revision, executablePath, folderPath, local, url};
|
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} revision
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2018-02-07 17:31:53 +00:00
|
|
|
_getFolderPath(revision) {
|
|
|
|
return path.join(this._downloadsFolder, this._platform + '-' + revision);
|
2017-12-08 21:39:13 +00:00
|
|
|
}
|
2017-07-12 21:46:41 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 17:31:53 +00:00
|
|
|
module.exports = BrowserFetcher;
|
2017-12-08 21:39:13 +00:00
|
|
|
|
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;
|
2018-02-07 17:31:53 +00:00
|
|
|
let downloadedBytes = 0;
|
|
|
|
let totalBytes = 0;
|
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);
|
2018-02-07 17:31:53 +00:00
|
|
|
totalBytes = parseInt(/** @type {string} */ (response.headers['content-length']), 10);
|
2017-06-21 20:51:06 +00:00
|
|
|
if (progressCallback)
|
2018-02-07 17:31:53 +00:00
|
|
|
response.on('data', onData);
|
2017-06-21 20:51:06 +00:00
|
|
|
});
|
|
|
|
request.on('error', error => reject(error));
|
|
|
|
return promise;
|
|
|
|
|
2018-02-07 17:31:53 +00:00
|
|
|
function onData(chunk) {
|
|
|
|
downloadedBytes += chunk.length;
|
|
|
|
progressCallback(downloadedBytes, totalBytes);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
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';
|
2018-01-09 02:10:13 +00:00
|
|
|
const request = require(driver).request(options, res => {
|
|
|
|
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location)
|
|
|
|
httpRequest(res.headers.location, method, response);
|
|
|
|
else
|
|
|
|
response(res);
|
|
|
|
});
|
2017-12-04 21:45:21 +00:00
|
|
|
request.end();
|
|
|
|
return request;
|
2017-09-01 16:47:57 +00:00
|
|
|
}
|
2018-02-07 17:31:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} BrowserFetcher.Options
|
|
|
|
* @property {string=} platform
|
|
|
|
* @property {string=} path
|
|
|
|
* @property {string=} host
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} BrowserFetcher.RevisionInfo
|
|
|
|
* @property {string} folderPath
|
|
|
|
* @property {string} executablePath
|
|
|
|
* @property {string} url
|
|
|
|
* @property {boolean} local
|
|
|
|
* @property {string} revision
|
|
|
|
*/
|