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
37 changes: 4 additions & 33 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ const pathValueRegExp = /^[\u0020-\u003A\u003D-\u007E]*$/;
*/
const maxAgeRegExp = /^-?\d+$/;

const __toString = Object.prototype.toString;

const NullObject = /* @__PURE__ */ (() => {
const C = function () {};
C.prototype = Object.create(null);
Expand Down Expand Up @@ -276,28 +274,13 @@ export type SerializeOptions = StringifyOptions &
* Serialize a name value pair into a cookie string suitable for
* http headers. An optional options object specifies cookie parameters.
*
* serialize('foo', 'bar', { httpOnly: true })
* => "foo=bar; httpOnly"
* stringifySetCookie({ name: 'foo', value: 'bar', httpOnly: true })
* => "foo=bar; HttpOnly"
*/
export function stringifySetCookie(
cookie: SetCookie,
options?: StringifyOptions,
): string;
export function stringifySetCookie(
name: string,
val: string,
options?: SerializeOptions,
): string;
export function stringifySetCookie(
_name: string | SetCookie,
_val?: string | StringifyOptions,
_opts?: SerializeOptions,
): string {
const cookie =
typeof _name === "object"
? _name
: { ..._opts, name: _name, value: String(_val) };
const options = typeof _val === "object" ? _val : _opts;
const enc = options?.encode || encodeURIComponent;

if (!cookieNameRegExp.test(cookie.name)) {
Expand Down Expand Up @@ -337,7 +320,7 @@ export function stringifySetCookie(
}

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

Expand Down Expand Up @@ -403,7 +386,7 @@ export function stringifySetCookie(
/**
* Deserialize a `Set-Cookie` header into an object.
*
* deserialize('foo=bar; httpOnly')
* parseSetCookie('foo=bar; HttpOnly')
* => { name: 'foo', value: 'bar', httpOnly: true }
*/
export function parseSetCookie(str: string, options?: ParseOptions): SetCookie {
Expand Down Expand Up @@ -533,15 +516,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]";
}

/**
* Backward compatibility exports.
*/
export { stringifySetCookie as serialize, parseCookie as parse };
4 changes: 0 additions & 4 deletions src/parse-cookie.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ describe("cookie.parseCookie", function () {
});
});

it("should have backward compatible export", function () {
expect(cookie.parse).toBe(cookie.parseCookie);
});

it("should parse cookie string to object", function () {
expect(cookie.parseCookie("foo=bar")).toEqual({ foo: "bar" });
expect(cookie.parseCookie("foo=123")).toEqual({ foo: "123" });
Expand Down
50 changes: 13 additions & 37 deletions src/stringify-set-cookie.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@ import * as cookie from "./index.js";

describe("cookie.stringifySetCookie", () => {
bench("simple", () => {
cookie.stringifySetCookie("foo", "bar");
cookie.stringifySetCookie({
name: "foo",
value: "bar",
});
});

bench("encode", () => {
cookie.stringifySetCookie("foo", "hello there!");
cookie.stringifySetCookie({
name: "foo",
value: "hello there!",
});
});

const expires = new Date("Wed, 21 Oct 2015 07:28:00 GMT");
bench("attributes", () => {
cookie.stringifySetCookie("foo", "bar", {
bench("all attributes", () => {
cookie.stringifySetCookie({
name: "foo",
value: "bar",
path: "/",
domain: "example.com",
maxAge: 3600,
Expand All @@ -25,7 +33,7 @@ describe("cookie.stringifySetCookie", () => {
});
});

bench("object input", () => {
bench("typical object", () => {
cookie.stringifySetCookie({
name: "foo",
value: "bar",
Expand All @@ -36,36 +44,4 @@ describe("cookie.stringifySetCookie", () => {
sameSite: "strict",
});
});

const setCookies10 = genSetCookies(10);
bench("10 set-cookies", () => {
for (const setCookie of setCookies10) {
cookie.stringifySetCookie(setCookie);
}
});

const setCookies100 = genSetCookies(100);
bench("100 set-cookies", () => {
for (const setCookie of setCookies100) {
cookie.stringifySetCookie(setCookie);
}
});
});

function genSetCookies(num: number): cookie.SetCookie[] {
const cookies: cookie.SetCookie[] = [];

for (let i = 0; i < num; i++) {
cookies.push({
name: "foo" + i,
value: "bar " + i,
path: "/foo" + i,
maxAge: 3600,
httpOnly: true,
secure: true,
sameSite: "lax",
});
}

return cookies;
}
Loading
Loading