-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (34 loc) · 839 Bytes
/
index.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
/*!
* sessions-provider-memory
* Copyright(c) 2017 Fangdun Cai <[email protected]> (https://fundon.me)
* MIT Licensed
*/
'use strict'
const TIMER = Symbol('timer')
module.exports = class MemoryProvider extends Map {
get(sid) {
const sess = super.get(sid)
if (!sess) return
const expires = sess.cookie.expires
if (expires && expires <= Date.now()) {
this.delete(sid)
return
}
return sess
}
set(sid, sess, expires) {
super.set(sid, sess)
// Should clear old timer
if (sess[TIMER]) {
sess.cookie.expires = new Date() + expires
clearTimeout(sess[TIMER])
}
Object.defineProperty(sess, TIMER, {
configurable: true,
enumerable: false,
writable: true,
value: setTimeout(() => this.delete(sid), expires).unref()
})
}
quit() {}
}