Skip to content

Commit

Permalink
fix(ext/fetch): Body#bodyUsed for static body (denoland#16080)
Browse files Browse the repository at this point in the history
This fixes a bug where `Body#bodyUsed` incorrectly returns `false`
for a body that has actually already been consumed, after `Body#body`
is called.
  • Loading branch information
marcosc90 authored Sep 29, 2022
1 parent 15ea624 commit 927f4e2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cli/tests/unit/response_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
4 changes: 4 additions & 0 deletions ext/fetch/22_body.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
const {
isReadableStreamDisturbed,
errorReadableStream,
readableStreamClose,
readableStreamDisturb,
createProxy,
ReadableStreamPrototype,
} = globalThis.__bootstrap.streams;
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions ext/web/06_streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,15 @@
reader[_closedPromise].resolve(undefined);
}

/**
* @template R
* @param {ReadableStream<R>} stream
* @returns {void}
*/
function readableStreamDisturb(stream) {
stream[_disturbed] = true;
}

/** @param {ReadableStreamDefaultController<any>} controller */
function readableStreamDefaultControllerCallPullIfNeeded(controller) {
const shouldPull = readableStreamDefaultcontrollerShouldCallPull(
Expand Down Expand Up @@ -5910,6 +5919,7 @@
createProxy,
writableStreamClose,
readableStreamClose,
readableStreamDisturb,
readableStreamForRid,
getReadableStreamRid,
Deferred,
Expand Down

0 comments on commit 927f4e2

Please sign in to comment.