Skip to content
This repository has been archived by the owner on Jul 24, 2019. It is now read-only.

Commit

Permalink
Merge pull request #440 from Medium/nick-npmconf
Browse files Browse the repository at this point in the history
use npm env variables instead of npmconf, update deps, and fix a bunch of other bugs cause by dep updates
  • Loading branch information
nicks committed Jan 25, 2016
2 parents 9938af6 + 2620fb2 commit 83341c1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 45 deletions.
87 changes: 52 additions & 35 deletions install.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ var fs = require('fs-extra')
var helper = require('./lib/phantomjs')
var kew = require('kew')
var md5 = require('md5')
var npmconf = require('npmconf')
var path = require('path')
var request = require('request')
var url = require('url')
Expand All @@ -41,8 +40,6 @@ var libPath = path.join(__dirname, 'lib')
var pkgPath = path.join(libPath, 'phantom')
var phantomPath = null

var npmConfPromise = kew.nfcall(npmconf.load)

// If the user manually installed PhantomJS, we want
// to use the existing version.
//
Expand Down Expand Up @@ -117,10 +114,10 @@ function exit(code) {
}


function findSuitableTempDirectory(npmConf) {
function findSuitableTempDirectory() {
var now = Date.now()
var candidateTmpDirs = [
process.env.TMPDIR || process.env.TEMP || npmConf.get('tmp'),
process.env.TMPDIR || process.env.TEMP || process.env.npm_config_tmp,
'/tmp',
path.join(process.cwd(), 'tmp')
]
Expand Down Expand Up @@ -149,14 +146,13 @@ function findSuitableTempDirectory(npmConf) {
}


function getRequestOptions(conf) {
var strictSSL = conf.get('strict-ssl')
function getRequestOptions() {
var strictSSL = !!process.env.npm_config_strict_ssl
if (process.version == 'v0.10.34') {
console.log('Node v0.10.34 detected, turning off strict ssl due to https://github.com/joyent/node/issues/8894')
strictSSL = false
}


var options = {
uri: getDownloadUrl(),
encoding: null, // Get response as a buffer
Expand All @@ -165,7 +161,9 @@ function getRequestOptions(conf) {
strictSSL: strictSSL
}

var proxyUrl = conf.get('https-proxy') || conf.get('http-proxy') || conf.get('proxy')
var proxyUrl = process.env.npm_config_https_proxy ||
process.env.npm_config_http_proxy ||
process.env.npm_config_proxy
if (proxyUrl) {

// Print using proxy
Expand All @@ -178,21 +176,46 @@ function getRequestOptions(conf) {

// Enable proxy
options.proxy = proxyUrl

// If going through proxy, use the user-agent string from the npm config
options.headers['User-Agent'] = conf.get('user-agent')
}

// Use the user-agent string from the npm config
options.headers['User-Agent'] = process.env.npm_config_user_agent

// Use certificate authority settings from npm
var ca = conf.get('ca')
var ca = process.env.npm_config_ca
if (!ca && process.env.npm_config_cafile) {
try {
ca = fs.readFileSync(process.env.npm_config_cafile)
} catch (e) {
console.error('Could not read cafile', process.env.npm_config_cafile, e)
}
}

if (ca) {
console.log('Using npmconf ca')
options.ca = ca
options.agentOptions = {
ca: ca
}
}

return options
}

function handleRequestError(error) {
if (error && error.stack && error.stack.indexOf('SELF_SIGNED_CERT_IN_CHAIN') != -1) {
console.error('Error making request, SELF_SIGNED_CERT_IN_CHAIN. ' +
'Please read https://github.com/Medium/phantomjs#i-am-behind-a-corporate-proxy-that-uses-self-signed-ssl-certificates-to-intercept-encrypted-traffic')
exit(1)
} else if (error) {
console.error('Error making request.\n' + error.stack + '\n\n' +
'Please report this full log at https://github.com/Medium/phantomjs')
exit(1)
} else {
console.error('Something unexpected happened, please report this full ' +
'log at https://github.com/Medium/phantomjs')
exit(1)
}
}

function requestBinary(requestOptions, filePath) {
var deferred = kew.defer()
Expand All @@ -218,25 +241,21 @@ function requestBinary(requestOptions, filePath) {
'If you continue to have issues, please report this full log at ' +
'https://github.com/Medium/phantomjs')
exit(1)
} else if (error && error.stack && error.stack.indexOf('SELF_SIGNED_CERT_IN_CHAIN') != -1) {
console.error('Error making request, SELF_SIGNED_CERT_IN_CHAIN. Please read https://github.com/Medium/phantomjs#i-am-behind-a-corporate-proxy-that-uses-self-signed-ssl-certificates-to-intercept-encrypted-traffic')
exit(1)
} else if (error) {
console.error('Error making request.\n' + error.stack + '\n\n' +
'Please report this full log at https://github.com/Medium/phantomjs')
exit(1)
} else {
console.error('Something unexpected happened, please report this full ' +
'log at https://github.com/Medium/phantomjs')
exit(1)
handleRequestError(error)
}
})).on('progress', function (state) {
if (!bar) {
bar = new progress(' [:bar] :percent :etas', {total: state.total, width: 40})
try {
if (!bar) {
bar = new progress(' [:bar] :percent', {total: state.size.total, width: 40})
}
bar.curr = state.size.transferred
bar.tick()
} catch (e) {
// It doesn't really matter if the progress bar doesn't update.
}
bar.curr = state.received
bar.tick(0)
})
.on('error', handleRequestError)

return deferred.promise
}
Expand Down Expand Up @@ -307,7 +326,8 @@ function tryPhantomjsInLib() {
var libModule = require('./lib/location.js')
if (libModule.location &&
getTargetPlatform() == libModule.platform &&
getTargetArch() == libModule.arch) {
getTargetArch() == libModule.arch &&
fs.statSync(path.join('./lib', libModule.location))) {
console.log('PhantomJS is previously installed at ' + libModule.location)
exit(0)
}
Expand Down Expand Up @@ -415,13 +435,10 @@ function downloadPhantomjs() {

var downloadUrl = downloadSpec.url
var downloadedFile
var conf

return npmConfPromise.then(function (_conf) {
conf = _conf

return kew.fcall(function () {
// Can't use a global version so start a download.
var tmpPath = findSuitableTempDirectory(conf)
var tmpPath = findSuitableTempDirectory()
var fileName = downloadUrl.split('/').pop()
downloadedFile = path.join(tmpPath, fileName)

Expand All @@ -438,7 +455,7 @@ function downloadPhantomjs() {
// Start the install.
console.log('Downloading', downloadUrl)
console.log('Saving to', downloadedFile)
return requestBinary(getRequestOptions(conf), downloadedFile)
return requestBinary(getRequestOptions(), downloadedFile)
})
}

Expand Down
19 changes: 9 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,17 @@
"test": "nodeunit --reporter=minimal test/tests.js && eslint install.js"
},
"dependencies": {
"adm-zip": "0.4.4",
"fs-extra": "~0.23.1",
"kew": "0.4.0",
"adm-zip": "~0.4.7",
"fs-extra": "~0.26.4",
"kew": "~0.7.0",
"md5": "~2.0.0",
"npmconf": "2.1.1",
"progress": "1.1.8",
"request": "2.42.0",
"request-progress": "0.3.1",
"which": "~1.0.5"
"progress": "~1.1.8",
"request": "~2.67.0",
"request-progress": "~2.0.1",
"which": "~1.2.2"
},
"devDependencies": {
"eslint": "1.5.1",
"nodeunit": "0.9.0"
"eslint": "1.10.3",
"nodeunit": "0.9.1"
}
}

0 comments on commit 83341c1

Please sign in to comment.