@@ -18,6 +18,7 @@ const removeTrailingSlashes = require('./util/trailing-slashes.js')
1818const getContents = require ( '@npmcli/installed-package-contents' )
1919const readPackageJsonFast = require ( 'read-package-json-fast' )
2020const readPackageJson = promisify ( require ( 'read-package-json' ) )
21+ const Minipass = require ( 'minipass' )
2122
2223// we only change ownership on unix platforms, and only if uid is 0
2324const selfOwner = process . getuid && process . getuid ( ) === 0 ? {
@@ -105,15 +106,10 @@ class FetcherBase {
105106 this [ _readPackageJson ] = readPackageJsonFast
106107 }
107108
108- // config values: npmjs (default), never, always
109- // we don't want to mutate the original value
110- if ( opts . replaceRegistryHost !== 'never'
111- && opts . replaceRegistryHost !== 'always'
112- ) {
113- this . replaceRegistryHost = 'npmjs'
114- } else {
115- this . replaceRegistryHost = opts . replaceRegistryHost
116- }
109+ // rrh is a registry hostname or 'never' or 'always'
110+ // defaults to registry.npmjs.org
111+ this . replaceRegistryHost = ( ! opts . replaceRegistryHost || opts . replaceRegistryHost === 'npmjs' ) ?
112+ 'registry.npmjs.org' : opts . replaceRegistryHost
117113
118114 this . defaultTag = opts . defaultTag || 'latest'
119115 this . registry = removeTrailingSlashes ( opts . registry || 'https://registry.npmjs.org' )
@@ -224,40 +220,42 @@ class FetcherBase {
224220 }
225221
226222 [ _istream ] ( stream ) {
227- // everyone will need one of these, either for verifying or calculating
228- // We always set it, because we have might only have a weak legacy hex
229- // sha1 in the packument, and this MAY upgrade it to a stronger algo.
230- // If we had an integrity, and it doesn't match, then this does not
231- // override that error; the istream will raise the error before it
232- // gets to the point of re-setting the integrity.
233- const istream = ssri . integrityStream ( this . opts )
234- istream . on ( 'integrity' , i => this . integrity = i )
235- stream . on ( 'error' , er => istream . emit ( 'error' , er ) )
236-
237- // if not caching this, just pipe through to the istream and return it
223+ // if not caching this, just return it
238224 if ( ! this . opts . cache || ! this [ _cacheFetches ] ) {
225+ // instead of creating a new integrity stream, we only piggyback on the
226+ // provided stream's events
227+ if ( stream . hasIntegrityEmitter ) {
228+ stream . on ( 'integrity' , i => this . integrity = i )
229+ return stream
230+ }
231+
232+ const istream = ssri . integrityStream ( this . opts )
233+ istream . on ( 'integrity' , i => this . integrity = i )
234+ stream . on ( 'error' , err => istream . emit ( 'error' , err ) )
239235 return stream . pipe ( istream )
240236 }
241237
242238 // we have to return a stream that gets ALL the data, and proxies errors,
243239 // but then pipe from the original tarball stream into the cache as well.
244240 // To do this without losing any data, and since the cacache put stream
245241 // is not a passthrough, we have to pipe from the original stream into
246- // the cache AFTER we pipe into the istream . Since the cache stream
242+ // the cache AFTER we pipe into the middleStream . Since the cache stream
247243 // has an asynchronous flush to write its contents to disk, we need to
248- // defer the istream end until the cache stream ends.
249- stream . pipe ( istream , { end : false } )
244+ // defer the middleStream end until the cache stream ends.
245+ const middleStream = new Minipass ( )
246+ stream . on ( 'error' , err => middleStream . emit ( 'error' , err ) )
247+ stream . pipe ( middleStream , { end : false } )
250248 const cstream = cacache . put . stream (
251249 this . opts . cache ,
252250 `pacote:tarball:${ this . from } ` ,
253251 this . opts
254252 )
253+ cstream . on ( 'integrity' , i => this . integrity = i )
254+ cstream . on ( 'error' , err => stream . emit ( 'error' , err ) )
255255 stream . pipe ( cstream )
256- // defer istream end until after cstream
257- // cache write errors should not crash the fetch, this is best-effort.
258- cstream . promise ( ) . catch ( ( ) => { } ) . then ( ( ) => istream . end ( ) )
259256
260- return istream
257+ cstream . promise ( ) . catch ( ( ) => { } ) . then ( ( ) => middleStream . end ( ) )
258+ return middleStream
261259 }
262260
263261 pickIntegrityAlgorithm ( ) {
0 commit comments