workers-rs icon indicating copy to clipboard operation
workers-rs copied to clipboard

[BUG] No way to return pre-compressed data with Content-Encoding from worker-rs due to missing encodeBody option

Open himikof opened this issue 1 year ago • 0 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

What version of workers-rs are you using?

0.2.0

What version of wrangler are you using?

3.53.0

Describe the bug

According to https://stackoverflow.com/a/64849685 and encodeBody documentation, the only way to return already-gzipped data from worker (without decompressing and compressing again) with Content-Encoding: gzip set (for the browser to decompress it automatically) is to set the encodeBody Response init option to "manual":

// Make a new response with the same body but using manual encoding.
resp = new Response(resp.body, {
  status: resp.status,
  headers: resp.headers,
  encodeBody: "manual"
});

// Modify headers and return.
resp.headers.set("Content-Encoding", "gzip");
return resp;

If encodeBody: "manual" is missing, Worker runtime will silently compress the body again, returning gzip-in-gzip data to the browser.

I have failed to find any reasonable way to set this option in worker-rs, because it can only be passed through ResponseInit object, and the current worker code:

  1. has no support for passing anything apart from status, headers and websocket through ResponseInit;
  2. has no support for using custom ResponseInit instances;
  3. constructs the ResponseInit object too late (after the fetch handler has completed processing), so the handler cannot customize it further.

Also, the encodeBody documentation is a bit confusing: it makes it seem like encodeBody is a property on Response, but according to my reading of workerd code, it can be only passed during Response construction through init (ResponseInit.encodeBody property, essentially), and is not exposed on the Response object in any way.

Steps To Reproduce

No response

himikof avatar May 04 '24 02:05 himikof