From 303703be47e30e0dda2fccca32230f3744c0a829 Mon Sep 17 00:00:00 2001 From: pilcrowOnPaper <80624252+pilcrowOnPaper@users.noreply.github.com> Date: Tue, 20 Jun 2023 23:35:41 +0900 Subject: [PATCH] Export `parseCookie()` (#752) --- .auri/$urtm2qa5.md | 6 ++++++ packages/lucia/src/utils/cookie.ts | 18 ++++++------------ packages/lucia/src/utils/index.ts | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) create mode 100644 .auri/$urtm2qa5.md diff --git a/.auri/$urtm2qa5.md b/.auri/$urtm2qa5.md new file mode 100644 index 000000000..9b148c8f0 --- /dev/null +++ b/.auri/$urtm2qa5.md @@ -0,0 +1,6 @@ +--- +package: "lucia" # package name +type: "minor" # "major", "minor", "patch" +--- + +Export `parseCookie()` from `/utils` \ No newline at end of file diff --git a/packages/lucia/src/utils/cookie.ts b/packages/lucia/src/utils/cookie.ts index 6494c9485..e10431b7c 100644 --- a/packages/lucia/src/utils/cookie.ts +++ b/packages/lucia/src/utils/cookie.ts @@ -11,17 +11,11 @@ const __toString = Object.prototype.toString; // eslint-disable-next-line no-control-regex const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/; -interface CookieParseOptions { - decode?(value: string): string; -} - -export const parseCookie = (str: string, options?: CookieParseOptions) => { +export const parseCookie = (str: string) => { if (typeof str !== "string") { throw new TypeError("argument str must be a string"); } - const obj: Record = {}; - const opt = options ?? {}; - const dec = opt.decode ?? decode; + const obj: Record = {}; let index = 0; while (index < str.length) { const eqIdx = str.indexOf("=", index); @@ -37,13 +31,13 @@ export const parseCookie = (str: string, options?: CookieParseOptions) => { } const key = str.slice(index, eqIdx).trim(); // only assign once - if (undefined === obj[key]) { + if (!(key in obj)) { let val = str.slice(eqIdx + 1, endIdx).trim(); // quoted values if (val.charCodeAt(0) === 0x22) { val = val.slice(1, -1); } - obj[key] = tryDecode(val, dec); + obj[key] = tryDecode(val); } index = endIdx + 1; } @@ -164,9 +158,9 @@ const isDate = (val: any): val is Date => { return __toString.call(val) === "[object Date]" || val instanceof Date; }; -const tryDecode = (str: string, decodeFunction: typeof decode) => { +const tryDecode = (str: string) => { try { - return decodeFunction(str); + return decode(str); } catch (e) { return str; } diff --git a/packages/lucia/src/utils/index.ts b/packages/lucia/src/utils/index.ts index 9a22a5116..df6af4c78 100644 --- a/packages/lucia/src/utils/index.ts +++ b/packages/lucia/src/utils/index.ts @@ -1,3 +1,3 @@ export { generateRandomString } from "./nanoid.js"; -export { serializeCookie } from "./cookie.js"; +export { serializeCookie, parseCookie } from "./cookie.js"; export { isWithinExpiration } from "./date.js";