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

#7226 - fixes NodeJS adapter for multiple set-cookie headers (and other header issues) #7227

Merged
merged 5 commits into from
Jun 6, 2023

Conversation

alex-sherwin
Copy link
Contributor

@alex-sherwin alex-sherwin commented May 28, 2023

Fixes #7226

What it does

Utilizes the new standard WebAPI Fetch Headers.getSetCookie() function to safely handle multiple set-cookie headers when converting from a WebAPI Response to a NodeJS ServerResponse

Modifies the existing nodeMiddleware logic which used to first set AstroCookies on ServerResponse.setHeader(...) and then called ServerResponse.writeHead(status, Response.headers), which meant any that if the WebAPI Response had any set-cookie headers on it, they would replace anything from AstroCookies (they were not merged, so any AstroCookies would get lost during response writing, and, multiple set-cookie headers from the Response.headers were written incorrectly as a CSV).

The new logic delegates appending AstroCookies values onto the WebAPI Response Headers object, so that a new single unified function safely converts from the WebAPI Response Headers into a NodeJS compatible OutgoingHttpHeaders object utilizing the new standard Headers.getSetCookie() function provided by the existing undici WebAPI polyfills.

Plus extensive test coverage.

Notes

The Fetch WebAPI standard has been updated with a function Headers.getSetCookie() (see https://developer.mozilla.org/en-US/docs/Web/API/Headers/getSetCookie)

The undici library that @astrojs/webapi utilizes for WebAPI polyfills on NodeJS added support for Headers.getSetCookie() in 5.19.0 (see https://github.com/nodejs/undici/releases/tag/v5.19.0)

Since Astro is already using undici@^5.22.0, this means Astro code already had access to this method without having to change any dependencies.

I think that this is probably the best most standards compliant way to fix this problem, and other [unrelated] existing pieces of code that are using copy/pasted set-cookie heading parsing logic (such as https://github.com/withastro/astro/blob/main/packages/integrations/netlify/src/netlify-functions.ts#LL140C26-L140C26) might want to consider changing to using Headers.getSetCookie() where appropriate since going forward it's a standards compliant way of dealing with the set-cookie special case in the WebAPI Headers type.

Alex Sherwin added 2 commits May 27, 2023 22:01
to safely handle multiple set-cookie headers when converting from a
WebAPI Response to a NodeJS ServerResponse

Modifies the existing nodeMiddleware logic which first set AstroCookies
on ServerResponse.setHeader(...) and then called
ServerResponse.writeHead(status, Response.headers) which means any that
if the WebAPI Response had any set-cookie headers on it, they would
replace anything from AstroCookies.

The new logic delegates appending AstroCookie values onto the WebAPI
Response Headers object, so that a single unified function safely
converts the WebAPI Response Headers into a NodeJS compatible
OutgoingHttpHeaders object utilizing the new standard
Headers.getSetCookie() function provided by the undici WebAPI polyfills.

Plus extensive test coverage.
@changeset-bot
Copy link

changeset-bot bot commented May 28, 2023

🦋 Changeset detected

Latest commit: b8f9026

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

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

@github-actions github-actions bot added the pkg: integration Related to any renderer integration (scope) label May 28, 2023
Copy link
Member

@natemoo-re natemoo-re left a comment

Choose a reason for hiding this comment

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

This PR is incredibly thorough with great tests, thanks for contributing!

@natemoo-re natemoo-re merged commit 4929332 into withastro:main Jun 6, 2023
@astrobot-houston astrobot-houston mentioned this pull request Jun 6, 2023
@alex-sherwin
Copy link
Contributor Author

alex-sherwin commented Jun 6, 2023

Thanks for merging!

I found the astro e2e/integration test harness stuff is really nicely done and was very easy to utilize for real-world integration/adapter test coverage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: integration Related to any renderer integration (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Node Adapter writes server.writeHead(..) headers incorrectly
2 participants