jshttp / cookie

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

Serialize object #119

Closed export-mike closed 3 years ago

export-mike commented 3 years ago

I have added support for serializing objects to a cookie string.

If there's added detail, tests etc that are needed, let me know. thanks

export-mike commented 3 years ago

I've updated to add the missing semi colon, to get this added at the correct time. Solution isn't ideal. It would require some further refactoring, so didn't want to deviate too much right now.

interesting though with regards to document.cookie vs Set-Cookie. Set-Cookie supports multiple. Whereas document.cookie does not.

I wasn't aware of this. how annoying! I guess by not supporting that we get expected behaviour in all environments.

Not sure how we could proceed really - This is quite useful to have this in a server side context.

Options

export-mike commented 3 years ago

I'm closing this PR as I don't have the capacity to continue further changes on this.

It feels like a useful change but only in a server context. document.cookie doesn't support multiple cookies in a single string

feel free to re-open I feel this is changing the libraries behaviour in a browser vs server context. its a trivial change to merge this in, if its useful - I don't think its useful after discovering the usage in a browser with document.cookie

spaceone commented 2 years ago

I needed this change as well for a client library. Cookies have to be split via ; (not only ;) (RFC 6265 Section 4.2.1). This library doesn't contain functionality to support this but it would be achievable with that change.

cookie.serialize({foo: "bar", bar: "baz"}) == 'foo=bar; bar=baz'. instead of [cookie.serialize('foo', 'bar'), cookie.serialize('bar', 'baz')].join('; ')