Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 29 additions & 63 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,107 +250,80 @@ export function serialize(
val: string,
options?: SerializeOptions,
): string {
const enc = options?.encode || encodeURIComponent;
const enc = options?.encode ?? encodeURIComponent;

if (!cookieNameRegExp.test(name)) {
throw new TypeError(`argument name is invalid: ${name}`);
}

const value = enc(val);

if (!cookieValueRegExp.test(value)) {
throw new TypeError(`argument val is invalid: ${val}`);
}

let str = name + "=" + value;
let str = `${name}=${value}`;
if (!options) return str;

if (options.maxAge !== undefined) {
if (!Number.isInteger(options.maxAge)) {
throw new TypeError(`option maxAge is invalid: ${options.maxAge}`);
}

str += "; Max-Age=" + options.maxAge;
str += `; Max-Age=${options.maxAge}`;
}

if (options.domain) {
if (!domainValueRegExp.test(options.domain)) {
throw new TypeError(`option domain is invalid: ${options.domain}`);
}

str += "; Domain=" + options.domain;
str += `; Domain=${options.domain}`;
}

if (options.path) {
if (!pathValueRegExp.test(options.path)) {
throw new TypeError(`option path is invalid: ${options.path}`);
}

str += "; Path=" + options.path;
str += `; Path=${options.path}`;
}

if (options.expires) {
if (
!isDate(options.expires) ||
!(options.expires instanceof Date) ||
!Number.isFinite(options.expires.valueOf())
) {
throw new TypeError(`option expires is invalid: ${options.expires}`);
}

str += "; Expires=" + options.expires.toUTCString();
}

if (options.httpOnly) {
str += "; HttpOnly";
}

if (options.secure) {
str += "; Secure";
str += `; Expires=${options.expires.toUTCString()}`;
}

if (options.partitioned) {
str += "; Partitioned";
}
if (options.httpOnly) str += "; HttpOnly";
if (options.secure) str += "; Secure";
if (options.partitioned) str += "; Partitioned";

if (options.priority) {
const priorityMap = { low: "Low", medium: "Medium", high: "High" } as const;
const priority =
typeof options.priority === "string"
? options.priority.toLowerCase()
: undefined;
switch (priority) {
case "low":
str += "; Priority=Low";
break;
case "medium":
str += "; Priority=Medium";
break;
case "high":
str += "; Priority=High";
break;
default:
throw new TypeError(`option priority is invalid: ${options.priority}`);
}
priorityMap[
String(options.priority).toLowerCase() as keyof typeof priorityMap
];
if (!priority)
throw new TypeError(`option priority is invalid: ${options.priority}`);
str += `; Priority=${priority}`;
}

if (options.sameSite) {
const sameSiteMap = {
strict: "Strict",
lax: "Lax",
none: "None",
true: "Strict",
} as const;
const sameSite =
typeof options.sameSite === "string"
? options.sameSite.toLowerCase()
: options.sameSite;
switch (sameSite) {
case true:
case "strict":
str += "; SameSite=Strict";
break;
case "lax":
str += "; SameSite=Lax";
break;
case "none":
str += "; SameSite=None";
break;
default:
throw new TypeError(`option sameSite is invalid: ${options.sameSite}`);
}
sameSiteMap[
String(options.sameSite).toLowerCase() as keyof typeof sameSiteMap
];
if (!sameSite)
throw new TypeError(`option sameSite is invalid: ${options.sameSite}`);
str += `; SameSite=${sameSite}`;
}

return str;
Expand All @@ -368,10 +341,3 @@ function decode(str: string): string {
return str;
}
}

/**
* Determine if value is a Date.
*/
function isDate(val: any): val is Date {
return __toString.call(val) === "[object Date]";
}