Skip to content

Commit

Permalink
fix: log tarball retrieval from cache (#403)
Browse files Browse the repository at this point in the history
If tarballs are cached, the npm install log contains no information on
where it got the tarball from. This helps troubleshoot issues with
caching and `preferOnline/preferOffline`, for example.

---------

Co-authored-by: Gar <wraithgar@github.com>
  • Loading branch information
mbtools and wraithgar authored Oct 15, 2024
1 parent a9fc4d1 commit 2b2948f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,15 @@ class FetcherBase {
// private
// Note: cacache will raise a EINTEGRITY error if the integrity doesn't match
#tarballFromCache () {
return cacache.get.stream.byDigest(this.cache, this.integrity, this.opts)
const startTime = Date.now()
const stream = cacache.get.stream.byDigest(this.cache, this.integrity, this.opts)
const elapsedTime = Date.now() - startTime
// cache is good, so log it as a hit in particular since there was no fetch logged
log.http(
'cache',
`${this.spec} ${elapsedTime}ms (cache hit)`
)
return stream
}

get [_.cacheFetches] () {
Expand Down
10 changes: 9 additions & 1 deletion test/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ t.test('extract', t => {
}).extract(target + '/badcache')
.then(({ resolved, integrity }) => {
t.match(logs, [
['http',
'cache',
/file:test\/fixtures\/abbrev-1.1.1.tgz.*(cache hit)/,
],
['warn', 'tar', 'zlib: incorrect header check'],
[
'silly',
Expand Down Expand Up @@ -298,7 +302,11 @@ t.test('extract', t => {
algorithm: 'sha512',
sri: Object,
}, 'got expected error')
t.same(logs, [
t.match(logs, [
['http',
'cache',
/file:test\/fixtures\/abbrev-1.1.1.tgz.*(cache hit)/,
],
[
'silly',
'tarball',
Expand Down

0 comments on commit 2b2948f

Please sign in to comment.