summaryrefslogtreecommitdiff
path: root/ext/http/00_serve.ts
diff options
context:
space:
mode:
authorLuca Casonato <hello@lcas.dev>2024-08-01 15:46:05 +0200
committerGitHub <noreply@github.com>2024-08-01 15:46:05 +0200
commit4eda9e64e9fd6075b74742ae877281ee4a7ab155 (patch)
tree93ad39f30c6f4f0cecb842f0dd214b1347ef548d /ext/http/00_serve.ts
parentc79cb339ef74e671c39f4bb13879b281eadd9aaa (diff)
fix(ext/http): correctly consume response body in `Deno.serve` (#24811)
Prior to this commit, you could return a `Response` created from a string or Uint8Array multiple times. Now you can't do that anymore.
Diffstat (limited to 'ext/http/00_serve.ts')
-rw-r--r--ext/http/00_serve.ts11
1 files changed, 11 insertions, 0 deletions
diff --git a/ext/http/00_serve.ts b/ext/http/00_serve.ts
index a58d19d76..8ed1a1d04 100644
--- a/ext/http/00_serve.ts
+++ b/ext/http/00_serve.ts
@@ -436,6 +436,11 @@ function fastSyncResponseOrStream(
const stream = respBody.streamOrStatic;
const body = stream.body;
+ if (body !== undefined) {
+ // We ensure the response has not been consumed yet in the caller of this
+ // function.
+ stream.consumed = true;
+ }
if (TypedArrayPrototypeGetSymbolToStringTag(body) === "Uint8Array") {
innerRequest?.close();
@@ -505,6 +510,12 @@ function mapToCallback(context, callback, onError) {
"Return value from serve handler must be a response or a promise resolving to a response",
);
}
+
+ if (response.bodyUsed) {
+ throw TypeError(
+ "The body of the Response returned from the serve handler has already been consumed.",
+ );
+ }
} catch (error) {
try {
response = await onError(error);