summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Nate Moore <natemoo-re@users.noreply.github.com> 2023-02-23 13:41:00 -0600
committerGravatar GitHub <noreply@github.com> 2023-02-23 13:41:00 -0600
commit504c7bacb8c1f2308a31e6c412825ba34983ba33 (patch)
tree67719861f21504a4dc3c6a19c33d820237eacdf3
parent48b54d92ea10c4c158a49e713cc14a33df5f69a2 (diff)
downloadastro-504c7bacb8c1f2308a31e6c412825ba34983ba33.tar.gz
astro-504c7bacb8c1f2308a31e6c412825ba34983ba33.tar.zst
astro-504c7bacb8c1f2308a31e6c412825ba34983ba33.zip
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
-rw-r--r--.changeset/six-crabs-hunt.md6
-rw-r--r--packages/astro/src/vite-plugin-astro-server/response.ts6
-rw-r--r--packages/astro/test/astro-cookies.test.js6
-rw-r--r--packages/integrations/image/src/build/ssg.ts18
4 files changed, 27 insertions, 9 deletions
diff --git a/.changeset/six-crabs-hunt.md b/.changeset/six-crabs-hunt.md
new file mode 100644
index 000000000..97fb04c35
--- /dev/null
+++ b/.changeset/six-crabs-hunt.md
@@ -0,0 +1,6 @@
+---
+'astro': patch
+'@astrojs/image': patch
+---
+
+Fix internal `getSetCookie` usage for `undici@5.20.x`
diff --git a/packages/astro/src/vite-plugin-astro-server/response.ts b/packages/astro/src/vite-plugin-astro-server/response.ts
index 4f1a3e6ab..008e7daab 100644
--- a/packages/astro/src/vite-plugin-astro-server/response.ts
+++ b/packages/astro/src/vite-plugin-astro-server/response.ts
@@ -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) {
diff --git a/packages/astro/test/astro-cookies.test.js b/packages/astro/test/astro-cookies.test.js
index 77e2cd2ba..0af8d30b7 100644
--- a/packages/astro/test/astro-cookies.test.js
+++ b/packages/astro/test/astro-cookies.test.js
@@ -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);
+ }
});
});
diff --git a/packages/integrations/image/src/build/ssg.ts b/packages/integrations/image/src/build/ssg.ts
index b432447e3..076144282 100644
--- a/packages/integrations/image/src/build/ssg.ts
+++ b/packages/integrations/image/src/build/ssg.ts
@@ -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,
@@ -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,