Skip to content

Commit 8044781

Browse files
authored
fix: log cache hits distinct from fetch (#273)
Avoid confusion between actual remote fetches and cache hits (which don't involve a fetch). Ref npm/pacote#403 (comment)
1 parent c046cca commit 8044781

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

lib/check-response.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,18 @@ function logRequest (method, res, startTime) {
4848
const cacheStr = cacheStatus ? ` (cache ${cacheStatus})` : ''
4949
const urlStr = cleanUrl(res.url)
5050

51-
log.http(
52-
'fetch',
53-
`${method.toUpperCase()} ${res.status} ${urlStr} ${elapsedTime}ms${attemptStr}${cacheStr}`
54-
)
51+
// If make-fetch-happen reports a cache hit, then there was no fetch
52+
if (cacheStatus === 'hit') {
53+
log.http(
54+
'cache',
55+
`${urlStr} ${elapsedTime}ms${attemptStr}${cacheStr}`
56+
)
57+
} else {
58+
log.http(
59+
'fetch',
60+
`${method.toUpperCase()} ${res.status} ${urlStr} ${elapsedTime}ms${attemptStr}${cacheStr}`
61+
)
62+
}
5563
}
5664

5765
function checkErrors (method, res, startTime, opts) {

test/check-response.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,9 @@ t.test('logs the value of x-local-cache-status when set', t => {
224224
startTime,
225225
})
226226
res.body.emit('end')
227-
t.equal(header, 'fetch')
227+
t.equal(header, 'cache')
228228
t.match(
229229
msg,
230-
/^GET 200 http:\/\/username:\*\*\*@example.com\/foo\/bar\/baz [0-9]+m?s \(cache hit\)$/
230+
/^http:\/\/username:\*\*\*@example.com\/foo\/bar\/baz [0-9]+m?s \(cache hit\)$/
231231
)
232232
})

0 commit comments

Comments
 (0)