tarball.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict'
  2. const BB = require('bluebird')
  3. const fetch = require('npm-registry-fetch')
  4. const manifest = require('./manifest')
  5. const optCheck = require('../../util/opt-check')
  6. const PassThrough = require('stream').PassThrough
  7. const ssri = require('ssri')
  8. const url = require('url')
  9. module.exports = tarball
  10. function tarball (spec, opts) {
  11. opts = optCheck(opts)
  12. const registry = fetch.pickRegistry(spec, opts)
  13. const stream = new PassThrough()
  14. let mani
  15. if (
  16. opts.resolved &&
  17. // spec.type === 'version' &&
  18. opts.resolved.indexOf(registry) === 0
  19. ) {
  20. // fakeChild is a shortcut to avoid looking up a manifest!
  21. mani = BB.resolve({
  22. name: spec.name,
  23. version: spec.fetchSpec,
  24. _integrity: opts.integrity,
  25. _resolved: opts.resolved,
  26. _fakeChild: true
  27. })
  28. } else {
  29. // We can't trust opts.resolved if it's going to a separate host.
  30. mani = manifest(spec, opts)
  31. }
  32. mani.then(mani => {
  33. !mani._fakeChild && stream.emit('manifest', mani)
  34. const fetchStream = fromManifest(mani, spec, opts).on(
  35. 'integrity', i => stream.emit('integrity', i)
  36. )
  37. fetchStream.on('error', err => stream.emit('error', err))
  38. fetchStream.pipe(stream)
  39. return null
  40. }).catch(err => stream.emit('error', err))
  41. return stream
  42. }
  43. module.exports.fromManifest = fromManifest
  44. function fromManifest (manifest, spec, opts) {
  45. opts = optCheck(opts)
  46. if (spec.scope) { opts = opts.concat({ scope: spec.scope }) }
  47. const stream = new PassThrough()
  48. const registry = fetch.pickRegistry(spec, opts)
  49. const uri = getTarballUrl(spec, registry, manifest, opts)
  50. fetch(uri, opts.concat({
  51. headers: {
  52. 'pacote-req-type': 'tarball',
  53. 'pacote-pkg-id': `registry:${manifest.name}@${uri}`
  54. },
  55. integrity: manifest._integrity,
  56. algorithms: [
  57. manifest._integrity
  58. ? ssri.parse(manifest._integrity).pickAlgorithm()
  59. : 'sha1'
  60. ],
  61. spec
  62. }, opts))
  63. .then(res => {
  64. const hash = res.headers.get('x-local-cache-hash')
  65. if (hash) {
  66. stream.emit('integrity', decodeURIComponent(hash))
  67. }
  68. res.body.on('error', err => stream.emit('error', err))
  69. res.body.pipe(stream)
  70. return null
  71. })
  72. .catch(err => stream.emit('error', err))
  73. return stream
  74. }
  75. function getTarballUrl (spec, registry, mani, opts) {
  76. const reg = url.parse(registry)
  77. const tarball = url.parse(mani._resolved)
  78. // https://github.com/npm/npm/pull/9471
  79. //
  80. // TL;DR: Some alternative registries host tarballs on http and packuments
  81. // on https, and vice-versa. There's also a case where people who can't use
  82. // SSL to access the npm registry, for example, might use
  83. // `--registry=http://registry.npmjs.org/`. In this case, we need to
  84. // rewrite `tarball` to match the protocol.
  85. //
  86. if (reg.hostname === tarball.hostname && reg.protocol !== tarball.protocol) {
  87. tarball.protocol = reg.protocol
  88. // Ports might be same host different protocol!
  89. if (reg.port !== tarball.port) {
  90. delete tarball.host
  91. tarball.port = reg.port
  92. }
  93. delete tarball.href
  94. }
  95. return url.format(tarball)
  96. }