-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
151 lines (133 loc) · 4.04 KB
/
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
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
'use strict';
var Cache = require('node-cache'),
debug = require('debug')('mongoose-store'),
one_day = 1000 * 60 * 60 * 24;
module.exports = function (session, mongoose) {
var MongooseStore = function (options) {
options = options || {};
options.modelName = options.modelName || 'Session';
options.ttl = options.ttl || one_day;
this.ttl = options.ttl;
this.cache = new Cache({ stdTTL: options.cache_ttl || 300, useClones: false }),
session.Store.call(this, options);
this.Session = mongoose.model(options.modelName, new mongoose.Schema({
_id: { type: mongoose.Schema.ObjectId, select: false },
sid: { type: String, index: true },
session: { type: String },
created: { type: Date, default: Date.now, expires: parseInt(options.ttl / 1000, 10) },
}, { strict: false }));
};
MongooseStore.prototype.__proto__ = session.Store.prototype;
MongooseStore.prototype.get = function (sid, fn) {
debug('GET %s', sid);
this.cache.get(sid, function (err, data) {
if (!err && data) {
debug('Returning session from cache');
return fn(null, data);
}
this.Session.findOne({ sid: sid }).exec(function (err, data) {
if (err) {
debug('GET error: %s', err);
return fn(err);
}
if (!data) {
debug('GET no session found.');
return fn();
}
else {
var result;
try {
result = JSON.parse(data.session);
}
catch(e) {
debug('Error on JSON Parse of %s', data.session);
debug('Error was %s', e);
return fn(e);
}
debug('Setting cache and returning valid session.');
this.cache.set(sid, result);
return fn(null, result);
}
}.bind(this));
}.bind(this));
};
MongooseStore.prototype.set = function (sid, sess, fn) {
debug('SET %s', sid);
try {
var expires = Date.now() + this.ttl;
if (sess.cookie) {
sess.cookie.expires = new Date(expires);
}
else {
sess.cookie = { expires: new Date(expires) };
}
var s = { session: JSON.stringify(sess), created: Date.now() };
debug('Session record is: %s', JSON.stringify(s));
this.Session.findOneAndUpdate({ sid: sid }, s, { upsert: true }).exec(function (err, data) {
if (err) {
debug('SET error in DB call %s', err);
return fn(err);
}
debug('Session updated.');
var result;
try {
debug('Try to JSON parse %s', data.session);
result = JSON.parse(data.session);
}
catch(e) {
debug('Error after SET on JSON Parse %s', err);
return fn(err);
}
debug('Set cache and return the session %s', data.session);
this.cache.set(sid, result);
return fn(null, result);
}.bind(this));
}
catch (err) {
debug('SET error before query %s', err);
return fn(err);
}
};
MongooseStore.prototype.destroy = function (sid, fn) {
debug('DESTROY %s', sid);
this.Session.findOneAndRemove({ sid: sid }).exec(function (err) {
if (err) {
debug('DESTROY error, %s', err);
return fn(err);
}
else {
debug('DESTROY success');
if(typeof fn === 'function'){
return fn();
}
}
});
};
MongooseStore.prototype.clearAll = function (fn) {
debug('CLEARALL');
this.Session.remove({}).exec(function (err) {
if (err) {
debug('CLEARALL error, %s', err);
return fn(err);
}
else {
debug('CLEARALL success');
if(typeof fn === 'function'){
return fn();
}
}
});
};
MongooseStore.prototype.keepAlive = function () {
debug('KEEPALIVE Querying Mongoose for empty set.');
this.Session.find({ noexits: true }, function (err) {
if (err) {
debug('KEEPALIVE error, %s', err);
}
else {
debug('KEEPALIVE success');
}
});
};
return MongooseStore;
};