tough-cookie 1.2.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 +180 -116
- package/lib/cookie.js +271 -50
- package/lib/memstore.js +31 -3
- package/lib/store.js +5 -1
- package/package.json +1 -1
- package/public-suffix.txt +302 -72
- package/test/cookie_sorting_test.js +66 -0
- package/test/cookie_to_json_test.js +2 -2
- package/test/ietf_test.js +1 -2
- package/test/jar_serialization_test.js +348 -0
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
# Synopsis
|
|
9
9
|
|
|
10
10
|
``` javascript
|
|
11
|
-
var tough = require('tough-cookie');
|
|
11
|
+
var tough = require('tough-cookie');
|
|
12
12
|
var Cookie = tough.Cookie;
|
|
13
13
|
var cookie = Cookie.parse(header);
|
|
14
14
|
cookie.value = 'somethingdifferent';
|
|
@@ -32,61 +32,51 @@ Why the name? NPM modules `cookie`, `cookies` and `cookiejar` were already take
|
|
|
32
32
|
|
|
33
33
|
# API
|
|
34
34
|
|
|
35
|
-
tough
|
|
36
|
-
=====
|
|
35
|
+
## tough
|
|
37
36
|
|
|
38
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".
|
|
39
38
|
|
|
40
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.
|
|
41
40
|
|
|
42
|
-
parseDate(string)
|
|
43
|
-
-----------------
|
|
41
|
+
### `parseDate(string)`
|
|
44
42
|
|
|
45
43
|
Parse a cookie date string into a `Date`. Parses according to RFC6265 Section 5.1.1, not `Date.parse()`.
|
|
46
44
|
|
|
47
|
-
formatDate(date)
|
|
48
|
-
----------------
|
|
45
|
+
### `formatDate(date)`
|
|
49
46
|
|
|
50
47
|
Format a Date into a RFC1123 string (the RFC6265-recommended format).
|
|
51
48
|
|
|
52
|
-
canonicalDomain(str)
|
|
53
|
-
--------------------
|
|
49
|
+
### `canonicalDomain(str)`
|
|
54
50
|
|
|
55
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).
|
|
56
52
|
|
|
57
|
-
domainMatch(str,domStr[,canonicalize=true])
|
|
58
|
-
-------------------------------------------
|
|
53
|
+
### `domainMatch(str,domStr[,canonicalize=true])`
|
|
59
54
|
|
|
60
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".
|
|
61
56
|
|
|
62
57
|
The `canonicalize` parameter will run the other two paramters through `canonicalDomain` or not.
|
|
63
58
|
|
|
64
|
-
defaultPath(path)
|
|
65
|
-
-----------------
|
|
59
|
+
### `defaultPath(path)`
|
|
66
60
|
|
|
67
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.
|
|
68
62
|
|
|
69
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.
|
|
70
64
|
|
|
71
|
-
pathMatch(reqPath,cookiePath)
|
|
72
|
-
-----------------------------
|
|
65
|
+
### `pathMatch(reqPath,cookiePath)`
|
|
73
66
|
|
|
74
67
|
Answers "does the request-path path-match a given cookie-path?" as per RFC6265 Section 5.1.4. Returns a boolean.
|
|
75
68
|
|
|
76
69
|
This is essentially a prefix-match where `cookiePath` is a prefix of `reqPath`.
|
|
77
70
|
|
|
78
|
-
parse(header)
|
|
79
|
-
----------------------------
|
|
71
|
+
### `parse(header)`
|
|
80
72
|
|
|
81
73
|
alias for `Cookie.parse(header)`
|
|
82
74
|
|
|
83
|
-
fromJSON(string)
|
|
84
|
-
----------------
|
|
75
|
+
### `fromJSON(string)`
|
|
85
76
|
|
|
86
77
|
alias for `Cookie.fromJSON(string)`
|
|
87
78
|
|
|
88
|
-
getPublicSuffix(hostname)
|
|
89
|
-
-------------------------
|
|
79
|
+
### `getPublicSuffix(hostname)`
|
|
90
80
|
|
|
91
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.
|
|
92
82
|
|
|
@@ -94,32 +84,35 @@ For example: `www.example.com` and `www.subdomain.example.com` both have public
|
|
|
94
84
|
|
|
95
85
|
For further information, see http://publicsuffix.org/. This module derives its list from that site.
|
|
96
86
|
|
|
97
|
-
cookieCompare(a,b)
|
|
98
|
-
------------------
|
|
87
|
+
### `cookieCompare(a,b)`
|
|
99
88
|
|
|
100
|
-
For use with `.sort()`, sorts a list of cookies into the recommended order given in the RFC (Section 5.4 step 2).
|
|
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:
|
|
90
|
+
|
|
91
|
+
* Longest `.path`
|
|
92
|
+
* oldest `.creation` (which has a 1ms precision, same as `Date`)
|
|
93
|
+
* lowest `.creationIndex` (to get beyond the 1ms precision)
|
|
101
94
|
|
|
102
95
|
``` javascript
|
|
103
96
|
var cookies = [ /* unsorted array of Cookie objects */ ];
|
|
104
97
|
cookies = cookies.sort(cookieCompare);
|
|
105
98
|
```
|
|
106
99
|
|
|
107
|
-
|
|
108
|
-
---------------------
|
|
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`.
|
|
109
101
|
|
|
110
|
-
|
|
102
|
+
### `permuteDomain(domain)`
|
|
111
103
|
|
|
104
|
+
Generates a list of all possible domains that `domainMatch()` the parameter. May be handy for implementing cookie stores.
|
|
112
105
|
|
|
113
|
-
permutePath(path)
|
|
114
|
-
-----------------
|
|
106
|
+
### `permutePath(path)`
|
|
115
107
|
|
|
116
108
|
Generates a list of all possible paths that `pathMatch()` the parameter. May be handy for implementing cookie stores.
|
|
117
109
|
|
|
118
|
-
Cookie
|
|
119
|
-
======
|
|
120
110
|
|
|
121
|
-
Cookie
|
|
122
|
-
|
|
111
|
+
## Cookie
|
|
112
|
+
|
|
113
|
+
Exported via `tough.Cookie`.
|
|
114
|
+
|
|
115
|
+
### `Cookie.parse(header)`
|
|
123
116
|
|
|
124
117
|
Parses a single Cookie or Set-Cookie HTTP header into a `Cookie` object. Returns `undefined` if the string can't be parsed.
|
|
125
118
|
|
|
@@ -132,13 +125,9 @@ else
|
|
|
132
125
|
cookies = [Cookie.parse(res.headers['set-cookie'])];
|
|
133
126
|
```
|
|
134
127
|
|
|
135
|
-
|
|
136
|
-
-----------------------
|
|
137
|
-
|
|
138
|
-
Convert a JSON string to a `Cookie` object. Does a `JSON.parse()` and converts the `.created`, `.lastAccessed` and `.expires` properties into `Date` objects.
|
|
128
|
+
### Properties
|
|
139
129
|
|
|
140
|
-
|
|
141
|
-
==========
|
|
130
|
+
Cookie object properties:
|
|
142
131
|
|
|
143
132
|
* _key_ - string - the name or key of the cookie (default "")
|
|
144
133
|
* _value_ - string - the value of the cookie (default "")
|
|
@@ -149,70 +138,81 @@ Properties
|
|
|
149
138
|
* _secure_ - boolean - the `Secure` cookie flag
|
|
150
139
|
* _httpOnly_ - boolean - the `HttpOnly` cookie flag
|
|
151
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)
|
|
152
143
|
|
|
153
144
|
After a cookie has been passed through `CookieJar.setCookie()` it will have the following additional attributes:
|
|
154
145
|
|
|
155
146
|
* _hostOnly_ - boolean - is this a host-only cookie (i.e. no Domain field was set, but was instead implied)
|
|
156
147
|
* _pathIsDefault_ - boolean - if true, there was no Path field on the cookie and `defaultPath()` was used to derive one.
|
|
157
|
-
*
|
|
148
|
+
* _creation_ - `Date` - **modified** from construction to when the cookie was added to the jar
|
|
158
149
|
* _lastAccessed_ - `Date` - last time the cookie got accessed. Will affect cookie cleaning once implemented. Using `cookiejar.getCookies(...)` will update this attribute.
|
|
159
150
|
|
|
160
|
-
|
|
161
|
-
------------
|
|
151
|
+
### `Cookie([{properties}])`
|
|
162
152
|
|
|
163
|
-
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.
|
|
164
154
|
|
|
165
|
-
|
|
166
|
-
-----------
|
|
155
|
+
### `.toString()`
|
|
167
156
|
|
|
168
157
|
encode to a Set-Cookie header value. The Expires cookie field is set using `formatDate()`, but is omitted entirely if `.expires` is `Infinity`.
|
|
169
158
|
|
|
170
|
-
|
|
171
|
-
---------------
|
|
159
|
+
### `.cookieString()`
|
|
172
160
|
|
|
173
161
|
encode to a Cookie header value (i.e. the `.key` and `.value` properties joined with '=').
|
|
174
162
|
|
|
175
|
-
|
|
176
|
-
-------------------
|
|
163
|
+
### `.setExpires(String)`
|
|
177
164
|
|
|
178
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.
|
|
179
166
|
|
|
180
|
-
|
|
181
|
-
-------------------
|
|
167
|
+
### `.setMaxAge(number)`
|
|
182
168
|
|
|
183
169
|
sets the maxAge in seconds. Coerces `-Infinity` to `"-Infinity"` and `Infinity` to `"Infinity"` so it JSON serializes correctly.
|
|
184
170
|
|
|
185
|
-
|
|
186
|
-
-----------------------------
|
|
171
|
+
### `.expiryTime([now=Date.now()])`
|
|
187
172
|
|
|
188
|
-
|
|
189
|
-
-----------------------------
|
|
173
|
+
### `.expiryDate([now=Date.now()])`
|
|
190
174
|
|
|
191
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.
|
|
192
176
|
|
|
193
|
-
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.
|
|
194
178
|
|
|
195
179
|
If Expires (`.expires`) is set, that's returned.
|
|
196
180
|
|
|
197
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).
|
|
198
182
|
|
|
199
|
-
|
|
200
|
-
---------
|
|
183
|
+
### `.TTL([now=Date.now()])`
|
|
201
184
|
|
|
202
185
|
compute the TTL relative to `now` (milliseconds). The same precedence rules as for `expiryTime`/`expiryDate` apply.
|
|
203
186
|
|
|
204
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.
|
|
205
188
|
|
|
206
|
-
|
|
207
|
-
---------------------
|
|
189
|
+
### `.canonicalizedDoman()`
|
|
208
190
|
|
|
209
|
-
|
|
210
|
-
----------
|
|
191
|
+
### `.cdomain()`
|
|
211
192
|
|
|
212
193
|
return the canonicalized `.domain` field. This is lower-cased and punycode (RFC3490) encoded if the domain has any non-ASCII characters.
|
|
213
194
|
|
|
214
|
-
|
|
215
|
-
|
|
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()`
|
|
216
216
|
|
|
217
217
|
Status: *IN PROGRESS*. Works for a few things, but is by no means comprehensive.
|
|
218
218
|
|
|
@@ -226,48 +226,41 @@ if (cookie.validate() === true) {
|
|
|
226
226
|
}
|
|
227
227
|
```
|
|
228
228
|
|
|
229
|
-
CookieJar
|
|
230
|
-
=========
|
|
231
229
|
|
|
232
|
-
|
|
233
|
-
|
|
230
|
+
## CookieJar
|
|
231
|
+
|
|
232
|
+
Exported via `tough.CookieJar`.
|
|
233
|
+
|
|
234
|
+
### `CookieJar([store],[rejectPublicSuffixes])`
|
|
234
235
|
|
|
235
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.
|
|
236
237
|
|
|
238
|
+
### Properties
|
|
237
239
|
|
|
238
|
-
|
|
239
|
-
----------
|
|
240
|
+
CookieJar object properties:
|
|
240
241
|
|
|
241
242
|
* _rejectPublicSuffixes_ - boolean - reject cookies with domains like "com" and "co.uk" (default: `true`)
|
|
242
243
|
|
|
243
244
|
Since eventually this module would like to support database/remote/etc. CookieJars, continuation passing style is used for CookieJar methods.
|
|
244
245
|
|
|
245
|
-
|
|
246
|
-
-------------------------------------------------------------------
|
|
246
|
+
### `.setCookie(cookieOrString, currentUrl, [{options},] cb(err,cookie))`
|
|
247
247
|
|
|
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 `.
|
|
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.
|
|
249
249
|
|
|
250
250
|
The `options` object can be omitted and can have the following properties:
|
|
251
251
|
|
|
252
252
|
* _http_ - boolean - default `true` - indicates if this is an HTTP or non-HTTP API. Affects HttpOnly cookies.
|
|
253
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`.
|
|
254
254
|
* _now_ - Date - default `new Date()` - what to use for the creation/access time of cookies
|
|
255
|
-
* _ignoreError_ - boolean - default `false` - silently ignore things like parse errors and invalid domains.
|
|
255
|
+
* _ignoreError_ - boolean - default `false` - silently ignore things like parse errors and invalid domains. `Store` errors aren't ignored by this option.
|
|
256
256
|
|
|
257
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).
|
|
258
258
|
|
|
259
|
-
|
|
260
|
-
-------------------------------------------------------
|
|
259
|
+
### `.setCookieSync(cookieOrString, currentUrl, [{options}])`
|
|
261
260
|
|
|
262
261
|
Synchronous version of `setCookie`; only works with synchronous stores (e.g. the default `MemoryCookieStore`).
|
|
263
262
|
|
|
264
|
-
|
|
265
|
-
-------------------------------------------------
|
|
266
|
-
|
|
267
|
-
__REMOVED__ removed in lieu of the CookieStore API below
|
|
268
|
-
|
|
269
|
-
.getCookies(currentUrl, [{options},] cb(err,cookies))
|
|
270
|
-
-----------------------------------------------------
|
|
263
|
+
### `.getCookies(currentUrl, [{options},] cb(err,cookies))`
|
|
271
264
|
|
|
272
265
|
Retrieve the list of cookies that can be sent in a Cookie header for the current url.
|
|
273
266
|
|
|
@@ -279,59 +272,99 @@ The `options` object can be omitted and can have the following properties:
|
|
|
279
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`.
|
|
280
273
|
* _now_ - Date - default `new Date()` - what to use for the creation/access time of cookies
|
|
281
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).
|
|
282
|
-
* _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).
|
|
283
276
|
|
|
284
277
|
The `.lastAccessed` property of the returned cookies will have been updated.
|
|
285
278
|
|
|
286
|
-
|
|
287
|
-
----------------------------------------
|
|
279
|
+
### `.getCookiesSync(currentUrl, [{options}])`
|
|
288
280
|
|
|
289
281
|
Synchronous version of `getCookies`; only works with synchronous stores (e.g. the default `MemoryCookieStore`).
|
|
290
282
|
|
|
291
|
-
|
|
292
|
-
---------------------
|
|
283
|
+
### `.getCookieString(...)`
|
|
293
284
|
|
|
294
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()`.
|
|
295
286
|
|
|
296
|
-
|
|
297
|
-
-------------------------
|
|
287
|
+
### `.getCookieStringSync(...)`
|
|
298
288
|
|
|
299
289
|
Synchronous version of `getCookieString`; only works with synchronous stores (e.g. the default `MemoryCookieStore`).
|
|
300
290
|
|
|
301
|
-
|
|
302
|
-
-------------------------
|
|
291
|
+
### `.getSetCookieStrings(...)`
|
|
303
292
|
|
|
304
293
|
Returns an array of strings suitable for **Set-Cookie** headers. Accepts the same options as `.getCookies()`. Simply maps the cookie array via `.toString()`.
|
|
305
294
|
|
|
306
|
-
|
|
307
|
-
-----------------------------
|
|
295
|
+
### `.getSetCookieStringsSync(...)`
|
|
308
296
|
|
|
309
297
|
Synchronous version of `getSetCookieStrings`; only works with synchronous stores (e.g. the default `MemoryCookieStore`).
|
|
310
298
|
|
|
311
|
-
|
|
312
|
-
|
|
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)`.
|
|
314
|
+
|
|
315
|
+
### `CookieJar.deserialize(serialized, [store], cb(err,object))`
|
|
313
316
|
|
|
314
|
-
|
|
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.
|
|
315
318
|
|
|
316
|
-
|
|
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
|
|
317
350
|
|
|
318
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.
|
|
319
352
|
|
|
320
|
-
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
|
|
321
356
|
|
|
322
357
|
All `domain` parameters will have been normalized before calling.
|
|
323
358
|
|
|
324
359
|
The Cookie store must have all of the following methods.
|
|
325
360
|
|
|
326
|
-
store.findCookie(domain, path, key, cb(err,cookie))
|
|
327
|
-
---------------------------------------------------
|
|
361
|
+
### `store.findCookie(domain, path, key, cb(err,cookie))`
|
|
328
362
|
|
|
329
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.
|
|
330
364
|
|
|
331
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).
|
|
332
366
|
|
|
333
|
-
store.findCookies(domain, path, cb(err,cookies))
|
|
334
|
-
------------------------------------------------
|
|
367
|
+
### `store.findCookies(domain, path, cb(err,cookies))`
|
|
335
368
|
|
|
336
369
|
Locates cookies matching the given domain and path. This is most often called in the context of `cookiejar.getCookies()` above.
|
|
337
370
|
|
|
@@ -341,8 +374,7 @@ The resulting list will be checked for applicability to the current request acco
|
|
|
341
374
|
|
|
342
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).
|
|
343
376
|
|
|
344
|
-
store.putCookie(cookie, cb(err))
|
|
345
|
-
--------------------------------
|
|
377
|
+
### `store.putCookie(cookie, cb(err))`
|
|
346
378
|
|
|
347
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.
|
|
348
380
|
|
|
@@ -350,12 +382,11 @@ The `cookie` object MUST NOT be modified; the caller will have already updated t
|
|
|
350
382
|
|
|
351
383
|
Pass an error if the cookie cannot be stored.
|
|
352
384
|
|
|
353
|
-
store.updateCookie(oldCookie, newCookie, cb(err))
|
|
354
|
-
-------------------------------------------------
|
|
385
|
+
### `store.updateCookie(oldCookie, newCookie, cb(err))`
|
|
355
386
|
|
|
356
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.
|
|
357
388
|
|
|
358
|
-
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).
|
|
359
390
|
|
|
360
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.
|
|
361
392
|
|
|
@@ -363,26 +394,59 @@ The `newCookie` and `oldCookie` objects MUST NOT be modified.
|
|
|
363
394
|
|
|
364
395
|
Pass an error if the newCookie cannot be stored.
|
|
365
396
|
|
|
366
|
-
store.removeCookie(domain, path, key, cb(err))
|
|
367
|
-
----------------------------------------------
|
|
397
|
+
### `store.removeCookie(domain, path, key, cb(err))`
|
|
368
398
|
|
|
369
399
|
Remove a cookie from the store (see notes on `findCookie` about the uniqueness constraint).
|
|
370
400
|
|
|
371
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.
|
|
372
402
|
|
|
373
|
-
store.removeCookies(domain, path, cb(err))
|
|
374
|
-
------------------------------------------
|
|
403
|
+
### `store.removeCookies(domain, path, cb(err))`
|
|
375
404
|
|
|
376
405
|
Removes matching cookies from the store. The `path` parameter is optional, and if missing means all paths in a domain should be removed.
|
|
377
406
|
|
|
378
407
|
Pass an error ONLY if removing any existing cookies failed.
|
|
379
408
|
|
|
380
|
-
|
|
381
|
-
|
|
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`.
|
|
382
420
|
|
|
383
|
-
|
|
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.
|
|
384
422
|
|
|
385
|
-
|
|
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
|
+
```
|
|
386
450
|
|
|
387
451
|
# Copyright and License
|
|
388
452
|
|