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/lib/memstore.js CHANGED
@@ -29,10 +29,9 @@
29
29
  * POSSIBILITY OF SUCH DAMAGE.
30
30
  */
31
31
  'use strict';
32
- var tough = require('./cookie');
33
32
  var Store = require('./store').Store;
34
- var permuteDomain = tough.permuteDomain;
35
- var pathMatch = tough.pathMatch;
33
+ var permuteDomain = require('./permuteDomain').permuteDomain;
34
+ var pathMatch = require('./pathMatch').pathMatch;
36
35
  var util = require('util');
37
36
 
38
37
  function MemoryCookieStore() {
@@ -42,6 +41,8 @@ function MemoryCookieStore() {
42
41
  util.inherits(MemoryCookieStore, Store);
43
42
  exports.MemoryCookieStore = MemoryCookieStore;
44
43
  MemoryCookieStore.prototype.idx = null;
44
+
45
+ // Since it's just a struct in RAM, this Store is synchronous
45
46
  MemoryCookieStore.prototype.synchronous = true;
46
47
 
47
48
  // force a default depth:
@@ -117,21 +118,21 @@ MemoryCookieStore.prototype.putCookie = function(cookie, cb) {
117
118
  cb(null);
118
119
  };
119
120
 
120
- MemoryCookieStore.prototype.updateCookie = function updateCookie(oldCookie, newCookie, cb) {
121
+ MemoryCookieStore.prototype.updateCookie = function(oldCookie, newCookie, cb) {
121
122
  // updateCookie() may avoid updating cookies that are identical. For example,
122
123
  // lastAccessed may not be important to some stores and an equality
123
124
  // comparison could exclude that field.
124
125
  this.putCookie(newCookie,cb);
125
126
  };
126
127
 
127
- MemoryCookieStore.prototype.removeCookie = function removeCookie(domain, path, key, cb) {
128
+ MemoryCookieStore.prototype.removeCookie = function(domain, path, key, cb) {
128
129
  if (this.idx[domain] && this.idx[domain][path] && this.idx[domain][path][key]) {
129
130
  delete this.idx[domain][path][key];
130
131
  }
131
132
  cb(null);
132
133
  };
133
134
 
134
- MemoryCookieStore.prototype.removeCookies = function removeCookies(domain, path, cb) {
135
+ MemoryCookieStore.prototype.removeCookies = function(domain, path, cb) {
135
136
  if (this.idx[domain]) {
136
137
  if (path) {
137
138
  delete this.idx[domain][path];
@@ -141,3 +142,29 @@ MemoryCookieStore.prototype.removeCookies = function removeCookies(domain, path,
141
142
  }
142
143
  return cb(null);
143
144
  };
145
+
146
+ MemoryCookieStore.prototype.getAllCookies = function(cb) {
147
+ var cookies = [];
148
+ var idx = this.idx;
149
+
150
+ var domains = Object.keys(idx);
151
+ domains.forEach(function(domain) {
152
+ var paths = Object.keys(idx[domain]);
153
+ paths.forEach(function(path) {
154
+ var keys = Object.keys(idx[domain][path]);
155
+ keys.forEach(function(key) {
156
+ if (key !== null) {
157
+ cookies.push(idx[domain][path][key]);
158
+ }
159
+ });
160
+ });
161
+ });
162
+
163
+ // Sort by creationIndex so deserializing retains the creation order.
164
+ // When implementing your own store, this SHOULD retain the order too
165
+ cookies.sort(function(a,b) {
166
+ return (a.creationIndex||0) - (b.creationIndex||0);
167
+ });
168
+
169
+ cb(null, cookies);
170
+ };
@@ -0,0 +1,61 @@
1
+ /*!
2
+ * Copyright (c) 2015, Salesforce.com, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright notice,
9
+ * this list of conditions and the following disclaimer.
10
+ *
11
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ * this list of conditions and the following disclaimer in the documentation
13
+ * and/or other materials provided with the distribution.
14
+ *
15
+ * 3. Neither the name of Salesforce.com nor the names of its contributors may
16
+ * be used to endorse or promote products derived from this software without
17
+ * specific prior written permission.
18
+ *
19
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
+ * POSSIBILITY OF SUCH DAMAGE.
30
+ */
31
+ "use strict";
32
+ /*
33
+ * "A request-path path-matches a given cookie-path if at least one of the
34
+ * following conditions holds:"
35
+ */
36
+ function pathMatch (reqPath, cookiePath) {
37
+ // "o The cookie-path and the request-path are identical."
38
+ if (cookiePath === reqPath) {
39
+ return true;
40
+ }
41
+
42
+ var idx = reqPath.indexOf(cookiePath);
43
+ if (idx === 0) {
44
+ // "o The cookie-path is a prefix of the request-path, and the last
45
+ // character of the cookie-path is %x2F ("/")."
46
+ if (cookiePath.substr(-1) === "/") {
47
+ return true;
48
+ }
49
+
50
+ // " o The cookie-path is a prefix of the request-path, and the first
51
+ // character of the request-path that is not included in the cookie- path
52
+ // is a %x2F ("/") character."
53
+ if (reqPath.substr(cookiePath.length, 1) === "/") {
54
+ return true;
55
+ }
56
+ }
57
+
58
+ return false;
59
+ }
60
+
61
+ exports.pathMatch = pathMatch;
@@ -0,0 +1,56 @@
1
+ /*!
2
+ * Copyright (c) 2015, Salesforce.com, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright notice,
9
+ * this list of conditions and the following disclaimer.
10
+ *
11
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ * this list of conditions and the following disclaimer in the documentation
13
+ * and/or other materials provided with the distribution.
14
+ *
15
+ * 3. Neither the name of Salesforce.com nor the names of its contributors may
16
+ * be used to endorse or promote products derived from this software without
17
+ * specific prior written permission.
18
+ *
19
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
+ * POSSIBILITY OF SUCH DAMAGE.
30
+ */
31
+ "use strict";
32
+ var pubsuffix = require('./pubsuffix');
33
+
34
+ // Gives the permutation of all possible domainMatch()es of a given domain. The
35
+ // array is in shortest-to-longest order. Handy for indexing.
36
+ function permuteDomain (domain) {
37
+ var pubSuf = pubsuffix.getPublicSuffix(domain);
38
+ if (!pubSuf) {
39
+ return null;
40
+ }
41
+ if (pubSuf == domain) {
42
+ return [domain];
43
+ }
44
+
45
+ var prefix = domain.slice(0, -(pubSuf.length + 1)); // ".example.com"
46
+ var parts = prefix.split('.').reverse();
47
+ var cur = pubSuf;
48
+ var permutations = [cur];
49
+ while (parts.length) {
50
+ cur = parts.shift() + '.' + cur;
51
+ permutations.push(cur);
52
+ }
53
+ return permutations;
54
+ }
55
+
56
+ exports.permuteDomain = permuteDomain;