redis-sessions
Advanced tools
Comparing version 0.2.0 to 0.3.0
78
index.js
@@ -27,3 +27,3 @@ // Generated by CoffeeScript 1.6.3 | ||
function RedisSessions(options) { | ||
var host, port, wipe; | ||
var host, port, wipe, _ref, _ref1; | ||
if (options == null) { | ||
@@ -34,2 +34,4 @@ options = {}; | ||
this._returnSessions = __bind(this._returnSessions, this); | ||
this._initErrors = __bind(this._initErrors, this); | ||
this._handleError = __bind(this._handleError, this); | ||
this.soid = __bind(this.soid, this); | ||
@@ -45,7 +47,12 @@ this.soapp = __bind(this.soapp, this); | ||
this.activity = __bind(this.activity, this); | ||
this._initErrors(); | ||
this.redisns = options.namespace || "rs"; | ||
this.redisns = this.redisns + ":"; | ||
port = options.port || 6379; | ||
host = options.host || "127.0.0.1"; | ||
this.redis = RedisInst.createClient(port, host); | ||
if ((options != null ? (_ref = options.client) != null ? (_ref1 = _ref.constructor) != null ? _ref1.name : void 0 : void 0 : void 0) === "RedisClient") { | ||
this.redis = options.client; | ||
} else { | ||
port = options.port || 6379; | ||
host = options.host || "127.0.0.1"; | ||
this.redis = RedisInst.createClient(port, host); | ||
} | ||
wipe = options.wipe || 600; | ||
@@ -401,2 +408,27 @@ if (wipe < 10) { | ||
RedisSessions.prototype._handleError = function(cb, err, data) { | ||
var _err, _ref; | ||
if (data == null) { | ||
data = {}; | ||
} | ||
if (_.isString(err)) { | ||
_err = new Error(); | ||
_err.name = err; | ||
_err.message = ((_ref = this._ERRORS) != null ? typeof _ref[err] === "function" ? _ref[err](data) : void 0 : void 0) || "unkown"; | ||
} else { | ||
_err = err; | ||
} | ||
cb(_err); | ||
}; | ||
RedisSessions.prototype._initErrors = function() { | ||
var key, msg, _ref; | ||
this._ERRORS = {}; | ||
_ref = this.ERRORS; | ||
for (key in _ref) { | ||
msg = _ref[key]; | ||
this._ERRORS[key] = _.template(msg); | ||
} | ||
}; | ||
RedisSessions.prototype._now = function() { | ||
@@ -485,3 +517,5 @@ return parseInt((new Date()).getTime() / 1000); | ||
if (!o[item]) { | ||
cb("No " + item + " supplied"); | ||
this._handleError(cb, "missingParameter", { | ||
item: item | ||
}); | ||
return false; | ||
@@ -491,3 +525,5 @@ } | ||
if (!this._VALID[item].test(o[item])) { | ||
cb("Invalid " + item + " format"); | ||
this._handleError(cb, "invalidFormat", { | ||
item: item | ||
}); | ||
return false; | ||
@@ -499,3 +535,5 @@ } | ||
if (_.isNaN(o.ttl) || !_.isNumber(o.ttl) || o.ttl < 10) { | ||
cb("ttl must be a positive integer >= 10"); | ||
this._handleError(cb, "invalidValue", { | ||
msg: "ttl must be a positive integer >= 10" | ||
}); | ||
return false; | ||
@@ -507,3 +545,5 @@ } | ||
if (_.isNaN(o[item]) || !_.isNumber(o[item]) || o[item] < 10) { | ||
cb("dt must be a positive integer >= 10"); | ||
this._handleError(cb, "invalidValue", { | ||
msg: "ttl must be a positive integer >= 10" | ||
}); | ||
return false; | ||
@@ -514,7 +554,11 @@ } | ||
if (!o[item]) { | ||
cb("No d supplied."); | ||
this._handleError(cb, "missingParameter", { | ||
item: item | ||
}); | ||
return false; | ||
} | ||
if (!_.isObject(o.d)) { | ||
cb("d must be an object."); | ||
this._handleError(cb, "invalidValue", { | ||
msg: "d must be an object" | ||
}); | ||
return false; | ||
@@ -524,3 +568,5 @@ } | ||
if (!keys.length) { | ||
cb("d must containt at least one key."); | ||
this._handleError(cb, "invalidValue", { | ||
msg: "d must containt at least one key." | ||
}); | ||
return false; | ||
@@ -530,3 +576,5 @@ } | ||
if (!_.isString(o.d[e]) && !_.isNumber(o.d[e]) && !_.isBoolean(o.d[e]) && !_.isNull(o.d[e])) { | ||
cb("d." + e + " has a forbidden type. Only strings, numbers, boolean and null are allowed."); | ||
this._handleError(cb, "invalidValue", { | ||
msg: "d." + e + " has a forbidden type. Only strings, numbers, boolean and null are allowed." | ||
}); | ||
return false; | ||
@@ -562,2 +610,8 @@ } | ||
RedisSessions.prototype.ERRORS = { | ||
"missingParameter": "No <%= item %> supplied", | ||
"invalidFormat": "Invalid <%= item %> format", | ||
"invalidValue": "<%= msg %>" | ||
}; | ||
return RedisSessions; | ||
@@ -564,0 +618,0 @@ |
{ | ||
"name": "redis-sessions", | ||
"description": "An advanced session store for Redis", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"author": "P. Liess <smrchy+npm@gmail.com>", | ||
@@ -6,0 +6,0 @@ "engines": { |
@@ -27,3 +27,3 @@ # Redis Sessions | ||
* `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. | ||
* `soapp`: Get an array of all sessions of an app which were active within the last *n* seconds. | ||
* Automatic cleanup of old sessions. | ||
@@ -30,0 +30,0 @@ |
@@ -36,3 +36,3 @@ // Generated by CoffeeScript 1.6.3 | ||
rs.get({}, function(err, resp) { | ||
err.should.equal("No app supplied"); | ||
err.message.should.equal("No app supplied"); | ||
done(); | ||
@@ -45,3 +45,3 @@ }); | ||
}, function(err, resp) { | ||
err.should.equal("Invalid app format"); | ||
err.message.should.equal("Invalid app format"); | ||
done(); | ||
@@ -54,3 +54,3 @@ }); | ||
}, function(err, resp) { | ||
err.should.equal("No token supplied"); | ||
err.message.should.equal("No token supplied"); | ||
done(); | ||
@@ -64,3 +64,3 @@ }); | ||
}, function(err, resp) { | ||
err.should.equal("Invalid token format"); | ||
err.message.should.equal("Invalid token format"); | ||
done(); | ||
@@ -74,3 +74,3 @@ }); | ||
}, function(err, resp) { | ||
err.should.equal("Invalid token format"); | ||
err.message.should.equal("Invalid token format"); | ||
done(); | ||
@@ -84,3 +84,3 @@ }); | ||
}, function(err, resp) { | ||
err.should.equal("Invalid token format"); | ||
err.message.should.equal("Invalid token format"); | ||
done(); | ||
@@ -104,3 +104,3 @@ }); | ||
rs.create({}, function(err, resp) { | ||
err.should.equal("No app supplied"); | ||
err.message.should.equal("No app supplied"); | ||
done(); | ||
@@ -113,3 +113,3 @@ }); | ||
}, function(err, resp) { | ||
err.should.equal("No id supplied"); | ||
err.message.should.equal("No id supplied"); | ||
done(); | ||
@@ -123,3 +123,3 @@ }); | ||
}, function(err, resp) { | ||
err.should.equal("No ip supplied"); | ||
err.message.should.equal("No ip supplied"); | ||
done(); | ||
@@ -135,3 +135,3 @@ }); | ||
}, function(err, resp) { | ||
err.should.equal("ttl must be a positive integer >= 10"); | ||
err.message.should.equal("ttl must be a positive integer >= 10"); | ||
done(); | ||
@@ -455,3 +455,3 @@ }); | ||
}, function(err, resp) { | ||
err.should.equal("No d supplied."); | ||
err.message.should.equal("No d supplied"); | ||
done(); | ||
@@ -466,3 +466,3 @@ }); | ||
}, function(err, resp) { | ||
err.should.equal("d must be an object."); | ||
err.message.should.equal("d must be an object"); | ||
done(); | ||
@@ -479,3 +479,3 @@ }); | ||
}, function(err, resp) { | ||
err.should.equal("d.arr has a forbidden type. Only strings, numbers, boolean and null are allowed."); | ||
err.message.should.equal("d.arr has a forbidden type. Only strings, numbers, boolean and null are allowed."); | ||
done(); | ||
@@ -494,3 +494,3 @@ }); | ||
}, function(err, resp) { | ||
err.should.equal("d.obj has a forbidden type. Only strings, numbers, boolean and null are allowed."); | ||
err.message.should.equal("d.obj has a forbidden type. Only strings, numbers, boolean and null are allowed."); | ||
done(); | ||
@@ -505,3 +505,3 @@ }); | ||
}, function(err, resp) { | ||
err.should.equal("d must containt at least one key."); | ||
err.message.should.equal("d must containt at least one key."); | ||
done(); | ||
@@ -508,0 +508,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
82542
1266