From 927f4e2e83719aac3dcc4d9ae422cbbf76bd7bcd Mon Sep 17 00:00:00 2001 From: Marcos Casagrande Date: Thu, 29 Sep 2022 17:38:04 +0200 Subject: [PATCH] fix(ext/fetch): `Body#bodyUsed` for static body (#16080) This fixes a bug where `Body#bodyUsed` incorrectly returns `false` for a body that has actually already been consumed, after `Body#body` is called. --- cli/tests/unit/response_test.ts | 10 ++++++++++ ext/fetch/22_body.js | 4 ++++ ext/web/06_streams.js | 10 ++++++++++ 3 files changed, 24 insertions(+) diff --git a/cli/tests/unit/response_test.ts b/cli/tests/unit/response_test.ts index c46218b627..c2a2301383 100644 --- a/cli/tests/unit/response_test.ts +++ b/cli/tests/unit/response_test.ts @@ -90,3 +90,13 @@ Deno.test(function customInspectFunction() { ); assertStringIncludes(Deno.inspect(Response.prototype), "Response"); }); + +Deno.test(async function responseBodyUsed() { + const response = new Response("body"); + assert(!response.bodyUsed); + await response.text(); + assert(response.bodyUsed); + // .body getter is needed so we can test the faulty code path + response.body; + assert(response.bodyUsed); +}); diff --git a/ext/fetch/22_body.js b/ext/fetch/22_body.js index 97a8a8db15..6e9a574478 100644 --- a/ext/fetch/22_body.js +++ b/ext/fetch/22_body.js @@ -28,6 +28,8 @@ const { isReadableStreamDisturbed, errorReadableStream, + readableStreamClose, + readableStreamDisturb, createProxy, ReadableStreamPrototype, } = globalThis.__bootstrap.streams; @@ -92,6 +94,8 @@ if (consumed) { this.streamOrStatic = new ReadableStream(); this.streamOrStatic.getReader(); + readableStreamDisturb(this.streamOrStatic); + readableStreamClose(this.streamOrStatic); } else { this.streamOrStatic = new ReadableStream({ start(controller) { diff --git a/ext/web/06_streams.js b/ext/web/06_streams.js index bd1714964a..bd3b79149b 100644 --- a/ext/web/06_streams.js +++ b/ext/web/06_streams.js @@ -1153,6 +1153,15 @@ reader[_closedPromise].resolve(undefined); } + /** + * @template R + * @param {ReadableStream} stream + * @returns {void} + */ + function readableStreamDisturb(stream) { + stream[_disturbed] = true; + } + /** @param {ReadableStreamDefaultController} controller */ function readableStreamDefaultControllerCallPullIfNeeded(controller) { const shouldPull = readableStreamDefaultcontrollerShouldCallPull( @@ -5910,6 +5919,7 @@ createProxy, writableStreamClose, readableStreamClose, + readableStreamDisturb, readableStreamForRid, getReadableStreamRid, Deferred,