| 1 | const path = require('path')
|
|---|
| 2 | const fs = require('fs')
|
|---|
| 3 | const get = require('simple-get')
|
|---|
| 4 | const pump = require('pump')
|
|---|
| 5 | const tfs = require('tar-fs')
|
|---|
| 6 | const zlib = require('zlib')
|
|---|
| 7 | const util = require('./util')
|
|---|
| 8 | const error = require('./error')
|
|---|
| 9 | const proxy = require('./proxy')
|
|---|
| 10 | const mkdirp = require('mkdirp-classic')
|
|---|
| 11 |
|
|---|
| 12 | function downloadPrebuild (downloadUrl, opts, cb) {
|
|---|
| 13 | let cachedPrebuild = util.cachedPrebuild(downloadUrl)
|
|---|
| 14 | const localPrebuild = util.localPrebuild(downloadUrl, opts)
|
|---|
| 15 | const tempFile = util.tempFile(cachedPrebuild)
|
|---|
| 16 | const log = opts.log || util.noopLogger
|
|---|
| 17 |
|
|---|
| 18 | if (opts.nolocal) return download()
|
|---|
| 19 |
|
|---|
| 20 | log.info('looking for local prebuild @', localPrebuild)
|
|---|
| 21 | fs.access(localPrebuild, fs.R_OK | fs.W_OK, function (err) {
|
|---|
| 22 | if (err && err.code === 'ENOENT') {
|
|---|
| 23 | return download()
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | log.info('found local prebuild')
|
|---|
| 27 | cachedPrebuild = localPrebuild
|
|---|
| 28 | unpack()
|
|---|
| 29 | })
|
|---|
| 30 |
|
|---|
| 31 | function download () {
|
|---|
| 32 | ensureNpmCacheDir(function (err) {
|
|---|
| 33 | if (err) return onerror(err)
|
|---|
| 34 |
|
|---|
| 35 | log.info('looking for cached prebuild @', cachedPrebuild)
|
|---|
| 36 | fs.access(cachedPrebuild, fs.R_OK | fs.W_OK, function (err) {
|
|---|
| 37 | if (!(err && err.code === 'ENOENT')) {
|
|---|
| 38 | log.info('found cached prebuild')
|
|---|
| 39 | return unpack()
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | log.http('request', 'GET ' + downloadUrl)
|
|---|
| 43 | const reqOpts = proxy({ url: downloadUrl }, opts)
|
|---|
| 44 |
|
|---|
| 45 | if (opts.token) {
|
|---|
| 46 | reqOpts.headers = {
|
|---|
| 47 | 'User-Agent': 'simple-get',
|
|---|
| 48 | Accept: 'application/octet-stream',
|
|---|
| 49 | Authorization: 'token ' + opts.token
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | const req = get(reqOpts, function (err, res) {
|
|---|
| 54 | if (err) return onerror(err)
|
|---|
| 55 | log.http(res.statusCode, downloadUrl)
|
|---|
| 56 | if (res.statusCode !== 200) return onerror()
|
|---|
| 57 | mkdirp(util.prebuildCache(), function () {
|
|---|
| 58 | log.info('downloading to @', tempFile)
|
|---|
| 59 | pump(res, fs.createWriteStream(tempFile), function (err) {
|
|---|
| 60 | if (err) return onerror(err)
|
|---|
| 61 | fs.rename(tempFile, cachedPrebuild, function (err) {
|
|---|
| 62 | if (err) return cb(err)
|
|---|
| 63 | log.info('renaming to @', cachedPrebuild)
|
|---|
| 64 | unpack()
|
|---|
| 65 | })
|
|---|
| 66 | })
|
|---|
| 67 | })
|
|---|
| 68 | })
|
|---|
| 69 |
|
|---|
| 70 | req.setTimeout(30 * 1000, function () {
|
|---|
| 71 | req.abort()
|
|---|
| 72 | })
|
|---|
| 73 | })
|
|---|
| 74 |
|
|---|
| 75 | function onerror (err) {
|
|---|
| 76 | fs.unlink(tempFile, function () {
|
|---|
| 77 | cb(err || error.noPrebuilts(opts))
|
|---|
| 78 | })
|
|---|
| 79 | }
|
|---|
| 80 | })
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | function unpack () {
|
|---|
| 84 | let binaryName
|
|---|
| 85 |
|
|---|
| 86 | const updateName = opts.updateName || function (entry) {
|
|---|
| 87 | if (/\.node$/i.test(entry.name)) binaryName = entry.name
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | log.info('unpacking @', cachedPrebuild)
|
|---|
| 91 |
|
|---|
| 92 | const options = {
|
|---|
| 93 | readable: true,
|
|---|
| 94 | writable: true,
|
|---|
| 95 | hardlinkAsFilesFallback: true
|
|---|
| 96 | }
|
|---|
| 97 | const extract = tfs.extract(opts.path, options).on('entry', updateName)
|
|---|
| 98 |
|
|---|
| 99 | pump(fs.createReadStream(cachedPrebuild), zlib.createGunzip(), extract,
|
|---|
| 100 | function (err) {
|
|---|
| 101 | if (err) return cb(err)
|
|---|
| 102 |
|
|---|
| 103 | let resolved
|
|---|
| 104 | if (binaryName) {
|
|---|
| 105 | try {
|
|---|
| 106 | resolved = path.resolve(opts.path || '.', binaryName)
|
|---|
| 107 | } catch (err) {
|
|---|
| 108 | return cb(err)
|
|---|
| 109 | }
|
|---|
| 110 | log.info('unpack', 'resolved to ' + resolved)
|
|---|
| 111 |
|
|---|
| 112 | if (opts.runtime === 'node' && opts.platform === process.platform && opts.abi === process.versions.modules && opts.arch === process.arch) {
|
|---|
| 113 | try {
|
|---|
| 114 | require(resolved)
|
|---|
| 115 | } catch (err) {
|
|---|
| 116 | return cb(err)
|
|---|
| 117 | }
|
|---|
| 118 | log.info('unpack', 'required ' + resolved + ' successfully')
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | cb(null, resolved)
|
|---|
| 123 | })
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | function ensureNpmCacheDir (cb) {
|
|---|
| 127 | const cacheFolder = util.npmCache()
|
|---|
| 128 | fs.access(cacheFolder, fs.R_OK | fs.W_OK, function (err) {
|
|---|
| 129 | if (err && err.code === 'ENOENT') {
|
|---|
| 130 | return makeNpmCacheDir()
|
|---|
| 131 | }
|
|---|
| 132 | cb(err)
|
|---|
| 133 | })
|
|---|
| 134 |
|
|---|
| 135 | function makeNpmCacheDir () {
|
|---|
| 136 | log.info('npm cache directory missing, creating it...')
|
|---|
| 137 | mkdirp(cacheFolder, cb)
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | module.exports = downloadPrebuild
|
|---|