tough-cookie 0.13.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +189 -127
- package/generate-pubsuffix.js +60 -26
- package/lib/cookie.js +330 -171
- package/lib/memstore.js +33 -6
- package/lib/pathMatch.js +61 -0
- package/lib/permuteDomain.js +56 -0
- package/lib/pubsuffix.js +25 -6
- package/lib/store.js +5 -1
- package/package.json +5 -8
- package/public-suffix.txt +5752 -672
- package/test/api_test.js +0 -16
- package/test/cookie_sorting_test.js +66 -0
- package/test/cookie_to_json_test.js +2 -2
- package/test/domain_and_path_test.js +2 -2
- package/test/ietf_test.js +1 -2
- package/test/jar_serialization_test.js +348 -0
- package/test/parsing_test.js +71 -133
- package/test/regression_test.js +15 -0
- package/tough-cookie-deps.tsv +0 -6
package/README.md
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
[RFC6265](
|
|
1
|
+
[RFC6265](https://tools.ietf.org/html/rfc6265) Cookies and CookieJar for Node.js
|
|
2
2
|
|
|
3
|
-
](https://travis-ci.org/goinstant/node-cookie)
|
|
3
|
+
[](https://travis-ci.org/SalesforceEng/tough-cookie)
|
|
6
4
|
|
|
7
5
|
[](https://npmjs.org/package/tough-cookie)
|
|
8
6
|

|
|
@@ -10,7 +8,7 @@
|
|
|
10
8
|
# Synopsis
|
|
11
9
|
|
|
12
10
|
``` javascript
|
|
13
|
-
var tough = require('tough-cookie');
|
|
11
|
+
var tough = require('tough-cookie');
|
|
14
12
|
var Cookie = tough.Cookie;
|
|
15
13
|
var cookie = Cookie.parse(header);
|
|
16
14
|
cookie.value = 'somethingdifferent';
|
|
@@ -30,65 +28,55 @@ It's _so_ easy!
|
|
|
30
28
|
|
|
31
29
|
`npm install tough-cookie`
|
|
32
30
|
|
|
33
|
-
Requires `punycode`, which should get installed automatically for you. Note that node.js v0.6.2+ bundles punycode by default.
|
|
34
|
-
|
|
35
31
|
Why the name? NPM modules `cookie`, `cookies` and `cookiejar` were already taken.
|
|
36
32
|
|
|
37
33
|
# API
|
|
38
34
|
|
|
39
|
-
tough
|
|
40
|
-
=====
|
|
35
|
+
## tough
|
|
41
36
|
|
|
42
37
|
Functions on the module you get from `require('tough-cookie')`. All can be used as pure functions and don't need to be "bound".
|
|
43
38
|
|
|
44
|
-
|
|
45
|
-
-----------------
|
|
39
|
+
**Note**: prior to 1.0.x, several of these functions took a `strict` parameter. This has since been removed from the API as it was no longer necessary.
|
|
46
40
|
|
|
47
|
-
|
|
41
|
+
### `parseDate(string)`
|
|
48
42
|
|
|
49
|
-
|
|
50
|
-
|
|
43
|
+
Parse a cookie date string into a `Date`. Parses according to RFC6265 Section 5.1.1, not `Date.parse()`.
|
|
44
|
+
|
|
45
|
+
### `formatDate(date)`
|
|
51
46
|
|
|
52
47
|
Format a Date into a RFC1123 string (the RFC6265-recommended format).
|
|
53
48
|
|
|
54
|
-
canonicalDomain(str)
|
|
55
|
-
--------------------
|
|
49
|
+
### `canonicalDomain(str)`
|
|
56
50
|
|
|
57
51
|
Transforms a domain-name into a canonical domain-name. The canonical domain-name is a trimmed, lowercased, stripped-of-leading-dot and optionally punycode-encoded domain-name (Section 5.1.2 of RFC6265). For the most part, this function is idempotent (can be run again on its output without ill effects).
|
|
58
52
|
|
|
59
|
-
domainMatch(str,domStr[,canonicalize=true])
|
|
60
|
-
-------------------------------------------
|
|
53
|
+
### `domainMatch(str,domStr[,canonicalize=true])`
|
|
61
54
|
|
|
62
55
|
Answers "does this real domain match the domain in a cookie?". The `str` is the "current" domain-name and the `domStr` is the "cookie" domain-name. Matches according to RFC6265 Section 5.1.3, but it helps to think of it as a "suffix match".
|
|
63
56
|
|
|
64
57
|
The `canonicalize` parameter will run the other two paramters through `canonicalDomain` or not.
|
|
65
58
|
|
|
66
|
-
defaultPath(path)
|
|
67
|
-
-----------------
|
|
59
|
+
### `defaultPath(path)`
|
|
68
60
|
|
|
69
61
|
Given a current request/response path, gives the Path apropriate for storing in a cookie. This is basically the "directory" of a "file" in the path, but is specified by Section 5.1.4 of the RFC.
|
|
70
62
|
|
|
71
63
|
The `path` parameter MUST be _only_ the pathname part of a URI (i.e. excludes the hostname, query, fragment, etc.). This is the `.pathname` property of node's `uri.parse()` output.
|
|
72
64
|
|
|
73
|
-
pathMatch(reqPath,cookiePath)
|
|
74
|
-
-----------------------------
|
|
65
|
+
### `pathMatch(reqPath,cookiePath)`
|
|
75
66
|
|
|
76
67
|
Answers "does the request-path path-match a given cookie-path?" as per RFC6265 Section 5.1.4. Returns a boolean.
|
|
77
68
|
|
|
78
69
|
This is essentially a prefix-match where `cookiePath` is a prefix of `reqPath`.
|
|
79
70
|
|
|
80
|
-
parse(header
|
|
81
|
-
----------------------------
|
|
71
|
+
### `parse(header)`
|
|
82
72
|
|
|
83
|
-
alias for `Cookie.parse(header
|
|
73
|
+
alias for `Cookie.parse(header)`
|
|
84
74
|
|
|
85
|
-
fromJSON(string)
|
|
86
|
-
----------------
|
|
75
|
+
### `fromJSON(string)`
|
|
87
76
|
|
|
88
77
|
alias for `Cookie.fromJSON(string)`
|
|
89
78
|
|
|
90
|
-
getPublicSuffix(hostname)
|
|
91
|
-
-------------------------
|
|
79
|
+
### `getPublicSuffix(hostname)`
|
|
92
80
|
|
|
93
81
|
Returns the public suffix of this hostname. The public suffix is the shortest domain-name upon which a cookie can be set. Returns `null` if the hostname cannot have cookies set for it.
|
|
94
82
|
|
|
@@ -96,34 +84,37 @@ For example: `www.example.com` and `www.subdomain.example.com` both have public
|
|
|
96
84
|
|
|
97
85
|
For further information, see http://publicsuffix.org/. This module derives its list from that site.
|
|
98
86
|
|
|
99
|
-
cookieCompare(a,b)
|
|
100
|
-
|
|
87
|
+
### `cookieCompare(a,b)`
|
|
88
|
+
|
|
89
|
+
For use with `.sort()`, sorts a list of cookies into the recommended order given in the RFC (Section 5.4 step 2). The sort algorithm is, in order of precedence:
|
|
101
90
|
|
|
102
|
-
|
|
91
|
+
* Longest `.path`
|
|
92
|
+
* oldest `.creation` (which has a 1ms precision, same as `Date`)
|
|
93
|
+
* lowest `.creationIndex` (to get beyond the 1ms precision)
|
|
103
94
|
|
|
104
95
|
``` javascript
|
|
105
96
|
var cookies = [ /* unsorted array of Cookie objects */ ];
|
|
106
97
|
cookies = cookies.sort(cookieCompare);
|
|
107
98
|
```
|
|
108
99
|
|
|
109
|
-
|
|
110
|
-
---------------------
|
|
100
|
+
**Note**: Since JavaScript's `Date` is limited to a 1ms precision, cookies within the same milisecond are entirely possible. This is especially true when using the `now` option to `.setCookie()`. The `.creationIndex` property is a per-process global counter, assigned during construction with `new Cookie()`. This preserves the spirit of the RFC sorting: older cookies go first. This works great for `MemoryCookieStore`, since `Set-Cookie` headers are parsed in order, but may not be so great for distributed systems. Sophisticated `Store`s may wish to set this to some other _logical clock_ such that if cookies A and B are created in the same millisecond, but cookie A is created before cookie B, then `A.creationIndex < B.creationIndex`. If you want to alter the global counter, which you probably _shouldn't_ do, it's stored in `Cookie.cookiesCreated`.
|
|
111
101
|
|
|
112
|
-
|
|
102
|
+
### `permuteDomain(domain)`
|
|
113
103
|
|
|
104
|
+
Generates a list of all possible domains that `domainMatch()` the parameter. May be handy for implementing cookie stores.
|
|
114
105
|
|
|
115
|
-
permutePath(path)
|
|
116
|
-
-----------------
|
|
106
|
+
### `permutePath(path)`
|
|
117
107
|
|
|
118
108
|
Generates a list of all possible paths that `pathMatch()` the parameter. May be handy for implementing cookie stores.
|
|
119
109
|
|
|
120
|
-
Cookie
|
|
121
|
-
======
|
|
122
110
|
|
|
123
|
-
Cookie
|
|
124
|
-
|
|
111
|
+
## Cookie
|
|
112
|
+
|
|
113
|
+
Exported via `tough.Cookie`.
|
|
114
|
+
|
|
115
|
+
### `Cookie.parse(header)`
|
|
125
116
|
|
|
126
|
-
Parses a single Cookie or Set-Cookie HTTP header into a `Cookie` object. Returns `undefined` if the string can't be parsed.
|
|
117
|
+
Parses a single Cookie or Set-Cookie HTTP header into a `Cookie` object. Returns `undefined` if the string can't be parsed.
|
|
127
118
|
|
|
128
119
|
Here's how to process the Set-Cookie header(s) on a node HTTP/HTTPS response:
|
|
129
120
|
|
|
@@ -134,13 +125,9 @@ else
|
|
|
134
125
|
cookies = [Cookie.parse(res.headers['set-cookie'])];
|
|
135
126
|
```
|
|
136
127
|
|
|
137
|
-
|
|
138
|
-
-----------------------
|
|
128
|
+
### Properties
|
|
139
129
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
Properties
|
|
143
|
-
==========
|
|
130
|
+
Cookie object properties:
|
|
144
131
|
|
|
145
132
|
* _key_ - string - the name or key of the cookie (default "")
|
|
146
133
|
* _value_ - string - the value of the cookie (default "")
|
|
@@ -151,70 +138,81 @@ Properties
|
|
|
151
138
|
* _secure_ - boolean - the `Secure` cookie flag
|
|
152
139
|
* _httpOnly_ - boolean - the `HttpOnly` cookie flag
|
|
153
140
|
* _extensions_ - `Array` - any unrecognized cookie attributes as strings (even if equal-signs inside)
|
|
141
|
+
* _creation_ - `Date` - when this cookie was constructed
|
|
142
|
+
* _creationIndex_ - number - set at construction, used to provide greater sort precision (please see `cookieCompare(a,b)` for a full explanation)
|
|
154
143
|
|
|
155
144
|
After a cookie has been passed through `CookieJar.setCookie()` it will have the following additional attributes:
|
|
156
145
|
|
|
157
146
|
* _hostOnly_ - boolean - is this a host-only cookie (i.e. no Domain field was set, but was instead implied)
|
|
158
147
|
* _pathIsDefault_ - boolean - if true, there was no Path field on the cookie and `defaultPath()` was used to derive one.
|
|
159
|
-
*
|
|
148
|
+
* _creation_ - `Date` - **modified** from construction to when the cookie was added to the jar
|
|
160
149
|
* _lastAccessed_ - `Date` - last time the cookie got accessed. Will affect cookie cleaning once implemented. Using `cookiejar.getCookies(...)` will update this attribute.
|
|
161
150
|
|
|
162
|
-
|
|
163
|
-
------------
|
|
151
|
+
### `Cookie([{properties}])`
|
|
164
152
|
|
|
165
|
-
Receives an options object that can contain any Cookie properties, uses the default for unspecified properties.
|
|
153
|
+
Receives an options object that can contain any of the above Cookie properties, uses the default for unspecified properties.
|
|
166
154
|
|
|
167
|
-
|
|
168
|
-
-----------
|
|
155
|
+
### `.toString()`
|
|
169
156
|
|
|
170
157
|
encode to a Set-Cookie header value. The Expires cookie field is set using `formatDate()`, but is omitted entirely if `.expires` is `Infinity`.
|
|
171
158
|
|
|
172
|
-
|
|
173
|
-
---------------
|
|
159
|
+
### `.cookieString()`
|
|
174
160
|
|
|
175
161
|
encode to a Cookie header value (i.e. the `.key` and `.value` properties joined with '=').
|
|
176
162
|
|
|
177
|
-
|
|
178
|
-
-------------------
|
|
163
|
+
### `.setExpires(String)`
|
|
179
164
|
|
|
180
165
|
sets the expiry based on a date-string passed through `parseDate()`. If parseDate returns `null` (i.e. can't parse this date string), `.expires` is set to `"Infinity"` (a string) is set.
|
|
181
166
|
|
|
182
|
-
|
|
183
|
-
-------------------
|
|
167
|
+
### `.setMaxAge(number)`
|
|
184
168
|
|
|
185
169
|
sets the maxAge in seconds. Coerces `-Infinity` to `"-Infinity"` and `Infinity` to `"Infinity"` so it JSON serializes correctly.
|
|
186
170
|
|
|
187
|
-
|
|
188
|
-
-----------------------------
|
|
171
|
+
### `.expiryTime([now=Date.now()])`
|
|
189
172
|
|
|
190
|
-
|
|
191
|
-
-----------------------------
|
|
173
|
+
### `.expiryDate([now=Date.now()])`
|
|
192
174
|
|
|
193
175
|
expiryTime() Computes the absolute unix-epoch milliseconds that this cookie expires. expiryDate() works similarly, except it returns a `Date` object. Note that in both cases the `now` parameter should be milliseconds.
|
|
194
176
|
|
|
195
|
-
Max-Age takes precedence over Expires (as per the RFC). The `.
|
|
177
|
+
Max-Age takes precedence over Expires (as per the RFC). The `.creation` attribute -- or, by default, the `now` paramter -- is used to offset the `.maxAge` attribute.
|
|
196
178
|
|
|
197
179
|
If Expires (`.expires`) is set, that's returned.
|
|
198
180
|
|
|
199
181
|
Otherwise, `expiryTime()` returns `Infinity` and `expiryDate()` returns a `Date` object for "Tue, 19 Jan 2038 03:14:07 GMT" (latest date that can be expressed by a 32-bit `time_t`; the common limit for most user-agents).
|
|
200
182
|
|
|
201
|
-
|
|
202
|
-
---------
|
|
183
|
+
### `.TTL([now=Date.now()])`
|
|
203
184
|
|
|
204
185
|
compute the TTL relative to `now` (milliseconds). The same precedence rules as for `expiryTime`/`expiryDate` apply.
|
|
205
186
|
|
|
206
187
|
The "number" `Infinity` is returned for cookies without an explicit expiry and `0` is returned if the cookie is expired. Otherwise a time-to-live in milliseconds is returned.
|
|
207
188
|
|
|
208
|
-
|
|
209
|
-
---------------------
|
|
189
|
+
### `.canonicalizedDoman()`
|
|
210
190
|
|
|
211
|
-
|
|
212
|
-
----------
|
|
191
|
+
### `.cdomain()`
|
|
213
192
|
|
|
214
193
|
return the canonicalized `.domain` field. This is lower-cased and punycode (RFC3490) encoded if the domain has any non-ASCII characters.
|
|
215
194
|
|
|
216
|
-
|
|
217
|
-
|
|
195
|
+
### `.toJSON()`
|
|
196
|
+
|
|
197
|
+
For convenience in using `JSON.serialize(cookie)`. Returns a plain-old `Object` that can be JSON-serialized.
|
|
198
|
+
|
|
199
|
+
Any `Date` properties (i.e., `.expires`, `.creation`, and `.lastAccessed`) are exported in ISO format (`.toISOString()`).
|
|
200
|
+
|
|
201
|
+
**NOTE**: Custom `Cookie` properties will be discarded. In tough-cookie 1.x, since there was no `.toJSON` method explicitly defined, all enumerable properties were captured. If you want a property to be serialized, add the property name to the `Cookie.serializableProperties` Array.
|
|
202
|
+
|
|
203
|
+
### `Cookie.fromJSON(strOrObj)`
|
|
204
|
+
|
|
205
|
+
Does the reverse of `cookie.toJSON()`. If passed a string, will `JSON.parse()` that first.
|
|
206
|
+
|
|
207
|
+
Any `Date` properties (i.e., `.expires`, `.creation`, and `.lastAccessed`) are parsed via `Date.parse()`, not the tough-cookie `parseDate`, since it's JavaScript/JSON-y timestamps being handled at this layer.
|
|
208
|
+
|
|
209
|
+
Returns `null` upon JSON parsing error.
|
|
210
|
+
|
|
211
|
+
### `.clone()`
|
|
212
|
+
|
|
213
|
+
Does a deep clone of this cookie, exactly implemented as `Cookie.fromJSON(cookie.toJSON())`.
|
|
214
|
+
|
|
215
|
+
### `.validate()`
|
|
218
216
|
|
|
219
217
|
Status: *IN PROGRESS*. Works for a few things, but is by no means comprehensive.
|
|
220
218
|
|
|
@@ -228,49 +226,41 @@ if (cookie.validate() === true) {
|
|
|
228
226
|
}
|
|
229
227
|
```
|
|
230
228
|
|
|
231
|
-
CookieJar
|
|
232
|
-
=========
|
|
233
229
|
|
|
234
|
-
|
|
235
|
-
|
|
230
|
+
## CookieJar
|
|
231
|
+
|
|
232
|
+
Exported via `tough.CookieJar`.
|
|
233
|
+
|
|
234
|
+
### `CookieJar([store],[rejectPublicSuffixes])`
|
|
236
235
|
|
|
237
236
|
Simply use `new CookieJar()`. If you'd like to use a custom store, pass that to the constructor otherwise a `MemoryCookieStore` will be created and used.
|
|
238
237
|
|
|
238
|
+
### Properties
|
|
239
239
|
|
|
240
|
-
|
|
241
|
-
----------
|
|
240
|
+
CookieJar object properties:
|
|
242
241
|
|
|
243
242
|
* _rejectPublicSuffixes_ - boolean - reject cookies with domains like "com" and "co.uk" (default: `true`)
|
|
244
243
|
|
|
245
244
|
Since eventually this module would like to support database/remote/etc. CookieJars, continuation passing style is used for CookieJar methods.
|
|
246
245
|
|
|
247
|
-
|
|
248
|
-
-------------------------------------------------------------------
|
|
246
|
+
### `.setCookie(cookieOrString, currentUrl, [{options},] cb(err,cookie))`
|
|
249
247
|
|
|
250
|
-
Attempt to set the cookie in the cookie jar. If the operation fails, an error will be given to the callback `cb`, otherwise the cookie is passed through. The cookie will have updated `.
|
|
248
|
+
Attempt to set the cookie in the cookie jar. If the operation fails, an error will be given to the callback `cb`, otherwise the cookie is passed through. The cookie will have updated `.creation`, `.lastAccessed` and `.hostOnly` properties.
|
|
251
249
|
|
|
252
250
|
The `options` object can be omitted and can have the following properties:
|
|
253
251
|
|
|
254
252
|
* _http_ - boolean - default `true` - indicates if this is an HTTP or non-HTTP API. Affects HttpOnly cookies.
|
|
255
253
|
* _secure_ - boolean - autodetect from url - indicates if this is a "Secure" API. If the currentUrl starts with `https:` or `wss:` then this is defaulted to `true`, otherwise `false`.
|
|
256
254
|
* _now_ - Date - default `new Date()` - what to use for the creation/access time of cookies
|
|
257
|
-
*
|
|
258
|
-
* _ignoreError_ - boolean - default `false` - silently ignore things like parse errors and invalid domains. CookieStore errors aren't ignored by this option.
|
|
255
|
+
* _ignoreError_ - boolean - default `false` - silently ignore things like parse errors and invalid domains. `Store` errors aren't ignored by this option.
|
|
259
256
|
|
|
260
257
|
As per the RFC, the `.hostOnly` property is set if there was no "Domain=" parameter in the cookie string (or `.domain` was null on the Cookie object). The `.domain` property is set to the fully-qualified hostname of `currentUrl` in this case. Matching this cookie requires an exact hostname match (not a `domainMatch` as per usual).
|
|
261
258
|
|
|
262
|
-
|
|
263
|
-
-------------------------------------------------------
|
|
259
|
+
### `.setCookieSync(cookieOrString, currentUrl, [{options}])`
|
|
264
260
|
|
|
265
261
|
Synchronous version of `setCookie`; only works with synchronous stores (e.g. the default `MemoryCookieStore`).
|
|
266
262
|
|
|
267
|
-
|
|
268
|
-
-------------------------------------------------
|
|
269
|
-
|
|
270
|
-
__REMOVED__ removed in lieu of the CookieStore API below
|
|
271
|
-
|
|
272
|
-
.getCookies(currentUrl, [{options},] cb(err,cookies))
|
|
273
|
-
-----------------------------------------------------
|
|
263
|
+
### `.getCookies(currentUrl, [{options},] cb(err,cookies))`
|
|
274
264
|
|
|
275
265
|
Retrieve the list of cookies that can be sent in a Cookie header for the current url.
|
|
276
266
|
|
|
@@ -282,59 +272,99 @@ The `options` object can be omitted and can have the following properties:
|
|
|
282
272
|
* _secure_ - boolean - autodetect from url - indicates if this is a "Secure" API. If the currentUrl starts with `https:` or `wss:` then this is defaulted to `true`, otherwise `false`.
|
|
283
273
|
* _now_ - Date - default `new Date()` - what to use for the creation/access time of cookies
|
|
284
274
|
* _expire_ - boolean - default `true` - perform expiry-time checking of cookies and asynchronously remove expired cookies from the store. Using `false` will return expired cookies and **not** remove them from the store (which is useful for replaying Set-Cookie headers, potentially).
|
|
285
|
-
* _allPaths_ - boolean - default `false` - if `true`, do not scope cookies by path. The default uses RFC-compliant path scoping. **Note**: may not be supported by the
|
|
275
|
+
* _allPaths_ - boolean - default `false` - if `true`, do not scope cookies by path. The default uses RFC-compliant path scoping. **Note**: may not be supported by the underlying store (the default `MemoryCookieStore` supports it).
|
|
286
276
|
|
|
287
277
|
The `.lastAccessed` property of the returned cookies will have been updated.
|
|
288
278
|
|
|
289
|
-
|
|
290
|
-
----------------------------------------
|
|
279
|
+
### `.getCookiesSync(currentUrl, [{options}])`
|
|
291
280
|
|
|
292
281
|
Synchronous version of `getCookies`; only works with synchronous stores (e.g. the default `MemoryCookieStore`).
|
|
293
282
|
|
|
294
|
-
|
|
295
|
-
---------------------
|
|
283
|
+
### `.getCookieString(...)`
|
|
296
284
|
|
|
297
285
|
Accepts the same options as `.getCookies()` but passes a string suitable for a Cookie header rather than an array to the callback. Simply maps the `Cookie` array via `.cookieString()`.
|
|
298
286
|
|
|
299
|
-
|
|
300
|
-
-------------------------
|
|
287
|
+
### `.getCookieStringSync(...)`
|
|
301
288
|
|
|
302
289
|
Synchronous version of `getCookieString`; only works with synchronous stores (e.g. the default `MemoryCookieStore`).
|
|
303
290
|
|
|
304
|
-
|
|
305
|
-
-------------------------
|
|
291
|
+
### `.getSetCookieStrings(...)`
|
|
306
292
|
|
|
307
293
|
Returns an array of strings suitable for **Set-Cookie** headers. Accepts the same options as `.getCookies()`. Simply maps the cookie array via `.toString()`.
|
|
308
294
|
|
|
309
|
-
|
|
310
|
-
-----------------------------
|
|
295
|
+
### `.getSetCookieStringsSync(...)`
|
|
311
296
|
|
|
312
297
|
Synchronous version of `getSetCookieStrings`; only works with synchronous stores (e.g. the default `MemoryCookieStore`).
|
|
313
298
|
|
|
314
|
-
|
|
315
|
-
|
|
299
|
+
### `.serialize(cb(err,serializedObject))`
|
|
300
|
+
|
|
301
|
+
Serialize the Jar if the underlying store supports `.getAllCookies`.
|
|
302
|
+
|
|
303
|
+
**NOTE**: Custom `Cookie` properties will be discarded. If you want a property to be serialized, add the property name to the `Cookie.serializableProperties` Array.
|
|
304
|
+
|
|
305
|
+
See [Serialization Format].
|
|
306
|
+
|
|
307
|
+
### `.serializeSync()`
|
|
308
|
+
|
|
309
|
+
Sync version of .serialize
|
|
310
|
+
|
|
311
|
+
### `.toJSON()`
|
|
312
|
+
|
|
313
|
+
Alias of .serializeSync() for the convenience of `JSON.stringify(cookiejar)`.
|
|
316
314
|
|
|
317
|
-
|
|
315
|
+
### `CookieJar.deserialize(serialized, [store], cb(err,object))`
|
|
318
316
|
|
|
319
|
-
|
|
317
|
+
A new Jar is created and the serialized Cookies are added to the underlying store. Each `Cookie` is added via `store.putCookie` in the order in which they appear in the serialization.
|
|
318
|
+
|
|
319
|
+
The `store` argument is optional, but should be an instance of `Store`. By default, a new instance of `MemoryCookieStore` is created.
|
|
320
|
+
|
|
321
|
+
As a convenience, if `serialized` is a string, it is passed through `JSON.parse` first. If that throws an error, this is passed to the callback.
|
|
322
|
+
|
|
323
|
+
### `CookieJar.deserializeSync(serialized, [store])`
|
|
324
|
+
|
|
325
|
+
Sync version of `.deserialize`. _Note_ that the `store` must be synchronous for this to work.
|
|
326
|
+
|
|
327
|
+
### `CookieJar.fromJSON(string)`
|
|
328
|
+
|
|
329
|
+
Alias of `.deserializeSync` to provide consistency with `Cookie.fromJSON()`.
|
|
330
|
+
|
|
331
|
+
### `.clone([store,]cb(err,newJar))`
|
|
332
|
+
|
|
333
|
+
Produces a deep clone of this jar. Modifications to the original won't affect the clone, and vice versa.
|
|
334
|
+
|
|
335
|
+
The `store` argument is optional, but should be an instance of `Store`. By default, a new instance of `MemoryCookieStore` is created. Transferring between store types is supported so long as the source implements `.getAllCookies()` and the destination implements `.putCookie()`.
|
|
336
|
+
|
|
337
|
+
### `.cloneSync([store])`
|
|
338
|
+
|
|
339
|
+
Synchronous version of `.clone`, returning a new `CookieJar` instance.
|
|
340
|
+
|
|
341
|
+
The `store` argument is optional, but must be a _synchronous_ `Store` instance if specified. If not passed, a new instance of `MemoryCookieStore` is used.
|
|
342
|
+
|
|
343
|
+
The _source_ and _destination_ must both be synchronous `Store`s. If one or both stores are asynchronous, use `.clone` instead. Recall that `MemoryCookieStore` supports both synchronous and asynchronous API calls.
|
|
344
|
+
|
|
345
|
+
## Store
|
|
346
|
+
|
|
347
|
+
Base class for CookieJar stores. Available as `tough.Store`.
|
|
348
|
+
|
|
349
|
+
## Store API
|
|
320
350
|
|
|
321
351
|
The storage model for each `CookieJar` instance can be replaced with a custom implementation. The default is `MemoryCookieStore` which can be found in the `lib/memstore.js` file. The API uses continuation-passing-style to allow for asynchronous stores.
|
|
322
352
|
|
|
323
|
-
Stores should inherit from the base `Store` class, which is available as `require('tough-cookie').Store`.
|
|
353
|
+
Stores should inherit from the base `Store` class, which is available as `require('tough-cookie').Store`.
|
|
354
|
+
|
|
355
|
+
Stores are asynchronous by default, but if `store.synchronous` is set to `true`, then the `*Sync` methods on the of the containing `CookieJar` can be used (however, the continuation-passing style
|
|
324
356
|
|
|
325
357
|
All `domain` parameters will have been normalized before calling.
|
|
326
358
|
|
|
327
359
|
The Cookie store must have all of the following methods.
|
|
328
360
|
|
|
329
|
-
store.findCookie(domain, path, key, cb(err,cookie))
|
|
330
|
-
---------------------------------------------------
|
|
361
|
+
### `store.findCookie(domain, path, key, cb(err,cookie))`
|
|
331
362
|
|
|
332
363
|
Retrieve a cookie with the given domain, path and key (a.k.a. name). The RFC maintains that exactly one of these cookies should exist in a store. If the store is using versioning, this means that the latest/newest such cookie should be returned.
|
|
333
364
|
|
|
334
365
|
Callback takes an error and the resulting `Cookie` object. If no cookie is found then `null` MUST be passed instead (i.e. not an error).
|
|
335
366
|
|
|
336
|
-
store.findCookies(domain, path, cb(err,cookies))
|
|
337
|
-
------------------------------------------------
|
|
367
|
+
### `store.findCookies(domain, path, cb(err,cookies))`
|
|
338
368
|
|
|
339
369
|
Locates cookies matching the given domain and path. This is most often called in the context of `cookiejar.getCookies()` above.
|
|
340
370
|
|
|
@@ -344,8 +374,7 @@ The resulting list will be checked for applicability to the current request acco
|
|
|
344
374
|
|
|
345
375
|
As of version 0.9.12, the `allPaths` option to `cookiejar.getCookies()` above will cause the path here to be `null`. If the path is `null`, path-matching MUST NOT be performed (i.e. domain-matching only).
|
|
346
376
|
|
|
347
|
-
store.putCookie(cookie, cb(err))
|
|
348
|
-
--------------------------------
|
|
377
|
+
### `store.putCookie(cookie, cb(err))`
|
|
349
378
|
|
|
350
379
|
Adds a new cookie to the store. The implementation SHOULD replace any existing cookie with the same `.domain`, `.path`, and `.key` properties -- depending on the nature of the implementation, it's possible that between the call to `fetchCookie` and `putCookie` that a duplicate `putCookie` can occur.
|
|
351
380
|
|
|
@@ -353,12 +382,11 @@ The `cookie` object MUST NOT be modified; the caller will have already updated t
|
|
|
353
382
|
|
|
354
383
|
Pass an error if the cookie cannot be stored.
|
|
355
384
|
|
|
356
|
-
store.updateCookie(oldCookie, newCookie, cb(err))
|
|
357
|
-
-------------------------------------------------
|
|
385
|
+
### `store.updateCookie(oldCookie, newCookie, cb(err))`
|
|
358
386
|
|
|
359
387
|
Update an existing cookie. The implementation MUST update the `.value` for a cookie with the same `domain`, `.path` and `.key`. The implementation SHOULD check that the old value in the store is equivalent to `oldCookie` - how the conflict is resolved is up to the store.
|
|
360
388
|
|
|
361
|
-
The `.lastAccessed` property will always be different between the two objects and `.
|
|
389
|
+
The `.lastAccessed` property will always be different between the two objects (to the precision possible via JavaScript's clock). Both `.creation` and `.creationIndex` are guaranteed to be the same. Stores MAY ignore or defer the `.lastAccessed` change at the cost of affecting how cookies are selected for automatic deletion (e.g., least-recently-used, which is up to the store to implement).
|
|
362
390
|
|
|
363
391
|
Stores may wish to optimize changing the `.value` of the cookie in the store versus storing a new cookie. If the implementation doesn't define this method a stub that calls `putCookie(newCookie,cb)` will be added to the store object.
|
|
364
392
|
|
|
@@ -366,25 +394,59 @@ The `newCookie` and `oldCookie` objects MUST NOT be modified.
|
|
|
366
394
|
|
|
367
395
|
Pass an error if the newCookie cannot be stored.
|
|
368
396
|
|
|
369
|
-
store.removeCookie(domain, path, key, cb(err))
|
|
370
|
-
----------------------------------------------
|
|
397
|
+
### `store.removeCookie(domain, path, key, cb(err))`
|
|
371
398
|
|
|
372
399
|
Remove a cookie from the store (see notes on `findCookie` about the uniqueness constraint).
|
|
373
400
|
|
|
374
401
|
The implementation MUST NOT pass an error if the cookie doesn't exist; only pass an error due to the failure to remove an existing cookie.
|
|
375
402
|
|
|
376
|
-
store.removeCookies(domain, path, cb(err))
|
|
377
|
-
------------------------------------------
|
|
403
|
+
### `store.removeCookies(domain, path, cb(err))`
|
|
378
404
|
|
|
379
|
-
Removes matching cookies from the store. The `path`
|
|
405
|
+
Removes matching cookies from the store. The `path` parameter is optional, and if missing means all paths in a domain should be removed.
|
|
380
406
|
|
|
381
407
|
Pass an error ONLY if removing any existing cookies failed.
|
|
382
408
|
|
|
383
|
-
|
|
409
|
+
### `store.getAllCookies(cb(err, cookies))`
|
|
410
|
+
|
|
411
|
+
Produces an `Array` of all cookies during `jar.serialize()`. The items in the array can be true `Cookie` objects or generic `Object`s with the [Serialization Format] data structure.
|
|
412
|
+
|
|
413
|
+
Cookies SHOULD be returned in creation order to preserve sorting via `compareCookies()`. For reference, `MemoryCookieStore` will sort by `.creationIndex` since it uses true `Cookie` objects internally. If you don't return the cookies in creation order, they'll still be sorted by creation time, but this only has a precision of 1ms. See `compareCookies` for more detail.
|
|
414
|
+
|
|
415
|
+
Pass an error if retrieval fails.
|
|
416
|
+
|
|
417
|
+
## MemoryCookieStore
|
|
418
|
+
|
|
419
|
+
Inherits from `Store`.
|
|
384
420
|
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
421
|
+
A just-in-memory CookieJar synchronous store implementation, used by default. Despite being a synchronous implementation, it's usable with both the synchronous and asynchronous forms of the `CookieJar` API.
|
|
422
|
+
|
|
423
|
+
# Serialization Format
|
|
424
|
+
|
|
425
|
+
**NOTE**: if you want to have custom `Cookie` properties serialized, add the property name to `Cookie.serializableProperties`.
|
|
426
|
+
|
|
427
|
+
```js
|
|
428
|
+
{
|
|
429
|
+
// The version of tough-cookie that serialized this jar.
|
|
430
|
+
version: 'tough-cookie@1.x.y',
|
|
431
|
+
|
|
432
|
+
// add the store type, to make humans happy:
|
|
433
|
+
storeType: 'MemoryCookieStore',
|
|
434
|
+
|
|
435
|
+
// CookieJar configuration:
|
|
436
|
+
rejectPublicSuffixes: true,
|
|
437
|
+
// ... future items go here
|
|
438
|
+
|
|
439
|
+
// Gets filled from jar.store.getAllCookies():
|
|
440
|
+
cookies: [
|
|
441
|
+
{
|
|
442
|
+
key: 'string',
|
|
443
|
+
value: 'string',
|
|
444
|
+
// ...
|
|
445
|
+
/* other Cookie.serializableProperties go here */
|
|
446
|
+
}
|
|
447
|
+
]
|
|
448
|
+
}
|
|
449
|
+
```
|
|
388
450
|
|
|
389
451
|
# Copyright and License
|
|
390
452
|
|