Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use getSetCookie, if available #6347

Merged
merged 5 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/six-crabs-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/image': patch
---

Fix internal `getSetCookie` usage for `[email protected]`
6 changes: 6 additions & 0 deletions packages/astro/src/vite-plugin-astro-server/response.ts
Original file line number Diff line number Diff line change
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') {
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved
_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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 [email protected] + 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 [email protected] + undici
try {
headers = Object.fromEntries(_headers.entries());
} catch {}
return {
status,
headers,
Expand Down