tough-cookie 2.4.2 → 3.0.1

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 CHANGED
@@ -57,7 +57,7 @@ Transforms a domain-name into a canonical domain-name. The canonical domain-nam
57
57
 
58
58
  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".
59
59
 
60
- The `canonicalize` parameter will run the other two paramters through `canonicalDomain` or not.
60
+ The `canonicalize` parameter will run the other two parameters through `canonicalDomain` or not.
61
61
 
62
62
  ### `defaultPath(path)`
63
63
 
@@ -186,7 +186,7 @@ sets the maxAge in seconds. Coerces `-Infinity` to `"-Infinity"` and `Infinity`
186
186
 
187
187
  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.
188
188
 
189
- 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.
189
+ Max-Age takes precedence over Expires (as per the RFC). The `.creation` attribute -- or, by default, the `now` parameter -- is used to offset the `.maxAge` attribute.
190
190
 
191
191
  If Expires (`.expires`) is set, that's returned.
192
192
 
@@ -198,7 +198,7 @@ compute the TTL relative to `now` (milliseconds). The same precedence rules as
198
198
 
199
199
  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.
200
200
 
201
- ### `.canonicalizedDoman()`
201
+ ### `.canonicalizedDomain()`
202
202
 
203
203
  ### `.cdomain()`
204
204
 
@@ -354,6 +354,16 @@ The `store` argument is optional, but must be a _synchronous_ `Store` instance i
354
354
 
355
355
  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.
356
356
 
357
+ ### `.removeAllCookies(cb(err))`
358
+
359
+ Removes all cookies from the jar.
360
+
361
+ This is a new backwards-compatible feature of `tough-cookie` version 2.5, so not all Stores will implement it efficiently. For Stores that do not implement `removeAllCookies`, the fallback is to call `removeCookie` after `getAllCookies`. If `getAllCookies` fails or isn't implemented in the Store, that error is returned. If one or more of the `removeCookie` calls fail, only the first error is returned.
362
+
363
+ ### `.removeAllCookiesSync()`
364
+
365
+ Sync version of `.removeAllCookies()`
366
+
357
367
  ## Store
358
368
 
359
369
  Base class for CookieJar stores. Available as `tough.Store`.
@@ -418,19 +428,29 @@ Removes matching cookies from the store. The `path` parameter is optional, and
418
428
 
419
429
  Pass an error ONLY if removing any existing cookies failed.
420
430
 
431
+ ### `store.removeAllCookies(cb(err))`
432
+
433
+ _Optional_. Removes all cookies from the store.
434
+
435
+ Pass an error if one or more cookies can't be removed.
436
+
437
+ **Note**: New method as of `tough-cookie` version 2.5, so not all Stores will implement this, plus some stores may choose not to implement this.
438
+
421
439
  ### `store.getAllCookies(cb(err, cookies))`
422
440
 
423
- 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.
441
+ _Optional_. 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.
424
442
 
425
443
  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.
426
444
 
427
445
  Pass an error if retrieval fails.
428
446
 
447
+ **Note**: not all Stores can implement this due to technical limitations, so it is optional.
448
+
429
449
  ## MemoryCookieStore
430
450
 
431
451
  Inherits from `Store`.
432
452
 
433
- 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.
453
+ 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. Supports serialization, `getAllCookies`, and `removeAllCookies`.
434
454
 
435
455
  ## Community Cookie Stores
436
456
 
@@ -473,7 +493,7 @@ These are some Store implementations authored and maintained by the community. T
473
493
 
474
494
  # Copyright and License
475
495
 
476
- (tl;dr: BSD-3-Clause with some MPL/2.0)
496
+ BSD-3-Clause:
477
497
 
