Skip to content

Commit

Permalink
Support experimental partitioned cookies (#12)
Browse files Browse the repository at this point in the history
Co-authored-by: Theodore Brown <[email protected]>
  • Loading branch information
stefansundin and theodorejb authored Apr 19, 2024
1 parent aed4655 commit 05b4f7b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,21 @@ Cookies.set('name', 'value', { sameSite: 'strict' });
Cookies.set('other', 'value', { sameSite: 'lax' });
```

### partitioned

Either `true` or `false`, indicating that the cookie should be stored using partitioned storage.
See [Cookies Having Independent Partitioned State (CHIPS)](https://developer.mozilla.org/en-US/docs/Web/Privacy/Privacy_sandbox/Partitioned_cookies) for more details.

**Default:** Cookie will not be partitioned.

**Examples:**

```javascript
Cookies.set('name', 'value', { secure: true, partitioned: true });
Cookies.get('name'); // => 'value'
Cookies.remove('name');
```

## Encoding

This project is [RFC 6265](http://tools.ietf.org/html/rfc6265#section-4.1.1)
Expand Down
24 changes: 20 additions & 4 deletions src/CookieAttributes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type CookieAttributes = BaseCookieAttributes & SameSiteCookieAttributes;
export type CookieAttributes = BaseCookieAttributes & SameSiteCookieAttributes & MaybePartitionedCookie;

interface BaseCookieAttributes {
/**
Expand Down Expand Up @@ -41,10 +41,26 @@ interface LaxStrictSameSiteCookieAttributes {
sameSite?: 'strict' | 'lax';
}

/**
* Cookies with `SameSite=None` must also specify 'Secure'
*/
/** Cookies with `SameSite=None` must also specify 'Secure' */
interface NoneSameSiteCookieAttributes {
sameSite: 'none';
secure: true;
}

type MaybePartitionedCookie = UnpartitionedCookieAttributes | PartitionedCookieAttributes;

interface UnpartitionedCookieAttributes {
/**
* If enabled, indicates that the cookie should be stored using partitioned storage.
*/
partitioned?: false;
}

/** Cookies with `Partitioned` must also specify 'Secure' */
interface PartitionedCookieAttributes {
/**
* Indicates that the cookie should be stored using partitioned storage.
*/
partitioned: true;
secure: true;
}
1 change: 1 addition & 0 deletions src/es-cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function stringifyAttributes(attributes: CookieAttributes): string {
+ stringifyAttribute('Domain', attributes.domain)
+ stringifyAttribute('Path', attributes.path)
+ stringifyAttribute('Secure', attributes.secure)
+ stringifyAttribute('Partitioned', attributes.partitioned)
+ stringifyAttribute('SameSite', attributes.sameSite);
}

Expand Down
10 changes: 10 additions & 0 deletions test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ describe('encode', function () {
assert.strictEqual(actual, 'c=v; Path=/');
});

it('should support true partitioned option', function () {
let actual = Cookies.encode('c', 'v', { secure: true, partitioned: true });
assert.strictEqual(actual, 'c=v; Secure; Partitioned');
});

it('should support false partitioned option', function () {
let actual = Cookies.encode('c', 'v', { partitioned: false });
assert.strictEqual(actual, 'c=v');
});

it('should support sameSite option', function() {
let strict = Cookies.encode('c', 'v', { sameSite: 'strict' });
assert.strictEqual(strict, 'c=v; SameSite=strict');
Expand Down

0 comments on commit 05b4f7b

Please sign in to comment.