Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit c705758

Browse files
authored
[Miniflare 2] Fix body.cancel() throwing undefined (#757)
1 parent 04134bb commit c705758

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

packages/core/src/standards/http.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,10 @@ export class Body<Inner extends BaseRequest | BaseResponse> {
265265
controller.byobRequest?.respond(0);
266266
}
267267
},
268-
cancel: (reason) => reader.cancel(reason),
268+
cancel: (reason) => {
269+
if (reader === undefined) reader = body.getReader();
270+
return reader.cancel(reason);
271+
},
269272
};
270273
// TODO: maybe set { highWaterMark: 0 } as a strategy here?
271274
bodyStream = new ReadableStream(source);

packages/core/test/standards/http.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,26 @@ test("Body: can pause, resume and cancel body stream", async (t) => {
179179
t.true(result.done);
180180
t.is(result.value, undefined);
181181
});
182+
test("Body: can cancel body directly", async (t) => {
183+
const chunks = ["123", "456", "789"];
184+
const bodyStream = new ReadableStream({
185+
pull(controller) {
186+
const chunk = chunks.shift();
187+
if (chunk) {
188+
controller.enqueue(utf8Encode(chunk));
189+
} else {
190+
controller.close();
191+
}
192+
},
193+
});
194+
const { body } = new Response(bodyStream);
195+
assert(body);
196+
197+
await body.cancel();
198+
const result = await body.getReader().read();
199+
t.true(result.done);
200+
t.is(result.value, undefined);
201+
});
182202
test("Body: throws on string chunks", async (t) => {
183203
const inputStream = new ReadableStream({
184204
start(controller) {

0 commit comments

Comments
 (0)