Skip to content
Open
Show file tree
Hide file tree
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
28 changes: 14 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export function parseCookie(str: string, options?: ParseOptions): Cookies {
// RFC 6265 sec 4.1.1, RFC 2616 2.2 defines a cookie name consists of one char minimum, plus '='.
if (len < 2) return obj;

const dec = options?.decode || decode;
const dec = options?.decode ?? defaultDecode;
let index = 0;

do {
Expand Down Expand Up @@ -160,7 +160,7 @@ export function stringifyCookie(
cookie: Cookies,
options?: StringifyOptions,
): string {
const enc = options?.encode || defaultEncode;
const enc = options?.encode ?? defaultEncode;
const keys = Object.keys(cookie);
let str = "";

Expand All @@ -176,7 +176,7 @@ export function stringifyCookie(
const value = enc(val);

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

if (str) str += "; ";
Expand Down Expand Up @@ -286,47 +286,47 @@ export function stringifySetCookie(
cookie: SetCookie,
options?: StringifyOptions,
): string {
const enc = options?.encode || defaultEncode;
const enc = options?.encode ?? defaultEncode;

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

const value = cookie.value == null ? "" : enc(cookie.value);

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

let str = cookie.name + "=" + value;

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

str += "; Max-Age=" + cookie.maxAge;
}

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

str += "; Domain=" + cookie.domain;
}

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

str += "; Path=" + cookie.path;
}

if (cookie.expires) {
if (!Number.isFinite(cookie.expires.valueOf())) {
throw new TypeError(`option expires is invalid: ${cookie.expires}`);
throw new TypeError(`cookie expires is invalid: ${cookie.expires}`);
}

str += "; Expires=" + cookie.expires.toUTCString();
Expand Down Expand Up @@ -360,7 +360,7 @@ export function stringifySetCookie(
str += "; Priority=High";
break;
default:
throw new TypeError(`option priority is invalid: ${cookie.priority}`);
throw new TypeError(`cookie priority is invalid: ${cookie.priority}`);
}
}

Expand All @@ -381,7 +381,7 @@ export function stringifySetCookie(
str += "; SameSite=None";
break;
default:
throw new TypeError(`option sameSite is invalid: ${cookie.sameSite}`);
throw new TypeError(`cookie sameSite is invalid: ${cookie.sameSite}`);
}
}

Expand All @@ -395,7 +395,7 @@ export function stringifySetCookie(
* => { name: 'foo', value: 'bar', httpOnly: true }
*/
export function parseSetCookie(str: string, options?: ParseOptions): SetCookie {
const dec = options?.decode || decode;
const dec = options?.decode ?? defaultDecode;
const len = str.length;
const endIdx = endIndex(str, 0, len);
let eqIdx = eqIndex(str, 0, len);
Expand Down Expand Up @@ -513,7 +513,7 @@ function valueSlice(str: string, min: number, max: number) {
/**
* URL-decode string value. Optimized to skip native call when no %.
*/
function decode(str: string): string {
function defaultDecode(str: string): string {
if (str.indexOf("%") === -1) return str;

try {
Expand Down
4 changes: 2 additions & 2 deletions src/stringify-cookie.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe("cookie.stringifyCookie", () => {

it("should error on invalid values", () => {
expect(() => stringifyCookie({ test: ";" }, { encode: (x) => x })).toThrow(
/cookie val is invalid/,
/cookie value is invalid/,
);
});

Expand Down Expand Up @@ -154,7 +154,7 @@ describe("cookie.stringifyCookie", () => {
it("should throw when custom encoder produces invalid value", () => {
expect(() =>
stringifyCookie({ foo: "bar" }, { encode: () => "invalid value" }),
).toThrow(/cookie val is invalid/);
).toThrow(/cookie value is invalid/);
});
});

Expand Down
16 changes: 8 additions & 8 deletions src/stringify-set-cookie.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe("cookie.stringifySetCookie", () => {
["foo\tbar"],
])("should throw for invalid name: %s", (name) => {
expect(() => stringifySetCookie({ name, value: "bar" })).toThrow(
/argument name is invalid/,
/cookie name is invalid/,
);
});

Expand Down Expand Up @@ -110,7 +110,7 @@ describe("cookie.stringifySetCookie", () => {
])("should throw for invalid domain: %s", (domain) => {
expect(() =>
stringifySetCookie({ name: "foo", value: "bar", domain }),
).toThrow(/option domain is invalid/);
).toThrow(/cookie domain is invalid/);
});
});

Expand Down Expand Up @@ -149,7 +149,7 @@ describe("cookie.stringifySetCookie", () => {
(value) => {
expect(() =>
stringifySetCookie({ name: "foo", value }, { encode: (x) => x }),
).toThrow(/argument val is invalid/);
).toThrow(/cookie value is invalid/);
},
);
});
Expand All @@ -162,7 +162,7 @@ describe("cookie.stringifySetCookie", () => {
value: "bar",
expires: new Date(NaN),
}),
).toThrow(/option expires is invalid/);
).toThrow(/cookie expires is invalid/);
});

it("should set expires to given date", () => {
Expand All @@ -184,7 +184,7 @@ describe("cookie.stringifySetCookie", () => {
])("should throw when maxAge is %s", (_label, maxAge) => {
expect(() =>
stringifySetCookie({ name: "foo", value: "bar", maxAge } as any),
).toThrow(/option maxAge is invalid/);
).toThrow(/cookie maxAge is invalid/);
});

it("should set max-age to value", () => {
Expand Down Expand Up @@ -223,7 +223,7 @@ describe("cookie.stringifySetCookie", () => {
])("should throw for invalid path: %s", (path) => {
expect(() =>
stringifySetCookie({ name: "foo", value: "bar", path }),
).toThrow(/option path is invalid/);
).toThrow(/cookie path is invalid/);
});
});

Expand Down Expand Up @@ -260,7 +260,7 @@ describe("cookie.stringifySetCookie", () => {
])("should throw on %s", (_label, priority) => {
expect(() =>
stringifySetCookie({ name: "foo", value: "bar", priority } as any),
).toThrow(/option priority is invalid/);
).toThrow(/cookie priority is invalid/);
});

it.each([
Expand All @@ -287,7 +287,7 @@ describe("cookie.stringifySetCookie", () => {
value: "bar",
sameSite: "foo" as any,
}),
).toThrow(/option sameSite is invalid/);
).toThrow(/cookie sameSite is invalid/);
});

it.each([
Expand Down
Loading