You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm currently tackling a project that requires fetching the size of a Response associated with a request within middleware.
Here's what I've attempted so far:
import{Hono}from'hono'constapp=newHono()// Middleware to log the response sizeconstresponseSizeLogger=async(c,next)=>{// Wrap the original `text` methodconstoriginalText=c.text.bind(c)c.text=(body,status=200)=>{constbodySize=Buffer.byteLength(body,'utf8')console.log(`Response size: ${bodySize} bytes`)returnoriginalText(body,status)}// Continue with the next middlewareawaitnext()}app.use('*',responseSizeLogger)// Example routeapp.get('/',(c)=>c.text('Hello World!'))exportdefaultapp
This implementation works fine, but it has a complexity issue, and I'm unsure if interacting with .text() in middleware is acceptable.
When attempting console.log(c.res) in the middleware, it yields:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm currently tackling a project that requires fetching the size of a Response associated with a request within middleware.
Here's what I've attempted so far:
This implementation works fine, but it has a complexity issue, and I'm unsure if interacting with .text() in middleware is acceptable.
When attempting
console.log(c.res)
in the middleware, it yields:However, any attempts to access the blob size (or length) have been unsuccessful, resulting in either wiping the response body or returning undefined.
Could you please assist me with this issue :(
Beta Was this translation helpful? Give feedback.
All reactions