util.js 4.2 KB

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