| 1 | /** |
| 2 | * @fileoverview Package-private helpers for the installer. |
| 3 | */ |
| 4 | |
| 5 | 'use strict' |
| 6 | |
| 7 | var cp = require('child_process') |
| 8 | var fs = require('fs-extra') |
| 9 | var hasha = require('hasha') |
| 10 | var helper = require('./phantomjs') |
| 11 | var kew = require('kew') |
| 12 | var path = require('path') |
| 13 | |
| 14 | var DEFAULT_CDN = 'https://github.com/Medium/phantomjs/releases/download/v2.1.1' |
| 15 | var libPath = __dirname |
| 16 | |
| 17 | /** |
| 18 | * Given a lib/location file of a PhantomJS previously installed with NPM, |
| 19 | * is there a valid PhantomJS binary at this lib/location. |
| 20 | * @return {Promise<string>} resolved location of phantomjs binary on success |
| 21 | */ |
| 22 | function findValidPhantomJsBinary(libPath) { |
| 23 | return kew.fcall(function () { |
| 24 | var libModule = require(libPath) |
| 25 | if (libModule.location && |
| 26 | getTargetPlatform() == libModule.platform && |
| 27 | getTargetArch() == libModule.arch) { |
| 28 | var resolvedLocation = path.resolve(path.dirname(libPath), libModule.location) |
| 29 | if (fs.statSync(resolvedLocation)) { |
| 30 | return checkPhantomjsVersion(resolvedLocation).then(function (matches) { |
| 31 | if (matches) { |
| 32 | return kew.resolve(resolvedLocation) |
| 33 | } |
| 34 | }) |
| 35 | } |
| 36 | } |
| 37 | return false |
| 38 | }).fail(function () { |
| 39 | return false |
| 40 | }) |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Check to make sure a given binary is the right version. |
| 45 | * @return {kew.Promise.<boolean>} |
| 46 | */ |
| 47 | function checkPhantomjsVersion(phantomPath) { |
| 48 | console.log('Found PhantomJS at', phantomPath, '...verifying') |
| 49 | return kew.nfcall(cp.execFile, phantomPath, ['--version']).then(function (stdout) { |
| 50 | var version = stdout.trim() |
| 51 | if (helper.version == version) { |
| 52 | return true |
| 53 | } else { |
| 54 | console.log('PhantomJS detected, but wrong version', stdout.trim(), '@', phantomPath + '.') |
| 55 | return false |
| 56 | } |
| 57 | }).fail(function (err) { |
| 58 | console.error('Error verifying phantomjs, continuing', err) |
| 59 | return false |
| 60 | }) |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Writes the location file with location and platform/arch metadata about the |
| 65 | * binary. |
| 66 | */ |
| 67 | function writeLocationFile(location) { |
| 68 | console.log('Writing location.js file') |
| 69 | if (getTargetPlatform() === 'win32') { |
| 70 | location = location.replace(/\\/g, '\\\\') |
| 71 | } |
| 72 | |
| 73 | var platform = getTargetPlatform() |
| 74 | var arch = getTargetArch() |
| 75 | |
| 76 | var contents = 'module.exports.location = "' + location + '"\n' |
| 77 | |
| 78 | if (/^[a-zA-Z0-9]*$/.test(platform) && /^[a-zA-Z0-9]*$/.test(arch)) { |
| 79 | contents += |
| 80 | 'module.exports.platform = "' + getTargetPlatform() + '"\n' + |
| 81 | 'module.exports.arch = "' + getTargetArch() + '"\n' |
| 82 | } |
| 83 | |
| 84 | fs.writeFileSync(path.join(libPath, 'location.js'), contents) |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @return {?{url: string, checksum: string}} Get the download URL and expected |
| 89 | * SHA-256 checksum for phantomjs. May return null if no download url exists. |
| 90 | */ |
| 91 | function getDownloadSpec() { |
| 92 | var cdnUrl = process.env.npm_config_phantomjs_cdnurl || |
| 93 | process.env.PHANTOMJS_CDNURL || |
| 94 | DEFAULT_CDN |
| 95 | var downloadUrl = cdnUrl + '/phantomjs-' + helper.version + '-' |
| 96 | var checksum = '' |
| 97 | |
| 98 | var platform = getTargetPlatform() |
| 99 | var arch = getTargetArch() |
| 100 | if (platform === 'linux' && arch === 'x64') { |
| 101 | downloadUrl += 'linux-x86_64.tar.bz2' |
| 102 | checksum = '86dd9a4bf4aee45f1a84c9f61cf1947c1d6dce9b9e8d2a907105da7852460d2f' |
| 103 | } else if (platform === 'linux' && arch == 'ia32') { |
| 104 | downloadUrl += 'linux-i686.tar.bz2' |
| 105 | checksum = '80e03cfeb22cc4dfe4e73b68ab81c9fdd7c78968cfd5358e6af33960464f15e3' |
| 106 | } else if (platform === 'darwin') { |
| 107 | downloadUrl += 'macosx.zip' |
| 108 | checksum = '538cf488219ab27e309eafc629e2bcee9976990fe90b1ec334f541779150f8c1' |
| 109 | } else if (platform === 'win32') { |
| 110 | downloadUrl += 'windows.zip' |
| 111 | checksum = 'd9fb05623d6b26d3654d008eab3adafd1f6350433dfd16138c46161f42c7dcc8' |
| 112 | } else { |
| 113 | return null |
| 114 | } |
| 115 | return {url: downloadUrl, checksum: checksum} |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Check to make sure that the file matches the checksum. |
| 120 | * @param {string} fileName |
| 121 | * @param {string} checksum |
| 122 | * @return {Promise.<boolean>} |
| 123 | */ |
| 124 | function verifyChecksum(fileName, checksum) { |
| 125 | return kew.resolve(hasha.fromFile(fileName, {algorithm: 'sha256'})).then(function (hash) { |
| 126 | var result = checksum == hash |
| 127 | if (result) { |
| 128 | console.log('Verified checksum of previously downloaded file') |
| 129 | } else { |
| 130 | console.log('Checksum did not match') |
| 131 | } |
| 132 | return result |
| 133 | }).fail(function (err) { |
| 134 | console.error('Failed to verify checksum: ', err) |
| 135 | return false |
| 136 | }) |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @return {string} |
| 141 | */ |
| 142 | function getTargetPlatform() { |
| 143 | return process.env.PHANTOMJS_PLATFORM || process.platform |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * @return {string} |
| 148 | */ |
| 149 | function getTargetArch() { |
| 150 | return process.env.PHANTOMJS_ARCH || process.arch |
| 151 | } |
| 152 | |
| 153 | module.exports = { |
| 154 | checkPhantomjsVersion: checkPhantomjsVersion, |
| 155 | getDownloadSpec: getDownloadSpec, |
| 156 | getTargetPlatform: getTargetPlatform, |
| 157 | getTargetArch: getTargetArch, |
| 158 | findValidPhantomJsBinary: findValidPhantomJsBinary, |
| 159 | verifyChecksum: verifyChecksum, |
| 160 | writeLocationFile: writeLocationFile |
| 161 | } |