-
Notifications
You must be signed in to change notification settings - Fork 13
/
redis-cookie-store.js
186 lines (153 loc) · 4.26 KB
/
redis-cookie-store.js
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// node core modules
const util = require('util');
// 3rd party modules
const async = require('async');
const _ = require('lodash');
const { Store, permuteDomain, permutePath, Cookie } = require('tough-cookie');
// internal modules
// putting id last to not break existing interface
function RedisCookieStore(redisClient, id) {
const self = this;
Store.call(self);
self.idx = {};
self.id = id || 'default';
self.client = redisClient;
self.synchronous = false;
}
util.inherits(RedisCookieStore, Store);
RedisCookieStore.prototype.getKeyName = function getKeyName(domain, path) {
const self = this;
if (path) {
return `cookie-store:${self.id}:cookie:${domain}:${path}`;
}
return `cookie-store:${self.id}:cookie:${domain}`;
};
RedisCookieStore.prototype.findCookie = function findCookie(
domain,
path,
cookieName,
cb,
) {
const self = this;
const { client } = self;
const keyName = self.getKeyName(domain, path);
client.hget(keyName, cookieName, cb);
};
const cookiePrefixRegexp = /(.*cookie:[^:]*:)/i;
const pathMatcher = ({ domainKeys, paths, client }, cb) => {
const [firstDomainKey] = domainKeys;
const [, domainPrefix] = firstDomainKey.match(cookiePrefixRegexp); // get domain key prefix e.g. "cookie:www.example.com"
const prefixedPaths = paths.map((path) => domainPrefix + path);
const keys = _.intersection(domainKeys, prefixedPaths).sort(
(a, b) => a.length - b.length,
);
const jobs = keys.map((key) => (next) => client.hgetall(key, next));
async.parallel(jobs, (err, results) => {
if (err) {
cb(err);
return;
}
cb(null, _.merge(...results));
});
};
RedisCookieStore.prototype.findCookies = function findCookies(
domain,
path,
cb,
) {
const self = this;
const { client } = self;
if (!domain) {
cb(null, []);
return;
}
const permutedDomains = permuteDomain(domain) || [domain];
// sort permuted domains (length ascending)
const sortedPermutedDomains = permutedDomains.sort(
(a, b) => a.length - b.length,
);
const paths = permutePath(path) || [path];
// prepare jobs to load cookie data from redis for each permuted domain
const jobs = sortedPermutedDomains.map((permutedDomain) => (next) => {
const keyName = `${self.getKeyName(permutedDomain)}:*`;
client.keys(keyName, (err, domainKeys) => {
if (err) {
next(err, null);
return;
}
if (!domainKeys || !domainKeys.length) {
next(null, {});
return;
}
pathMatcher({ domainKeys, paths, client }, next);
});
});
async.parallel(jobs, (err, results) => {
if (err) {
cb(err);
return;
}
cb(
err,
_.values(_.merge(...results)).map((cookieString) => {
const cookie = Cookie.parse(cookieString);
if (!cookie.domain) {
cookie.domain = domain;
}
return cookie;
}),
);
});
};
RedisCookieStore.prototype.putCookie = function putCookie(cookie, cb) {
const self = this;
const { client } = self;
const { key: cookieName, domain, path } = cookie;
const keyName = self.getKeyName(domain, path);
const cookieString = cookie.toString();
client.hset(keyName, cookieName, cookieString, cb);
};
RedisCookieStore.prototype.updateCookie = function updateCookie(
oldCookie,
newCookie,
cb,
) {
const self = this;
// 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.
self.putCookie(newCookie, cb);
};
RedisCookieStore.prototype.removeCookie = function removeCookie(
domain,
path,
cookieName,
cb,
) {
const self = this;
const { client } = self;
const keyName = self.getKeyName(domain, path);
client.hdel(keyName, cookieName, cb);
};
RedisCookieStore.prototype.removeCookies = function removeCookies(
domain,
path,
cb,
) {
const self = this;
const { client } = self;
if (path) {
const keyName = self.getKeyName(domain, path);
client.del(keyName, cb);
return;
}
const keyName = `${self.getKeyName(domain)}:*`;
client.keys(keyName, (err, keys) => {
if (err) {
cb(err);
return;
}
async.each(keys, (key, done) => client.del(key, done), cb);
});
};
module.exports = RedisCookieStore;