1 // Copyright 2012 The Obvious Corporation.
4 * This simply fetches the right version of phantom for the current platform.
9 var requestProgress = require('request-progress')
10 var progress = require('progress')
11 var extractZip = require('extract-zip')
12 var cp = require('child_process')
13 var fs = require('fs-extra')
14 var helper = require('./lib/phantomjs')
15 var kew = require('kew')
16 var path = require('path')
17 var request = require('request')
18 var url = require('url')
19 var util = require('./lib/util')
20 var which = require('which')
21 var os = require('os')
23 var originalPath = process.env.PATH
25 var checkPhantomjsVersion = util.checkPhantomjsVersion
26 var getTargetPlatform = util.getTargetPlatform
27 var getTargetArch = util.getTargetArch
28 var getDownloadSpec = util.getDownloadSpec
29 var findValidPhantomJsBinary = util.findValidPhantomJsBinary
30 var verifyChecksum = util.verifyChecksum
31 var writeLocationFile = util.writeLocationFile
33 // If the process exits without going through exit(), then we did not complete.
36 process.on('exit', function () {
38 console.log('Install exited unexpectedly')
43 // NPM adds bin directories to the path, which will cause `which` to find the
44 // bin for this package not the actual phantomjs bin. Also help out people who
45 // put ./bin on their path
46 process.env.PATH = helper.cleanPath(originalPath)
48 var libPath = path.join(__dirname, 'lib')
49 var pkgPath = path.join(libPath, 'phantom')
50 var phantomPath = null
52 // If the user manually installed PhantomJS, we want
53 // to use the existing version.
55 // Do not re-use a manually-installed PhantomJS with
56 // a different version.
58 // Do not re-use an npm-installed PhantomJS, because
59 // that can lead to weird circular dependencies between
60 // local versions and global versions.
61 // https://github.com/Obvious/phantomjs/issues/85
62 // https://github.com/Medium/phantomjs/pull/184
64 .then(tryPhantomjsInLib)
65 .then(tryPhantomjsOnPath)
66 .then(downloadPhantomjs)
67 .then(extractDownload)
68 .then(function (extractedPath) {
69 return copyIntoPlace(extractedPath, pkgPath)
72 var location = getTargetPlatform() === 'win32' ?
73 path.join(pkgPath, 'bin', 'phantomjs.exe') :
74 path.join(pkgPath, 'bin' ,'phantomjs')
77 // Ensure executable is executable by all users
78 fs.chmodSync(location, '755')
80 if (err.code == 'ENOENT') {
81 console.error('chmod failed: phantomjs was not successfully copied to', location)
87 var relativeLocation = path.relative(libPath, location)
88 writeLocationFile(relativeLocation)
90 console.log('Done. Phantomjs binary available at', location)
93 .fail(function (err) {
94 console.error('Phantom installation failed', err, err.stack)
100 process.env.PATH = originalPath
101 process.exit(code || 0)
105 function findSuitableTempDirectory() {
107 var candidateTmpDirs = [
108 process.env.npm_config_tmp,
110 path.join(process.cwd(), 'tmp')
113 for (var i = 0; i < candidateTmpDirs.length; i++) {
114 var candidatePath = candidateTmpDirs[i]
115 if (!candidatePath) continue
118 candidatePath = path.join(path.resolve(candidatePath), 'phantomjs')
119 fs.mkdirsSync(candidatePath, '0777')
120 // Make double sure we have 0777 permissions; some operating systems
121 // default umask does not allow write by default.
122 fs.chmodSync(candidatePath, '0777')
123 var testFile = path.join(candidatePath, now + '.tmp')
124 fs.writeFileSync(testFile, 'test')
125 fs.unlinkSync(testFile)
128 console.log(candidatePath, 'is not writable:', e.message)
132 console.error('Can not find a writable tmp directory, please report issue ' +
133 'on https://github.com/Medium/phantomjs/issues with as much ' +
134 'information as possible.')
139 function getRequestOptions() {
140 var strictSSL = !!process.env.npm_config_strict_ssl
141 if (process.version == 'v0.10.34') {
142 console.log('Node v0.10.34 detected, turning off strict ssl due to https://github.com/joyent/node/issues/8894')
147 uri: getDownloadUrl(),
148 encoding: null, // Get response as a buffer
149 followRedirect: true, // The default download path redirects to a CDN URL.
154 var proxyUrl = process.env.npm_config_https_proxy ||
155 process.env.npm_config_http_proxy ||
156 process.env.npm_config_proxy
160 var proxy = url.parse(proxyUrl)
163 proxy.auth = proxy.auth.replace(/:.*$/, ':******')
165 console.log('Using proxy ' + url.format(proxy))
168 options.proxy = proxyUrl
171 // Use the user-agent string from the npm config
172 options.headers['User-Agent'] = process.env.npm_config_user_agent
174 // Use certificate authority settings from npm
175 var ca = process.env.npm_config_ca
176 if (!ca && process.env.npm_config_cafile) {
178 ca = fs.readFileSync(process.env.npm_config_cafile, {encoding: 'utf8'})
179 .split(/\n(?=-----BEGIN CERTIFICATE-----)/g)
181 // Comments at the beginning of the file result in the first
182 // item not containing a certificate - in this case the
183 // download will fail
184 if (ca.length > 0 && !/-----BEGIN CERTIFICATE-----/.test(ca[0])) {
189 console.error('Could not read cafile', process.env.npm_config_cafile, e)
194 console.log('Using npmconf ca')
195 options.agentOptions = {
204 function handleRequestError(error) {
205 if (error && error.stack && error.stack.indexOf('SELF_SIGNED_CERT_IN_CHAIN') != -1) {
206 console.error('Error making request, SELF_SIGNED_CERT_IN_CHAIN. ' +
207 'Please read https://github.com/Medium/phantomjs#i-am-behind-a-corporate-proxy-that-uses-self-signed-ssl-certificates-to-intercept-encrypted-traffic')
210 console.error('Error making request.\n' + error.stack + '\n\n' +
211 'Please report this full log at https://github.com/Medium/phantomjs')
214 console.error('Something unexpected happened, please report this full ' +
215 'log at https://github.com/Medium/phantomjs')
220 function requestBinary(requestOptions, filePath) {
221 var deferred = kew.defer()
223 var writePath = filePath + '-download-' + Date.now()
225 console.log('Receiving...')
227 requestProgress(request(requestOptions, function (error, response, body) {
229 if (!error && response.statusCode === 200) {
230 fs.writeFileSync(writePath, body)
231 console.log('Received ' + Math.floor(body.length / 1024) + 'K total.')
232 fs.renameSync(writePath, filePath)
233 deferred.resolve(filePath)
235 } else if (response) {
236 console.error('Error requesting archive.\n' +
237 'Status: ' + response.statusCode + '\n' +
238 'Request options: ' + JSON.stringify(requestOptions, null, 2) + '\n' +
239 'Response headers: ' + JSON.stringify(response.headers, null, 2) + '\n' +
240 'Make sure your network and proxy settings are correct.\n\n' +
241 'If you continue to have issues, please report this full log at ' +
242 'https://github.com/Medium/phantomjs')
245 handleRequestError(error)
247 })).on('progress', function (state) {
250 bar = new progress(' [:bar] :percent', {total: state.size.total, width: 40})
252 bar.curr = state.size.transferred
255 // It doesn't really matter if the progress bar doesn't update.
258 .on('error', handleRequestError)
260 return deferred.promise
264 function extractDownload(filePath) {
265 var deferred = kew.defer()
266 // extract to a unique directory in case multiple processes are
267 // installing and extracting at once
268 var extractedPath = filePath + '-extract-' + Date.now()
269 var options = {cwd: extractedPath}
271 fs.mkdirsSync(extractedPath, '0777')
272 // Make double sure we have 0777 permissions; some operating systems
273 // default umask does not allow write by default.
274 fs.chmodSync(extractedPath, '0777')
276 if (filePath.substr(-4) === '.zip') {
277 console.log('Extracting zip contents')
278 extractZip(path.resolve(filePath), {dir: extractedPath}, function(err) {
280 console.error('Error extracting zip')
283 deferred.resolve(extractedPath)
288 console.log('Extracting tar contents (via spawned process)')
289 cp.execFile('tar', ['jxf', path.resolve(filePath)], options, function (err) {
291 console.error('Error extracting archive')
294 deferred.resolve(extractedPath)
298 return deferred.promise
302 function copyIntoPlace(extractedPath, targetPath) {
303 console.log('Removing', targetPath)
304 return kew.nfcall(fs.remove, targetPath).then(function () {
305 // Look for the extracted directory, so we can rename it.
306 var files = fs.readdirSync(extractedPath)
307 for (var i = 0; i < files.length; i++) {
308 var file = path.join(extractedPath, files[i])
309 if (fs.statSync(file).isDirectory() && file.indexOf(helper.version) != -1) {
310 console.log('Copying extracted folder', file, '->', targetPath)
311 return kew.nfcall(fs.move, file, targetPath)
315 console.log('Could not find extracted file', files)
316 throw new Error('Could not find extracted file')
321 * Check to see if the binary in lib is OK to use. If successful, exit the process.
323 function tryPhantomjsInLib() {
324 return kew.fcall(function () {
325 return findValidPhantomJsBinary(path.resolve(__dirname, './lib/location.js'))
326 }).then(function (binaryLocation) {
327 if (binaryLocation) {
328 console.log('PhantomJS is previously installed at', binaryLocation)
331 }).fail(function () {
332 // silently swallow any errors
337 * Check to see if the binary on PATH is OK to use. If successful, exit the process.
339 function tryPhantomjsOnPath() {
340 if (getTargetPlatform() != process.platform || getTargetArch() != process.arch) {
341 console.log('Building for target platform ' + getTargetPlatform() + '/' + getTargetArch() +
342 '. Skipping PATH search')
343 return kew.resolve(false)
346 return kew.nfcall(which, 'phantomjs')
347 .then(function (result) {
349 console.log('Considering PhantomJS found at', phantomPath)
351 // Horrible hack to avoid problems during global install. We check to see if
352 // the file `which` found is our own bin script.
353 if (phantomPath.indexOf(path.join('npm', 'phantomjs')) !== -1) {
354 console.log('Looks like an `npm install -g` on windows; skipping installed version.')
358 var contents = fs.readFileSync(phantomPath, 'utf8')
359 if (/NPM_INSTALL_MARKER/.test(contents)) {
360 console.log('Looks like an `npm install -g`')
362 var phantomLibPath = path.resolve(fs.realpathSync(phantomPath), '../../lib/location')
363 return findValidPhantomJsBinary(phantomLibPath)
364 .then(function (binaryLocation) {
365 if (binaryLocation) {
366 writeLocationFile(binaryLocation)
367 console.log('PhantomJS linked at', phantomLibPath)
370 console.log('Could not link global install, skipping...')
373 return checkPhantomjsVersion(phantomPath).then(function (matches) {
375 writeLocationFile(phantomPath)
376 console.log('PhantomJS is already installed on PATH at', phantomPath)
382 console.log('PhantomJS not found on PATH')
384 .fail(function (err) {
385 console.error('Error checking path, continuing', err)
391 * @return {?string} Get the download URL for phantomjs.
392 * May return null if no download url exists.
394 function getDownloadUrl() {
395 var spec = getDownloadSpec()
396 return spec && spec.url
400 * Download phantomjs, reusing the existing copy on disk if available.
401 * Exits immediately if there is no binary to download.
402 * @return {Promise.<string>} The path to the downloaded file.
404 function downloadPhantomjs() {
405 var downloadSpec = getDownloadSpec()
408 'Unexpected platform or architecture: ' + getTargetPlatform() + '/' + getTargetArch() + '\n' +
409 'It seems there is no binary available for your platform/architecture\n' +
410 'Try to install PhantomJS globally')
414 var downloadUrl = downloadSpec.url
417 return kew.fcall(function () {
418 // Can't use a global version so start a download.
419 var tmpPath = findSuitableTempDirectory()
420 var fileName = downloadUrl.split('/').pop()
421 downloadedFile = path.join(tmpPath, fileName)
423 if (fs.existsSync(downloadedFile)) {
424 console.log('Download already available at', downloadedFile)
425 return verifyChecksum(downloadedFile, downloadSpec.checksum)
428 }).then(function (verified) {
430 return downloadedFile
433 // Start the install.
434 console.log('Downloading', downloadUrl)
435 console.log('Saving to', downloadedFile)
436 return requestBinary(getRequestOptions(), downloadedFile)