redis-sessions
Advanced tools
Comparing version 0.1.8 to 0.1.9
109
index.js
@@ -73,23 +73,14 @@ // Generated by CoffeeScript 1.6.2 | ||
thekey = "" + this.redisns + options.app + ":" + options.token; | ||
this.redis.hmget(thekey, "id", "r", "w", "ttl", "d", "la", function(err, resp) { | ||
this.redis.hmget(thekey, "id", "r", "w", "ttl", "d", "la", "ip", function(err, resp) { | ||
var mc, o; | ||
if (resp[0] === null) { | ||
cb(null, {}); | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
o = { | ||
id: resp[0], | ||
r: Number(resp[1]), | ||
w: Number(resp[2]), | ||
ttl: Number(resp[3]), | ||
idle: now - resp[5] | ||
}; | ||
if (o.ttl < o.idle) { | ||
o = _this._prepareSession(resp); | ||
if (o === null) { | ||
cb(null, {}); | ||
return; | ||
} | ||
if (resp[4]) { | ||
o.d = JSON.parse(resp[4]); | ||
} | ||
if (options._noupdate) { | ||
@@ -186,2 +177,68 @@ cb(null, o); | ||
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: [] | ||
}); | ||
} | ||
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.set = function(options, cb) { | ||
@@ -258,2 +315,26 @@ var _this = this; | ||
RedisSessions.prototype._prepareSession = function(session) { | ||
var now, o; | ||
now = this._now(); | ||
if (session[0] === null) { | ||
return null; | ||
} | ||
o = { | ||
id: session[0], | ||
r: Number(session[1]), | ||
w: Number(session[2]), | ||
ttl: Number(session[3]), | ||
idle: now - session[5], | ||
ip: session[6] | ||
}; | ||
if (o.ttl < o.idle) { | ||
return null; | ||
} | ||
if (session[4]) { | ||
o.d = JSON.parse(session[4]); | ||
} | ||
return o; | ||
}; | ||
RedisSessions.prototype._VALID = { | ||
@@ -260,0 +341,0 @@ app: /^([a-zA-Z0-9_-]){3,20}$/, |
{ | ||
"name": "redis-sessions", | ||
"description": "A universal session store for Redis", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"author": "P. Liess <smrchy+npm@gmail.com>", | ||
@@ -6,0 +6,0 @@ "engines": { |
@@ -24,5 +24,5 @@ # Redis Sessions | ||
* TODO: Get an array of all sessions of an app, complete with `lastactivity`, `ip` which were active within the last *n* seconds. | ||
* 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. | ||
* TODO: Get all sessions of a single id. | ||
* 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. | ||
@@ -186,3 +186,34 @@ | ||
``` | ||
### Sessions of Id | ||
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. | ||
```javascript | ||
rs.soid({ | ||
app: app, | ||
id: "bulkuser_999"}, | ||
function(err, resp) { | ||
/* | ||
resp contains the sessions: | ||
{ sessions: | ||
[ { id: 'bulkuser_999', | ||
r: 1, | ||
w: 1, | ||
ttl: 30, | ||
idle: 0, | ||
ip: '127.0.0.2' }, | ||
{ id: 'bulkuser_999', | ||
r: 1, | ||
w: 1, | ||
ttl: 7200, | ||
idle: 0, | ||
ip: '127.0.0.1' } | ||
] | ||
} | ||
*/ | ||
}); | ||
``` | ||
### Killall | ||
@@ -189,0 +220,0 @@ |
// Generated by CoffeeScript 1.6.2 | ||
(function() { | ||
var RedisSessions, async, path, should, _; | ||
var RedisSessions, async, should, _; | ||
_ = require("underscore"); | ||
path = require("path"); | ||
should = require("should"); | ||
@@ -244,2 +242,28 @@ | ||
}); | ||
it('Create a session for bulkuser_999 with valid data: should return a token', function(done) { | ||
rs.create({ | ||
app: app2, | ||
id: "bulkuser_999", | ||
ip: "127.0.0.2", | ||
ttl: 30 | ||
}, function(err, resp) { | ||
should.not.exist(err); | ||
should.exist(resp); | ||
resp.should.have.keys('token'); | ||
done(); | ||
}); | ||
}); | ||
it('Check if we have 2 sessions for bulkuser_999', 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(2); | ||
resp.sessions[0].id.should.equal("bulkuser_999"); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
@@ -254,3 +278,3 @@ describe('GET: Part 2', function() { | ||
resp.should.be.a('object'); | ||
resp.should.have.keys('id', 'r', 'w', 'ttl', 'idle'); | ||
resp.should.have.keys('id', 'r', 'w', 'ttl', 'idle', 'ip'); | ||
resp.id.should.equal("user1"); | ||
@@ -268,3 +292,3 @@ resp.ttl.should.equal(30); | ||
resp.should.be.a('object'); | ||
resp.should.have.keys('id', 'r', 'w', 'ttl', 'idle'); | ||
resp.should.have.keys('id', 'r', 'w', 'ttl', 'idle', 'ip'); | ||
resp.id.should.equal("user1"); | ||
@@ -318,3 +342,3 @@ resp.ttl.should.equal(30); | ||
resp.should.be.a('object'); | ||
resp.should.have.keys('id', 'r', 'w', 'ttl', 'idle'); | ||
resp.should.have.keys('id', 'r', 'w', 'ttl', 'idle', 'ip'); | ||
resp.id.should.equal("user2"); | ||
@@ -321,0 +345,0 @@ resp.ttl.should.equal(10); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
59584
962
232