memstore.js
2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
var tough = require('./cookie');
var permuteDomain = tough.permuteDomain;
var permutePath = tough.permutePath;
var util = require('util');
function MemoryCookieStore() {
this.idx = {};
}
module.exports.MemoryCookieStore = MemoryCookieStore;
MemoryCookieStore.prototype.idx = null;
// force a default depth:
MemoryCookieStore.prototype.inspect = function inspect() {
return "{ idx: "+util.inspect(this.idx, false, 2)+' }';
};
MemoryCookieStore.prototype.findCookie = function findCookie(domain, path, key, cb) {
if (!this.idx[domain]) return cb(null,undefined);
if (!this.idx[domain][path]) return cb(null,undefined);
return cb(null,this.idx[domain][path][key]||null);
};
MemoryCookieStore.prototype.findCookies = function findCookies(domain, path, cb) {
var results = [];
if (!domain) return cb(null,[]);
var pathMatcher;
if (!path) {
// null or '/' means "all paths"
pathMatcher = function matchAll(domainIndex) {
for (var curPath in domainIndex) {
var pathIndex = domainIndex[curPath];
for (var key in pathIndex) {
results.push(pathIndex[key]);
}
}
};
} else if (path === '/') {
pathMatcher = function matchSlash(domainIndex) {
var pathIndex = domainIndex['/'];
if (!pathIndex) return;
for (var key in pathIndex) {
results.push(pathIndex[key]);
}
};
} else {
var paths = permutePath(path) || [path];
pathMatcher = function matchRFC(domainIndex) {
paths.forEach(function(curPath) {
var pathIndex = domainIndex[curPath];
if (!pathIndex) return;
for (var key in pathIndex) {
results.push(pathIndex[key]);
}
});
};
}
var domains = permuteDomain(domain) || [domain];
var idx = this.idx;
domains.forEach(function(curDomain) {
var domainIndex = idx[curDomain];
if (!domainIndex) return;
pathMatcher(domainIndex);
});
cb(null,results);
};
MemoryCookieStore.prototype.putCookie = function putCookie(cookie, cb) {
if (!this.idx[cookie.domain]) this.idx[cookie.domain] = {};
if (!this.idx[cookie.domain][cookie.path]) this.idx[cookie.domain][cookie.path] = {};
this.idx[cookie.domain][cookie.path][cookie.key] = cookie;
cb(null);
};
MemoryCookieStore.prototype.updateCookie = function updateCookie(oldCookie, newCookie, cb) {
// updateCookie() may avoid updating cookies that are identical. For example,
// lastAccessed may not be important to some stores and an equality
// comparison could exclude that field.
this.putCookie(newCookie,cb);
};
MemoryCookieStore.prototype.removeCookie = function removeCookie(domain, path, key, cb) {
if (this.idx[domain] && this.idx[domain][path] && this.idx[domain][path][key]) {
delete this.idx[domain][path][key];
}
cb(null);
};
MemoryCookieStore.prototype.removeCookies = function removeCookies(domain, path, cb) {
if (!this.idx[domain]) {
if (path) {
delete this.idx[domain][path];
} else {
delete this.idx[domain];
}
}
return cb(null);
};