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

Serializable #42

Merged
merged 10 commits into from
Jun 10, 2015
Merged

Serializable #42

merged 10 commits into from
Jun 10, 2015

Conversation

stash-sfdc
Copy link
Contributor

WIP until documentation is done. The branch name is misleading; I intend for this to be for all store types, not just MemoryCookieStore (which is what I started out with).


Add a getAllCookies interface to CookieStore so that stores can export all cookies. In the case of stores where this is intractable or undesirable, they can elect to not implement getAllCookies.

The serialized format is vaguely:

  var serialized = {
    // The version of tough-cookie that serialized this jar. Generally a good
    // practice since future versions can make data import decisions based on
    // known past behavior. When/if this matters, use `semver`.
    version: 'tough-cookie@'+VERSION,

    // add the store type, to make humans happy:
    storeType: type,

    // CookieJar configuration:
    rejectPublicSuffixes: !!this.rejectPublicSuffixes,

    // this gets filled from getAllCookies after calling `toJSON` on them:
    cookies: []
  };

Importing the cookies repeatedly calls store.putCookie(), so this should work for all CookieStore implementations, in theory. Cookie.fromJSON is called on each item in the cookies Array before being passed to .putCookie().

@stash-sfdc
Copy link
Contributor Author

@lalitkapoor @inikulin @apsavin initial thoughts? Did I miss some way this might break existing clients? Can someone spot-check this with request and/or domjs?

I'd like to polish this up with documentation, etc. if yall are good with it.

"looks good": function(str) {
assert.match(str, /"maxAge":null/);
"absent": function(str) {
assert.match(str, /(?!"maxAge":null)/); // NB: negative RegExp
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this since it === what's in the prototype.

if (prop === 'expires' ||
prop === 'creation' ||
prop === 'lastAccessed')
{
c[prop] = obj[prop] == "Infinity" ? "Infinity" : new Date(obj[prop]);
if (obj[prop] === null) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this check because new Date was failing, obviously. ;)

@lalitkapoor
Copy link
Contributor

@stash-sfdc looks great! - left one comment above

@lalitkapoor
Copy link
Contributor

will give it a whirl with request

@lalitkapoor
Copy link
Contributor

all request tests pass. Clone the memory store jar is a frequent request in request - this is awesome. 👍

@inikulin
Copy link
Contributor

LGTM 👍

@stash-sfdc
Copy link
Contributor Author

@apsavin good point. Old habits die hard so I really appreciate you pointing these out!

This replaces the private `_creationRuntimeIdx` and `_initialCreationTime`
properties with a public `.creationIndex` property.

Cookies now get a creation time at construction, but this is replaced
when `jar.setCookie` according to either the wall time or the `now`
option to that method.

Prior to this change, cookies were returned by MemoryCookieStore's
getAllCookies in a strange order (sorted by domain then path then key),
which breaks `cookieCompare` for cookies with creation times in the same
millisecond.

Sorting by `.creationIndex` and then removing that property will allow
the deserializer to assign new process-specific `.creationIndex` values.
But, if the underlying `Store` wants to set that upon retrieval, then
they can do that too.

By making the property non-enumerable, `assert.deepEqual` can work as
expected ;)

After a cookie has been passed through `CookieJar.setCookie()` it will have the following additional attributes:

* _hostOnly_ - boolean - is this a host-only cookie (i.e. no Domain field was set, but was instead implied)
* _pathIsDefault_ - boolean - if true, there was no Path field on the cookie and `defaultPath()` was used to derive one.
* _created_ - `Date` - when this cookie was added to the jar
* _creation_ - `Date` - **modified** from construction to when the cookie was added to the jar
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack, this was always creation, never created :(

@stash-sfdc
Copy link
Contributor Author

OK, revisions to retain creation sort order (see a5dfae6) and documentation are in.

Is anyone going to get mad that custom properties are being dropped in .toJSON()? If so, I guess semver says this should go to 2.0.0 since it's an API-breaking change.

@lalitkapoor
Copy link
Contributor

I think we should follow semver, and since it's breaking let's do the major bump, followed by a release party :P

@apsavin
Copy link
Contributor

apsavin commented May 27, 2015

Why somebody will want to use custom properties?

@inikulin
Copy link
Contributor

Ok, I think we can merge now (but we need to bump version to 2.0.0 first). @stash Something else is left to do?

@stash-sfdc
Copy link
Contributor Author

Nothing left, I think (thanks for the nudge). I'll re-review and tag 2.0 if it's good.

stash-sfdc added a commit that referenced this pull request Jun 10, 2015
Serializable! NB: Cookie toJSON API-breaking change
@stash-sfdc stash-sfdc merged commit 2384e69 into master Jun 10, 2015
@stash-sfdc
Copy link
Contributor Author

Alright, party like it's Fri, 31 Dec 1999 23:59:59 GMT ;)

https://twitter.com/jstash/status/608759537936367617

@inikulin
Copy link
Contributor

🎉

@lalitkapoor
Copy link
Contributor

Image of Cookie Monster Party

@stash-sfdc
Copy link
Contributor Author

haha, brilliant! thanks @lalitkapoor :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants