mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
refactor: move ChromiumDownloader under lib/ (#1554)
This patch: - renames ChromiumDownloader into just Downloader (this is in preparation for different products download) - moves Downloader from utils/ to lib/. This unifies all of the production-critical code in the lib/. Drive-by: make Downloader a regular class.
This commit is contained in:
@@ -1,245 +0,0 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
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');
|
||||
// @ts-ignore
|
||||
const ProxyAgent = require('https-proxy-agent');
|
||||
// @ts-ignore
|
||||
const getProxyForUrl = require('proxy-from-env').getProxyForUrl;
|
||||
|
||||
const DOWNLOADS_FOLDER = path.join(__dirname, '..', '.local-chromium');
|
||||
const DEFAULT_DOWNLOAD_HOST = 'https://storage.googleapis.com';
|
||||
|
||||
const downloadURLs = {
|
||||
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',
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
/**
|
||||
* @return {!Array<string>}
|
||||
*/
|
||||
supportedPlatforms: function() {
|
||||
return Object.keys(downloadURLs);
|
||||
},
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
currentPlatform: function() {
|
||||
const platform = os.platform();
|
||||
if (platform === 'darwin')
|
||||
return 'mac';
|
||||
if (platform === 'linux')
|
||||
return 'linux';
|
||||
if (platform === 'win32')
|
||||
return os.arch() === 'x64' ? 'win64' : 'win32';
|
||||
return '';
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {string} platform
|
||||
* @param {string} revision
|
||||
* @return {!Promise<boolean>}
|
||||
*/
|
||||
canDownloadRevision: function(platform, revision) {
|
||||
console.assert(downloadURLs[platform], 'Unknown platform: ' + platform);
|
||||
|
||||
const url = util.format(downloadURLs[platform], DEFAULT_DOWNLOAD_HOST, revision);
|
||||
|
||||
let resolve;
|
||||
const promise = new Promise(x => resolve = x);
|
||||
const request = httpRequest(url, 'HEAD', response => {
|
||||
resolve(response.statusCode === 200);
|
||||
});
|
||||
request.on('error', error => {
|
||||
console.error(error);
|
||||
resolve(false);
|
||||
});
|
||||
return promise;
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {string} platform
|
||||
* @param {string} revision
|
||||
* @param {string} [downloadHost=DEFAULT_DOWNLOAD_HOST]
|
||||
* @param {?function(number, number)} progressCallback
|
||||
* @return {!Promise}
|
||||
*/
|
||||
downloadRevision: function(platform, revision, downloadHost = DEFAULT_DOWNLOAD_HOST, progressCallback) {
|
||||
let url = downloadURLs[platform];
|
||||
console.assert(url, `Unsupported platform: ${platform}`);
|
||||
url = util.format(url, downloadHost, revision);
|
||||
const zipPath = path.join(DOWNLOADS_FOLDER, `download-${platform}-${revision}.zip`);
|
||||
const folderPath = getFolderPath(platform, revision);
|
||||
if (fs.existsSync(folderPath))
|
||||
return;
|
||||
if (!fs.existsSync(DOWNLOADS_FOLDER))
|
||||
fs.mkdirSync(DOWNLOADS_FOLDER);
|
||||
return downloadFile(url, zipPath, progressCallback)
|
||||
.then(() => extractZip(zipPath, folderPath))
|
||||
.catch(err => err)
|
||||
.then(err => {
|
||||
if (fs.existsSync(zipPath))
|
||||
fs.unlinkSync(zipPath);
|
||||
if (err)
|
||||
throw err;
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* @return {!Array<!{platform:string, revision: string}>}
|
||||
*/
|
||||
downloadedRevisions: function() {
|
||||
if (!fs.existsSync(DOWNLOADS_FOLDER))
|
||||
return [];
|
||||
const fileNames = fs.readdirSync(DOWNLOADS_FOLDER);
|
||||
return fileNames.map(fileName => parseFolderPath(fileName)).filter(revision => !!revision);
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {string} platform
|
||||
* @param {string} revision
|
||||
* @return {!Promise}
|
||||
*/
|
||||
removeRevision: function(platform, revision) {
|
||||
console.assert(downloadURLs[platform], `Unsupported platform: ${platform}`);
|
||||
const folderPath = getFolderPath(platform, revision);
|
||||
console.assert(fs.existsSync(folderPath));
|
||||
return new Promise(fulfill => removeRecursive(folderPath, fulfill));
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {string} platform
|
||||
* @param {string} revision
|
||||
* @return {!{folderPath: string, executablePath: string, downloaded: boolean}}
|
||||
*/
|
||||
revisionInfo: function(platform, revision) {
|
||||
console.assert(downloadURLs[platform], `Unsupported platform: ${platform}`);
|
||||
const folderPath = getFolderPath(platform, revision);
|
||||
let executablePath = '';
|
||||
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
|
||||
throw 'Unsupported platform: ' + platform;
|
||||
return {
|
||||
executablePath,
|
||||
folderPath,
|
||||
downloaded: fs.existsSync(folderPath)
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} platform
|
||||
* @param {string} revision
|
||||
* @return {string}
|
||||
*/
|
||||
function getFolderPath(platform, revision) {
|
||||
return path.join(DOWNLOADS_FOLDER, platform + '-' + revision);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} folderPath
|
||||
* @return {?{platform: string, revision: string}}
|
||||
*/
|
||||
function parseFolderPath(folderPath) {
|
||||
const name = path.basename(folderPath);
|
||||
const splits = name.split('-');
|
||||
if (splits.length !== 2)
|
||||
return null;
|
||||
const [platform, revision] = splits;
|
||||
if (!downloadURLs[platform])
|
||||
return null;
|
||||
return {platform, revision};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} url
|
||||
* @param {string} destinationPath
|
||||
* @param {?function(number, number)} progressCallback
|
||||
* @return {!Promise}
|
||||
*/
|
||||
function downloadFile(url, destinationPath, progressCallback) {
|
||||
let fulfill, reject;
|
||||
|
||||
const promise = new Promise((x, y) => { fulfill = x; reject = y; });
|
||||
|
||||
const request = httpRequest(url, 'GET', 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
|
||||
response.resume();
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
const file = fs.createWriteStream(destinationPath);
|
||||
file.on('finish', () => fulfill());
|
||||
file.on('error', error => reject(error));
|
||||
response.pipe(file);
|
||||
const totalBytes = parseInt(/** @type {string} */ (response.headers['content-length']), 10);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} zipPath
|
||||
* @param {string} folderPath
|
||||
* @return {!Promise<?Error>}
|
||||
*/
|
||||
function extractZip(zipPath, folderPath) {
|
||||
return new Promise(fulfill => extract(zipPath, {dir: folderPath}, fulfill));
|
||||
}
|
||||
|
||||
function httpRequest(url, method, response) {
|
||||
/** @type {Object} */
|
||||
const options = URL.parse(url);
|
||||
options.method = method;
|
||||
|
||||
const proxyURL = getProxyForUrl(url);
|
||||
if (proxyURL) {
|
||||
/** @type {Object} */
|
||||
const parsedProxyURL = URL.parse(proxyURL);
|
||||
parsedProxyURL.secureProxy = parsedProxyURL.protocol === 'https:';
|
||||
|
||||
options.agent = new ProxyAgent(parsedProxyURL);
|
||||
options.rejectUnauthorized = false;
|
||||
}
|
||||
|
||||
const driver = options.protocol === 'https:' ? 'https' : 'http';
|
||||
const request = require(driver).request(options, response);
|
||||
request.end();
|
||||
return request;
|
||||
}
|
||||
@@ -15,10 +15,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
const Downloader = require('./ChromiumDownloader');
|
||||
const Downloader = require('../lib/Downloader');
|
||||
const https = require('https');
|
||||
const OMAHA_PROXY = 'https://omahaproxy.appspot.com/all.json';
|
||||
|
||||
const downloader = Downloader.createDefault();
|
||||
|
||||
const colors = {
|
||||
reset: '\x1b[0m',
|
||||
red: '\x1b[31m',
|
||||
@@ -71,7 +73,7 @@ async function checkOmahaProxyAvailability() {
|
||||
return;
|
||||
}
|
||||
const table = new Table([27, 7, 7, 7, 7]);
|
||||
table.drawRow([''].concat(Downloader.supportedPlatforms()));
|
||||
table.drawRow([''].concat(downloader.supportedPlatforms()));
|
||||
for (const platform of platforms) {
|
||||
// Trust only to the main platforms.
|
||||
if (platform.os !== 'mac' && platform.os !== 'win' && platform.os !== 'win64' && platform.os !== 'linux')
|
||||
@@ -93,7 +95,7 @@ async function checkOmahaProxyAvailability() {
|
||||
*/
|
||||
async function checkRangeAvailability(fromRevision, toRevision) {
|
||||
const table = new Table([10, 7, 7, 7, 7]);
|
||||
table.drawRow([''].concat(Downloader.supportedPlatforms()));
|
||||
table.drawRow([''].concat(downloader.supportedPlatforms()));
|
||||
const inc = fromRevision < toRevision ? 1 : -1;
|
||||
for (let revision = fromRevision; revision !== toRevision; revision += inc)
|
||||
await checkAndDrawRevisionAvailability(table, '', revision);
|
||||
@@ -106,8 +108,8 @@ async function checkRangeAvailability(fromRevision, toRevision) {
|
||||
*/
|
||||
async function checkAndDrawRevisionAvailability(table, name, revision) {
|
||||
const promises = [];
|
||||
for (const platform of Downloader.supportedPlatforms())
|
||||
promises.push(Downloader.canDownloadRevision(platform, revision));
|
||||
for (const platform of downloader.supportedPlatforms())
|
||||
promises.push(downloader.canDownloadRevision(platform, revision));
|
||||
const availability = await Promise.all(promises);
|
||||
const allAvailable = availability.every(e => !!e);
|
||||
const values = [name + ' ' + (allAvailable ? colors.green + revision + colors.reset : revision)];
|
||||
|
||||
@@ -21,6 +21,7 @@ const Message = require('../Message');
|
||||
|
||||
const EXCLUDE_CLASSES = new Set([
|
||||
'Connection',
|
||||
'Downloader',
|
||||
'EmulationManager',
|
||||
'FrameManager',
|
||||
'Helper',
|
||||
|
||||
Reference in New Issue
Block a user