From 9286e105684dc035f5954e6ad8e6d4a8a41dd446 Mon Sep 17 00:00:00 2001 From: Erika <3019731+Princesseuh@users.noreply.github.com> Date: Thu, 16 Feb 2023 20:26:00 +0100 Subject: [PATCH] fix(cookies): Fix cookies not being all returned on Node 16 after Undici upgrade (#6265) * fix(cookies): Fix cookies not being all returned on Node 16 after Undici upgrade * test(cookies): Oops --- .../src/vite-plugin-astro-server/response.ts | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/packages/astro/src/vite-plugin-astro-server/response.ts b/packages/astro/src/vite-plugin-astro-server/response.ts index 911ccf413..1a4d8ca99 100644 --- a/packages/astro/src/vite-plugin-astro-server/response.ts +++ b/packages/astro/src/vite-plugin-astro-server/response.ts @@ -55,20 +55,12 @@ export function writeHtmlResponse(res: http.ServerResponse, statusCode: number, export async function writeWebResponse(res: http.ServerResponse, webResponse: Response) { const { status, headers, body } = webResponse; - let _headers = {}; - if ('raw' in headers) { - // Node fetch allows you to get the raw headers, which includes multiples of the same type. - // This is needed because Set-Cookie *must* be called for each cookie, and can't be - // concatenated together. - type HeadersWithRaw = Headers & { - raw: () => Record; - }; + const _headers = Object.fromEntries(headers.entries()); - for (const [key, value] of Object.entries((headers as HeadersWithRaw).raw())) { - res.setHeader(key, value); - } - } else { - _headers = Object.fromEntries(headers.entries()); + // Undici 5.19.1 includes a `getSetCookie` helper that returns an array of all the `set-cookies` headers. + // Previously, `headers.entries()` would already have those merged, but it seems like this isn't the case anymore, weird. + if ((headers as any)['getSetCookie']) { + _headers['set-cookie'] = (headers as any).getSetCookie(); } // Attach any set-cookie headers added via Astro.cookies.set()