Skip to content

Commit e0f39b6

Browse files
authored
Cancel the unused stream (body) (#785)
1 parent d736b10 commit e0f39b6

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

.changeset/crazy-birds-jam.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
Cancel the unused stream (body)

packages/cloudflare/src/cli/templates/images.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ export async function fetchImage(fetcher: Fetcher | undefined, imageUrl: string,
7979

8080
try {
8181
let contentType: string | undefined;
82-
// body1 is eventually used for the response
83-
// body2 is used to detect the content type
84-
const [body1, body2] = imgResponse.body.tee();
85-
const reader = body2.getReader({ mode: "byob" });
82+
// respBody is eventually used for the response
83+
// contentBody is used to detect the content type
84+
const [respBody, contentBody] = imgResponse.body.tee();
85+
const reader = contentBody.getReader({ mode: "byob" });
8686
const { value } = await reader.read(new Uint8Array(buffer));
8787
// Release resources by calling `reader.cancel()`
8888
// `ctx.waitUntil` keeps the runtime running until the promise settles without having to wait here.
@@ -106,9 +106,12 @@ export async function fetchImage(fetcher: Fetcher | undefined, imageUrl: string,
106106
headers.set("content-type", contentType);
107107
headers.set("content-disposition", __IMAGES_CONTENT_DISPOSITION__);
108108
headers.set("content-security-policy", __IMAGES_CONTENT_SECURITY_POLICY__);
109-
return new Response(body1, { ...imgResponse, headers });
109+
return new Response(respBody, { ...imgResponse, headers });
110110
}
111111

112+
// Cancel the unused stream
113+
ctx.waitUntil(respBody.cancel());
114+
112115
return new Response('"url" parameter is valid but image type is not allowed', {
113116
status: 400,
114117
});

0 commit comments

Comments
 (0)