koajs / bodyparser

Koa body parsing middleware
MIT License
1.31k stars 117 forks source link

feat: add check for closed requests #162

Closed zbrydon closed 3 months ago

zbrydon commented 5 months ago

Checklist

Changes

Added a check for req.ctx.closed to ensure that, when a request is closed (canceled) before reaching the bodyparser middleware, the body of the closed request is not attempted to be parsed (which would result in an error). Instead, if the request is closed, then the middleware returns a 499 Client Closed Request status.

Reason

Errors can occur when parsing bodies of closed requests - stream not readable. This indicates the request was cancelled. This can happen when body parser is not on the "synchronous path" of a request - e.g. if Auth is verified asynchronously from headers before bothering to parse the bodies. This allows time for users to occasionally close the request, resulting in a 500 'stream not readable' error.

3imed-jaberi commented 4 months ago

@zbrydon Thanks for your contributions, could you please add test cases!

zbrydon commented 4 months ago

Thanks @3imed-jaberi, I have added a test 😄