From 4eda9e64e9fd6075b74742ae877281ee4a7ab155 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Thu, 1 Aug 2024 15:46:05 +0200 Subject: 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. --- ext/http/00_serve.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'ext/http/00_serve.ts') 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); -- cgit v1.2.3