11cb0ef41Sopenharmony_ciconst Fetcher = require('./fetcher.js')
21cb0ef41Sopenharmony_ciconst FileFetcher = require('./file.js')
31cb0ef41Sopenharmony_ciconst _tarballFromResolved = Symbol.for('pacote.Fetcher._tarballFromResolved')
41cb0ef41Sopenharmony_ciconst pacoteVersion = require('../package.json').version
51cb0ef41Sopenharmony_ciconst fetch = require('npm-registry-fetch')
61cb0ef41Sopenharmony_ciconst { Minipass } = require('minipass')
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst _cacheFetches = Symbol.for('pacote.Fetcher._cacheFetches')
91cb0ef41Sopenharmony_ciconst _headers = Symbol('_headers')
101cb0ef41Sopenharmony_ciclass RemoteFetcher extends Fetcher {
111cb0ef41Sopenharmony_ci  constructor (spec, opts) {
121cb0ef41Sopenharmony_ci    super(spec, opts)
131cb0ef41Sopenharmony_ci    this.resolved = this.spec.fetchSpec
141cb0ef41Sopenharmony_ci    const resolvedURL = new URL(this.resolved)
151cb0ef41Sopenharmony_ci    if (this.replaceRegistryHost !== 'never'
161cb0ef41Sopenharmony_ci      && (this.replaceRegistryHost === 'always'
171cb0ef41Sopenharmony_ci      || this.replaceRegistryHost === resolvedURL.host)) {
181cb0ef41Sopenharmony_ci      this.resolved = new URL(resolvedURL.pathname, this.registry).href
191cb0ef41Sopenharmony_ci    }
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci    // nam is a fermented pork sausage that is good to eat
221cb0ef41Sopenharmony_ci    const nameat = this.spec.name ? `${this.spec.name}@` : ''
231cb0ef41Sopenharmony_ci    this.pkgid = opts.pkgid ? opts.pkgid : `remote:${nameat}${this.resolved}`
241cb0ef41Sopenharmony_ci  }
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  // Don't need to cache tarball fetches in pacote, because make-fetch-happen
271cb0ef41Sopenharmony_ci  // will write into cacache anyway.
281cb0ef41Sopenharmony_ci  get [_cacheFetches] () {
291cb0ef41Sopenharmony_ci    return false
301cb0ef41Sopenharmony_ci  }
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  [_tarballFromResolved] () {
331cb0ef41Sopenharmony_ci    const stream = new Minipass()
341cb0ef41Sopenharmony_ci    stream.hasIntegrityEmitter = true
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci    const fetchOpts = {
371cb0ef41Sopenharmony_ci      ...this.opts,
381cb0ef41Sopenharmony_ci      headers: this[_headers](),
391cb0ef41Sopenharmony_ci      spec: this.spec,
401cb0ef41Sopenharmony_ci      integrity: this.integrity,
411cb0ef41Sopenharmony_ci      algorithms: [this.pickIntegrityAlgorithm()],
421cb0ef41Sopenharmony_ci    }
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci    // eslint-disable-next-line promise/always-return
451cb0ef41Sopenharmony_ci    fetch(this.resolved, fetchOpts).then(res => {
461cb0ef41Sopenharmony_ci      res.body.on('error',
471cb0ef41Sopenharmony_ci        /* istanbul ignore next - exceedingly rare and hard to simulate */
481cb0ef41Sopenharmony_ci        er => stream.emit('error', er)
491cb0ef41Sopenharmony_ci      )
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci      res.body.on('integrity', i => {
521cb0ef41Sopenharmony_ci        this.integrity = i
531cb0ef41Sopenharmony_ci        stream.emit('integrity', i)
541cb0ef41Sopenharmony_ci      })
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci      res.body.pipe(stream)
571cb0ef41Sopenharmony_ci    }).catch(er => stream.emit('error', er))
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci    return stream
601cb0ef41Sopenharmony_ci  }
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  [_headers] () {
631cb0ef41Sopenharmony_ci    return {
641cb0ef41Sopenharmony_ci      // npm will override this, but ensure that we always send *something*
651cb0ef41Sopenharmony_ci      'user-agent': this.opts.userAgent ||
661cb0ef41Sopenharmony_ci        `pacote/${pacoteVersion} node/${process.version}`,
671cb0ef41Sopenharmony_ci      ...(this.opts.headers || {}),
681cb0ef41Sopenharmony_ci      'pacote-version': pacoteVersion,
691cb0ef41Sopenharmony_ci      'pacote-req-type': 'tarball',
701cb0ef41Sopenharmony_ci      'pacote-pkg-id': this.pkgid,
711cb0ef41Sopenharmony_ci      ...(this.integrity ? { 'pacote-integrity': String(this.integrity) }
721cb0ef41Sopenharmony_ci      : {}),
731cb0ef41Sopenharmony_ci      ...(this.opts.headers || {}),
741cb0ef41Sopenharmony_ci    }
751cb0ef41Sopenharmony_ci  }
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  get types () {
781cb0ef41Sopenharmony_ci    return ['remote']
791cb0ef41Sopenharmony_ci  }
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  // getting a packument and/or manifest is the same as with a file: spec.
821cb0ef41Sopenharmony_ci  // unpack the tarball stream, and then read from the package.json file.
831cb0ef41Sopenharmony_ci  packument () {
841cb0ef41Sopenharmony_ci    return FileFetcher.prototype.packument.apply(this)
851cb0ef41Sopenharmony_ci  }
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  manifest () {
881cb0ef41Sopenharmony_ci    return FileFetcher.prototype.manifest.apply(this)
891cb0ef41Sopenharmony_ci  }
901cb0ef41Sopenharmony_ci}
911cb0ef41Sopenharmony_cimodule.exports = RemoteFetcher
92