H
Hono3mo ago
Kaelwd

Trace when body is consumed

I have a middleware that's basically
if (cache.has(key)) {
return cache.get(key).clone()
}
await next()
if (c.res.ok) cache.set(key, c.res.clone())
if (cache.has(key)) {
return cache.get(key).clone()
}
await next()
if (c.res.ok) cache.set(key, c.res.clone())
cache is https://www.npmjs.com/package/node-cache with useClones=false This works great locally but when deployed to production it randomly throws TypeError: Response.clone: Body has already been consumed. on the first clone. Does anyone know if there's an easy way to tell what else is reading the body? This is the only thing using the cache instance so I don't understand how it's even possible. Server and dev both have node 22.8.0
1 Reply
Kaelwd
KaelwdOP3mo ago
really weird, there's 10+ seconds between when the response is created and when it is consumed
const res = c.res.clone()
const timeout = setInterval(() => {
if (res.bodyUsed) {
c.log.debug('body used')
clearInterval(timeout)
}
})
cache.set(key, res)
const res = c.res.clone()
const timeout = setInterval(() => {
if (res.bodyUsed) {
c.log.debug('body used')
clearInterval(timeout)
}
})
cache.set(key, res)
[00:33:31.955] DEBUG: GET /one/banners 304 98ms
[00:33:32.873] DEBUG: GET /one/banners 200 2ms
[00:33:34.166] DEBUG: GET /one/banners 304 2ms
[00:33:37.971] DEBUG: GET /one/banners 304 2ms
[00:33:42.575] DEBUG: body used
[00:33:31.955] DEBUG: GET /one/banners 304 98ms
[00:33:32.873] DEBUG: GET /one/banners 200 2ms
[00:33:34.166] DEBUG: GET /one/banners 304 2ms
[00:33:37.971] DEBUG: GET /one/banners 304 2ms
[00:33:42.575] DEBUG: body used
I can only reproduce it in dev if I use the etag middleware too, and even then it doesn't happen consistently it can't be node-cache cleaning up because cache.get still returns
Want results from more Discord servers?
Add your server