Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Miniflare 3] Allow custom services to respond with Content-Encoding and multiple Set-Cookie headers #613

Merged
merged 2 commits into from
Jun 27, 2023

Conversation

mrbbot
Copy link
Contributor

@mrbbot mrbbot commented Jun 27, 2023

Previously, if the response from a custom service included a Content-Encoding header, we would respond with decoded content to workerd without removing the Content-Encoding header. This caused workerd to try to decode already decoded content, resulting in an error. This was particularly a problem with wrangler pages dev, as upstream dev servers could very easily return gzipped content.

This change updates the writeResponse() function with code from Miniflare 2, to re-encode the body before responding.

Closes cloudflare/workers-sdk#3408


Previously, if a custom service returned a response with multiple Set-Cookie headers, they would be combined into a single header.

This change uses the set-cookie-parser package to split the header, ensuring Headers#getSetCookie()/ Headers#getAll("Set-Cookie") return all cookies in a Worker.

Previously, if the response from a custom service included a
`Content-Encoding` header, we would respond with decoded content to
`workerd` without removing the `Content-Encoding` header. This caused
`workerd` to try to decode already decoded content, resulting in an
error. This was particularly a problem with `wrangler pages dev`, as
upstream dev servers could very easily return gzipped content.

This change updates the `writeResponse()` function with code from
Miniflare 2, to re-encode the body before responding.

Note, due to a fixed but unreleased bug in `undici`
(nodejs/undici#2159), defining a custom service that directly proxies
somewhere else with `fetch()` like `wrangler pages dev` will fail, if
`Content-Encoding` contains multiple encodings.

Closes cloudflare/workers-sdk#3408
Previously, if a custom service returned a response with multiple
`Set-Cookie` headers, they would be combined into a single header.

This change uses the `set-cookie-parser` package to split the
header, ensuring `Headers#getSetCookie()`/
`Headers#getAll("Set-Cookie")` return all cookies in a Worker.
@mrbbot mrbbot requested a review from a team June 27, 2023 11:11
@changeset-bot
Copy link

changeset-bot bot commented Jun 27, 2023

⚠️ No Changeset found

Latest commit: f5e4b14

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

script: `export default {
async fetch(request, env, ctx) {
const res = await env.CUSTOM.fetch(request);
return Response.json(res.headers.getSetCookie());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL getSetCookie 🤯

@petebacondarwin
Copy link

Why was Ubuntu latest cancelled?

@mrbbot
Copy link
Contributor Author

mrbbot commented Jun 27, 2023

@petebacondarwin the Ubuntu 16.13.0 tests have been particularly flakey, and sometimes the test runner times out without actually terminating the job. We're yet to figure out what's causing this. Rather than burning actions minutes, the job was cancelled and retried.

@mrbbot mrbbot merged commit 715a86f into tre Jun 27, 2023
8 checks passed
@mrbbot mrbbot deleted the bcoll/tre-custom-service-encoding-cookies branch June 27, 2023 16:08
Copy link

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this is merged. I was wondering how the test for the _transformsForContentEncoding() works. Are you just using that to setup the encodings but then checking that the actual writeResponse() code does the correct thing? In other words, does this test actually check the writeResponse() code?

packages/miniflare/test/index.spec.ts Show resolved Hide resolved
@mrbbot
Copy link
Contributor Author

mrbbot commented Jun 28, 2023

Here's a little diagram:
IMG_0632
writeResponse() is called when the loopback server which hosts the CUSTOM service responds to workerd. We could probably omit the upstream server started by useServer(), and just return an encoded response from CUSTOM, but this test is meant to model the situation with wrangler pages dev.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants