OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-present eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 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/>. |
| 16 */ |
| 17 |
| 18 /* eslint-env node */ |
| 19 /* eslint no-console: "off" */ |
| 20 |
| 21 "use strict"; |
| 22 |
| 23 const fs = require("fs"); |
| 24 const https = require("https"); |
| 25 const path = require("path"); |
| 26 |
| 27 const extractZip = require("extract-zip"); |
| 28 |
| 29 function getChromiumExecutable(chromiumDir) |
| 30 { |
| 31 switch (process.platform) |
| 32 { |
| 33 case "win32": |
| 34 return path.join(chromiumDir, "chrome-win32", "chrome.exe"); |
| 35 case "linux": |
| 36 return path.join(chromiumDir, "chrome-linux", "chrome"); |
| 37 case "darwin": |
| 38 return path.join(chromiumDir, "chrome-mac", "Chromium.app", "Contents", |
| 39 "MacOS", "Chromium"); |
| 40 default: |
| 41 throw new Error("Unexpected platform"); |
| 42 } |
| 43 } |
| 44 |
| 45 function ensureChromium(chromiumRevision) |
| 46 { |
| 47 let {platform} = process; |
| 48 if (platform == "win32") |
| 49 platform += "-" + process.arch; |
| 50 let buildTypes = { |
| 51 "win32-ia32": ["Win", "chrome-win32.zip"], |
| 52 "win32-x64": ["Win_x64", "chrome-win32.zip"], |
| 53 "linux": ["Linux_x64", "chrome-linux.zip"], |
| 54 "darwin": ["Mac", "chrome-mac.zip"] |
| 55 }; |
| 56 |
| 57 if (!buildTypes.hasOwnProperty(platform)) |
| 58 { |
| 59 let err = new Error(`Cannot run browser tests, ${platform} is unsupported`); |
| 60 return Promise.reject(err); |
| 61 } |
| 62 |
| 63 |
| 64 return Promise.resolve().then(() => |
| 65 { |
| 66 let snapshotsDir = path.join(__dirname, "..", "..", "chromium-snapshots"); |
| 67 let chromiumDir = path.join(snapshotsDir, |
| 68 `chromium-${platform}-${chromiumRevision}`); |
| 69 if (fs.existsSync(chromiumDir)) |
| 70 return chromiumDir; |
| 71 |
| 72 if (!fs.existsSync(path.dirname(chromiumDir))) |
| 73 fs.mkdirSync(path.dirname(chromiumDir)); |
| 74 |
| 75 let [dir, fileName] = buildTypes[platform]; |
| 76 let archive = path.join(snapshotsDir, "download-cache", |
| 77 `${chromiumRevision}-${fileName}`); |
| 78 |
| 79 return Promise.resolve() |
| 80 .then(() => |
| 81 { |
| 82 if (!fs.existsSync(archive)) |
| 83 { |
| 84 let url = `https://www.googleapis.com/download/storage/v1/b/chromium-b
rowser-snapshots/o/${dir}%2F${chromiumRevision}%2F${fileName}?alt=media`; |
| 85 console.info("Downloading Chromium..."); |
| 86 return download(url, archive); |
| 87 } |
| 88 console.info(`Reusing cached archive ${archive}`); |
| 89 }) |
| 90 .then(() => unzipArchive(archive, chromiumDir)) |
| 91 .then(() => chromiumDir); |
| 92 }).then(dir => getChromiumExecutable(dir)); |
| 93 } |
| 94 |
| 95 function download(url, destFile) |
| 96 { |
| 97 return new Promise((resolve, reject) => |
| 98 { |
| 99 let cacheDir = path.dirname(destFile); |
| 100 if (!fs.existsSync(cacheDir)) |
| 101 fs.mkdirSync(cacheDir); |
| 102 let tempDest = destFile + "-" + process.pid; |
| 103 let writable = fs.createWriteStream(tempDest); |
| 104 |
| 105 https.get(url, response => |
| 106 { |
| 107 if (response.statusCode != 200) |
| 108 { |
| 109 reject( |
| 110 new Error(`Unexpected server response: ${response.statusCode}`)); |
| 111 response.resume(); |
| 112 return; |
| 113 } |
| 114 |
| 115 response.pipe(writable) |
| 116 .on("error", error => |
| 117 { |
| 118 writable.close(); |
| 119 fs.unlinkSync(tempDest); |
| 120 reject(error); |
| 121 }) |
| 122 .on("close", () => |
| 123 { |
| 124 writable.close(); |
| 125 fs.renameSync(tempDest, destFile); |
| 126 resolve(); |
| 127 }); |
| 128 }).on("error", reject); |
| 129 }); |
| 130 } |
| 131 |
| 132 function unzipArchive(archive, destDir) |
| 133 { |
| 134 return new Promise((resolve, reject) => |
| 135 { |
| 136 extractZip(archive, {dir: destDir}, err => |
| 137 { |
| 138 if (err) |
| 139 reject(err); |
| 140 else |
| 141 resolve(); |
| 142 }); |
| 143 }); |
| 144 } |
| 145 |
| 146 module.exports.ensureChromium = ensureChromium; |
OLD | NEW |