-
Notifications
You must be signed in to change notification settings - Fork 1
/
connect-medea.js
79 lines (61 loc) · 1.68 KB
/
connect-medea.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
var medea = require('medea')
var ttl = require('medea-ttl');
var oneDay = 86400;
module.exports = function(connect){
var Store = connect.session.Store;
function MedeaStore(options) {
var self = this;
options = options || {};
Store.call(this, options);
this.prefix = null == options.prefix
? 'sess:'
: options.prefix;
this.client = ttl(options.client || medea(options));
this.ttl = options.ttl;
options.dirname = options.dirname || process.cwd() + '/data';
this.client.open(options.dirname, function(err) {
if (err) {
this.emit('disconnect');
throw err;
return;
}
self.emit('connect');
});
};
MedeaStore.prototype.__proto__ = Store.prototype;
MedeaStore.prototype.get = function(sid, fn){
var self = this;
sid = this.prefix + sid;
this.client.get(sid, function(err, data){
if (err) return fn(err);
if (!data) return fn();
var result;
data = data.toString();
try {
result = JSON.parse(data);
} catch (err) {
return fn(err);
}
return fn(null, result);
});
};
MedeaStore.prototype.set = function(sid, sess, fn){
sid = this.prefix + sid;
try {
var maxAge = sess.cookie.maxAge
, ttl = this.ttl
, sess = JSON.stringify(sess);
ttl = ttl || ('number' == typeof maxAge ? maxAge : oneDay);
this.client.put(sid, sess, ttl, function(err) {
fn && fn.apply(this, arguments);
});
} catch (err) {
fn && fn(err);
}
};
MedeaStore.prototype.destroy = function(sid, fn){
sid = this.prefix + sid;
this.client.remove(sid, fn);
};
return MedeaStore;
};