cloudflare / workers-sdk

⛅️ Home to Wrangler, the CLI for Cloudflare Workers®
https://developers.cloudflare.com/workers/
Apache License 2.0
2.71k stars 712 forks source link

🐛 BUG: Your worker called response.clone(), but did not read the body of both clones #3259

Open GeorgeTailor opened 1 year ago

GeorgeTailor commented 1 year ago

Which Cloudflare product(s) does this pertain to?

Wrangler

What version of Wrangler are you using?

3.0.0

What operating system are you using?

Windows

Describe the Bug

After upgrading to 3.0.0 new message started appearing in the console:

Your worker called response.clone(), but did not read the body of both clones. This is wasteful, as it forces the system to buffer the entire response body in memory, rather than streaming it through. This may cause your worker to be unexpectedly terminated for going over the memory limit. If you only meant to copy the response headers and metadata (e.g. in order to be able to modify them), use `new Response(response.body, response)` instead.  

However, it does not say which exactly part of code is to blame. I scanned my code and I do not call response.clone() directly anywhere, also, I have multiple levels of functions, making it hard to pin-point where the issue is and whether the issue is there at all.

russelldavis commented 9 months ago

Here's a simple fix: https://gist.github.com/russelldavis/6b1a1bf15898655ec18037bd737d075d

If you have middleware that reads the body of the request, you'll need to update it to call request.clone().

It'd be great if cloudflare would fix this (it's a breaking change, but that's what major versions are for), but in the meanwhile, you can fix this in your own project by applying the patch above using patch-package or pnpm patch.