From ae67e197967653a06df9c02b9a9c106e3c15343a Mon Sep 17 00:00:00 2001 From: Alexander Gribochkin Date: Wed, 13 Dec 2023 16:17:52 +0300 Subject: [PATCH 1/5] feat: partitioned cookies --- src/index.ts | 4 ++++ src/types.ts | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/src/index.ts b/src/index.ts index 6b920be..587a400 100644 --- a/src/index.ts +++ b/src/index.ts @@ -188,6 +188,10 @@ export function serialize( } } + if (opt.partitioned) { + str += "; Partitioned" + } + return str; } diff --git a/src/types.ts b/src/types.ts index a6aea2a..2d8365f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -108,6 +108,14 @@ export interface CookieSerializeOptions { * not have an HTTPS connection. */ secure?: boolean | undefined; + /** + * Cookies Having Independent Partitioned State (CHIPS, also known as Partitioned cookies) + * allows developers to opt a cookie into partitioned storage, with a separate + * cookie jar per top-level site. + * + * @see https://developer.mozilla.org/en-US/docs/Web/Privacy/Partitioned_cookies + */ + partitioned?: boolean; } /** From 604ed43b94c5a74b2758a2674bafa6a724200733 Mon Sep 17 00:00:00 2001 From: Alexander Gribochkin Date: Wed, 13 Dec 2023 16:17:57 +0300 Subject: [PATCH 2/5] test: partitioned cookies --- test/serialize.test.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/serialize.test.ts b/test/serialize.test.ts index 8057114..3800386 100644 --- a/test/serialize.test.ts +++ b/test/serialize.test.ts @@ -261,4 +261,14 @@ describe("serialize(name, value, options)", () => { expect(serialize("foo", "bar", { secure: false })).toBe("foo=bar"); }); }); + + describe('with "partitioned" option', () => { + it("should include partitioned flag when true", () => { + expect(serialize("foo", "bar", { partitioned: true })).toBe("foo=bar; Partitioned"); + }); + + it("should not include partitioned flag when false", () => { + expect(serialize("foo", "bar", { partitioned: false })).toBe("foo=bar"); + }); + }); }); From 2b62e72429ed41eaef5f1d5793846e927be50889 Mon Sep 17 00:00:00 2001 From: Alexander Gribochkin Date: Wed, 13 Dec 2023 16:20:57 +0300 Subject: [PATCH 3/5] fix: run lint:fix --- src/index.ts | 29 +++++++++++++++++++---------- src/types.ts | 6 +++--- test/serialize.test.ts | 4 +++- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/index.ts b/src/index.ts index 587a400..92e24f9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -150,17 +150,21 @@ export function serialize( : opt.priority; switch (priority) { - case "low": + case "low": { str += "; Priority=Low"; break; - case "medium": + } + case "medium": { str += "; Priority=Medium"; break; - case "high": + } + case "high": { str += "; Priority=High"; break; - default: + } + default: { throw new TypeError("option priority is invalid"); + } } } @@ -171,25 +175,30 @@ export function serialize( : opt.sameSite; switch (sameSite) { - case true: + case true: { str += "; SameSite=Strict"; break; - case "lax": + } + case "lax": { str += "; SameSite=Lax"; break; - case "strict": + } + case "strict": { str += "; SameSite=Strict"; break; - case "none": + } + case "none": { str += "; SameSite=None"; break; - default: + } + default: { throw new TypeError("option sameSite is invalid"); + } } } if (opt.partitioned) { - str += "; Partitioned" + str += "; Partitioned"; } return str; diff --git a/src/types.ts b/src/types.ts index 2d8365f..9fc1e88 100644 --- a/src/types.ts +++ b/src/types.ts @@ -109,10 +109,10 @@ export interface CookieSerializeOptions { */ secure?: boolean | undefined; /** - * Cookies Having Independent Partitioned State (CHIPS, also known as Partitioned cookies) - * allows developers to opt a cookie into partitioned storage, with a separate + * Cookies Having Independent Partitioned State (CHIPS, also known as Partitioned cookies) + * allows developers to opt a cookie into partitioned storage, with a separate * cookie jar per top-level site. - * + * * @see https://developer.mozilla.org/en-US/docs/Web/Privacy/Partitioned_cookies */ partitioned?: boolean; diff --git a/test/serialize.test.ts b/test/serialize.test.ts index 3800386..371990d 100644 --- a/test/serialize.test.ts +++ b/test/serialize.test.ts @@ -264,7 +264,9 @@ describe("serialize(name, value, options)", () => { describe('with "partitioned" option', () => { it("should include partitioned flag when true", () => { - expect(serialize("foo", "bar", { partitioned: true })).toBe("foo=bar; Partitioned"); + expect(serialize("foo", "bar", { partitioned: true })).toBe( + "foo=bar; Partitioned" + ); }); it("should not include partitioned flag when false", () => { From 5a3fc7d0d3dfad48b0f6e8095bd689d1d59a8c42 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 29 Mar 2024 20:31:15 +0100 Subject: [PATCH 4/5] lint --- test/serialize.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/serialize.test.ts b/test/serialize.test.ts index 334a3dd..5ad32c5 100644 --- a/test/serialize.test.ts +++ b/test/serialize.test.ts @@ -265,7 +265,7 @@ describe("serialize(name, value, options)", () => { describe('with "partitioned" option', () => { it("should include partitioned flag when true", () => { expect(serialize("foo", "bar", { partitioned: true })).toBe( - "foo=bar; Partitioned" + "foo=bar; Partitioned", ); }); From 083d2a2183f7223c4fedbf0001576d1ee7761a7c Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 29 Mar 2024 20:32:20 +0100 Subject: [PATCH 5/5] sync docs with @types/cookie --- src/types.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/types.ts b/src/types.ts index 9fc1e88..7f97f24 100644 --- a/src/types.ts +++ b/src/types.ts @@ -109,11 +109,14 @@ export interface CookieSerializeOptions { */ secure?: boolean | undefined; /** - * Cookies Having Independent Partitioned State (CHIPS, also known as Partitioned cookies) - * allows developers to opt a cookie into partitioned storage, with a separate - * cookie jar per top-level site. + * Specifies the `boolean` value for the [`Partitioned` `Set-Cookie`](rfc-cutler-httpbis-partitioned-cookies) + * attribute. When truthy, the `Partitioned` attribute is set, otherwise it is not. By default, the + * `Partitioned` attribute is not set. * - * @see https://developer.mozilla.org/en-US/docs/Web/Privacy/Partitioned_cookies + * **note** This is an attribute that has not yet been fully standardized, and may change in the future. + * This also means many clients may ignore this attribute until they understand it. + * + * More information about can be found in [the proposal](https://github.com/privacycg/CHIPS) */ partitioned?: boolean; }