Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81146 views
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 Store = require('./store').Store;
33
var permuteDomain = require('./permuteDomain').permuteDomain;
34
var pathMatch = require('./pathMatch').pathMatch;
35
var util = require('util');
36
37
function MemoryCookieStore() {
38
Store.call(this);
39
this.idx = {};
40
}
41
util.inherits(MemoryCookieStore, Store);
42
exports.MemoryCookieStore = MemoryCookieStore;
43
MemoryCookieStore.prototype.idx = null;
44
MemoryCookieStore.prototype.synchronous = true;
45
46
// force a default depth:
47
MemoryCookieStore.prototype.inspect = function() {
48
return "{ idx: "+util.inspect(this.idx, false, 2)+' }';
49
};
50
51
MemoryCookieStore.prototype.findCookie = function(domain, path, key, cb) {
52
if (!this.idx[domain]) {
53
return cb(null,undefined);
54
}
55
if (!this.idx[domain][path]) {
56
return cb(null,undefined);
57
}
58
return cb(null,this.idx[domain][path][key]||null);
59
};
60
61
MemoryCookieStore.prototype.findCookies = function(domain, path, cb) {
62
var results = [];
63
if (!domain) {
64
return cb(null,[]);
65
}
66
67
var pathMatcher;
68
if (!path) {
69
// null means "all paths"
70
pathMatcher = function matchAll(domainIndex) {
71
for (var curPath in domainIndex) {
72
var pathIndex = domainIndex[curPath];
73
for (var key in pathIndex) {
74
results.push(pathIndex[key]);
75
}
76
}
77
};
78
79
} else {
80
pathMatcher = function matchRFC(domainIndex) {
81
//NOTE: we should use path-match algorithm from S5.1.4 here
82
//(see : https://github.com/ChromiumWebApps/chromium/blob/b3d3b4da8bb94c1b2e061600df106d590fda3620/net/cookies/canonical_cookie.cc#L299)
83
Object.keys(domainIndex).forEach(function (cookiePath) {
84
if (pathMatch(path, cookiePath)) {
85
var pathIndex = domainIndex[cookiePath];
86
87
for (var key in pathIndex) {
88
results.push(pathIndex[key]);
89
}
90
}
91
});
92
};
93
}
94
95
var domains = permuteDomain(domain) || [domain];
96
var idx = this.idx;
97
domains.forEach(function(curDomain) {
98
var domainIndex = idx[curDomain];
99
if (!domainIndex) {
100
return;
101
}
102
pathMatcher(domainIndex);
103
});
104
105
cb(null,results);
106
};
107
108
MemoryCookieStore.prototype.putCookie = function(cookie, cb) {
109
if (!this.idx[cookie.domain]) {
110
this.idx[cookie.domain] = {};
111
}
112
if (!this.idx[cookie.domain][cookie.path]) {
113
this.idx[cookie.domain][cookie.path] = {};
114
}
115
this.idx[cookie.domain][cookie.path][cookie.key] = cookie;
116
cb(null);
117
};
118
119
MemoryCookieStore.prototype.updateCookie = function updateCookie(oldCookie, newCookie, cb) {
120
// updateCookie() may avoid updating cookies that are identical. For example,
121
// lastAccessed may not be important to some stores and an equality
122
// comparison could exclude that field.
123
this.putCookie(newCookie,cb);
124
};
125
126
MemoryCookieStore.prototype.removeCookie = function removeCookie(domain, path, key, cb) {
127
if (this.idx[domain] && this.idx[domain][path] && this.idx[domain][path][key]) {
128
delete this.idx[domain][path][key];
129
}
130
cb(null);
131
};
132
133
MemoryCookieStore.prototype.removeCookies = function removeCookies(domain, path, cb) {
134
if (this.idx[domain]) {
135
if (path) {
136
delete this.idx[domain][path];
137
} else {
138
delete this.idx[domain];
139
}
140
}
141
return cb(null);
142
};
143
144