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

Unified Diff: chromium_process.js

Issue 29435631: Issue 5230 - Properly download Chromium on macOS (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Addressed review comments. Created May 18, 2017, 1:22 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | package.json » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chromium_process.js
===================================================================
--- a/chromium_process.js
+++ b/chromium_process.js
@@ -22,17 +22,17 @@
const childProcess = require("child_process");
const fs = require("fs");
const https = require("https");
const os = require("os");
const path = require("path");
const remoteInterface = require("chrome-remote-interface");
-const unzip = require("unzip");
+const extractZip = require("extract-zip");
const CHROMIUM_REVISION = 467222;
function rmdir(dirPath)
{
for (let file of fs.readdirSync(dirPath))
{
let filePath = path.join(dirPath, file);
@@ -88,44 +88,99 @@
};
if (!buildTypes.hasOwnProperty(platform))
{
let err = new Error(`Cannot run browser tests, ${platform} is unsupported`);
return Promise.reject(err);
}
- let chromiumDir = path.join(__dirname, "chromium-snapshots",
- `chromium-${platform}-${CHROMIUM_REVISION}`);
- if (fs.existsSync(chromiumDir))
- return Promise.resolve(getChromiumExecutable(chromiumDir));
+
+ return Promise.resolve().then(() =>
+ {
+ let snapshotsDir = path.join(__dirname, "chromium-snapshots");
+ let chromiumDir = path.join(snapshotsDir,
+ `chromium-${platform}-${CHROMIUM_REVISION}`);
+ if (fs.existsSync(chromiumDir))
+ return chromiumDir;
+
+ if (!fs.existsSync(path.dirname(chromiumDir)))
+ fs.mkdirSync(path.dirname(chromiumDir));
+
+ let [dir, fileName] = buildTypes[platform];
+ let archive = path.join(snapshotsDir, "download-cache",
+ `${CHROMIUM_REVISION}-${fileName}`);
- if (!fs.existsSync(path.dirname(chromiumDir)))
- fs.mkdirSync(path.dirname(chromiumDir));
+ return Promise.resolve()
+ .then(() =>
+ {
+ if (!fs.existsSync(archive))
+ {
+ let url = `https://www.googleapis.com/download/storage/v1/b/chromium-browser-snapshots/o/${dir}%2F${CHROMIUM_REVISION}%2F${fileName}?alt=media`;
+ console.info("Downloading Chromium...");
+ return download(url, archive);
+ }
+ console.info(`Reusing cached archive ${archive}`);
+ })
+ .then(() => unzipArchive(archive, chromiumDir))
+ .then(() => chromiumDir);
+ }).then(dir => getChromiumExecutable(dir));
+}
+
+function download(url, destFile)
+{
return new Promise((resolve, reject) =>
{
- console.info("Downloading Chromium...");
- let [dir, fileName] = buildTypes[platform];
- let url = `https://www.googleapis.com/download/storage/v1/b/chromium-browser-snapshots/o/${dir}%2F${CHROMIUM_REVISION}%2F${fileName}?alt=media`;
+ let cacheDir = path.dirname(destFile);
+ if (!fs.existsSync(cacheDir))
+ fs.mkdirSync(cacheDir);
+ let tempDest = destFile + "-" + process.pid;
+ let writable = fs.createWriteStream(tempDest);
+
https.get(url, response =>
{
if (response.statusCode != 200)
{
- reject(new Error(`Unexpected server response: ${response.statusCode}`));
+ reject(
+ new Error(`Unexpected server response: ${response.statusCode}`));
response.resume();
return;
}
- response.pipe(unzip.Extract({path: chromiumDir}))
- .on("error", reject)
- .on("close", () => resolve(getChromiumExecutable(chromiumDir)));
+ response.pipe(writable)
+ .on("error", error =>
+ {
+ writable.close();
+ fs.unlinkSync(tempDest);
+ reject(error);
+ })
+ .on("close", () =>
+ {
+ writable.close();
+ fs.renameSync(tempDest, destFile);
+ resolve();
+ });
}).on("error", reject);
});
}
+function unzipArchive(archive, destDir)
+{
+ return new Promise((resolve, reject) =>
+ {
+ extractZip(archive, {dir: destDir}, err =>
+ {
+ if (err)
+ reject(err);
+ else
+ resolve();
+ });
+ });
+}
+
function startChromium(chromiumPath)
{
fs.chmodSync(chromiumPath, fs.constants.S_IRWXU);
let dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "chromium-data"));
let child = null;
return {
kill: () => child && child.kill(),
« no previous file with comments | « no previous file | package.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld