source: node_modules/prebuild-install/util.js@ 81bc7da

finki-main main
Last change on this file since 81bc7da was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago

Initial commit

  • Property mode set to 100644
File size: 4.2 KB
RevLine 
[81bc7da]1const path = require('path')
2const github = require('github-from-package')
3const home = require('os').homedir
4const crypto = require('crypto')
5const expandTemplate = require('expand-template')()
6
7function 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
30function getApiUrl (opts) {
31 return github(opts.pkg).replace('github.com', 'api.github.com/repos') + '/releases'
32}
33
34function getAssetUrl (opts, assetId) {
35 return getApiUrl(opts) + '/assets/' + assetId
36}
37
38function 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
63function getEnvPrefix (pkgName) {
64 return 'npm_config_' + (pkgName || '').replace(/[^a-zA-Z0-9]/g, '_').replace(/^_/, '')
65}
66
67function getHostMirrorUrl (opts) {
68 const propName = getEnvPrefix(opts.pkg.name) + '_binary_host'
69 return process.env[propName] || process.env[propName + '_mirror']
70}
71
72function trimSlashes (str) {
73 if (str) return str.replace(/^\.\/|^\/|\/$/g, '')
74}
75
76function 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
81function 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
86function prebuildCache () {
87 return path.join(npmCache(), '_prebuilds')
88}
89
90function tempFile (cached) {
91 return cached + '.' + process.pid + '-' + Math.random().toString(16).slice(2) + '.tmp'
92}
93
94function 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
112function 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
118const 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
133exports.getDownloadUrl = getDownloadUrl
134exports.getApiUrl = getApiUrl
135exports.getAssetUrl = getAssetUrl
136exports.urlTemplate = urlTemplate
137exports.cachedPrebuild = cachedPrebuild
138exports.localPrebuild = localPrebuild
139exports.prebuildCache = prebuildCache
140exports.npmCache = npmCache
141exports.tempFile = tempFile
142exports.packageOrigin = packageOrigin
143exports.noopLogger = noopLogger
Note: See TracBrowser for help on using the repository browser.