-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
187 lines (140 loc) · 6.21 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
// Connect - MongoDB - Simple
// Copyright(c) 2013 BeauCoo
// Author: Rick Cotter
// MIT Licensed
//
// Reasoning:
// - Few dependencies equals easy to keep up to date.
// - Let what could be dependencies do what they do best i.e. https://github.com/mongodb/node-mongodb-native
// - Decoupled from Connect (for no particular reason a la connect-redis)
//
// Attribution:
// - Initial implementation inspired by connect-mongodb
// - Updates by nathanbower (from un-committed pull request #60)
// - Heavy referencing to connect-redis
var util = require('util');
var _ = require('lodash');
// Return the `MongoStore` extending `connect`'s session Store.
module.exports = function (connect, testDbCollection) {
"use strict";
var Store = connect.session.Store;
var dbCollection = testDbCollection ? testDbCollection : null;
var modifyFunc = null;
var ttl = null;
// Connect does not seem to guarantee providing callbacks
function getSafeCallback(callback) {
return (callback ? callback : function () {
});
}
// Initialize
// - 'db' is a required and open MongoDb connect
// - 'options' (optional):
// - 'ttl' (optional) is the time-to-live for sessions. If omitted, the cookie's maxAge will be used.
// - 'reapIntervalMs' (optional) specifies how often to check and remove expired sessions. Must be 1000ms or greater.
// - 'collectionName' (optional) specifies the sessions collection to use. Defaults to 'sessions'.
// - 'logReaping' (optional) true/false will log out when reaping occurs.
// - ...and is passed on to the Connect session Store.
// - 'aModifyFunc' (optional) can:
// - a) modify the given session and return null.
// - b) merge into the root session document by returning a hash.
// - c) or a combination of the two.
// - It has signature function (session) { return {...} or null; }
// - Is useful for adding meta-data that can be used by external queries.
// - 'callback' (optional) to know when instance creation has completed as async set up e.g. setting the collection.
function MongoStore(db, options, aModifyFunc, callback) {
if (!(this instanceof MongoStore)) {
return new MongoStore(db, options, aModifyFunc, callback);
}
options = options || {};
Store.call(this, options);
if (options.ttl) {
ttl = options.ttl;
}
modifyFunc = aModifyFunc;
var self = this;
function reap() {
dbCollection.remove({expires:{'$lte':Date.now()}}, function () {
if (options.logReaping === true) {
console.log("Reaping sessions at %s", new Date().toISOString());
}
});
}
// Start Reaping i.e polling for expired sessions for removal
// - Presumed that reaping is desired for lifetime of the process
// - Database connectivity can be intermittent in nature but is desired to be constant
// - The database driver does not reliable emit 'close' events. They are only emitted when no callback
// is provided to db.close() which is out of this module's scope.
// - Reaping is thus terminated on process exit
function startReaping() {
if (options.reapIntervalMs && 500 <= options.reapIntervalMs) {
console.log("Reaping sessions enabled at %s every %d milliseconds", new Date().toISOString(), options.reapIntervalMs);
var timerHandle = setInterval(reap, options.reapIntervalMs);
process.on('exit', function () {
console.log("Reaping sessions closed on process exit at %s", new Date().toISOString());
clearInterval(timerHandle);
});
} else {
console.log("Reaping sessions disabled");
}
}
callback = getSafeCallback(callback);
function getCollectionCallback(err, collection) {
if (err) {
return callback(err);
}
dbCollection = collection;
startReaping(); // Ensure collection exists before reaping begins
callback(null, self);
}
// Get collection
if (dbCollection) {
getCollectionCallback(null, dbCollection); // For testing
} else {
db.collection(options.collectionName || 'sessions', getCollectionCallback);
}
return this;
}
util.inherits(MongoStore, Store);
// Attempt to fetch session by the given `sid`.
MongoStore.prototype.get = function (sid, callback) {
callback = getSafeCallback(callback);
dbCollection.findOne({_id:sid}, function (err, data) {
if (err || !data) {
return callback(err);
}
var session = JSON.parse(data.session);
callback(null, session);
});
};
// Commit the given `session` object associated with the given `sid`
MongoStore.prototype.set = function (sid, session, callback) {
var update = {};
if (modifyFunc) {
var result = modifyFunc(session);
if (result) {
_.merge(update, result);
}
}
update.session = JSON.stringify(session);
var maxAgeSeconds = session.cookie.maxAge;
var calculatedTtl = ttl || (('number' === typeof maxAgeSeconds) ? (maxAgeSeconds * 1000) : 86400000); // Default to one day
update.expires = Date.now() + calculatedTtl;
callback = getSafeCallback(callback);
dbCollection.update({_id:sid}, {$set:update}, {upsert:true}, function (err, data) {
return callback.apply(this, arguments);
});
};
// Destroy the session associated with the given `sid`
MongoStore.prototype.destroy = function (sid, callback) {
dbCollection.remove({_id:sid}, getSafeCallback(callback));
};
// Fetch number of sessions
MongoStore.prototype.length = function (callback) {
dbCollection.count({}, getSafeCallback(callback));
};
// Clear all sessions
MongoStore.prototype.clear = function (callback) {
dbCollection.drop(getSafeCallback(callback));
};
return MongoStore;
};