deno_std
deno_std copied to clipboard
`serve` swallows errors when `Response` object body has been consumed already
Describe the bug
Steps to Reproduce
await serve(async (req: Request) => {
const res = await handler(req);
// Consumes the body! Deno won't be able to read it afterwards!
console.log("%c <= ", "color: magenta", res.status, await res.text());
return res;
});
Expected behavior
Should see some error output that the Request object didn't have any body content available to reply with
Environment
- OS: [e.g. Ubuntu 20.04, MacOS 11] macOS 12
- deno version: 1.22.0
- std version: 0.140.0
Hi @aslilac, just clarifying. Are you saying that handler somehow consumes req.body but calling req.text() thereafter doesn’t throw?
Sorry @aslilac, I didn't read the title. @kt3k, this should be an easy fix - something like:
if (res.body instanceof ReadableStream && res.bodyUsed) {
throw new Error("Response body has already been consumed.");
}
Are there any cases where this check is not desirable? If not, I'll submit the PR.
sounds worth trying to me.