redis-sessions
Advanced tools
Comparing version 0.1.9 to 0.1.10
148
index.js
// Generated by CoffeeScript 1.6.2 | ||
/* | ||
Redis Sessions | ||
The MIT License (MIT) | ||
Copyright © 2013 Patrick Liess, http://www.tcs.de | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
(function() { | ||
@@ -19,3 +34,10 @@ var RedisInst, RedisSessions, _, | ||
this.redisns = redisns != null ? redisns : "rs:"; | ||
this.soid = __bind(this.soid, this); | ||
this.set = __bind(this.set, this); | ||
this.killsoid = __bind(this.killsoid, this); | ||
this.killall = __bind(this.killall, this); | ||
this.kill = __bind(this.kill, this); | ||
this.get = __bind(this.get, this); | ||
this.create = __bind(this.create, this); | ||
this.activity = __bind(this.activity, this); | ||
this.redis = RedisInst.createClient(redisport, redishost); | ||
@@ -150,3 +172,3 @@ } | ||
appkey = "" + this.redisns + options.app + ":_sessions"; | ||
return this.redis.zrange(appkey, 0, -1, function(err, resp) { | ||
this.redis.zrange(appkey, 0, -1, function(err, resp) { | ||
var e, globalkeys, mc, tokenkeys, _i, _len; | ||
@@ -178,3 +200,3 @@ | ||
RedisSessions.prototype.soid = function(options, cb) { | ||
RedisSessions.prototype.killsoid = function(options, cb) { | ||
var _this = this; | ||
@@ -187,3 +209,3 @@ | ||
this.redis.zrevrange("" + this.redisns + options.app + ":_sessions", 0, -1, function(err, resp) { | ||
var e, mc, toget, _i, _len; | ||
var e, mc, token, _i, _len; | ||
@@ -196,30 +218,24 @@ if (err) { | ||
cb(null, { | ||
sessions: [] | ||
kill: 0 | ||
}); | ||
return; | ||
} | ||
toget = []; | ||
mc = []; | ||
for (_i = 0, _len = resp.length; _i < _len; _i++) { | ||
e = resp[_i]; | ||
if (e.split(':')[1] === options.id) { | ||
toget.push(e.split(':')[0]); | ||
if (!(e.split(':')[1] === options.id)) { | ||
continue; | ||
} | ||
token = e.split(':')[0]; | ||
mc.push(["zrem", "" + _this.redisns + options.app + ":_sessions", "" + token + ":" + options.id]); | ||
mc.push(["zrem", "" + _this.redisns + "SESSIONS", "" + options.app + ":" + token + ":" + options.id]); | ||
mc.push(["del", "" + _this.redisns + options.app + ":" + token]); | ||
} | ||
if (!toget.length) { | ||
if (!mc.length) { | ||
cb(null, { | ||
sessions: [] | ||
kill: 0 | ||
}); | ||
} | ||
mc = (function() { | ||
var _j, _len1, _results; | ||
_results = []; | ||
for (_j = 0, _len1 = toget.length; _j < _len1; _j++) { | ||
e = toget[_j]; | ||
_results.push(["hmget", "" + this.redisns + options.app + ":" + e, "id", "r", "w", "ttl", "d", "la", "ip"]); | ||
} | ||
return _results; | ||
}).call(_this); | ||
_this.redis.multi(mc).exec(function(err, resp) { | ||
var o; | ||
var total, _j, _len1; | ||
@@ -230,14 +246,14 @@ if (err) { | ||
} | ||
o = (function() { | ||
var _j, _len1, _results; | ||
_results = []; | ||
for (_j = 0, _len1 = resp.length; _j < _len1; _j++) { | ||
e = resp[_j]; | ||
_results.push(this._prepareSession(e)); | ||
} | ||
return _results; | ||
}).call(_this); | ||
total = 0; | ||
for (_j = 0, _len1 = resp.length; _j < _len1; _j++) { | ||
e = resp[_j]; | ||
total += e; | ||
} | ||
total = total / 3; | ||
if (total !== resp.length / 3) { | ||
cb("Unknow Error: " + JSON.stringify(resp), null); | ||
return; | ||
} | ||
cb(null, { | ||
sessions: o | ||
kill: total | ||
}); | ||
@@ -296,2 +312,69 @@ }); | ||
RedisSessions.prototype.soid = function(options, cb) { | ||
var _this = this; | ||
options = this._validate(options, ["app", "id"], cb); | ||
if (options === false) { | ||
return; | ||
} | ||
this.redis.zrevrange("" + this.redisns + options.app + ":_sessions", 0, -1, function(err, resp) { | ||
var e, mc, toget, _i, _len; | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
if (!resp.length) { | ||
cb(null, { | ||
sessions: [] | ||
}); | ||
return; | ||
} | ||
toget = []; | ||
for (_i = 0, _len = resp.length; _i < _len; _i++) { | ||
e = resp[_i]; | ||
if (e.split(':')[1] === options.id) { | ||
toget.push(e.split(':')[0]); | ||
} | ||
} | ||
if (!toget.length) { | ||
cb(null, { | ||
sessions: [] | ||
}); | ||
return; | ||
} | ||
mc = (function() { | ||
var _j, _len1, _results; | ||
_results = []; | ||
for (_j = 0, _len1 = toget.length; _j < _len1; _j++) { | ||
e = toget[_j]; | ||
_results.push(["hmget", "" + this.redisns + options.app + ":" + e, "id", "r", "w", "ttl", "d", "la", "ip"]); | ||
} | ||
return _results; | ||
}).call(_this); | ||
_this.redis.multi(mc).exec(function(err, resp) { | ||
var o; | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
o = (function() { | ||
var _j, _len1, _results; | ||
_results = []; | ||
for (_j = 0, _len1 = resp.length; _j < _len1; _j++) { | ||
e = resp[_j]; | ||
_results.push(this._prepareSession(e)); | ||
} | ||
return _results; | ||
}).call(_this); | ||
cb(null, { | ||
sessions: o | ||
}); | ||
}); | ||
}); | ||
}; | ||
RedisSessions.prototype._createMultiStatement = function(app, token, id, ttl) { | ||
@@ -312,4 +395,3 @@ var now; | ||
} | ||
t += 'Z' + new Date().getTime().toString(36); | ||
return t; | ||
return t + 'Z' + new Date().getTime().toString(36); | ||
}; | ||
@@ -316,0 +398,0 @@ |
{ | ||
"name": "redis-sessions", | ||
"description": "A universal session store for Redis", | ||
"version": "0.1.9", | ||
"version": "0.1.10", | ||
"author": "P. Liess <smrchy+npm@gmail.com>", | ||
@@ -6,0 +6,0 @@ "engines": { |
@@ -5,6 +5,6 @@ # Redis Sessions | ||
There is a need to maintain a **universal session across different application server platforms**. | ||
This is a NodeJS module to keep sessions in a Redis datastore and add some useful methods. | ||
The main purpose of this module is to generalize sessions across application server platforms. We use nginx reverse proxy to route parts of a website to a NodeJS server and other parts could be Python, .net, PHP, Coldfusion or Java servers. You can then use [rest-sessions](https://github.com/smrchy/rest-sessions) to access the same sessions in your other app server via a REST interface. | ||
## Installation | ||
@@ -14,18 +14,32 @@ | ||
Make sure Redis runs locally on localhost and run the test: `npm test` | ||
## Basics | ||
* Every session belongs to an app (e.g. `webapp`, `app_cust123`). | ||
* A session is created by supplying the app and an id (usually the unique id of the user). A token will be returned. | ||
* A session is queried with the app and token. | ||
* Additional data (key/value) can be stored in the session. | ||
* A session can be killed with the app and token. | ||
* All sessions of an app can be killed. | ||
* `create`: A session is created by supplying the app and an id (usually the unique id of the user). A token will be returned. | ||
* `get`: A session is queried with the app and token. | ||
* `set`: Additional data (key/value) can be stored in the session. | ||
* `kill`: A session can be killed with the app and token. | ||
* `killall`: All sessions of an app can be killed. | ||
## Additional methods | ||
* TODO: Get an array of all sessions of an app, complete with `idle`, `ip` which were active within the last *n* seconds. | ||
* Get the amount of active sessions of an app within the last *n* seconds. | ||
* Get all sessions of a single id. | ||
* TODO: Kill all sessions that belong to a single id. E.g. log out user123 on all devices. | ||
* `activity`: Get the amount of active sessions of an app within the last *n* seconds. | ||
* `soid`: Get all sessions of a single id. | ||
* `killsoid`: Kill all sessions that belong to a single id. E.g. log out user123 on all devices. | ||
* TODO: Get an array of all sessions of an app which were active within the last *n* seconds. | ||
## Performance | ||
With Redis being run on the same machine the test script (run via `npm test`) on a 2009 iMac: | ||
* Creates 1000 sessions in around 200ms. | ||
* Gets those 1000 sessions and validates them in around 240ms. | ||
* Removes those 1000 sessions in 8ms. | ||
## Useage via REST | ||
See [rest-sessions](https://github.com/smrchy/rest-sessions). | ||
## Usage in NodeJS | ||
@@ -189,3 +203,3 @@ | ||
Get all sessions within an app that belong to a single id. This would be all sessions from a single user in case he is logged in on different browsers / devices. | ||
Get all sessions within an app that belong to a single id. This would be all sessions of a single user in case he is logged in on different browsers / devices. | ||
@@ -220,2 +234,18 @@ ```javascript | ||
### Kill all sessions of an id | ||
Kill all sessions of an id within an app: | ||
```javascript | ||
rs.killsoid({app: app, id: 'bulkuser_999'}, | ||
function(err, resp) { | ||
/* | ||
resp contains the result: | ||
{kill: 2} // The amount of sessions that were killed | ||
*/ | ||
}); | ||
``` | ||
### Killall | ||
@@ -236,1 +266,11 @@ | ||
``` | ||
## The MIT License (MIT) | ||
Copyright © 2013 Patrick Liess, http://www.tcs.de | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
@@ -14,3 +14,3 @@ // Generated by CoffeeScript 1.6.2 | ||
describe('Redis-Sessions Test', function() { | ||
var app1, app2, rs, token1, token2, token3; | ||
var app1, app2, bulksessions, rs, token1, token2, token3; | ||
@@ -23,2 +23,3 @@ rs = null; | ||
token3 = null; | ||
bulksessions = []; | ||
before(function(done) { | ||
@@ -227,2 +228,3 @@ done(); | ||
e.should.have.keys('token'); | ||
bulksessions.push(e.token); | ||
e.token.length.should.equal(64); | ||
@@ -245,2 +247,25 @@ } | ||
}); | ||
it('Get 1000 sessions for app2: succeed', function(done) { | ||
var e, i, pq, _i, _len; | ||
pq = []; | ||
for (i = _i = 0, _len = bulksessions.length; _i < _len; i = ++_i) { | ||
e = bulksessions[i]; | ||
pq.push({ | ||
app: app2, | ||
token: e | ||
}); | ||
} | ||
async.map(pq, rs.get, function(err, resp) { | ||
var _j, _len1; | ||
resp.length.should.equal(1000); | ||
for (i = _j = 0, _len1 = resp.length; _j < _len1; i = ++_j) { | ||
e = resp[i]; | ||
e.should.have.keys('id', 'r', 'w', 'ttl', 'idle', 'ip'); | ||
e.id.should.equal("bulkuser_" + i); | ||
} | ||
done(); | ||
}); | ||
}); | ||
it('Create a session for bulkuser_999 with valid data: should return a token', function(done) { | ||
@@ -272,2 +297,26 @@ rs.create({ | ||
}); | ||
it('Remove those 2 sessions for bulkuser_999', function(done) { | ||
rs.killsoid({ | ||
app: app2, | ||
id: "bulkuser_999" | ||
}, function(err, resp) { | ||
should.not.exist(err); | ||
should.exist(resp); | ||
resp.should.have.keys('kill'); | ||
resp.kill.should.equal(2); | ||
done(); | ||
}); | ||
}); | ||
it('Check if we have still have sessions for bulkuser_999: should return 0', function(done) { | ||
rs.soid({ | ||
app: app2, | ||
id: "bulkuser_999" | ||
}, function(err, resp) { | ||
should.not.exist(err); | ||
should.exist(resp); | ||
resp.should.have.keys('sessions'); | ||
resp.sessions.length.should.equal(0); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
@@ -274,0 +323,0 @@ describe('GET: Part 2', function() { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
69856
1082
272