Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partitioned #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ The following fields can be added to the mentioned object:
| `path` | A `string` that limits the access of the cookie to that path. | The current path. |
| `secure` | A `boolean` indicating whether the cookie shall only be accessible over a secure connection or not. | `false` |
| `sameSite` | A `string` that specifies SameSite attribute that restricts cookie access based on the site context. | `null` |
| `partitioned` | A `boolean` indicating whether the cookie is partitioned and stored separately per top-level site to prevent cross-site tracking. | `false` |

You can customize the default settings by manipulating `cookie.defaults`.

Expand Down
5 changes: 4 additions & 1 deletion dist/cookie.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ cookie.set = function (key, value, options) {
sameSite = sameSite ? ';SameSite=' + sameSite : '';
if (options.sameSite === null) sameSite = '';

document.cookie = utils.encode(key) + '=' + utils.encode(value) + expires + path + domain + secure + sameSite;
var partitioned = options.partitioned || this.defaults.partitioned ? ';partitioned' : '';
if (options.partitioned === false) partitioned = '';

document.cookie = utils.encode(key) + '=' + utils.encode(value) + expires + path + domain + secure + sameSite + partitioned;
}

return this; // Return the `cookie` object to make chaining possible.
Expand Down
5 changes: 4 additions & 1 deletion dist/cookie.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ cookie.set = function (key, value, options) {
sameSite = sameSite ? ';SameSite=' + sameSite : '';
if (options.sameSite === null) sameSite = '';

document.cookie = utils.encode(key) + '=' + utils.encode(value) + expires + path + domain + secure + sameSite;
var partitioned = options.partitioned || this.defaults.partitioned ? ';partitioned' : '';
if (options.partitioned === false) partitioned = '';

document.cookie = utils.encode(key) + '=' + utils.encode(value) + expires + path + domain + secure + sameSite + partitioned;
}

return this; // Return the `cookie` object to make chaining possible.
Expand Down
9 changes: 6 additions & 3 deletions dist/cookie.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.cookie = factory());
}(this, function () { 'use strict';
}(this, (function () { 'use strict';

// Copyright (c) Florian Hartmann, https://github.com/florian https://github.com/florian/cookie.js

Expand Down Expand Up @@ -92,7 +92,10 @@
sameSite = sameSite ? ';SameSite=' + sameSite : '';
if (options.sameSite === null) sameSite = '';

document.cookie = utils.encode(key) + '=' + utils.encode(value) + expires + path + domain + secure + sameSite;
var partitioned = options.partitioned || this.defaults.partitioned ? ';partitioned' : '';
if (options.partitioned === false) partitioned = '';

document.cookie = utils.encode(key) + '=' + utils.encode(value) + expires + path + domain + secure + sameSite + partitioned;
}

return this; // Return the `cookie` object to make chaining possible.
Expand Down Expand Up @@ -178,4 +181,4 @@

return cookie;

}));
})));
2 changes: 1 addition & 1 deletion dist/cookie.umd.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ cookie.set = function (key, value, options) {
sameSite = sameSite ? ';SameSite=' + sameSite : '';
if (options.sameSite === null) sameSite = '';

document.cookie = utils.encode(key) + '=' + utils.encode(value) + expires + path + domain + secure + sameSite;
var partitioned = options.partitioned || this.defaults.partitioned ? ';partitioned' : '';
if (options.partitioned === false) partitioned = '';

document.cookie = utils.encode(key) + '=' + utils.encode(value) + expires + path + domain + secure + sameSite + partitioned;
}

return this; // Return the `cookie` object to make chaining possible.
Expand Down
20 changes: 20 additions & 0 deletions tests/shared_no_jsdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,26 @@ exports.test = function(context) {

mockStorage.should.deep.equal(['a=1']);
});

it('should set partitioned flag for a cookie', function() {
cookie.set('a', '1', { partitioned: true });

mockStorage.should.deep.equal(['a=1;partitioned']);
});

it('uses the default partitioned flag', function() {
cookie.defaults = { partitioned: true };
cookie.set('a', '1');

mockStorage.should.deep.equal(['a=1;partitioned']);
});

it('allows to override the default partitioned flag', function() {
cookie.defaults = { partitioned: true };
cookie.set('a', '1', { partitioned: false });

mockStorage.should.deep.equal(['a=1']);
});
});
});
};