jshttp / cookie

HTTP server cookie parsing and serialization
MIT License
1.36k stars 159 forks source link

fix: expires date validation for other date formats #155

Closed robinbraemer closed 1 year ago

robinbraemer commented 1 year ago

Problem: When I was working with AuthJS, your 'cookie' module currently throws an error when the 'expires' option is provided in a non-standard date format, such as "2023-11-26T09:50:03.088Z." This error limits the flexibility of date handling in the module.

I printed what opt.expires would be, and the second 'it me' is the one AuthJS provides, to my understanding:

[next] it me Fri Oct 27 2023 12:05:00 GMT+0200 (GMT+02:00)
[next] it me 2023-11-26T09:50:03.088Z
[next] - error node_modules/.pnpm/cookie@0.5.0/node_modules/cookie/index.js (161:12) @ serialize
[next] - error option expires is invalid

Solution: This PR updates the code to handle a broader range of date formats for the 'expires' option by using the JavaScript Date constructor for parsing.

Testing: I tested that the changes support previous edge cases and my case format that previously caused an error.

Additional Notes: This change doesn't impact other functionality and maintains compatibility with existing code while enhancing date format flexibility.

Your feedback on this fix is appreciated. Thank you for reviewing the PR.

dougwilson commented 1 year ago

You need to pass a Date object for the expires option, not a string. Parsing a string into a Date object is an operation you need to do before calling this module. This is not a bug and accepting a string and trying to parse it is not really something that will be added here, I'm sorry. You need to parse into a Date object according to your needs before calling this module.