Skip to content

Commit

Permalink
Export parseCookie() (#752)
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowonpaper authored Jun 20, 2023
1 parent f4eb05e commit 303703b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .auri/$urtm2qa5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
package: "lucia" # package name
type: "minor" # "major", "minor", "patch"
---

Export `parseCookie()` from `/utils`
18 changes: 6 additions & 12 deletions packages/lucia/src/utils/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any, string | undefined> = {};
const opt = options ?? {};
const dec = opt.decode ?? decode;
const obj: Record<string, string> = {};
let index = 0;
while (index < str.length) {
const eqIdx = str.indexOf("=", index);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/lucia/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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";

0 comments on commit 303703b

Please sign in to comment.