@@ -79,10 +79,10 @@ export async function fetchImage(fetcher: Fetcher | undefined, imageUrl: string,
79
79
80
80
try {
81
81
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" } ) ;
86
86
const { value } = await reader . read ( new Uint8Array ( buffer ) ) ;
87
87
// Release resources by calling `reader.cancel()`
88
88
// `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,
106
106
headers . set ( "content-type" , contentType ) ;
107
107
headers . set ( "content-disposition" , __IMAGES_CONTENT_DISPOSITION__ ) ;
108
108
headers . set ( "content-security-policy" , __IMAGES_CONTENT_SECURITY_POLICY__ ) ;
109
- return new Response ( body1 , { ...imgResponse , headers } ) ;
109
+ return new Response ( respBody , { ...imgResponse , headers } ) ;
110
110
}
111
111
112
+ // Cancel the unused stream
113
+ ctx . waitUntil ( respBody . cancel ( ) ) ;
114
+
112
115
return new Response ( '"url" parameter is valid but image type is not allowed' , {
113
116
status : 400 ,
114
117
} ) ;
0 commit comments