Use getSetCookie, if available (#6347)

* 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
This commit is contained in:
Nate Moore 2023-02-23 13:41:00 -06:00 committed by GitHub
parent 48b54d92ea
commit 504c7bacb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 9 deletions

View file

@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/image': patch
---
Fix internal `getSetCookie` usage for `undici@5.20.x`

View file

@ -57,6 +57,12 @@ export async function writeWebResponse(res: http.ServerResponse, webResponse: Re
const _headers = Object.fromEntries(headers.entries()); 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() // Attach any set-cookie headers added via Astro.cookies.set()
const setCookieHeaders = Array.from(getSetCookiesFromResponse(webResponse)); const setCookieHeaders = Array.from(getSetCookiesFromResponse(webResponse));
if (setCookieHeaders.length) { if (setCookieHeaders.length) {

View file

@ -45,7 +45,11 @@ describe('Astro.cookies', () => {
method: 'POST', method: 'POST',
}); });
expect(response.status).to.equal(200); expect(response.status).to.equal(200);
// 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); expect(response.headers.has('set-cookie')).to.equal(true);
}
}); });
}); });

View file

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