remote.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. const Fetcher = require('./fetcher.js')
  2. const FileFetcher = require('./file.js')
  3. const _tarballFromResolved = Symbol.for('pacote.Fetcher._tarballFromResolved')
  4. const pacoteVersion = require('../package.json').version
  5. const fetch = require('npm-registry-fetch')
  6. const ssri = require('ssri')
  7. const Minipass = require('minipass')
  8. // The default registry URL is a string of great magic.
  9. const magic = /^https?:\/\/registry\.npmjs\.org\//
  10. const _cacheFetches = Symbol.for('pacote.Fetcher._cacheFetches')
  11. const _headers = Symbol('_headers')
  12. class RemoteFetcher extends Fetcher {
  13. constructor (spec, opts) {
  14. super(spec, opts)
  15. this.resolved = this.spec.fetchSpec
  16. if (magic.test(this.resolved) && !magic.test(this.registry + '/'))
  17. this.resolved = this.resolved.replace(magic, this.registry + '/')
  18. // nam is a fermented pork sausage that is good to eat
  19. const nameat = this.spec.name ? `${this.spec.name}@` : ''
  20. this.pkgid = opts.pkgid ? opts.pkgid : `remote:${nameat}${this.resolved}`
  21. }
  22. // Don't need to cache tarball fetches in pacote, because make-fetch-happen
  23. // will write into cacache anyway.
  24. get [_cacheFetches] () {
  25. return false
  26. }
  27. [_tarballFromResolved] () {
  28. const stream = new Minipass()
  29. const fetchOpts = {
  30. ...this.opts,
  31. headers: this[_headers](),
  32. spec: this.spec,
  33. integrity: this.integrity,
  34. algorithms: [ this.pickIntegrityAlgorithm() ],
  35. }
  36. fetch(this.resolved, fetchOpts).then(res => {
  37. const hash = res.headers.get('x-local-cache-hash')
  38. if (hash) {
  39. this.integrity = decodeURIComponent(hash)
  40. }
  41. res.body.on('error',
  42. /* istanbul ignore next - exceedingly rare and hard to simulate */
  43. er => stream.emit('error', er)
  44. ).pipe(stream)
  45. }).catch(er => stream.emit('error', er))
  46. return stream
  47. }
  48. [_headers] () {
  49. return {
  50. // npm will override this, but ensure that we always send *something*
  51. 'user-agent': this.opts.userAgent ||
  52. `pacote/${pacoteVersion} node/${process.version}`,
  53. ...(this.opts.headers || {}),
  54. 'pacote-version': pacoteVersion,
  55. 'pacote-req-type': 'tarball',
  56. 'pacote-pkg-id': this.pkgid,
  57. ...(this.integrity ? { 'pacote-integrity': String(this.integrity) }
  58. : {}),
  59. ...(this.opts.headers || {}),
  60. }
  61. }
  62. get types () {
  63. return ['remote']
  64. }
  65. // getting a packument and/or manifest is the same as with a file: spec.
  66. // unpack the tarball stream, and then read from the package.json file.
  67. packument () {
  68. return FileFetcher.prototype.packument.apply(this)
  69. }
  70. manifest () {
  71. return FileFetcher.prototype.manifest.apply(this)
  72. }
  73. }
  74. module.exports = RemoteFetcher