source: trip-planner-front/node_modules/node-gyp/lib/install.js@ bdd6491

Last change on this file since bdd6491 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 14.0 KB
RevLine 
[6a3a178]1'use strict'
2
3const fs = require('graceful-fs')
4const os = require('os')
5const tar = require('tar')
6const path = require('path')
7const crypto = require('crypto')
8const log = require('npmlog')
9const semver = require('semver')
10const request = require('request')
11const processRelease = require('./process-release')
12const win = process.platform === 'win32'
13const getProxyFromURI = require('./proxy')
14
15function install (fs, gyp, argv, callback) {
16 var release = processRelease(argv, gyp, process.version, process.release)
17
18 // ensure no double-callbacks happen
19 function cb (err) {
20 if (cb.done) {
21 return
22 }
23 cb.done = true
24 if (err) {
25 log.warn('install', 'got an error, rolling back install')
26 // roll-back the install if anything went wrong
27 gyp.commands.remove([release.versionDir], function () {
28 callback(err)
29 })
30 } else {
31 callback(null, release.version)
32 }
33 }
34
35 // Determine which node dev files version we are installing
36 log.verbose('install', 'input version string %j', release.version)
37
38 if (!release.semver) {
39 // could not parse the version string with semver
40 return callback(new Error('Invalid version number: ' + release.version))
41 }
42
43 if (semver.lt(release.version, '0.8.0')) {
44 return callback(new Error('Minimum target version is `0.8.0` or greater. Got: ' + release.version))
45 }
46
47 // 0.x.y-pre versions are not published yet and cannot be installed. Bail.
48 if (release.semver.prerelease[0] === 'pre') {
49 log.verbose('detected "pre" node version', release.version)
50 if (gyp.opts.nodedir) {
51 log.verbose('--nodedir flag was passed; skipping install', gyp.opts.nodedir)
52 callback()
53 } else {
54 callback(new Error('"pre" versions of node cannot be installed, use the --nodedir flag instead'))
55 }
56 return
57 }
58
59 // flatten version into String
60 log.verbose('install', 'installing version: %s', release.versionDir)
61
62 // the directory where the dev files will be installed
63 var devDir = path.resolve(gyp.devDir, release.versionDir)
64
65 // If '--ensure' was passed, then don't *always* install the version;
66 // check if it is already installed, and only install when needed
67 if (gyp.opts.ensure) {
68 log.verbose('install', '--ensure was passed, so won\'t reinstall if already installed')
69 fs.stat(devDir, function (err) {
70 if (err) {
71 if (err.code === 'ENOENT') {
72 log.verbose('install', 'version not already installed, continuing with install', release.version)
73 go()
74 } else if (err.code === 'EACCES') {
75 eaccesFallback(err)
76 } else {
77 cb(err)
78 }
79 return
80 }
81 log.verbose('install', 'version is already installed, need to check "installVersion"')
82 var installVersionFile = path.resolve(devDir, 'installVersion')
83 fs.readFile(installVersionFile, 'ascii', function (err, ver) {
84 if (err && err.code !== 'ENOENT') {
85 return cb(err)
86 }
87 var installVersion = parseInt(ver, 10) || 0
88 log.verbose('got "installVersion"', installVersion)
89 log.verbose('needs "installVersion"', gyp.package.installVersion)
90 if (installVersion < gyp.package.installVersion) {
91 log.verbose('install', 'version is no good; reinstalling')
92 go()
93 } else {
94 log.verbose('install', 'version is good')
95 cb()
96 }
97 })
98 })
99 } else {
100 go()
101 }
102
103 function getContentSha (res, callback) {
104 var shasum = crypto.createHash('sha256')
105 res.on('data', function (chunk) {
106 shasum.update(chunk)
107 }).on('end', function () {
108 callback(null, shasum.digest('hex'))
109 })
110 }
111
112 function go () {
113 log.verbose('ensuring nodedir is created', devDir)
114
115 // first create the dir for the node dev files
116 fs.mkdir(devDir, { recursive: true }, function (err, created) {
117 if (err) {
118 if (err.code === 'EACCES') {
119 eaccesFallback(err)
120 } else {
121 cb(err)
122 }
123 return
124 }
125
126 if (created) {
127 log.verbose('created nodedir', created)
128 }
129
130 // now download the node tarball
131 var tarPath = gyp.opts.tarball
132 var badDownload = false
133 var extractCount = 0
134 var contentShasums = {}
135 var expectShasums = {}
136
137 // checks if a file to be extracted from the tarball is valid.
138 // only .h header files and the gyp files get extracted
139 function isValid (path) {
140 var isValid = valid(path)
141 if (isValid) {
142 log.verbose('extracted file from tarball', path)
143 extractCount++
144 } else {
145 // invalid
146 log.silly('ignoring from tarball', path)
147 }
148 return isValid
149 }
150
151 // download the tarball and extract!
152 if (tarPath) {
153 return tar.extract({
154 file: tarPath,
155 strip: 1,
156 filter: isValid,
157 cwd: devDir
158 }).then(afterTarball, cb)
159 }
160
161 try {
162 var req = download(gyp, process.env, release.tarballUrl)
163 } catch (e) {
164 return cb(e)
165 }
166
167 // something went wrong downloading the tarball?
168 req.on('error', function (err) {
169 if (err.code === 'ENOTFOUND') {
170 return cb(new Error('This is most likely not a problem with node-gyp or the package itself and\n' +
171 'is related to network connectivity. In most cases you are behind a proxy or have bad \n' +
172 'network settings.'))
173 }
174 badDownload = true
175 cb(err)
176 })
177
178 req.on('close', function () {
179 if (extractCount === 0) {
180 cb(new Error('Connection closed while downloading tarball file'))
181 }
182 })
183
184 req.on('response', function (res) {
185 if (res.statusCode !== 200) {
186 badDownload = true
187 cb(new Error(res.statusCode + ' response downloading ' + release.tarballUrl))
188 return
189 }
190 // content checksum
191 getContentSha(res, function (_, checksum) {
192 var filename = path.basename(release.tarballUrl).trim()
193 contentShasums[filename] = checksum
194 log.verbose('content checksum', filename, checksum)
195 })
196
197 // start unzipping and untaring
198 res.pipe(tar.extract({
199 strip: 1,
200 cwd: devDir,
201 filter: isValid
202 }).on('close', afterTarball).on('error', cb))
203 })
204
205 // invoked after the tarball has finished being extracted
206 function afterTarball () {
207 if (badDownload) {
208 return
209 }
210 if (extractCount === 0) {
211 return cb(new Error('There was a fatal problem while downloading/extracting the tarball'))
212 }
213 log.verbose('tarball', 'done parsing tarball')
214 var async = 0
215
216 if (win) {
217 // need to download node.lib
218 async++
219 downloadNodeLib(deref)
220 }
221
222 // write the "installVersion" file
223 async++
224 var installVersionPath = path.resolve(devDir, 'installVersion')
225 fs.writeFile(installVersionPath, gyp.package.installVersion + '\n', deref)
226
227 // Only download SHASUMS.txt if we downloaded something in need of SHA verification
228 if (!tarPath || win) {
229 // download SHASUMS.txt
230 async++
231 downloadShasums(deref)
232 }
233
234 if (async === 0) {
235 // no async tasks required
236 cb()
237 }
238
239 function deref (err) {
240 if (err) {
241 return cb(err)
242 }
243
244 async--
245 if (!async) {
246 log.verbose('download contents checksum', JSON.stringify(contentShasums))
247 // check content shasums
248 for (var k in contentShasums) {
249 log.verbose('validating download checksum for ' + k, '(%s == %s)', contentShasums[k], expectShasums[k])
250 if (contentShasums[k] !== expectShasums[k]) {
251 cb(new Error(k + ' local checksum ' + contentShasums[k] + ' not match remote ' + expectShasums[k]))
252 return
253 }
254 }
255 cb()
256 }
257 }
258 }
259
260 function downloadShasums (done) {
261 log.verbose('check download content checksum, need to download `SHASUMS256.txt`...')
262 log.verbose('checksum url', release.shasumsUrl)
263 try {
264 var req = download(gyp, process.env, release.shasumsUrl)
265 } catch (e) {
266 return cb(e)
267 }
268
269 req.on('error', done)
270 req.on('response', function (res) {
271 if (res.statusCode !== 200) {
272 done(new Error(res.statusCode + ' status code downloading checksum'))
273 return
274 }
275
276 var chunks = []
277 res.on('data', function (chunk) {
278 chunks.push(chunk)
279 })
280 res.on('end', function () {
281 var lines = Buffer.concat(chunks).toString().trim().split('\n')
282 lines.forEach(function (line) {
283 var items = line.trim().split(/\s+/)
284 if (items.length !== 2) {
285 return
286 }
287
288 // 0035d18e2dcf9aad669b1c7c07319e17abfe3762 ./node-v0.11.4.tar.gz
289 var name = items[1].replace(/^\.\//, '')
290 expectShasums[name] = items[0]
291 })
292
293 log.verbose('checksum data', JSON.stringify(expectShasums))
294 done()
295 })
296 })
297 }
298
299 function downloadNodeLib (done) {
300 log.verbose('on Windows; need to download `' + release.name + '.lib`...')
301 var archs = ['ia32', 'x64', 'arm64']
302 var async = archs.length
303 archs.forEach(function (arch) {
304 var dir = path.resolve(devDir, arch)
305 var targetLibPath = path.resolve(dir, release.name + '.lib')
306 var libUrl = release[arch].libUrl
307 var libPath = release[arch].libPath
308 var name = arch + ' ' + release.name + '.lib'
309 log.verbose(name, 'dir', dir)
310 log.verbose(name, 'url', libUrl)
311
312 fs.mkdir(dir, { recursive: true }, function (err) {
313 if (err) {
314 return done(err)
315 }
316 log.verbose('streaming', name, 'to:', targetLibPath)
317
318 try {
319 var req = download(gyp, process.env, libUrl, cb)
320 } catch (e) {
321 return cb(e)
322 }
323
324 req.on('error', done)
325 req.on('response', function (res) {
326 if (res.statusCode === 403 || res.statusCode === 404) {
327 if (arch === 'arm64') {
328 // Arm64 is a newer platform on Windows and not all node distributions provide it.
329 log.verbose(`${name} was not found in ${libUrl}`)
330 } else {
331 log.warn(`${name} was not found in ${libUrl}`)
332 }
333 return
334 } else if (res.statusCode !== 200) {
335 done(new Error(res.statusCode + ' status code downloading ' + name))
336 return
337 }
338
339 getContentSha(res, function (_, checksum) {
340 contentShasums[libPath] = checksum
341 log.verbose('content checksum', libPath, checksum)
342 })
343
344 var ws = fs.createWriteStream(targetLibPath)
345 ws.on('error', cb)
346 req.pipe(ws)
347 })
348 req.on('end', function () { --async || done() })
349 })
350 })
351 } // downloadNodeLib()
352 }) // mkdir()
353 } // go()
354
355 /**
356 * Checks if a given filename is "valid" for this installation.
357 */
358
359 function valid (file) {
360 // header files
361 var extname = path.extname(file)
362 return extname === '.h' || extname === '.gypi'
363 }
364
365 /**
366 * The EACCES fallback is a workaround for npm's `sudo` behavior, where
367 * it drops the permissions before invoking any child processes (like
368 * node-gyp). So what happens is the "nobody" user doesn't have
369 * permission to create the dev dir. As a fallback, make the tmpdir() be
370 * the dev dir for this installation. This is not ideal, but at least
371 * the compilation will succeed...
372 */
373
374 function eaccesFallback (err) {
375 var noretry = '--node_gyp_internal_noretry'
376 if (argv.indexOf(noretry) !== -1) {
377 return cb(err)
378 }
379 var tmpdir = os.tmpdir()
380 gyp.devDir = path.resolve(tmpdir, '.node-gyp')
381 var userString = ''
382 try {
383 // os.userInfo can fail on some systems, it's not critical here
384 userString = ` ("${os.userInfo().username}")`
385 } catch (e) {}
386 log.warn('EACCES', 'current user%s does not have permission to access the dev dir "%s"', userString, devDir)
387 log.warn('EACCES', 'attempting to reinstall using temporary dev dir "%s"', gyp.devDir)
388 if (process.cwd() === tmpdir) {
389 log.verbose('tmpdir == cwd', 'automatically will remove dev files after to save disk space')
390 gyp.todo.push({ name: 'remove', args: argv })
391 }
392 gyp.commands.install([noretry].concat(argv), cb)
393 }
394}
395
396function download (gyp, env, url) {
397 log.http('GET', url)
398
399 var requestOpts = {
400 uri: url,
401 headers: {
402 'User-Agent': 'node-gyp v' + gyp.version + ' (node ' + process.version + ')',
403 Connection: 'keep-alive'
404 }
405 }
406
407 var cafile = gyp.opts.cafile
408 if (cafile) {
409 requestOpts.ca = readCAFile(cafile)
410 }
411
412 // basic support for a proxy server
413 var proxyUrl = getProxyFromURI(gyp, env, url)
414 if (proxyUrl) {
415 if (/^https?:\/\//i.test(proxyUrl)) {
416 log.verbose('download', 'using proxy url: "%s"', proxyUrl)
417 requestOpts.proxy = proxyUrl
418 } else {
419 log.warn('download', 'ignoring invalid "proxy" config setting: "%s"', proxyUrl)
420 }
421 }
422
423 var req = request(requestOpts)
424 req.on('response', function (res) {
425 log.http(res.statusCode, url)
426 })
427
428 return req
429}
430
431function readCAFile (filename) {
432 // The CA file can contain multiple certificates so split on certificate
433 // boundaries. [\S\s]*? is used to match everything including newlines.
434 var ca = fs.readFileSync(filename, 'utf8')
435 var re = /(-----BEGIN CERTIFICATE-----[\S\s]*?-----END CERTIFICATE-----)/g
436 return ca.match(re)
437}
438
439module.exports = function (gyp, argv, callback) {
440 return install(fs, gyp, argv, callback)
441}
442module.exports.test = {
443 download: download,
444 install: install,
445 readCAFile: readCAFile
446}
447module.exports.usage = 'Install node development files for the specified node version.'
Note: See TracBrowser for help on using the repository browser.