Skip to content

Commit

Permalink
fix: bump undici to v5.20.0 (#6355)
Browse files Browse the repository at this point in the history
* 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 <princssdev@gmail.com>
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
  • Loading branch information
3 people committed Feb 24, 2023
1 parent 098341f commit 5aa6580
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
6 changes: 6 additions & 0 deletions .changeset/yellow-kings-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@astrojs/webapi': patch
'@astrojs/telemetry': patch
---

Update `undici` to v5.20.0
6 changes: 4 additions & 2 deletions packages/astro/src/core/cookies/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ function getFromResponse(response: Response): AstroCookies | undefined {
}
}

export function* getSetCookiesFromResponse(response: Response): Generator<string, void, unknown> {
export function* getSetCookiesFromResponse(response: Response): Generator<string, string[]> {
const cookies = getFromResponse(response);
if (!cookies) {
return;
return [];
}
for (const headerValue of cookies.headers()) {
yield headerValue;
}

return [];
}
19 changes: 12 additions & 7 deletions packages/astro/src/vite-plugin-astro-server/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/webapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 2 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5aa6580

Please sign in to comment.