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.
Plugin Code: https://github.com/TanStack/router/blob/854a7ef23dcd7b986af32e4efbb91861af58b9ca/packages/start-plugin-core/src/dev-server-plugin/plugin.ts#L3
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. 👀
Environment
Node Version: v20.19.2
Package Version: srvx@0.9.5
Describe the bug
Node adapter's
sendNodeResponselosesset-cookieheader when set multiple cookie at onceReproduction
See: https://stackblitz.com/edit/srvx-multi-header-demo?file=run.mjs
When we set header before
sendNodeResponse, only oneset-cookieheader 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-Cookieheader isn't working in the local development server.TanStack Start builds a vite dev server plugin that uses
NodeRequestandsendNodeResponseto convert Node.js-style requests and responses into the fetch handler pattern.Plugin Code: https://github.com/TanStack/router/blob/854a7ef23dcd7b986af32e4efbb91861af58b9ca/packages/start-plugin-core/src/dev-server-plugin/plugin.ts#L3
Conclusion & Proposed fix
The root cause is in the Node.js HTTP Server's
writeHeadfunction (node/lib/_http_server.js). When we setHeader first,writeHeadwill 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. 👀