Skip to content

Commit

Permalink
Support expires values larger than the maximum date (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
akh9804 committed Apr 19, 2024
1 parent 12fe90b commit 5a74fc1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/es-cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ function stringifyAttribute(name: string, value: string | boolean | undefined):

function stringifyAttributes(attributes: CookieAttributes): string {
if (typeof attributes.expires === 'number') {
let expires = new Date();
expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5);
attributes.expires = expires;
const milliseconds = Math.min(Date.now() + attributes.expires * 864e+5, 864e+13);
attributes.expires = new Date(milliseconds);
}

return stringifyAttribute('Expires', attributes.expires ? attributes.expires.toUTCString() : '')
Expand Down
6 changes: 6 additions & 0 deletions test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ describe('encode', function () {
assert.strictEqual(actual, 'c=v; Expires=' + twentyOneDaysFromNow.toUTCString());
});

it('should work with expires above the maximum date', function () {
let maximum = new Date(864e+13);
let actual = Cookies.encode('c', 'v', { expires: 864e+14 });
assert.strictEqual(actual, 'c=v; Expires=' + maximum.toUTCString());
});

it('should work with expires as fraction of a day', function () {
let getAttributeValue = function (createdCookie: string, attributeName: string) {
let pairs = createdCookie.split('; ');
Expand Down

0 comments on commit 5a74fc1

Please sign in to comment.