Repository navigation
Expand file tree
/
Copy pathmemstore.js
More file actions
69 lines (60 loc) · 2.08 KB
/
Copy pathmemstore.js
File metadata and controls
69 lines (60 loc) · 2.08 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
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 || !path) return cb(null,[]);
var domains = permuteDomain(domain)||[domain], dlen = domains.length;
var paths = permutePath(path), plen = paths.length;
for (var i=0; i<dlen; i++) {
var curDomain = domains[i];
var domainIndex = this.idx[curDomain];
if (!domainIndex) continue;
for (var j=0; j<plen; j++) {
var curPath = paths[j];
var pathIndex = domainIndex[curPath];
if (!pathIndex) continue;
for (var key in pathIndex) {
results.push(pathIndex[key]);
}
}
}
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.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);
};