Skip to content

Commit

Permalink
[Miniflare 2] Fix body.cancel() throwing undefined (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
jahands committed Jan 29, 2024
1 parent 04134bb commit c705758
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/core/src/standards/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,10 @@ export class Body<Inner extends BaseRequest | BaseResponse> {
controller.byobRequest?.respond(0);
}
},
cancel: (reason) => reader.cancel(reason),
cancel: (reason) => {
if (reader === undefined) reader = body.getReader();
return reader.cancel(reason);
},
};
// TODO: maybe set { highWaterMark: 0 } as a strategy here?
bodyStream = new ReadableStream(source);
Expand Down
20 changes: 20 additions & 0 deletions packages/core/test/standards/http.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,26 @@ test("Body: can pause, resume and cancel body stream", async (t) => {
t.true(result.done);
t.is(result.value, undefined);
});
test("Body: can cancel body directly", async (t) => {
const chunks = ["123", "456", "789"];
const bodyStream = new ReadableStream({
pull(controller) {
const chunk = chunks.shift();
if (chunk) {
controller.enqueue(utf8Encode(chunk));
} else {
controller.close();
}
},
});
const { body } = new Response(bodyStream);
assert(body);

await body.cancel();
const result = await body.getReader().read();
t.true(result.done);
t.is(result.value, undefined);
});
test("Body: throws on string chunks", async (t) => {
const inputStream = new ReadableStream({
start(controller) {
Expand Down

0 comments on commit c705758

Please sign in to comment.