Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the size of the response? #13

Open
lazydefi1 opened this issue Oct 19, 2022 · 2 comments
Open

How to get the size of the response? #13

lazydefi1 opened this issue Oct 19, 2022 · 2 comments
Labels
question Further information is requested

Comments

@lazydefi1
Copy link

In a middleware, is there a way to get the size of the response ctx.res sent to the client?

@mattreid1 mattreid1 added the question Further information is requested label Oct 19, 2022
@mattreid1
Copy link
Owner

There isn't a way natively to do it that I know of. Bao uses the Bun Response type so you would have to use that to calculate it. Off the top of my head, I'd probably enumerate the headers to calculate their length, add the response length (e.g. array buffer, text, etc.), and then add the status line length. I might be missing something else but that's the gist of it. If that doesn't work, you could try using a reverse proxy to read its exact length?

@lazydefi1
Copy link
Author

lazydefi1 commented Oct 25, 2022

I see what you mean but because the server is computing the Content-length header somewhere, I thought it might be possible to get this header as well.

Here is my attempt:

app.after((ctx: Context) => {
  console.log('➡️', ctx.req.method, ctx.res.status, ctx.req.url, ctx.res.headers.get('Content-length'), 'bytes');
  return ctx;
});

However, here is the result:

➡️ GET 200 http://0.0.0.0:3000/ null bytes

It is strange because the status is accessible but not the computed header...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants