Skip to content

Commit

Permalink
fix(cache): add both full and minified packument to cache (npm#7516)
Browse files Browse the repository at this point in the history
This will fix the `npm cache add` to cache package with same header as
it's used in `npm install` by adding extra manifest call with
`fullMetadata: true` while caching to match behaviour with install
command which internally request manifest with fullMetadata.

## References
Closes npm#7465
  • Loading branch information
milaninfy committed May 22, 2024
1 parent 9e6686b commit 9122fb6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
9 changes: 7 additions & 2 deletions lib/commands/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,20 @@ class Cache extends BaseCommand {
throw this.usageError('First argument to `add` is required')
}

return Promise.all(args.map(spec => {
await Promise.all(args.map(async spec => {
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.
return pacote.tarball.stream(spec, stream => {
await pacote.tarball.stream(spec, stream => {
stream.resume()
return stream.promise()
}, { ...this.npm.flatOptions })

await pacote.manifest(spec, {
...this.npm.flatOptions,
fullMetadata: true,
})
}))
}

Expand Down
14 changes: 11 additions & 3 deletions test/lib/commands/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ t.test('cache add single pkg', async t => {
registry: npm.config.get('registry'),
})
const manifest = registry.manifest({ name: pkg })
await registry.package({ manifest, tarballs: { '1.0.0': path.join(npm.prefix, 'package') } })
await registry.package({
manifest,
times: 2,
tarballs: { '1.0.0': path.join(npm.prefix, 'package') },
})
await npm.exec('cache', ['add', pkg])
t.equal(joinedOutput(), '')
// eslint-disable-next-line max-len
Expand Down Expand Up @@ -99,9 +103,13 @@ t.test('cache add multiple pkgs', async t => {
})
const manifest = registry.manifest({ name: pkg })
const manifest2 = registry.manifest({ name: pkg2 })
await registry.package({ manifest, tarballs: { '1.0.0': path.join(npm.prefix, 'package') } })
await registry.package({
manifest: manifest2, tarballs: { '1.0.0': path.join(npm.prefix, 'package') },
manifest,
times: 2,
tarballs: { '1.0.0': path.join(npm.prefix, 'package') },
})
await registry.package({
manifest: manifest2, times: 2, tarballs: { '1.0.0': path.join(npm.prefix, 'package') },
})
await npm.exec('cache', ['add', pkg, pkg2])
t.equal(joinedOutput(), '')
Expand Down

0 comments on commit 9122fb6

Please sign in to comment.