Skip to content

Commit 9d244a7

Browse files
committed
refactor: upgrade cookie-es to v3
1 parent 766cd39 commit 9d244a7

4 files changed

Lines changed: 312 additions & 12 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"automd": "^0.4.3",
6666
"changelogen": "^0.6.2",
6767
"connect": "^3.7.0",
68-
"cookie-es": "^2.0.0",
68+
"cookie-es": "^3.0.0",
6969
"crossws": "^0.4.4",
7070
"elysia": "^1.4.28",
7171
"esbuild": "^0.27.4",

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/cookie.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CookieSerializeOptions, SetCookie } from "cookie-es";
1+
import type { CookieSerializeOptions } from "cookie-es";
22
import type { H3Event, HTTPEvent } from "../event.ts";
33
import { parse as parseCookie, serialize as serializeCookie, parseSetCookie } from "cookie-es";
44
import { validateData } from "./internal/validate.ts";
@@ -19,7 +19,7 @@ const CHUNKS_MAX_LENGTH = 4000;
1919
* const cookies = parseCookies(event)
2020
* ```
2121
*/
22-
export function parseCookies(event: HTTPEvent): Record<string, string> {
22+
export function parseCookies(event: HTTPEvent): Record<string, string | undefined> {
2323
return parseCookie(event.req.headers.get("cookie") || "");
2424
}
2525

@@ -42,7 +42,7 @@ export function getValidatedCookies<Event extends HTTPEvent, S extends StandardS
4242
export function getValidatedCookies<Event extends HTTPEvent, OutputT>(
4343
event: Event,
4444
validate: (
45-
data: Record<string, string>,
45+
data: Record<string, string | undefined>,
4646
) => ValidateResult<OutputT> | Promise<ValidateResult<OutputT>>,
4747
options?: { onError?: () => ErrorDetails },
4848
): Promise<OutputT>;
@@ -95,10 +95,14 @@ export function setCookie(
9595
}
9696

9797
// Merge and deduplicate unique set-cookie headers
98-
const newCookieKey = _getDistinctCookieKey(name, (options || {}) as SetCookie);
98+
const newCookieKey = _getDistinctCookieKey(name, options || {});
9999
event.res.headers.delete("set-cookie");
100100
for (const cookie of currentCookies) {
101-
const _key = _getDistinctCookieKey(cookie.split("=")?.[0], parseSetCookie(cookie));
101+
const parsed = parseSetCookie(cookie);
102+
if (!parsed) {
103+
continue;
104+
}
105+
const _key = _getDistinctCookieKey(cookie.split("=")?.[0], parsed);
102106
if (_key === newCookieKey) {
103107
continue;
104108
}
@@ -237,7 +241,7 @@ export function deleteChunkedCookie(
237241
*
238242
* @see https://httpwg.org/specs/rfc6265.html#rfc.section.4.1.2
239243
*/
240-
function _getDistinctCookieKey(name: string, options: Partial<SetCookie>) {
244+
function _getDistinctCookieKey(name: string, options: { domain?: string; path?: string }) {
241245
return [name, options.domain || "", options.path || "/"].join(";");
242246
}
243247

0 commit comments

Comments
 (0)