Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: chromium_process.js

Issue 29435631: Issue 5230 - Properly download Chromium on macOS (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created May 10, 2017, 2:13 p.m.
Right Patch Set: Addressed review comments. Created May 18, 2017, 1:22 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | package.json » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 /* eslint-env node */ 18 /* eslint-env node */
19 /* eslint no-console: "off" */ 19 /* eslint no-console: "off" */
20 20
21 "use strict"; 21 "use strict";
22 22
23 const childProcess = require("child_process"); 23 const childProcess = require("child_process");
24 const fs = require("fs"); 24 const fs = require("fs");
25 const https = require("https"); 25 const https = require("https");
26 const os = require("os"); 26 const os = require("os");
27 const path = require("path"); 27 const path = require("path");
28 28
29 const remoteInterface = require("chrome-remote-interface"); 29 const remoteInterface = require("chrome-remote-interface");
30 const DecompressZip = require('decompress-zip'); 30 const extractZip = require("extract-zip");
hub 2017/05/10 14:15:27 I had to use a different module that actually work
Wladimir Palant 2017/05/16 14:21:15 If we don't need streaming support then we should
hub 2017/05/16 15:49:30 yauzl doesn't have an easy "extract archive API".
Wladimir Palant 2017/05/17 10:37:02 You are right. extract-zip sounds good.
hub 2017/05/17 17:25:02 Done.
31 31
32 const CHROMIUM_REVISION = 467222; 32 const CHROMIUM_REVISION = 467222;
33 33
34 function rmdir(dirPath) 34 function rmdir(dirPath)
35 { 35 {
36 for (let file of fs.readdirSync(dirPath)) 36 for (let file of fs.readdirSync(dirPath))
37 { 37 {
38 let filePath = path.join(dirPath, file); 38 let filePath = path.join(dirPath, file);
39 try 39 try
40 { 40 {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 "linux": ["Linux_x64", "chrome-linux.zip"], 86 "linux": ["Linux_x64", "chrome-linux.zip"],
87 "darwin": ["Mac", "chrome-mac.zip"] 87 "darwin": ["Mac", "chrome-mac.zip"]
88 }; 88 };
89 89
90 if (!buildTypes.hasOwnProperty(platform)) 90 if (!buildTypes.hasOwnProperty(platform))
91 { 91 {
92 let err = new Error(`Cannot run browser tests, ${platform} is unsupported`); 92 let err = new Error(`Cannot run browser tests, ${platform} is unsupported`);
93 return Promise.reject(err); 93 return Promise.reject(err);
94 } 94 }
95 95
96 let chromiumDownloadCache = path.join(__dirname, "chromium-snapshots", 96
97 "download-cache"); 97 return Promise.resolve().then(() =>
98 let chromiumDir = path.join(__dirname, "chromium-snapshots", 98 {
99 `chromium-${platform}-${CHROMIUM_REVISION}`); 99 let snapshotsDir = path.join(__dirname, "chromium-snapshots");
100 if (fs.existsSync(chromiumDir)) 100 let chromiumDir = path.join(snapshotsDir,
101 return Promise.resolve(getChromiumExecutable(chromiumDir)); 101 `chromium-${platform}-${CHROMIUM_REVISION}`);
102 102 if (fs.existsSync(chromiumDir))
103 if (!fs.existsSync(path.dirname(chromiumDir))) 103 return chromiumDir;
104 fs.mkdirSync(path.dirname(chromiumDir)); 104
105 105 if (!fs.existsSync(path.dirname(chromiumDir)))
106 let [dir, fileName] = buildTypes[platform]; 106 fs.mkdirSync(path.dirname(chromiumDir));
107 const archive = path.join(chromiumDownloadCache, 107
Wladimir Palant 2017/05/16 14:21:15 I'm not a big fan of using `const` for things that
hub 2017/05/16 15:49:30 Done.
108 let [dir, fileName] = buildTypes[platform];
109 let archive = path.join(snapshotsDir, "download-cache",
108 `${CHROMIUM_REVISION}-${fileName}`); 110 `${CHROMIUM_REVISION}-${fileName}`);
109 if (fs.existsSync(archive)) 111
110 { 112 return Promise.resolve()
111 console.info(`Reusing cached archive ${archive}`); 113 .then(() =>
112 return unzipChromium(archive, chromiumDir); 114 {
113 } 115 if (!fs.existsSync(archive))
116 {
117 let url = `https://www.googleapis.com/download/storage/v1/b/chromium-b rowser-snapshots/o/${dir}%2F${CHROMIUM_REVISION}%2F${fileName}?alt=media`;
118 console.info("Downloading Chromium...");
119 return download(url, archive);
120 }
121 console.info(`Reusing cached archive ${archive}`);
122 })
123 .then(() => unzipArchive(archive, chromiumDir))
124 .then(() => chromiumDir);
125 }).then(dir => getChromiumExecutable(dir));
126 }
127
128 function download(url, destFile)
129 {
114 return new Promise((resolve, reject) => 130 return new Promise((resolve, reject) =>
115 { 131 {
116 console.info("Downloading Chromium..."); 132 let cacheDir = path.dirname(destFile);
117 let url = `https://www.googleapis.com/download/storage/v1/b/chromium-browser -snapshots/o/${dir}%2F${CHROMIUM_REVISION}%2F${fileName}?alt=media`; 133 if (!fs.existsSync(cacheDir))
118 134 fs.mkdirSync(cacheDir);
119 if (!fs.existsSync(chromiumDownloadCache)) 135 let tempDest = destFile + "-" + process.pid;
120 fs.mkdirSync(chromiumDownloadCache); 136 let writable = fs.createWriteStream(tempDest);
121 const writable = fs.createWriteStream(archive);
Wladimir Palant 2017/05/16 14:21:15 Same as above, I'd rather not have this marked as
hub 2017/05/16 15:49:30 Done.
122 137
123 https.get(url, response => 138 https.get(url, response =>
124 { 139 {
125 if (response.statusCode != 200) 140 if (response.statusCode != 200)
126 { 141 {
127 reject(new Error(`Unexpected server response: ${response.statusCode}`)); 142 reject(
143 new Error(`Unexpected server response: ${response.statusCode}`));
128 response.resume(); 144 response.resume();
129 return; 145 return;
130 } 146 }
131 147
132 response.pipe(writable) 148 response.pipe(writable)
133 .on("error", reject ) 149 .on("error", error =>
Wladimir Palant 2017/05/16 14:21:15 What happens with the write stream if an error occ
hub 2017/05/16 15:49:30 Good idea let's unlink the file.
hub 2017/05/16 15:49:30 If I run eslint on that file I get: 229:46 erro
Wladimir Palant 2017/05/17 10:37:02 You should update eslint-config-eyeo package, supp
hub 2017/05/17 17:25:02 Acknowledged.
134 .on("close", () => { 150 {
135 writable.close(); 151 writable.close();
136 unzipChromium(archive, chromiumDir).catch(reject); 152 fs.unlinkSync(tempDest);
Wladimir Palant 2017/05/16 14:21:15 Where is resolve being called here?
hub 2017/05/16 15:49:30 Not sure what happens here because it works, but I
153 reject(error);
154 })
155 .on("close", () =>
156 {
157 writable.close();
158 fs.renameSync(tempDest, destFile);
159 resolve();
137 }); 160 });
138 }).on("error", reject); 161 }).on("error", reject);
Wladimir Palant 2017/05/16 14:21:15 Given how complicated the download logic gets, I t
139 }); 162 });
140 } 163 }
141 164
142 function unzipChromium(archive, chromiumDir) 165 function unzipArchive(archive, destDir)
Wladimir Palant 2017/05/16 14:21:15 Nit: this is a generic function, so name and param
hub 2017/05/16 15:49:30 Done.
143 { 166 {
144 return new Promise((resolve, reject) => 167 return new Promise((resolve, reject) =>
145 { 168 {
146 let unzipper = new DecompressZip(archive); 169 extractZip(archive, {dir: destDir}, err =>
147 unzipper.on('extract', () => { 170 {
148 resolve(getChromiumExecutable(chromiumDir)); 171 if (err)
Wladimir Palant 2017/05/16 14:21:15 The getChromiumExecutable() call should stay in en
hub 2017/05/16 15:49:30 I'll resolve the promise with the destination path
Wladimir Palant 2017/05/17 10:37:02 This doesn't make much sense either - the destinat
hub 2017/05/17 17:25:02 Done.
172 reject(err);
173 else
174 resolve();
149 }); 175 });
150 unzipper.on('error', reject);
151 unzipper.extract({path: chromiumDir});
152 }); 176 });
153 } 177 }
154 178
155 function startChromium(chromiumPath) 179 function startChromium(chromiumPath)
156 { 180 {
157 fs.chmodSync(chromiumPath, fs.constants.S_IRWXU); 181 fs.chmodSync(chromiumPath, fs.constants.S_IRWXU);
158 182
159 let dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "chromium-data")); 183 let dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "chromium-data"));
160 let child = null; 184 let child = null;
161 return { 185 return {
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 { 318 {
295 child.kill(); 319 child.kill();
296 return result; 320 return result;
297 }).catch(error => 321 }).catch(error =>
298 { 322 {
299 child.kill(); 323 child.kill();
300 throw error; 324 throw error;
301 }); 325 });
302 }); 326 });
303 }; 327 };
LEFTRIGHT
« no previous file | package.json » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld