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) {
237 req.on('close', function () {
238 if (extractCount === 0) {
239 cb(new Error('Connection closed while downloading tarball file'))
243 req.on('response', function (res) {
244 if (res.statusCode !== 200) {
246 cb(new Error(res.statusCode + ' status code downloading tarball'))
250 getContentSha(res, function (_, checksum) {
251 var filename = path.basename(tarballUrl).trim()
252 contentShasums[filename] = checksum
253 log.verbose('content checksum', filename, checksum)
256 // start unzipping and untaring
257 req.pipe(gunzip).pipe(extracter)
260 // invoked after the tarball has finished being extracted
261 function afterTarball () {
262 if (badDownload) return
263 if (extractCount === 0) {
264 return cb(new Error('There was a fatal problem while downloading/extracting the tarball'))
266 log.verbose('tarball', 'done parsing tarball')
270 // need to download node.lib
272 downloadNodeLib(deref)
275 // write the "installVersion" file
277 var installVersionPath = path.resolve(devDir, 'installVersion')
278 fs.writeFile(installVersionPath, gyp.package.installVersion + '\n', deref)
280 // download SHASUMS.txt
282 downloadShasums(deref)
285 // no async tasks required
289 function deref (err) {
290 if (err) return cb(err)
294 log.verbose('download contents checksum', JSON.stringify(contentShasums))
295 // check content shasums
296 for (var k in contentShasums) {
297 log.verbose('validating download checksum for ' + k, '(%s == %s)', contentShasums[k], expectShasums[k])
298 if (contentShasums[k] !== expectShasums[k]) {
299 cb(new Error(k + ' local checksum ' + contentShasums[k] + ' not match remote ' + expectShasums[k]))
308 function downloadShasums(done) {
309 var shasumsFile = (checksumAlgo === 'sha256') ? 'SHASUMS256.txt' : 'SHASUMS.txt'
310 log.verbose('check download content checksum, need to download `' + shasumsFile + '`...')
311 var shasumsPath = path.resolve(devDir, shasumsFile)
312 , shasumsUrl = distUrl + '/v' + version + '/' + shasumsFile
314 log.verbose('checksum url', shasumsUrl)
315 var req = download(shasumsUrl)
317 req.on('error', done)
318 req.on('response', function (res) {
319 if (res.statusCode !== 200) {
320 done(new Error(res.statusCode + ' status code downloading checksum'))
325 res.on('data', function (chunk) {
328 res.on('end', function () {
329 var lines = Buffer.concat(chunks).toString().trim().split('\n')
330 lines.forEach(function (line) {
331 var items = line.trim().split(/\s+/)
332 if (items.length !== 2) return
334 // 0035d18e2dcf9aad669b1c7c07319e17abfe3762 ./node-v0.11.4.tar.gz
335 var name = items[1].replace(/^\.\//, '')
336 expectShasums[name] = items[0]
339 log.verbose('checksum data', JSON.stringify(expectShasums))
345 function downloadNodeLib (done) {
346 log.verbose('on Windows; need to download `node.lib`...')
347 var dir32 = path.resolve(devDir, 'ia32')
348 , dir64 = path.resolve(devDir, 'x64')
349 , nodeLibPath32 = path.resolve(dir32, 'node.lib')
350 , nodeLibPath64 = path.resolve(dir64, 'node.lib')
351 , nodeLibUrl32 = distUrl + '/v' + version + '/node.lib'
352 , nodeLibUrl64 = distUrl + '/v' + version + '/x64/node.lib'
354 log.verbose('32-bit node.lib dir', dir32)
355 log.verbose('64-bit node.lib dir', dir64)
356 log.verbose('`node.lib` 32-bit url', nodeLibUrl32)
357 log.verbose('`node.lib` 64-bit url', nodeLibUrl64)
360 mkdir(dir32, function (err) {
361 if (err) return done(err)
362 log.verbose('streaming 32-bit node.lib to:', nodeLibPath32)
364 var req = download(nodeLibUrl32)
366 req.on('error', done)
367 req.on('response', function (res) {
368 if (res.statusCode !== 200) {
369 done(new Error(res.statusCode + ' status code downloading 32-bit node.lib'))
373 getContentSha(res, function (_, checksum) {
374 contentShasums['node.lib'] = checksum
375 log.verbose('content checksum', 'node.lib', checksum)
378 var ws = fs.createWriteStream(nodeLibPath32)
382 req.on('end', function () {
386 mkdir(dir64, function (err) {
387 if (err) return done(err)
388 log.verbose('streaming 64-bit node.lib to:', nodeLibPath64)
390 var req = download(nodeLibUrl64)
392 req.on('error', done)
393 req.on('response', function (res) {
394 if (res.statusCode !== 200) {
395 done(new Error(res.statusCode + ' status code downloading 64-bit node.lib'))
399 getContentSha(res, function (_, checksum) {
400 contentShasums['x64/node.lib'] = checksum
401 log.verbose('content checksum', 'x64/node.lib', checksum)
404 var ws = fs.createWriteStream(nodeLibPath64)
408 req.on('end', function () {
412 } // downloadNodeLib()
419 * Checks if a given filename is "valid" for this installation.
422 function valid (file) {
424 return minimatch(file, '*.h', { matchBase: true }) ||
425 minimatch(file, '*.gypi', { matchBase: true })
429 * The EACCES fallback is a workaround for npm's `sudo` behavior, where
430 * it drops the permissions before invoking any child processes (like
431 * node-gyp). So what happens is the "nobody" user doesn't have
432 * permission to create the dev dir. As a fallback, make the tmpdir() be
433 * the dev dir for this installation. This is not ideal, but at least
434 * the compilation will succeed...
437 function eaccesFallback () {
438 var tmpdir = osenv.tmpdir()
439 gyp.devDir = path.resolve(tmpdir, '.node-gyp')
440 log.warn('EACCES', 'user "%s" does not have permission to access the dev dir "%s"', osenv.user(), devDir)
441 log.warn('EACCES', 'attempting to reinstall using temporary dev dir "%s"', gyp.devDir)
442 if (process.cwd() == tmpdir) {
443 log.verbose('tmpdir == cwd', 'automatically will remove dev files after to save disk space')
444 gyp.todo.push({ name: 'remove', args: argv })
446 gyp.commands.install(argv, cb)