forked from expressjs/session
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmart-store.js
49 lines (37 loc) · 1.04 KB
/
smart-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
'use strict'
var session = require('../../')
var util = require('util')
module.exports = SmartStore
function SmartStore () {
session.Store.call(this)
this.sessions = Object.create(null)
}
util.inherits(SmartStore, session.Store)
SmartStore.prototype.destroy = function destroy (sid, callback) {
delete this.sessions[sid]
setImmediate(callback, null)
}
SmartStore.prototype.get = function get (sid, callback) {
var sess = this.sessions[sid]
if (!sess) {
return
}
// parse
sess = JSON.parse(sess)
if (sess.cookie) {
// expand expires into Date object
sess.cookie.expires = typeof sess.cookie.expires === 'string'
? new Date(sess.cookie.expires)
: sess.cookie.expires
// destroy expired session
if (sess.cookie.expires && sess.cookie.expires <= Date.now()) {
delete this.sessions[sid]
sess = null
}
}
setImmediate(callback, null, sess)
}
SmartStore.prototype.set = function set (sid, sess, callback) {
this.sessions[sid] = JSON.stringify(sess)
setImmediate(callback, null)
}