From 5aa6580f775405a4443835bf7eb81f0c65e5aed6 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Fri, 24 Feb 2023 18:10:33 +0000 Subject: [PATCH] fix: bump `undici` to v5.20.0 (#6355) * fix: bump `undici` to v5.20.0 * fix(cookies): Hopefully the last time we mess with undici and cookies * chore: add @astrojs/telemetry to changeset --------- Co-authored-by: Princesseuh Co-authored-by: Nate Moore --- .changeset/yellow-kings-rhyme.md | 6 ++++++ packages/astro/src/core/cookies/response.ts | 6 ++++-- .../src/vite-plugin-astro-server/response.ts | 19 ++++++++++++------- packages/webapi/package.json | 2 +- pnpm-lock.yaml | 11 ++--------- 5 files changed, 25 insertions(+), 19 deletions(-) create mode 100644 .changeset/yellow-kings-rhyme.md diff --git a/.changeset/yellow-kings-rhyme.md b/.changeset/yellow-kings-rhyme.md new file mode 100644 index 0000000000..9b07297728 --- /dev/null +++ b/.changeset/yellow-kings-rhyme.md @@ -0,0 +1,6 @@ +--- +'@astrojs/webapi': patch +'@astrojs/telemetry': patch +--- + +Update `undici` to v5.20.0 diff --git a/packages/astro/src/core/cookies/response.ts b/packages/astro/src/core/cookies/response.ts index d04ce1ef38..18d72ab1c7 100644 --- a/packages/astro/src/core/cookies/response.ts +++ b/packages/astro/src/core/cookies/response.ts @@ -15,12 +15,14 @@ function getFromResponse(response: Response): AstroCookies | undefined { } } -export function* getSetCookiesFromResponse(response: Response): Generator { +export function* getSetCookiesFromResponse(response: Response): Generator { const cookies = getFromResponse(response); if (!cookies) { - return; + return []; } for (const headerValue of cookies.headers()) { yield headerValue; } + + return []; } diff --git a/packages/astro/src/vite-plugin-astro-server/response.ts b/packages/astro/src/vite-plugin-astro-server/response.ts index 008e7daabd..a7cc6e0936 100644 --- a/packages/astro/src/vite-plugin-astro-server/response.ts +++ b/packages/astro/src/vite-plugin-astro-server/response.ts @@ -55,19 +55,24 @@ export function writeHtmlResponse(res: http.ServerResponse, statusCode: number, export async function writeWebResponse(res: http.ServerResponse, webResponse: Response) { const { status, headers, body } = webResponse; + // Attach any set-cookie headers added via Astro.cookies.set() + const setCookieHeaders = Array.from(getSetCookiesFromResponse(webResponse)); + setCookieHeaders.forEach((cookie) => { + headers.append('set-cookie', cookie); + }); + 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(); + if (headers.has('set-cookie')) { + if ('getSetCookie' in headers && typeof headers.getSetCookie === 'function') { + _headers['set-cookie'] = headers.getSetCookie(); + } else { + _headers['set-cookie'] = headers.get('set-cookie')!; + } } - // Attach any set-cookie headers added via Astro.cookies.set() - const setCookieHeaders = Array.from(getSetCookiesFromResponse(webResponse)); - if (setCookieHeaders.length) { - res.setHeader('Set-Cookie', setCookieHeaders); - } res.writeHead(status, _headers); if (body) { if (Symbol.for('astro.responseBody') in webResponse) { diff --git a/packages/webapi/package.json b/packages/webapi/package.json index 82d9ee1095..5316909b30 100644 --- a/packages/webapi/package.json +++ b/packages/webapi/package.json @@ -50,7 +50,7 @@ "bugs": "https://github.com/withastro/astro/issues", "homepage": "https://github.com/withastro/astro/tree/main/packages/webapi#readme", "dependencies": { - "undici": "5.18.0" + "undici": "5.20.0" }, "devDependencies": { "@rollup/plugin-alias": "^3.1.9", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 257e854965..fa8b964c16 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3678,10 +3678,10 @@ importers: rollup: ^2.79.1 tslib: ^2.4.0 typescript: ~4.7.3 - undici: 5.18.0 + undici: 5.20.0 urlpattern-polyfill: ^1.0.0-rc5 dependencies: - undici: 5.18.0 + undici: 5.20.0 devDependencies: '@rollup/plugin-alias': 3.1.9_rollup@2.79.1 '@rollup/plugin-inject': 4.0.4_rollup@2.79.1 @@ -14871,13 +14871,6 @@ packages: jiti: 1.17.0 dev: false - /undici/5.18.0: - resolution: {integrity: sha512-1iVwbhonhFytNdg0P4PqyIAXbdlVZVebtPDvuM36m66mRw4OGrCm2MYynJv/UENFLdP13J1nPVQzVE2zTs1OeA==} - engines: {node: '>=12.18'} - dependencies: - busboy: 1.6.0 - dev: false - /undici/5.20.0: resolution: {integrity: sha512-J3j60dYzuo6Eevbawwp1sdg16k5Tf768bxYK4TUJRH7cBM4kFCbf3mOnM/0E3vQYXvpxITbbWmBafaDbxLDz3g==} engines: {node: '>=12.18'}