-
Notifications
You must be signed in to change notification settings - Fork 35
Description
Environment
Node Version: v20.19.2
Package Version: srvx@0.9.5
Describe the bug
Node adapter's sendNodeResponse loses set-cookie header when set multiple cookie at once
Reproduction
See: https://stackblitz.com/edit/srvx-multi-header-demo?file=run.mjs
When we set header before sendNodeResponse, only one set-cookie header remains at response. If we remove the setHeader, the result is as expected.
Background
I'm building a full-stack web app with TanStack Start and setting up a JWT authentication middleware that uses both access token and refresh token with cookie. However, I encountered an issue where the Set-Cookie header isn't working in the local development server.
TanStack Start builds a vite dev server plugin that uses NodeRequest and sendNodeResponse to convert Node.js-style requests and responses into the fetch handler pattern.
Conclusion & Proposed fix
The root cause is in the Node.js HTTP Server's writeHead function (node/lib/_http_server.js). When we setHeader first, writeHead will override previous headers with same keys and only the last one preserved.
The proposed fix is merge the duplicated key header into array first, then call the writeHead with the merged header array value.
I just implement this on my fork, and create some unit tests.
Happy to submit a PR if above approach sounds good. 👀