UNPKG

9.01 kBJavaScriptView Raw
1/**
2 * Copyright 2017 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17const os = require('os');
18const fs = require('fs');
19const path = require('path');
20const extract = require('extract-zip');
21const util = require('util');
22const URL = require('url');
23const {helper, assert} = require('./helper');
24const removeRecursive = require('rimraf');
25// @ts-ignore
26const ProxyAgent = require('https-proxy-agent');
27// @ts-ignore
28const getProxyForUrl = require('proxy-from-env').getProxyForUrl;
29
30const DEFAULT_DOWNLOAD_HOST = 'https://storage.googleapis.com';
31const downloadURLs = {
32 linux: '%s/chromium-browser-snapshots/Linux_x64/%d/chrome-linux.zip',
33 mac: '%s/chromium-browser-snapshots/Mac/%d/chrome-mac.zip',
34 win32: '%s/chromium-browser-snapshots/Win/%d/chrome-win32.zip',
35 win64: '%s/chromium-browser-snapshots/Win_x64/%d/chrome-win32.zip',
36};
37
38const readdirAsync = helper.promisify(fs.readdir.bind(fs));
39const mkdirAsync = helper.promisify(fs.mkdir.bind(fs));
40const unlinkAsync = helper.promisify(fs.unlink.bind(fs));
41const chmodAsync = helper.promisify(fs.chmod.bind(fs));
42
43function existsAsync(filePath) {
44 let fulfill = null;
45 const promise = new Promise(x => fulfill = x);
46 fs.access(filePath, err => fulfill(!err));
47 return promise;
48}
49
50class BrowserFetcher {
51 /**
52 * @param {!BrowserFetcher.Options=} options
53 */
54 constructor(options = {}) {
55 this._downloadsFolder = options.path || path.join(helper.projectRoot(), '.local-chromium');
56 this._downloadHost = options.host || DEFAULT_DOWNLOAD_HOST;
57 this._platform = options.platform || '';
58 if (!this._platform) {
59 const platform = os.platform();
60 if (platform === 'darwin')
61 this._platform = 'mac';
62 else if (platform === 'linux')
63 this._platform = 'linux';
64 else if (platform === 'win32')
65 this._platform = os.arch() === 'x64' ? 'win64' : 'win32';
66 assert(this._platform, 'Unsupported platform: ' + os.platform());
67 }
68 const supportedPlatforms = ['mac', 'linux', 'win32', 'win64'];
69 assert(supportedPlatforms.includes(this._platform), 'Unsupported platform: ' + this._platform);
70 }
71
72 /**
73 * @return {string}
74 */
75 platform() {
76 return this._platform;
77 }
78
79 /**
80 * @param {string} revision
81 * @return {!Promise<boolean>}
82 */
83 canDownload(revision) {
84 const url = util.format(downloadURLs[this._platform], this._downloadHost, revision);
85
86 let resolve;
87 const promise = new Promise(x => resolve = x);
88 const request = httpRequest(url, 'HEAD', response => {
89 resolve(response.statusCode === 200);
90 });
91 request.on('error', error => {
92 console.error(error);
93 resolve(false);
94 });
95 return promise;
96 }
97
98 /**
99 * @param {string} revision
100 * @param {?function(number, number)} progressCallback
101 * @return {!Promise<!BrowserFetcher.RevisionInfo>}
102 */
103 async download(revision, progressCallback) {
104 let url = downloadURLs[this._platform];
105 url = util.format(url, this._downloadHost, revision);
106 const zipPath = path.join(this._downloadsFolder, `download-${this._platform}-${revision}.zip`);
107 const folderPath = this._getFolderPath(revision);
108 if (await existsAsync(folderPath))
109 return this.revisionInfo(revision);
110 if (!(await existsAsync(this._downloadsFolder)))
111 await mkdirAsync(this._downloadsFolder);
112 try {
113 await downloadFile(url, zipPath, progressCallback);
114 await extractZip(zipPath, folderPath);
115 } finally {
116 if (await existsAsync(zipPath))
117 await unlinkAsync(zipPath);
118 }
119 const revisionInfo = this.revisionInfo(revision);
120 if (revisionInfo)
121 await chmodAsync(revisionInfo.executablePath, 0o755);
122 return revisionInfo;
123 }
124
125 /**
126 * @return {!Promise<!Array<string>>}
127 */
128 async localRevisions() {
129 if (!await existsAsync(this._downloadsFolder))
130 return [];
131 const fileNames = await readdirAsync(this._downloadsFolder);
132 return fileNames.map(fileName => parseFolderPath(fileName)).filter(entry => entry && entry.platform === this._platform).map(entry => entry.revision);
133 }
134
135 /**
136 * @param {string} revision
137 * @return {!Promise}
138 */
139 async remove(revision) {
140 const folderPath = this._getFolderPath(revision);
141 assert(await existsAsync(folderPath), `Failed to remove: revision ${revision} is not downloaded`);
142 await new Promise(fulfill => removeRecursive(folderPath, fulfill));
143 }
144
145 /**
146 * @param {string} revision
147 * @return {!BrowserFetcher.RevisionInfo}
148 */
149 revisionInfo(revision) {
150 const folderPath = this._getFolderPath(revision);
151 let executablePath = '';
152 if (this._platform === 'mac')
153 executablePath = path.join(folderPath, 'chrome-mac', 'Chromium.app', 'Contents', 'MacOS', 'Chromium');
154 else if (this._platform === 'linux')
155 executablePath = path.join(folderPath, 'chrome-linux', 'chrome');
156 else if (this._platform === 'win32' || this._platform === 'win64')
157 executablePath = path.join(folderPath, 'chrome-win32', 'chrome.exe');
158 else
159 throw new Error('Unsupported platform: ' + this._platform);
160 let url = downloadURLs[this._platform];
161 url = util.format(url, this._downloadHost, revision);
162 const local = fs.existsSync(folderPath);
163 return {revision, executablePath, folderPath, local, url};
164 }
165
166 /**
167 * @param {string} revision
168 * @return {string}
169 */
170 _getFolderPath(revision) {
171 return path.join(this._downloadsFolder, this._platform + '-' + revision);
172 }
173}
174
175module.exports = BrowserFetcher;
176
177/**
178 * @param {string} folderPath
179 * @return {?{platform: string, revision: string}}
180 */
181function parseFolderPath(folderPath) {
182 const name = path.basename(folderPath);
183 const splits = name.split('-');
184 if (splits.length !== 2)
185 return null;
186 const [platform, revision] = splits;
187 if (!downloadURLs[platform])
188 return null;
189 return {platform, revision};
190}
191
192/**
193 * @param {string} url
194 * @param {string} destinationPath
195 * @param {?function(number, number)} progressCallback
196 * @return {!Promise}
197 */
198function downloadFile(url, destinationPath, progressCallback) {
199 let fulfill, reject;
200 let downloadedBytes = 0;
201 let totalBytes = 0;
202
203 const promise = new Promise((x, y) => { fulfill = x; reject = y; });
204
205 const request = httpRequest(url, 'GET', response => {
206 if (response.statusCode !== 200) {
207 const error = new Error(`Download failed: server returned code ${response.statusCode}. URL: ${url}`);
208 // consume response data to free up memory
209 response.resume();
210 reject(error);
211 return;
212 }
213 const file = fs.createWriteStream(destinationPath);
214 file.on('finish', () => fulfill());
215 file.on('error', error => reject(error));
216 response.pipe(file);
217 totalBytes = parseInt(/** @type {string} */ (response.headers['content-length']), 10);
218 if (progressCallback)
219 response.on('data', onData);
220 });
221 request.on('error', error => reject(error));
222 return promise;
223
224 function onData(chunk) {
225 downloadedBytes += chunk.length;
226 progressCallback(downloadedBytes, totalBytes);
227 }
228}
229
230/**
231 * @param {string} zipPath
232 * @param {string} folderPath
233 * @return {!Promise<?Error>}
234 */
235function extractZip(zipPath, folderPath) {
236 return new Promise((fulfill, reject) => extract(zipPath, {dir: folderPath}, err => {
237 if (err)
238 reject(err);
239 else
240 fulfill();
241 }));
242}
243
244function httpRequest(url, method, response) {
245 /** @type {Object} */
246 const options = URL.parse(url);
247 options.method = method;
248
249 const proxyURL = getProxyForUrl(url);
250 if (proxyURL) {
251 /** @type {Object} */
252 const parsedProxyURL = URL.parse(proxyURL);
253 parsedProxyURL.secureProxy = parsedProxyURL.protocol === 'https:';
254
255 options.agent = new ProxyAgent(parsedProxyURL);
256 options.rejectUnauthorized = false;
257 }
258
259 const driver = options.protocol === 'https:' ? 'https' : 'http';
260 const request = require(driver).request(options, res => {
261 if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location)
262 httpRequest(res.headers.location, method, response);
263 else
264 response(res);
265 });
266 request.end();
267 return request;
268}
269
270/**
271 * @typedef {Object} BrowserFetcher.Options
272 * @property {string=} platform
273 * @property {string=} path
274 * @property {string=} host
275 */
276
277/**
278 * @typedef {Object} BrowserFetcher.RevisionInfo
279 * @property {string} folderPath
280 * @property {string} executablePath
281 * @property {string} url
282 * @property {boolean} local
283 * @property {string} revision
284 */