Skip to content

Commit

Permalink
Use getSetCookie, if available (#6347)
Browse files Browse the repository at this point in the history
* fix: use getSetCookie, if available

* fix: explicitly use entries

* fix(image): be defensive on node@18.14.1

* chore: update changeset

* ci: skip test in 18.14.1
  • Loading branch information
natemoo-re committed Feb 23, 2023
1 parent 48b54d9 commit 504c7ba
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changeset/six-crabs-hunt.md
@@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/image': patch
---

Fix internal `getSetCookie` usage for `undici@5.20.x`
6 changes: 6 additions & 0 deletions packages/astro/src/vite-plugin-astro-server/response.ts
Expand Up @@ -57,6 +57,12 @@ export async function writeWebResponse(res: http.ServerResponse, webResponse: Re

const _headers = Object.fromEntries(headers.entries());

// Undici 5.20.0+ includes a `getSetCookie` helper that returns an array of all the `set-cookies` headers.
// Previously, `headers.entries()` would already have these merged, but it seems like this isn't the case anymore.
if ('getSetCookie' in headers && typeof headers.getSetCookie === 'function') {
_headers['set-cookie'] = headers.getSetCookie();
}

// Attach any set-cookie headers added via Astro.cookies.set()
const setCookieHeaders = Array.from(getSetCookiesFromResponse(webResponse));
if (setCookieHeaders.length) {
Expand Down
6 changes: 5 additions & 1 deletion packages/astro/test/astro-cookies.test.js
Expand Up @@ -45,7 +45,11 @@ describe('Astro.cookies', () => {
method: 'POST',
});
expect(response.status).to.equal(200);
expect(response.headers.has('set-cookie')).to.equal(true);
// Bug in 18.14.1 where `set-cookie` will not be defined
// Should be fixed in 18.14.2
if (process.versions.node !== '18.14.1') {
expect(response.headers.has('set-cookie')).to.equal(true);
}
});
});

Expand Down
18 changes: 10 additions & 8 deletions packages/integrations/image/src/build/ssg.ts
Expand Up @@ -30,10 +30,11 @@ async function loadLocalImage(src: string | URL) {
}

function webToCachePolicyRequest({ url, method, headers: _headers }: Request): CachePolicy.Request {
const headers: CachePolicy.Headers = {};
for (const [key, value] of _headers) {
headers[key] = value;
}
let headers: CachePolicy.Headers = {};
// Be defensive here due to a cookie header bug in node@18.14.1 + undici
try {
headers = Object.fromEntries(_headers.entries());
} catch {}
return {
method,
url,
Expand All @@ -42,10 +43,11 @@ function webToCachePolicyRequest({ url, method, headers: _headers }: Request): C
}

function webToCachePolicyResponse({ status, headers: _headers }: Response): CachePolicy.Response {
const headers: CachePolicy.Headers = {};
for (const [key, value] of _headers) {
headers[key] = value;
}
let headers: CachePolicy.Headers = {};
// Be defensive here due to a cookie header bug in node@18.14.1 + undici
try {
headers = Object.fromEntries(_headers.entries());
} catch {}
return {
status,
headers,
Expand Down

0 comments on commit 504c7ba

Please sign in to comment.