Skip to content

Commit 0e487d2

Browse files
committed
Explicitly add stream to cache
Pacote doesn't do this automatically anymore Closes npm/pacote#73 & Closes #2160
1 parent ffead4a commit 0e487d2

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

lib/cache.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const pacote = require('pacote')
55
const path = require('path')
66
const rimraf = promisify(require('rimraf'))
77
const BaseCommand = require('./base-command.js')
8+
const MinipassPipeline = require('minipass-pipeline')
9+
const npa = require('npm-package-arg')
810

911
class Cache extends BaseCommand {
1012
static get description () {
@@ -105,12 +107,19 @@ with --force.`)
105107

106108
log.silly('cache add', 'spec', spec)
107109

108-
// we ask pacote for the thing, and then just throw the data
109-
// away so that it tee-pipes it into the cache like it does
110-
// for a normal request.
111110
await pacote.tarball.stream(spec, stream => {
112-
stream.resume()
113-
return stream.promise()
111+
const nspec = npa(spec)
112+
if (nspec.type === 'remote' || nspec.registry) {
113+
stream.resume()
114+
return stream.promise()
115+
}
116+
return new MinipassPipeline(
117+
stream,
118+
cacache.put.stream(
119+
path.join(this.npm.config.get('cache'), '_cacache'),
120+
`npm-cache-add:${spec}`
121+
)
122+
).promise()
114123
}, this.npm.flatOptions)
115124
}
116125

test/lib/cache.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,20 @@ const npmlog = {
2020
},
2121
}
2222

23+
const pipeline = {
24+
pipe: () => pipeline,
25+
promise: () => Promise.resolve(),
26+
resume: () => {},
27+
}
28+
2329
let tarballStreamSpec = ''
2430
let tarballStreamOpts = {}
2531
const pacote = {
2632
tarball: {
2733
stream: (spec, handler, opts) => {
2834
tarballStreamSpec = spec
2935
tarballStreamOpts = opts
30-
return handler({
31-
resume: () => {},
32-
promise: () => Promise.resolve(),
33-
})
36+
return handler(pipeline)
3437
},
3538
},
3639
}
@@ -41,10 +44,16 @@ const cacacheVerifyStats = {
4144
totalEntries: 1,
4245
runTime: { total: 2000 },
4346
}
47+
4448
const cacache = {
4549
verify: (path) => {
4650
return cacacheVerifyStats
4751
},
52+
put: {
53+
stream: (cache, stream) => {
54+
return pipeline
55+
},
56+
},
4857
}
4958

5059
const Cache = requireInject('../../lib/cache.js', {

0 commit comments

Comments
 (0)