Skip to content

Commit

Permalink
fix(proxy): split cookie headers properly with native node fetch (#408)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jun 20, 2023
1 parent bb59c69 commit eeaf03b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ on:
jobs:
ci:
runs-on: ubuntu-latest
strategy:
matrix:
node: [16, 18, 20]
steps:
- uses: actions/checkout@v3
- run: corepack enable
- uses: actions/setup-node@v3
with:
node-version: 16
node-version: ${{ matrix.node }}
cache: "pnpm"
- run: pnpm install
- run: pnpm lint
Expand Down
21 changes: 14 additions & 7 deletions src/utils/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export async function sendProxy(
);
event.node.res.statusMessage = sanitizeStatusMessage(response.statusText);

const cookies: string[] = [];

for (const [key, value] of response.headers.entries()) {
if (key === "content-encoding") {
continue;
Expand All @@ -81,7 +83,16 @@ export async function sendProxy(
continue;
}
if (key === "set-cookie") {
const cookies = splitCookiesString(value).map((cookie) => {
cookies.push(...splitCookiesString(value));
continue;
}
event.node.res.setHeader(key, value);
}

if (cookies.length > 0) {
event.node.res.setHeader(
"set-cookie",
cookies.map((cookie) => {
if (opts.cookieDomainRewrite) {
cookie = rewriteCookieProperty(
cookie,
Expand All @@ -97,12 +108,8 @@ export async function sendProxy(
);
}
return cookie;
});
event.node.res.setHeader("set-cookie", cookies);
continue;
}

event.node.res.setHeader(key, value);
})
);
}

// Directly send consumed _data
Expand Down

0 comments on commit eeaf03b

Please sign in to comment.