-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmemorystore.js
295 lines (243 loc) · 6.14 KB
/
memorystore.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*!
* memorystore
* Copyright(c) 2020 Rocco Musolino <@roccomuso>
* MIT Licensed
*/
var debug = require('debug')('memorystore')
var LRU = require('lru-cache')
var util = require('util')
/**
* One day in milliseconds.
*/
var oneDay = 86400000
function getTTL (options, sess, sid) {
if (typeof options.ttl === 'number') return options.ttl
if (typeof options.ttl === 'function') return options.ttl(options, sess, sid)
if (options.ttl) throw new TypeError('`options.ttl` must be a number or function.')
var maxAge = (sess && sess.cookie) ? sess.cookie.maxAge : null
return (typeof maxAge === 'number'
? Math.floor(maxAge)
: oneDay)
}
function prune (store) {
debug('Pruning expired entries')
store.forEach(function (value, key) {
store.get(key)
})
}
var defer = typeof setImmediate === 'function'
? setImmediate
: function (fn) { process.nextTick(fn.bind.apply(fn, arguments)) }
/**
* Return the `MemoryStore` extending `express`'s session Store.
*
* @param {object} express session
* @return {Function}
* @api public
*/
module.exports = function (session) {
/**
* Express's session Store.
*/
var Store = session.Store
/**
* Initialize MemoryStore with the given `options`.
*
* @param {Object} options
* @api public
*/
function MemoryStore (options) {
if (!(this instanceof MemoryStore)) {
throw new TypeError('Cannot call MemoryStore constructor as a function')
}
options = options || {}
Store.call(this, options)
this.options = {}
this.options.checkPeriod = options.checkPeriod
this.options.max = options.max || Infinity
this.options.ttl = options.ttl
this.options.dispose = options.dispose
this.options.stale = options.stale
this.options.noDisposeOnSet = options.noDisposeOnSet
this.serializer = options.serializer || JSON
this.store = LRU(this.options)
debug('Init MemoryStore')
this.startInterval()
}
/**
* Inherit from `Store`.
*/
util.inherits(MemoryStore, Store)
/**
* Attempt to fetch session by the given `sid`.
*
* @param {String} sid
* @param {Function} fn
* @api public
*/
MemoryStore.prototype.get = function (sid, fn) {
var store = this.store
debug('GET "%s"', sid)
var data = store.get(sid)
if (!data) return fn()
debug('GOT %s', data)
var err = null
var result
try {
result = this.serializer.parse(data)
} catch (er) {
err = er
}
fn && defer(fn, err, result)
}
/**
* Commit the given `sess` object associated with the given `sid`.
*
* @param {String} sid
* @param {Session} sess
* @param {Function} fn
* @api public
*/
MemoryStore.prototype.set = function (sid, sess, fn) {
var store = this.store
var ttl = getTTL(this.options, sess, sid)
try {
var jsess = this.serializer.stringify(sess)
} catch (err) {
fn && defer(fn, err)
}
store.set(sid, jsess, ttl)
debug('SET "%s" %s ttl:%s', sid, jsess, ttl)
fn && defer(fn, null)
}
/**
* Destroy the session associated with the given `sid`.
*
* @param {String} sid
* @api public
*/
MemoryStore.prototype.destroy = function (sid, fn) {
var store = this.store
if (Array.isArray(sid)) {
sid.forEach(function (s) {
debug('DEL "%s"', s)
store.del(s)
})
} else {
debug('DEL "%s"', sid)
store.del(sid)
}
fn && defer(fn, null)
}
/**
* Refresh the time-to-live for the session with the given `sid`.
*
* @param {String} sid
* @param {Session} sess
* @param {Function} fn
* @api public
*/
MemoryStore.prototype.touch = function (sid, sess, fn) {
var store = this.store
var ttl = getTTL(this.options, sess, sid)
debug('EXPIRE "%s" ttl:%s', sid, ttl)
var err = null
if (store.get(sid) !== undefined) {
try {
var s = this.serializer.parse(store.get(sid))
s.cookie = sess.cookie
store.set(sid, this.serializer.stringify(s), ttl)
} catch (e) {
err = e
}
}
fn && defer(fn, err)
}
/**
* Fetch all sessions' ids
*
* @param {Function} fn
* @api public
*/
MemoryStore.prototype.ids = function (fn) {
var store = this.store
var Ids = store.keys()
debug('Getting IDs: %s', Ids)
fn && defer(fn, null, Ids)
}
/**
* Fetch all sessions
*
* @param {Function} fn
* @api public
*/
MemoryStore.prototype.all = function (fn) {
var store = this.store
var self = this
debug('Fetching all sessions')
var err = null
var result = {}
try {
store.forEach(function (val, key) {
result[key] = self.serializer.parse(val)
})
} catch (e) {
err = e
}
fn && defer(fn, err, result)
}
/**
* Delete all sessions from the store
*
* @param {Function} fn
* @api public
*/
MemoryStore.prototype.clear = function (fn) {
var store = this.store
debug('delete all sessions from the store')
store.reset()
fn && defer(fn, null)
}
/**
* Get the count of all sessions in the store
*
* @param {Function} fn
* @api public
*/
MemoryStore.prototype.length = function (fn) {
var store = this.store
debug('getting length', store.itemCount)
fn && defer(fn, null, store.itemCount)
}
/**
* Start the check interval
* @api public
*/
MemoryStore.prototype.startInterval = function () {
var self = this
var ms = this.options.checkPeriod
if (ms && typeof ms === 'number') {
clearInterval(this._checkInterval)
debug('starting periodic check for expired sessions')
this._checkInterval = setInterval(function () {
prune(self.store) // iterates over the entire cache proactively pruning old entries
}, Math.floor(ms)).unref()
}
}
/**
* Stop the check interval
* @api public
*/
MemoryStore.prototype.stopInterval = function () {
debug('stopping periodic check for expired sessions')
clearInterval(this._checkInterval)
}
/**
* Remove only expired entries from the store
* @api public
*/
MemoryStore.prototype.prune = function () {
prune(this.store)
}
return MemoryStore
}