Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
fix: sending cookies in regional remix server with polyfills (#709)
Browse files Browse the repository at this point in the history
* fix: regional remix server sending multiple cookies

* fix: react regional server sending multiple cookies

* fix: sending cookies in regional remix server with polyfills
  • Loading branch information
luke-rucker authored Jul 18, 2024
1 parent 7dd9a64 commit 7e07d41
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
17 changes: 16 additions & 1 deletion platform/functions/react-server/regional-server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const createApigHandler = (build) => {
...Object.fromEntries(response.headers.entries()),
"Transfer-Encoding": "chunked",
},
cookies: response.headers.getSetCookie(),
cookies: accumulateCookies(response.headers),
};
if (response.body) {
const reader = response.body;
Expand All @@ -127,6 +127,21 @@ const createApigHandler = (build) => {
});
};

const accumulateCookies = (headers) => {
// node >= 19.7.0 with no remix fetch polyfill
if (typeof headers.getSetCookie === "function") {
return headers.getSetCookie();
}
// node < 19.7.0 or with remix fetch polyfill
const cookies = [];
for (let [key, value] of headers.entries()) {
if (key === "set-cookie") {
cookies.push(value);
}
}
return cookies;
};

const streamToNodeStream = async (reader, writer) => {
let readResult = await reader.read();
while (!readResult.done) {
Expand Down
17 changes: 16 additions & 1 deletion platform/functions/remix-server/regional-server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const createApigHandler = (build) => {
...Object.fromEntries(response.headers.entries()),
"Transfer-Encoding": "chunked",
},
cookies: response.headers.getSetCookie(),
cookies: accumulateCookies(response.headers),
};

const writer = awslambda.HttpResponseStream.from(
Expand All @@ -130,6 +130,21 @@ const createApigHandler = (build) => {
});
};

const accumulateCookies = (headers) => {
// node >= 19.7.0 with no remix fetch polyfill
if (typeof headers.getSetCookie === "function") {
return headers.getSetCookie();
}
// node < 19.7.0 or with remix fetch polyfill
const cookies = [];
for (let [key, value] of headers.entries()) {
if (key === "set-cookie") {
cookies.push(value);
}
}
return cookies;
};

const streamToNodeStream = async (reader, writer) => {
let readResult = await reader.read();
while (!readResult.done) {
Expand Down

0 comments on commit 7e07d41

Please sign in to comment.