-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
281 lines (240 loc) · 7.88 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
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
/*
* This file is part of the bass-express-session module.
*
* (c) Anthony Matarazzo <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
// core libs
const { EventEmitter } = require('events');
/**
* express-session store module function factory
* @param {Object} session The express session object
* @return {BassStore} class
*/
module.exports = session => class BassStore extends (session && session.Store || EventEmitter) {
/**
*
* @param {Object} options
* @throws TypeError if a Bass session cannot be found or created
*/
constructor(options) {
const {
ttl,
prefix,
bass,
manager,
document,
expireField,
dataField,
sidField
} = options;
super(options);
this.prefix = prefix === null || prefix === undefined ? 'sess' : prefix;
this.ttl = ttl;
this.hasTtl = (ttl && ttl > 0) || ttl === undefined;
// we want the session from bass - try to resolve it
let session = bass;
if (session instanceof Function) {
session = session();
}
if (!session) {
throw new TypeError(
'Expecting options.bass to be an instance of Bass, Session or function returning one.');
}
if (session.createSession instanceof Function) {
session = session.createSession();
}
if (!(session.getManager instanceof Function)) {
throw new TypeError('Could not resolve a Bass Session.');
}
this.bass = session;
this.manager = this.bass.getManager(manager);
this.document = document;
this.expireField = expireField;
this.dataField = dataField;
this.sidField = sidField;
// bass should already be connected (persistent), emit connect right away
this.emit('connect');
}
/**
* Get the sid with the prefix attached
*
* @param {String} sid
* @returns {String}
*/
psid(sid) {
return this.prefix + sid;
}
/**
* Create a new empty session document
* @param {String} psid The session id, already prefixed
* @returns {Object}
*/
createDocument(psid) {
return this.manager.createDocument(this.document, {
[this.sidField]: psid,
[this.dataField]: {}
});
}
/**
* Get the configured TTL
*
* @param {String} sid
* @param {Object} session
* @returns {Number}
*/
getTtl(sid, session) {
if (!isNaN(this.ttl)) {
return this.ttl;
}
if (typeof this.ttl === 'function') {
return this.ttl(this, sid, session);
}
return 0;
}
/**
* Get find-by criteria
*
* @param {Object} [criteria] Criteria to start with
* @param {Date} [expireDate] The date to check expireField against
* @returns {Object}
*/
criteria(criteria = {}, expireDate = new Date()) {
if (this.expireField && this.hasTtl) {
criteria[this.expireField] = {'$gt': expireDate};
}
return criteria;
}
/**
* This optional method is used to get all sessions in the store as an array.
* The callback should be called as callback(error, sessions).
*
* @param {Function} callback
*/
all(callback) {
this.manager.findBy(this.document, this.criteria()).then(documents => {
callback(null, documents.map(document => document[this.dataField]));
}).catch(err => callback(err));
}
/**
* This required method is used to get a session from the store given a session ID (sid).
* The callback should be called as callback(error, session).
*
* @param sid
* @param callback
*/
get(sid, callback) {
const psid = this.psid(sid);
this.manager.findOneBy(this.document, this.criteria({
[this.sidField]: psid
})).then(document => {
if (!document) {
callback();
return;
}
callback(null, document[this.dataField]);
}).catch(err => callback(err));
}
/**
* This required method is used to upsert a session into the store given a session ID (sid)
* and session (session) object. The callback should be called as callback(error) once the
* session has been set in the store.
*
* @param sid
* @param session
* @param callback
*/
set(sid, session, callback) {
const psid = this.psid(sid);
this.manager.findOneBy(this.document, this.criteria({
[this.sidField]: psid
})).then(document => {
if (!document) {
document = this.createDocument(psid);
}
if (this.expireField && this.hasTtl) {
let d = new Date();
d.setTime(d.getTime() + (this.getTtl(sid, session) * 1000));
document[this.expireField] = d;
}
document[this.dataField] = session;
this.manager.persist(document);
this.manager.flush(document).then(() => {
callback(null, session);
}).catch(err => callback(err));
});
}
/**
* This required method is used to destroy/delete a session from the store given a session ID
* The callback should be called as callback(error) once the session is destroyed.
*
* @param {String|Array<{String}>} sid
* @param {Function} callback
*/
destroy(sid, callback) {
if (!Array.isArray(sid)) {
sid = [sid];
}
Promise.all(
// map sid's to removeBy promises
sid.map(id => this.manager.removeBy(this.document, {[this.sidField]: this.psid(id)}))
).then(data => {
// when all sid's are removed, callback
callback(null);
}).catch(err => callback(err));
}
/**
* This optional method is used to delete all sessions from the store.
* The callback should be called as callback(error) once the store is cleared.
*
* @param {Function} callback
*/
clear(callback) {
this.manager.removeBy(this.document, {}).then(() => {
callback();
}).catch(err => callback(err));
}
/**
* This optional method is used to get the count of all sessions in the store.
* The callback should be called as callback(error, len).
*
* @param callback
*/
length(callback) {
this.manager.findCountBy(this.document, this.criteria())
.then(count => callback(null, count))
.catch(err => callback(err));
}
/**
* This recommended method is used to "touch" a given session given a session ID (sid) and
* session (session) object.
*
* The callback should be called as callback(error) once the session has been touched.
*
* This is primarily used when the store will automatically delete idle sessions and this
* method is used to signal to the store the given session is active, potentially resetting
* the idle timer.
*
* @param {String|Array<String>} sid
* @param {Object} session
* @param {Function} callback
*/
touch(sid, session, callback) {
if (!this.expireField || !this.hasTtl) {
callback(null);
return;
}
if (!Array.isArray(sid)) {
sid = [sid];
}
let d = new Date();
d.setTime(d.getTime() + (this.getTtl(sid, session) * 1000));
this.manager.updateBy(
this.document,
{[this.sidField]: {'$in': sid.map(id => this.psid(id))}},
{[this.expireField]: d, [this.dataField]: session}
).then(() => callback(null)).catch(err => callback(err));
}
};