Index: chromium_process.js |
=================================================================== |
--- a/chromium_process.js |
+++ b/chromium_process.js |
@@ -20,19 +20,20 @@ |
"use strict"; |
const childProcess = require("child_process"); |
const fs = require("fs"); |
const https = require("https"); |
const os = require("os"); |
const path = require("path"); |
+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.
|
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,41 +89,96 @@ |
}; |
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 new Promise((resolve, reject) => |
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.
|
+ { |
+ const snapshotsDir = path.join(__dirname, "chromium-snapshots"); |
+ const chromiumDir = path.join(snapshotsDir, |
+ `chromium-${platform}-${CHROMIUM_REVISION}`); |
+ if (fs.existsSync(chromiumDir)) |
+ resolve(chromiumDir); |
+ else |
+ { |
+ if (!fs.existsSync(path.dirname(chromiumDir))) |
+ fs.mkdirSync(path.dirname(chromiumDir)); |
- if (!fs.existsSync(path.dirname(chromiumDir))) |
- fs.mkdirSync(path.dirname(chromiumDir)); |
+ let [dir, fileName] = buildTypes[platform]; |
+ const archive = path.join(snapshotsDir, "download-cache", |
+ `${CHROMIUM_REVISION}-${fileName}`); |
+ const url = `https://www.googleapis.com/download/storage/v1/b/chromium-browser-snapshots/o/${dir}%2F${CHROMIUM_REVISION}%2F${fileName}?alt=media`; |
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
|
+ |
+ downloadCache(url, archive) |
+ .then(() => unzipArchive(archive, chromiumDir)) |
+ .then(() => resolve(chromiumDir)); |
+ } |
+ }).then(dir => getChromiumExecutable(dir)); |
+} |
+ |
+function downloadCache(url, destArchive) |
Wladimir Palant
2017/05/18 11:18:27
Nit: the function name sounds strange, downloadCac
hub
2017/05/18 13:22:37
fixing it.
|
+{ |
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`; |
- https.get(url, response => |
+ if (fs.existsSync(destArchive)) |
+ { |
+ console.info(`Reusing cached archive ${destArchive}`); |
+ resolve(); |
+ } |
+ else |
{ |
- if (response.statusCode != 200) |
+ console.info("Downloading Chromium..."); |
+ let cacheDir = path.dirname(destArchive); |
+ if (!fs.existsSync(cacheDir)) |
+ fs.mkdirSync(cacheDir); |
+ let tempDest = destArchive + "-" + process.pid; |
+ let writable = fs.createWriteStream(tempDest); |
+ |
+ https.get(url, response => |
{ |
- reject(new Error(`Unexpected server response: ${response.statusCode}`)); |
- response.resume(); |
- return; |
- } |
+ if (response.statusCode != 200) |
+ { |
+ reject(new Error(`Unexpected server response: ${response.statusCode}`)); |
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.
|
+ response.resume(); |
+ return; |
+ } |
- response.pipe(unzip.Extract({path: chromiumDir})) |
- .on("error", reject) |
- .on("close", () => resolve(getChromiumExecutable(chromiumDir))); |
- }).on("error", reject); |
+ response.pipe(writable) |
+ .on("error", error => |
+ { |
+ fs.unlinkSync(tempDest); |
+ reject(error); |
+ }) |
+ .on("close", () => |
+ { |
+ writable.close(); |
+ fs.renameSync(tempDest, destArchive); |
+ 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")); |