Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly add stream to cache on cache add #2976

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const pacote = require('pacote')
const path = require('path')
const rimraf = promisify(require('rimraf'))
const BaseCommand = require('./base-command.js')
const MinipassPipeline = require('minipass-pipeline')
const npa = require('npm-package-arg')

class Cache extends BaseCommand {
static get description () {
Expand Down Expand Up @@ -105,12 +107,19 @@ with --force.`)

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

// we ask pacote for the thing, and then just throw the data
// away so that it tee-pipes it into the cache like it does
// for a normal request.
await pacote.tarball.stream(spec, stream => {
stream.resume()
return stream.promise()
const nspec = npa(spec)
if (nspec.type === 'remote' || nspec.registry) {
stream.resume()
return stream.promise()
}
return new MinipassPipeline(
stream,
cacache.put.stream(
path.join(this.npm.config.get('cache'), '_cacache'),
`npm-cache-add:${spec}`
)
).promise()
}, this.npm.flatOptions)
}

Expand Down
17 changes: 13 additions & 4 deletions test/lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@ const npmlog = {
},
}

const pipeline = {
pipe: () => pipeline,
promise: () => Promise.resolve(),
resume: () => {},
}

let tarballStreamSpec = ''
let tarballStreamOpts = {}
const pacote = {
tarball: {
stream: (spec, handler, opts) => {
tarballStreamSpec = spec
tarballStreamOpts = opts
return handler({
resume: () => {},
promise: () => Promise.resolve(),
})
return handler(pipeline)
},
},
}
Expand All @@ -41,10 +44,16 @@ const cacacheVerifyStats = {
totalEntries: 1,
runTime: { total: 2000 },
}

const cacache = {
verify: (path) => {
return cacacheVerifyStats
},
put: {
stream: (cache, stream) => {
return pipeline
},
},
}

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