-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
152 lines (137 loc) · 3.98 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
var util = require('util'),
MongoClient = require('mongodb').MongoClient;
/**
* Return the `MongoStore` extending `connect`'s session Store.
*
* @param {object} connect
* @return {Function}
* @api public
*/
module.exports = function(connect) {
/**
* Initialize a new `MongoStore`.
*
* @api public
*/
function MongoStore(uri, options) {
var self = this
this.options = options || (options = {})
options.collectionName || (options.collectionName = 'sessions')
// 1 day
options.ttl || (options.ttl = 24 * 60 * 60 * 1000)
// 60 s
options.cleanupInterval || (options.cleanupInterval = 60 * 1000)
options.server || (options.server = {})
options.server.auto_reconnect != null || (options.server.auto_reconnect = true)
this._error = function(err) {
if (err) self.emit('error', err)
}
// It's a Db instance.
if (uri.collection) {
this.db = uri
this._setup()
} else {
MongoClient.connect(uri, options, function(err, db) {
if (err) return self._error(err)
self.db = db
self._setup()
})
}
}
util.inherits(MongoStore, connect.session.Store)
/**
* Attempt to fetch session by the given `id`.
*
* @param {String} id
* @param {Function} callback
* @api public
*/
MongoStore.prototype.get = function(id, callback) {
this.collection.findOne({_id: id}, function(err, doc) {
callback(err, doc ? doc.sess : null)
})
}
/**
* Commit the given `sess` object associated with the given `id`.
*
* @param {String} id
* @param {Session} sess
* @param {Function} [callback]
* @api public
*/
MongoStore.prototype.set = function(id, sess, callback) {
this.collection.update(
{_id: id},
{$set: {
sess: sess,
expires: Date.now() + this.options.ttl
}},
{upsert: true},
callback || this._error
)
}
/**
* Destroy the session associated with the given `id`.
*
* @param {String} id
* @param {Function} [callback]
* @api public
*/
MongoStore.prototype.destroy = function(id, callback) {
this.collection.remove({_id: id}, callback || this._error)
}
/**
* Invoke the given callback `callback` with all active sessions.
*
* @param {Function} callback
* @api public
*/
MongoStore.prototype.all = function(callback) {
this.collection.find().toArray(function(err, docs) {
var sess = []
if (err) return callback(err)
docs.forEach(function(doc) {
sess.push(doc.sess)
})
callback(null, sess)
})
}
/**
* Clear all sessions.
*
* @param {Function} [callback]
* @api public
*/
MongoStore.prototype.clear = function(callback) {
this.collection.remove({}, callback || this._error)
}
/**
* Fetch number of sessions.
*
* @param {Function} callback
* @api public
*/
MongoStore.prototype.length = function(callback) {
this.collection.count({}, callback)
}
/**
* Setup collection, cleanup, error handler.
*/
MongoStore.prototype._setup = function() {
var self = this
this.db
.on('error', this._error)
.createCollection(
this.options.collectionName,
function(err, collection) {
if (err) return self._error(err)
self.collection = collection
setInterval(function() {
collection.remove({expires: {$lt: Date.now()}}, self._error)
}, self.options.cleanupInterval)
self.emit('connect')
}
)
}
return MongoStore
}