-
Notifications
You must be signed in to change notification settings - Fork 343
/
Copy pathconnect-redis.js
175 lines (147 loc) · 4.32 KB
/
connect-redis.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
/*!
* Connect - Redis
* Copyright(c) 2010-2020 TJ Holowaychuk <[email protected]>
* MIT Licensed
*/
module.exports = function (session) {
const Store = session.Store
// All callbacks should have a noop if none provided for compatibility
// with the most Redis clients.
const noop = () => {}
class RedisStore extends Store {
constructor(options = {}) {
super(options)
if (!options.client) {
throw new Error("A client must be directly provided to the RedisStore")
}
this.prefix = options.prefix == null ? "sess:" : options.prefix
this.scanCount = Number(options.scanCount) || 100
this.serializer = options.serializer || JSON
this.client = options.client
this.ttl = options.ttl || 86400 // One day in seconds.
this.disableTTL = options.disableTTL || false
this.disableTouch = options.disableTouch || false
}
get(sid, cb = noop) {
let key = this.prefix + sid
this.client.get(key, (err, data) => {
if (err) return cb(err)
if (!data) return cb()
let result
try {
result = this.serializer.parse(data)
} catch (err) {
return cb(err)
}
return cb(null, result)
})
}
set(sid, sess, cb = noop) {
let args = [this.prefix + sid]
let value
try {
value = this.serializer.stringify(sess)
} catch (er) {
return cb(er)
}
args.push(value)
let ttl = 1
if (!this.disableTTL) {
ttl = this._getTTL(sess)
args.push("EX", ttl)
}
if (ttl > 0) {
this.client.set(args, cb)
} else {
// If the resulting TTL is negative we can delete / destroy the key
this.destroy(sid, cb)
}
}
touch(sid, sess, cb = noop) {
if (this.disableTouch || this.disableTTL) return cb()
let key = this.prefix + sid
this.client.expire(key, this._getTTL(sess), (err, ret) => {
if (err) return cb(err)
if (ret !== 1) return cb(null, "EXPIRED")
cb(null, "OK")
})
}
destroy(sid, cb = noop) {
let key = this.prefix + sid
this.client.del(key, cb)
}
clear(cb = noop) {
this._getAllKeys((err, keys) => {
if (err) return cb(err)
this.client.del(keys, cb)
})
}
length(cb = noop) {
this._getAllKeys((err, keys) => {
if (err) return cb(err)
return cb(null, keys.length)
})
}
ids(cb = noop) {
let prefixLen = this.prefix.length
this._getAllKeys((err, keys) => {
if (err) return cb(err)
keys = keys.map((key) => key.substr(prefixLen))
return cb(null, keys)
})
}
all(cb = noop) {
let prefixLen = this.prefix.length
this._getAllKeys((err, keys) => {
if (err) return cb(err)
if (keys.length === 0) return cb(null, [])
this.client.mget(keys, (err, sessions) => {
if (err) return cb(err)
let result
try {
result = sessions.reduce((accum, data, index) => {
if (!data) return accum
data = this.serializer.parse(data)
data.id = keys[index].substr(prefixLen)
accum.push(data)
return accum
}, [])
} catch (e) {
err = e
}
return cb(err, result)
})
})
}
_getTTL(sess) {
let ttl
if (sess && sess.cookie && sess.cookie.expires) {
let ms = Number(new Date(sess.cookie.expires)) - Date.now()
ttl = Math.ceil(ms / 1000)
} else {
ttl = this.ttl
}
return ttl
}
_getAllKeys(cb = noop) {
let pattern = this.prefix + "*"
this._scanKeys({}, 0, pattern, this.scanCount, cb)
}
_scanKeys(keys = {}, cursor, pattern, count, cb = noop) {
let args = [cursor, "match", pattern, "count", count]
this.client.scan(args, (err, data) => {
if (err) return cb(err)
let [nextCursorId, scanKeys] = data
for (let key of scanKeys) {
keys[key] = true
}
// This can be a string or a number. We check both.
if (Number(nextCursorId) !== 0) {
return this._scanKeys(keys, nextCursorId, pattern, count, cb)
}
cb(null, Object.keys(keys))
})
}
}
return RedisStore
}