forked from wez/connect-riak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (112 loc) · 3 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
/**
* Module dependencies.
*/
var riak = require('riak-js');
/**
* Return the `RiakStore` extending `connect`'s session Store.
*
* @param {object} connect
* @return {Function}
* @api public
*/
module.exports = function(session) {
/**
* Connect's Store.
*/
var Store = session.Store;
/**
* Initialize RiakStore with the given `options`.
*
* @param {Object} options
* @api public
*/
function RiakStore(options) {
options = options || {};
Store.call(this, options);
this.client = options.client || new riak.getClient(options);
this.bucket = options.bucket || '_sessions';
this.dbOptions = { encodeUri: true, debug: false };
if (options.reapInterval > 0) {
setInterval(function() {
reapSessions(this);
}.bind(this), options.reapInterval);
}
};
/**
* Reap sessions, ignoring errors
*
* @param {Object} this
* @api public
*/
function reapSessions(self) {
self.client.mapreduce || self.client
.add(self.bucket)
.map(function(v) {
var data = Riak.mapValuesJson(v)[0];
// lame spidermonkey can't parse ISO8601 dates
var d = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(data.cookie.expires);
if (d) d = new Date(Date.UTC(+d[1], +d[2] - 1, +d[3], +d[4], +d[5], +d[6]));
if (+d - +new Date() <= 0) {
return [v.key];
} else {
return [];
}
})
.reduce('Riak.filterNotFound')
.run(function(err, expired) {
if (!err && expired && expired.unshift) {
expired.forEach(function(e) {
self.client.remove(self.bucket, e);
});
}
});
};
/**
* Inherit from `Store`.
*/
RiakStore.prototype.__proto__ = Store.prototype;
/**
* Attempt to fetch session by the given `sid`.
*
* @param {String} sid
* @param {Function} callback
* @api public
*/
RiakStore.prototype.get = function(sid, callback) {
this.client.get(this.bucket, sid, this.dbOptions, function(err, data, meta) {
if (err && err.notFound) return callback();
if (err) return callback(err);
callback(null, data);
});
};
/**
* Commit the given `session` object associated with the given `sid`.
*
* @param {String} sid
* @param {Session} session
* @param {Function} callback
* @api public
*/
RiakStore.prototype.set = function(sid, session, callback) {
this.client.save(this.bucket, sid, session, this.dbOptions, callback);
};
/**
* Destroy the session associated with the given `sid`.
*
* @param {String} sid
* @api public
*/
RiakStore.prototype.destroy = function(sid, callback) {
this.client.remove(this.bucket, sid, this.dbOptions, callback);
};
/**
* Fetch number of sessions.
*
* @param {Function} callback
* @api public
*/
RiakStore.prototype.length = function(callback) {
this.client.count(this.bucket, callback);
};
return RiakStore;
};