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: Rework the code to be clearer. Address the other review issues. Created May 17, 2017, 5:23 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 const process = require("process");
Wladimir Palant 2017/05/18 11:18:27 You don't need this require, process is a global v
hub 2017/05/18 13:22:37 Acknowledged.
29 28
30 const remoteInterface = require("chrome-remote-interface"); 29 const remoteInterface = require("chrome-remote-interface");
31 const extractZip = require("extract-zip"); 30 const extractZip = require("extract-zip");
32 31
33 const CHROMIUM_REVISION = 467222; 32 const CHROMIUM_REVISION = 467222;
34 33
35 function rmdir(dirPath) 34 function rmdir(dirPath)
36 { 35 {
37 for (let file of fs.readdirSync(dirPath)) 36 for (let file of fs.readdirSync(dirPath))
38 { 37 {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 "darwin": ["Mac", "chrome-mac.zip"] 87 "darwin": ["Mac", "chrome-mac.zip"]
89 }; 88 };
90 89
91 if (!buildTypes.hasOwnProperty(platform)) 90 if (!buildTypes.hasOwnProperty(platform))
92 { 91 {
93 let err = new Error(`Cannot run browser tests, ${platform} is unsupported`); 92 let err = new Error(`Cannot run browser tests, ${platform} is unsupported`);
94 return Promise.reject(err); 93 return Promise.reject(err);
95 } 94 }
96 95
97 96
98 return new Promise((resolve, reject) => 97 return Promise.resolve().then(() =>
Wladimir Palant 2017/05/18 11:18:28 There is no async action here that isn't based on
hub 2017/05/18 13:22:37 Done.
99 { 98 {
100 const snapshotsDir = path.join(__dirname, "chromium-snapshots"); 99 let snapshotsDir = path.join(__dirname, "chromium-snapshots");
101 const chromiumDir = path.join(snapshotsDir, 100 let chromiumDir = path.join(snapshotsDir,
102 `chromium-${platform}-${CHROMIUM_REVISION}`); 101 `chromium-${platform}-${CHROMIUM_REVISION}`);
103 if (fs.existsSync(chromiumDir)) 102 if (fs.existsSync(chromiumDir))
104 resolve(chromiumDir); 103 return chromiumDir;
105 else 104
106 { 105 if (!fs.existsSync(path.dirname(chromiumDir)))
107 if (!fs.existsSync(path.dirname(chromiumDir))) 106 fs.mkdirSync(path.dirname(chromiumDir));
108 fs.mkdirSync(path.dirname(chromiumDir)); 107
109 108 let [dir, fileName] = buildTypes[platform];
110 let [dir, fileName] = buildTypes[platform]; 109 let archive = path.join(snapshotsDir, "download-cache",
111 const archive = path.join(snapshotsDir, "download-cache", 110 `${CHROMIUM_REVISION}-${fileName}`);
112 `${CHROMIUM_REVISION}-${fileName}`); 111
113 const url = `https://www.googleapis.com/download/storage/v1/b/chromium-bro wser-snapshots/o/${dir}%2F${CHROMIUM_REVISION}%2F${fileName}?alt=media`; 112 return Promise.resolve()
Wladimir Palant 2017/05/18 11:18:28 You changed a bunch more variables from let to con
hub 2017/05/18 13:22:37 I ended up using const like in C++ which is not ex
114 113 .then(() =>
115 downloadCache(url, archive) 114 {
116 .then(() => unzipArchive(archive, chromiumDir)) 115 if (!fs.existsSync(archive))
117 .then(() => resolve(chromiumDir)); 116 {
118 } 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);
119 }).then(dir => getChromiumExecutable(dir)); 125 }).then(dir => getChromiumExecutable(dir));
120 } 126 }
121 127
122 function downloadCache(url, destArchive) 128 function download(url, destFile)
Wladimir Palant 2017/05/18 11:18:27 Nit: the function name sounds strange, downloadCac
hub 2017/05/18 13:22:37 fixing it.
123 { 129 {
124 return new Promise((resolve, reject) => 130 return new Promise((resolve, reject) =>
125 { 131 {
126 if (fs.existsSync(destArchive)) 132 let cacheDir = path.dirname(destFile);
127 { 133 if (!fs.existsSync(cacheDir))
128 console.info(`Reusing cached archive ${destArchive}`); 134 fs.mkdirSync(cacheDir);
129 resolve(); 135 let tempDest = destFile + "-" + process.pid;
130 } 136 let writable = fs.createWriteStream(tempDest);
131 else 137
132 { 138 https.get(url, response =>
133 console.info("Downloading Chromium..."); 139 {
134 let cacheDir = path.dirname(destArchive); 140 if (response.statusCode != 200)
135 if (!fs.existsSync(cacheDir)) 141 {
136 fs.mkdirSync(cacheDir); 142 reject(
137 let tempDest = destArchive + "-" + process.pid; 143 new Error(`Unexpected server response: ${response.statusCode}`));
138 let writable = fs.createWriteStream(tempDest); 144 response.resume();
139 145 return;
140 https.get(url, response => 146 }
141 { 147
142 if (response.statusCode != 200) 148 response.pipe(writable)
143 { 149 .on("error", error =>
144 reject(new Error(`Unexpected server response: ${response.statusCode}`) ); 150 {
Wladimir Palant 2017/05/18 11:18:28 I'm pretty sure that ESLint dislikes the length of
hub 2017/05/18 13:22:37 it does. Fixing.
145 response.resume(); 151 writable.close();
146 return; 152 fs.unlinkSync(tempDest);
147 } 153 reject(error);
148 154 })
149 response.pipe(writable) 155 .on("close", () =>
150 .on("error", error => 156 {
151 { 157 writable.close();
152 fs.unlinkSync(tempDest); 158 fs.renameSync(tempDest, destFile);
153 reject(error); 159 resolve();
154 }) 160 });
155 .on("close", () => 161 }).on("error", reject);
156 {
157 writable.close();
158 fs.renameSync(tempDest, destArchive);
159 resolve();
160 });
161 }).on("error", reject);
162 }
163 }); 162 });
164 } 163 }
165 164
166 function unzipArchive(archive, destDir) 165 function unzipArchive(archive, destDir)
167 { 166 {
168 return new Promise((resolve, reject) => 167 return new Promise((resolve, reject) =>
169 { 168 {
170 extractZip(archive, {dir: destDir}, err => 169 extractZip(archive, {dir: destDir}, err =>
171 { 170 {
172 if (err) 171 if (err)
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 { 318 {
320 child.kill(); 319 child.kill();
321 return result; 320 return result;
322 }).catch(error => 321 }).catch(error =>
323 { 322 {
324 child.kill(); 323 child.kill();
325 throw error; 324 throw error;
326 }); 325 });
327 }); 326 });
328 }; 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