| [81bc7da] | 1 | const path = require('path')
|
|---|
| 2 | const github = require('github-from-package')
|
|---|
| 3 | const home = require('os').homedir
|
|---|
| 4 | const crypto = require('crypto')
|
|---|
| 5 | const expandTemplate = require('expand-template')()
|
|---|
| 6 |
|
|---|
| 7 | function getDownloadUrl (opts) {
|
|---|
| 8 | const pkgName = opts.pkg.name.replace(/^@[a-zA-Z0-9_\-.~]+\//, '')
|
|---|
| 9 | return expandTemplate(urlTemplate(opts), {
|
|---|
| 10 | name: pkgName,
|
|---|
| 11 | package_name: pkgName,
|
|---|
| 12 | version: opts.pkg.version,
|
|---|
| 13 | major: opts.pkg.version.split('.')[0],
|
|---|
| 14 | minor: opts.pkg.version.split('.')[1],
|
|---|
| 15 | patch: opts.pkg.version.split('.')[2],
|
|---|
| 16 | prerelease: opts.pkg.version.split('-')[1],
|
|---|
| 17 | build: opts.pkg.version.split('+')[1],
|
|---|
| 18 | abi: opts.abi || process.versions.modules,
|
|---|
| 19 | node_abi: process.versions.modules,
|
|---|
| 20 | runtime: opts.runtime || 'node',
|
|---|
| 21 | platform: opts.platform,
|
|---|
| 22 | arch: opts.arch,
|
|---|
| 23 | libc: opts.libc || '',
|
|---|
| 24 | configuration: (opts.debug ? 'Debug' : 'Release'),
|
|---|
| 25 | module_name: opts.pkg.binary && opts.pkg.binary.module_name,
|
|---|
| 26 | tag_prefix: opts['tag-prefix']
|
|---|
| 27 | })
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | function getApiUrl (opts) {
|
|---|
| 31 | return github(opts.pkg).replace('github.com', 'api.github.com/repos') + '/releases'
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | function getAssetUrl (opts, assetId) {
|
|---|
| 35 | return getApiUrl(opts) + '/assets/' + assetId
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | function urlTemplate (opts) {
|
|---|
| 39 | if (typeof opts.download === 'string') {
|
|---|
| 40 | return opts.download
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | const packageName = '{name}-v{version}-{runtime}-v{abi}-{platform}{libc}-{arch}.tar.gz'
|
|---|
| 44 | const hostMirrorUrl = getHostMirrorUrl(opts)
|
|---|
| 45 |
|
|---|
| 46 | if (hostMirrorUrl) {
|
|---|
| 47 | return hostMirrorUrl + '/{tag_prefix}{version}/' + packageName
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | if (opts.pkg.binary && opts.pkg.binary.host) {
|
|---|
| 51 | return [
|
|---|
| 52 | opts.pkg.binary.host,
|
|---|
| 53 | opts.pkg.binary.remote_path,
|
|---|
| 54 | opts.pkg.binary.package_name || packageName
|
|---|
| 55 | ].map(function (path) {
|
|---|
| 56 | return trimSlashes(path)
|
|---|
| 57 | }).filter(Boolean).join('/')
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | return github(opts.pkg) + '/releases/download/{tag_prefix}{version}/' + packageName
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | function getEnvPrefix (pkgName) {
|
|---|
| 64 | return 'npm_config_' + (pkgName || '').replace(/[^a-zA-Z0-9]/g, '_').replace(/^_/, '')
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | function getHostMirrorUrl (opts) {
|
|---|
| 68 | const propName = getEnvPrefix(opts.pkg.name) + '_binary_host'
|
|---|
| 69 | return process.env[propName] || process.env[propName + '_mirror']
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | function trimSlashes (str) {
|
|---|
| 73 | if (str) return str.replace(/^\.\/|^\/|\/$/g, '')
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | function cachedPrebuild (url) {
|
|---|
| 77 | const digest = crypto.createHash('sha512').update(url).digest('hex').slice(0, 6)
|
|---|
| 78 | return path.join(prebuildCache(), digest + '-' + path.basename(url).replace(/[^a-zA-Z0-9.]+/g, '-'))
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | function npmCache () {
|
|---|
| 82 | const env = process.env
|
|---|
| 83 | return env.npm_config_cache || (env.APPDATA ? path.join(env.APPDATA, 'npm-cache') : path.join(home(), '.npm'))
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | function prebuildCache () {
|
|---|
| 87 | return path.join(npmCache(), '_prebuilds')
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | function tempFile (cached) {
|
|---|
| 91 | return cached + '.' + process.pid + '-' + Math.random().toString(16).slice(2) + '.tmp'
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | function packageOrigin (env, pkg) {
|
|---|
| 95 | // npm <= 6: metadata is stored on disk in node_modules
|
|---|
| 96 | if (pkg._from) {
|
|---|
| 97 | return pkg._from
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | // npm 7: metadata is exposed to environment by arborist
|
|---|
| 101 | if (env.npm_package_from) {
|
|---|
| 102 | // NOTE: seems undefined atm (npm 7.0.2)
|
|---|
| 103 | return env.npm_package_from
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | if (env.npm_package_resolved) {
|
|---|
| 107 | // NOTE: not sure about the difference with _from, but it's all we have
|
|---|
| 108 | return env.npm_package_resolved
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | function localPrebuild (url, opts) {
|
|---|
| 113 | const propName = getEnvPrefix(opts.pkg.name) + '_local_prebuilds'
|
|---|
| 114 | const prefix = process.env[propName] || opts['local-prebuilds'] || 'prebuilds'
|
|---|
| 115 | return path.join(prefix, path.basename(url))
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | const noopLogger = {
|
|---|
| 119 | http: function () {},
|
|---|
| 120 | silly: function () {},
|
|---|
| 121 | debug: function () {},
|
|---|
| 122 | info: function () {},
|
|---|
| 123 | warn: function () {},
|
|---|
| 124 | error: function () {},
|
|---|
| 125 | critical: function () {},
|
|---|
| 126 | alert: function () {},
|
|---|
| 127 | emergency: function () {},
|
|---|
| 128 | notice: function () {},
|
|---|
| 129 | verbose: function () {},
|
|---|
| 130 | fatal: function () {}
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | exports.getDownloadUrl = getDownloadUrl
|
|---|
| 134 | exports.getApiUrl = getApiUrl
|
|---|
| 135 | exports.getAssetUrl = getAssetUrl
|
|---|
| 136 | exports.urlTemplate = urlTemplate
|
|---|
| 137 | exports.cachedPrebuild = cachedPrebuild
|
|---|
| 138 | exports.localPrebuild = localPrebuild
|
|---|
| 139 | exports.prebuildCache = prebuildCache
|
|---|
| 140 | exports.npmCache = npmCache
|
|---|
| 141 | exports.tempFile = tempFile
|
|---|
| 142 | exports.packageOrigin = packageOrigin
|
|---|
| 143 | exports.noopLogger = noopLogger
|
|---|