refactor: upgrade cookie-es to v3 · h3js/h3@9d244a7

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

}

96969797

// 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