From 5a74fc11a1c71b95750139a17e1618cc5b0b3845 Mon Sep 17 00:00:00 2001 From: akh9804 Date: Sat, 20 Apr 2024 00:39:48 +0900 Subject: [PATCH] Support expires values larger than the maximum date (#11) --- src/es-cookie.ts | 5 ++--- test/tests.ts | 6 ++++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/es-cookie.ts b/src/es-cookie.ts index 0fdb20f..1a99bd4 100644 --- a/src/es-cookie.ts +++ b/src/es-cookie.ts @@ -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() : '') diff --git a/test/tests.ts b/test/tests.ts index 2c1d7b1..a039181 100644 --- a/test/tests.ts +++ b/test/tests.ts @@ -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('; ');