2 module.exports = exports = install
4 exports.usage = 'Install node development files for the specified node version.'
10 var fs = require('graceful-fs')
11 , osenv = require('osenv')
12 , tar = require('tar')
13 , rm = require('rimraf')
14 , path = require('path')
15 , crypto = require('crypto')
16 , zlib = require('zlib')
17 , log = require('npmlog')
18 , semver = require('semver')
19 , fstream = require('fstream')
20 , request = require('request')
21 , minimatch = require('minimatch')
22 , mkdir = require('mkdirp')
23 , win = process.platform == 'win32'
25 function install (gyp, argv, callback) {
27 // ensure no double-callbacks happen
32 log.warn('install', 'got an error, rolling back install')
33 // roll-back the install if anything went wrong
34 gyp.commands.remove([ version ], function (err2) {
38 callback(null, version)
42 var distUrl = gyp.opts['dist-url'] || gyp.opts.disturl || 'https://iojs.org/dist'
45 // Determine which node dev files version we are installing
46 var versionStr = argv[0] || gyp.opts.target || process.version
47 log.verbose('install', 'input version string %j', versionStr)
49 // parse the version to normalize and ensure it's valid
50 var version = semver.parse(versionStr)
52 return callback(new Error('Invalid version number: ' + versionStr))
55 if (semver.lt(versionStr, '0.8.0')) {
56 return callback(new Error('Minimum target version is `0.8.0` or greater. Got: ' + versionStr))
59 // 0.x.y-pre versions are not published yet and cannot be installed. Bail.
60 if (version.prerelease[0] === 'pre') {
61 log.verbose('detected "pre" node version', versionStr)
62 if (gyp.opts.nodedir) {
63 log.verbose('--nodedir flag was passed; skipping install', gyp.opts.nodedir)
66 callback(new Error('"pre" versions of node cannot be installed, use the --nodedir flag instead'))
71 // flatten version into String
72 version = version.version
73 log.verbose('install', 'installing version: %s', version)
75 // distributions starting with 0.10.0 contain sha256 checksums
76 var checksumAlgo = semver.gte(version, '0.10.0') ? 'sha256' : 'sha1'
78 // the directory where the dev files will be installed
79 var devDir = path.resolve(gyp.devDir, version)
81 // If '--ensure' was passed, then don't *always* install the version;
82 // check if it is already installed, and only install when needed
83 if (gyp.opts.ensure) {
84 log.verbose('install', '--ensure was passed, so won\'t reinstall if already installed')
85 fs.stat(devDir, function (err, stat) {
87 if (err.code == 'ENOENT') {
88 log.verbose('install', 'version not already installed, continuing with install', version)
90 } else if (err.code == 'EACCES') {
97 log.verbose('install', 'version is already installed, need to check "installVersion"')
98 var installVersionFile = path.resolve(devDir, 'installVersion')
99 fs.readFile(installVersionFile, 'ascii', function (err, ver) {
100 if (err && err.code != 'ENOENT') {
103 var installVersion = parseInt(ver, 10) || 0
104 log.verbose('got "installVersion"', installVersion)
105 log.verbose('needs "installVersion"', gyp.package.installVersion)
106 if (installVersion < gyp.package.installVersion) {
107 log.verbose('install', 'version is no good; reinstalling')
110 log.verbose('install', 'version is good')
119 function download (url) {
126 'User-Agent': 'node-gyp v' + gyp.version + ' (node ' + process.version + ')'
130 // basic support for a proxy server
131 var proxyUrl = gyp.opts.proxy
132 || process.env.http_proxy
133 || process.env.HTTP_PROXY
134 || process.env.npm_config_proxy
136 if (/^https?:\/\//i.test(proxyUrl)) {
137 log.verbose('download', 'using proxy url: "%s"', proxyUrl)
138 requestOpts.proxy = proxyUrl
140 log.warn('download', 'ignoring invalid "proxy" config setting: "%s"', proxyUrl)
144 // The "request" constructor can throw sometimes apparently :(
145 // See: https://github.com/TooTallNate/node-gyp/issues/114
146 req = request(requestOpts)
151 req.on('response', function (res) {
152 log.http(res.statusCode, url)
158 function getContentSha(res, callback) {
159 var shasum = crypto.createHash(checksumAlgo)
160 res.on('data', function (chunk) {
162 }).on('end', function () {
163 callback(null, shasum.digest('hex'))
169 log.verbose('ensuring nodedir is created', devDir)
171 // first create the dir for the node dev files
172 mkdir(devDir, function (err, created) {
174 if (err.code == 'EACCES') {
183 log.verbose('created nodedir', created)
186 // now download the node tarball
187 var tarPath = gyp.opts['tarball']
188 var tarballUrl = tarPath ? tarPath : distUrl + '/v' + version + '/iojs-v' + version + '.tar.gz'
189 , badDownload = false
191 , gunzip = zlib.createGunzip()
192 , extracter = tar.Extract({ path: devDir, strip: 1, filter: isValid })
194 var contentShasums = {}
195 var expectShasums = {}
197 // checks if a file to be extracted from the tarball is valid.
198 // only .h header files and the gyp files get extracted
199 function isValid () {
200 var name = this.path.substring(devDir.length + 1)
201 var isValid = valid(name)
202 if (name === '' && this.type === 'Directory') {
203 // the first directory entry is ok
207 log.verbose('extracted file from tarball', name)
211 log.silly('ignoring from tarball', name)
216 gunzip.on('error', cb)
217 extracter.on('error', cb)
218 extracter.on('end', afterTarball)
220 // download the tarball, gunzip and extract!
223 var input = fs.createReadStream(tarballUrl)
224 input.pipe(gunzip).pipe(extracter)
228 var req = download(tarballUrl)
231 // something went wrong downloading the tarball?
232 req.on('error', function (err) {
233 if (err.code === 'ENOTFOUND') {
234 return cb(new Error('This is most likely not a problem with node-gyp or the package itself and\n' +
235 'is related to network connectivity. In most cases you are behind a proxy or have bad \n' +
236 'network settings.'))
242 req.on('close', function () {
243 if (extractCount === 0) {
244 cb(new Error('Connection closed while downloading tarball file'))
248 req.on('response', function (res) {
249 if (res.statusCode !== 200) {
251 cb(new Error(res.statusCode + ' response dowloading ' + tarballUrl))
255 getContentSha(res, function (_, checksum) {
256 var filename = path.basename(tarballUrl).trim()
257 contentShasums[filename] = checksum
258 log.verbose('content checksum', filename, checksum)
261 // start unzipping and untaring
262 req.pipe(gunzip).pipe(extracter)
265 // invoked after the tarball has finished being extracted
266 function afterTarball () {
267 if (badDownload) return
268 if (extractCount === 0) {
269 return cb(new Error('There was a fatal problem while downloading/extracting the tarball'))
271 log.verbose('tarball', 'done parsing tarball')
275 // need to download iojs.lib
277 downloadNodeLib(deref)
280 // write the "installVersion" file
282 var installVersionPath = path.resolve(devDir, 'installVersion')
283 fs.writeFile(installVersionPath, gyp.package.installVersion + '\n', deref)
285 // Only download SHASUMS.txt if not using tarPath override
287 // download SHASUMS.txt
289 downloadShasums(deref)
293 // no async tasks required
297 function deref (err) {
298 if (err) return cb(err)
302 log.verbose('download contents checksum', JSON.stringify(contentShasums))
303 // check content shasums
304 for (var k in contentShasums) {
305 log.verbose('validating download checksum for ' + k, '(%s == %s)', contentShasums[k], expectShasums[k])
306 if (contentShasums[k] !== expectShasums[k]) {
307 cb(new Error(k + ' local checksum ' + contentShasums[k] + ' not match remote ' + expectShasums[k]))
316 function downloadShasums(done) {
317 var shasumsFile = (checksumAlgo === 'sha256') ? 'SHASUMS256.txt' : 'SHASUMS.txt'
318 log.verbose('check download content checksum, need to download `' + shasumsFile + '`...')
319 var shasumsPath = path.resolve(devDir, shasumsFile)
320 , shasumsUrl = distUrl + '/v' + version + '/' + shasumsFile
322 log.verbose('checksum url', shasumsUrl)
323 var req = download(shasumsUrl)
325 req.on('error', done)
326 req.on('response', function (res) {
327 if (res.statusCode !== 200) {
328 done(new Error(res.statusCode + ' status code downloading checksum'))
333 res.on('data', function (chunk) {
336 res.on('end', function () {
337 var lines = Buffer.concat(chunks).toString().trim().split('\n')
338 lines.forEach(function (line) {
339 var items = line.trim().split(/\s+/)
340 if (items.length !== 2) return
342 // 0035d18e2dcf9aad669b1c7c07319e17abfe3762 ./node-v0.11.4.tar.gz
343 var name = items[1].replace(/^\.\//, '')
344 expectShasums[name] = items[0]
347 log.verbose('checksum data', JSON.stringify(expectShasums))
353 function downloadNodeLib (done) {
354 log.verbose('on Windows; need to download `iojs.lib`...')
355 var dir32 = path.resolve(devDir, 'ia32')
356 , dir64 = path.resolve(devDir, 'x64')
357 , nodeLibPath32 = path.resolve(dir32, 'iojs.lib')
358 , nodeLibPath64 = path.resolve(dir64, 'iojs.lib')
359 , nodeLibUrl32 = distUrl + '/v' + version + '/win-x86/iojs.lib'
360 , nodeLibUrl64 = distUrl + '/v' + version + '/win-x64/iojs.lib'
362 log.verbose('32-bit iojs.lib dir', dir32)
363 log.verbose('64-bit iojs.lib dir', dir64)
364 log.verbose('`iojs.lib` 32-bit url', nodeLibUrl32)
365 log.verbose('`iojs.lib` 64-bit url', nodeLibUrl64)
368 mkdir(dir32, function (err) {
369 if (err) return done(err)
370 log.verbose('streaming 32-bit iojs.lib to:', nodeLibPath32)
372 var req = download(nodeLibUrl32)
374 req.on('error', done)
375 req.on('response', function (res) {
376 if (res.statusCode !== 200) {
377 done(new Error(res.statusCode + ' status code downloading 32-bit iojs.lib'))
381 getContentSha(res, function (_, checksum) {
382 contentShasums['win-x86/iojs.lib'] = checksum
383 log.verbose('content checksum', 'win-x86/iojs.lib', checksum)
386 var ws = fs.createWriteStream(nodeLibPath32)
390 req.on('end', function () {
394 mkdir(dir64, function (err) {
395 if (err) return done(err)
396 log.verbose('streaming 64-bit iojs.lib to:', nodeLibPath64)
398 var req = download(nodeLibUrl64)
400 req.on('error', done)
401 req.on('response', function (res) {
402 if (res.statusCode !== 200) {
403 done(new Error(res.statusCode + ' status code downloading 64-bit iojs.lib'))
407 getContentSha(res, function (_, checksum) {
408 contentShasums['win-x64/iojs.lib'] = checksum
409 log.verbose('content checksum', 'win-x64/iojs.lib', checksum)
412 var ws = fs.createWriteStream(nodeLibPath64)
416 req.on('end', function () {
420 } // downloadNodeLib()
427 * Checks if a given filename is "valid" for this installation.
430 function valid (file) {
432 return minimatch(file, '*.h', { matchBase: true }) ||
433 minimatch(file, '*.gypi', { matchBase: true })
437 * The EACCES fallback is a workaround for npm's `sudo` behavior, where
438 * it drops the permissions before invoking any child processes (like
439 * node-gyp). So what happens is the "nobody" user doesn't have
440 * permission to create the dev dir. As a fallback, make the tmpdir() be
441 * the dev dir for this installation. This is not ideal, but at least
442 * the compilation will succeed...
445 function eaccesFallback () {
446 var tmpdir = osenv.tmpdir()
447 gyp.devDir = path.resolve(tmpdir, '.node-gyp')
448 log.warn('EACCES', 'user "%s" does not have permission to access the dev dir "%s"', osenv.user(), devDir)
449 log.warn('EACCES', 'attempting to reinstall using temporary dev dir "%s"', gyp.devDir)
450 if (process.cwd() == tmpdir) {
451 log.verbose('tmpdir == cwd', 'automatically will remove dev files after to save disk space')
452 gyp.todo.push({ name: 'remove', args: argv })
454 gyp.commands.install(argv, cb)