478
498
  ```text
479
499
  Copyright (c) 2015, Salesforce.com, Inc.
package/lib/cookie.js CHANGED
@@ -29,13 +29,14 @@
29
29
  * POSSIBILITY OF SUCH DAMAGE.
30
30
  */
31
31
  'use strict';
32
- var net = require('net');
33
32
  var urlParse = require('url').parse;
33
+ var util = require('util');
34
+ var ipRegex = require('ip-regex')({ exact: true });
34
35
  var pubsuffix = require('./pubsuffix-psl');
35
36
  var Store = require('./store').Store;
36
37
  var MemoryCookieStore = require('./memstore').MemoryCookieStore;
37
38
  var pathMatch = require('./pathMatch').pathMatch;
38
- var VERSION = require('../package.json').version;
39
+ var VERSION = require('./version');
39
40
 
40
41
  var punycode;
41
42
  try {
@@ -319,7 +320,7 @@ function domainMatch(str, domStr, canonicalize) {
319
320
  /* "All of the following [three] conditions hold:" (order adjusted from the RFC) */
320
321
 
321
322
  /* "* The string is a host name (i.e., not an IP address)." */
322
- if (net.isIP(str)) {
323
+ if (ipRegex.test(str)) {
323
324
  return false;
324
325
  }
325
326
 
@@ -756,6 +757,12 @@ Cookie.prototype.inspect = function inspect() {
756
757
  '"';
757
758
  };
758
759
 
760
+ // Use the new custom inspection symbol to add the custom inspect function if
761
+ // available.
762
+ if (util.inspect.custom) {
763
+ Cookie.prototype[util.inspect.custom] = Cookie.prototype.inspect;
764
+ }
765
+
759
766
  Cookie.prototype.toJSON = function() {
760
767
  var obj = {};
761
768
 
@@ -1000,11 +1007,17 @@ CookieJar.prototype.setCookie = function(cookie, url, options, cb) {
1000
1007
  }
1001
1008
 
1002
1009
  // S5.3 step 1
1003
- if (!(cookie instanceof Cookie)) {
1010
+ if (typeof(cookie) === 'string' || cookie instanceof String) {
1004
1011
  cookie = Cookie.parse(cookie, { loose: loose });
1012
+ if (!cookie) {
1013
+ err = new Error("Cookie failed to parse");
1014
+ return cb(options.ignoreError ? null : err);
1015
+ }
1005
1016
  }
1006
- if (!cookie) {
1007
- err = new Error("Cookie failed to parse");
1017
+ else if (!(cookie instanceof Cookie)) {
1018
+ // If you're seeing this error, and are passing in a Cookie object,
1019
+ // it *might* be a Cookie object from another loaded version of tough-cookie.
1020
+ err = new Error("First argument to setCookie must be a Cookie object or string");
1008
1021
  return cb(options.ignoreError ? null : err);
1009
1022
  }
1010
1023
 
@@ -1364,7 +1377,6 @@ CookieJar.deserializeSync = function(strOrObj, store) {
1364
1377
  };
1365
1378
  CookieJar.fromJSON = CookieJar.deserializeSync;
1366
1379
 
1367
- CAN_BE_SYNC.push('clone');
1368
1380
  CookieJar.prototype.clone = function(newStore, cb) {
1369
1381
  if (arguments.length === 1) {
1370
1382
  cb = newStore;
@@ -1375,10 +1387,61 @@ CookieJar.prototype.clone = function(newStore, cb) {
1375
1387
  if (err) {
1376
1388
  return cb(err);
1377
1389
  }
1378
- CookieJar.deserialize(newStore, serialized, cb);
1390
+ CookieJar.deserialize(serialized, newStore, cb);
1379
1391
  });
1380
1392
  };
1381
1393
 
1394
+ CAN_BE_SYNC.push('removeAllCookies');
1395
+ CookieJar.prototype.removeAllCookies = function(cb) {
1396
+ var store = this.store;
1397
+
1398
+ // Check that the store implements its own removeAllCookies(). The default
1399
+ // implementation in Store will immediately call the callback with a "not
1400
+ // implemented" Error.
1401
+ if (store.removeAllCookies instanceof Function &&
1402
+ store.removeAllCookies !== Store.prototype.removeAllCookies)
1403
+ {
1404
+ return store.removeAllCookies(cb);
1405
+ }
1406
+
1407
+ store.getAllCookies(function(err, cookies) {
1408
+ if (err) {
1409
+ return cb(err);
1410
+ }
1411
+
1412
+ if (cookies.length === 0) {
1413
+ return cb(null);
1414
+ }
1415
+
1416
+ var completedCount = 0;
1417
+ var removeErrors = [];
1418
+
1419
+ function removeCookieCb(removeErr) {
1420
+ if (removeErr) {
1421
+ removeErrors.push(removeErr);
1422
+ }
1423
+
1424
+ completedCount++;
1425
+
1426
+ if (completedCount === cookies.length) {
1427
+ return cb(removeErrors.length ? removeErrors[0] : null);
1428
+ }
1429
+ }
1430
+
1431
+ cookies.forEach(function(cookie) {
1432
+ store.removeCookie(cookie.domain, cookie.path, cookie.key, removeCookieCb);
1433
+ });
1434
+ });
1435
+ };
1436
+
1437
+ CookieJar.prototype._cloneSync = syncWrap('clone');
1438
+ CookieJar.prototype.cloneSync = function(newStore) {
1439
+ if (!newStore.synchronous) {
1440
+ throw new Error('CookieJar clone destination store is not synchronous; use async API instead.');
1441
+ }
1442
+ return this._cloneSync(newStore);
1443
+ };
1444
+
1382
1445
  // Use a closure to provide a true imperative API for synchronous stores.
1383
1446
  function syncWrap(method) {
1384
1447
  return function() {
@@ -1406,6 +1469,7 @@ CAN_BE_SYNC.forEach(function(method) {
1406
1469
  CookieJar.prototype[method+'Sync'] = syncWrap(method);
1407
1470
  });
1408
1471
 
1472
+ exports.version = VERSION;
1409
1473
  exports.CookieJar = CookieJar;
1410
1474
  exports.Cookie = Cookie;
1411
1475
  exports.Store = Store;
package/lib/memstore.js CHANGED
@@ -50,6 +50,12 @@ MemoryCookieStore.prototype.inspect = function() {
50
50
  return "{ idx: "+util.inspect(this.idx, false, 2)+' }';
51
51
  };
52
52
 
53
+ // Use the new custom inspection symbol to add the custom inspect function if
54
+ // available.
55
+ if (util.inspect.custom) {
56
+ MemoryCookieStore.prototype[util.inspect.custom] = MemoryCookieStore.prototype.inspect;
57
+ }
58
+
53
59
  MemoryCookieStore.prototype.findCookie = function(domain, path, key, cb) {
54
60
  if (!this.idx[domain]) {
55
61
  return cb(null,undefined);
@@ -143,6 +149,11 @@ MemoryCookieStore.prototype.removeCookies = function(domain, path, cb) {
143
149
  return cb(null);
144
150
  };
145
151
 
152
+ MemoryCookieStore.prototype.removeAllCookies = function(cb) {
153
+ this.idx = {};
154
+ return cb(null);
155
+ }
156
+
146
157
  MemoryCookieStore.prototype.getAllCookies = function(cb) {
147
158
  var cookies = [];
148
159
  var idx = this.idx;
package/lib/store.js CHANGED
@@ -66,6 +66,10 @@ Store.prototype.removeCookies = function(domain, path, cb) {
66
66
  throw new Error('removeCookies is not implemented');
67
67
  };
68
68
 
69
+ Store.prototype.removeAllCookies = function(cb) {
70
+ throw new Error('removeAllCookies is not implemented');
71
+ }
72
+
69
73
  Store.prototype.getAllCookies = function(cb) {
70
74
  throw new Error('getAllCookies is not implemented (therefore jar cannot be serialized)');
71
75
  };
package/lib/version.js ADDED
@@ -0,0 +1,2 @@
1
+ // generated by genversion
2
+ module.exports = '3.0.1'
package/package.json CHANGED
@@ -43,7 +43,7 @@
43
43
  "RFC6265",
44
44
  "RFC2965"
45
45
  ],
46
- "version": "2.4.2",
46
+ "version": "3.0.1",
47
47
  "homepage": "https://github.com/salesforce/tough-cookie",
48
48
  "repository": {
49
49
  "type": "git",
@@ -57,20 +57,23 @@
57
57
  "lib"
58
58
  ],
59
59
  "scripts": {
60
+ "version": "genversion lib/version.js && git add lib/version.js",
60
61
  "test": "vows test/*_test.js",
61
62
  "cover": "nyc --reporter=lcov --reporter=html vows test/*_test.js"
62
63
  },
63
64
  "engines": {
64
- "node": ">=0.8"
65
+ "node": ">=6"
65
66
  },
66
67
  "devDependencies": {
67
68
  "async": "^1.4.2",
69
+ "genversion": "^2.1.0",
68
70
  "nyc": "^11.6.0",
69
71
  "string.prototype.repeat": "^0.2.0",
70
- "vows": "^0.8.1"
72
+ "vows": "^0.8.2"
71
73
  },
72
74
  "dependencies": {
73
- "psl": "^1.1.24",
74
- "punycode": "^1.4.1"
75
+ "ip-regex": "^2.1.0",
76
+ "psl": "^1.1.28",
77
+ "punycode": "^2.1.1"
75
78
  }
76
79
  }