@@ -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