Add ratelimits to collab

pull/365/head
Tim Robinson 2016-09-29 21:46:48 +00:00
rodzic f46f266c0a
commit cf52ebf36a
2 zmienionych plików z 20 dodań i 5 usunięć

8
node_modules/c9/ratelimit.js wygenerowano vendored
Wyświetl plik

@ -30,8 +30,14 @@ function ratelimit(key, duration, max) {
return true;
}
function resolveValue(obj, path) {
return path.split('.').reduce(function(prev, curr) {
return prev ? prev[curr] : undefined;
}, obj);
}
return function(req, res, next) {
var handle = req.params[key];
var handle = resolveValue(req.params, key);
requests[handle] = requests[handle] || [];
if (requests[handle].length >= max) {

17
node_modules/c9/ratelimit_test.js wygenerowano vendored
Wyświetl plik

@ -31,7 +31,16 @@ describe("ratelimit", function() {
});
});
it("Should work with deep keys", function (done) {
var limiter = ratelimit("user.id", 10, 1);
limiter({params: {user: {id: "hey"}}}, null, function (err) {
assert(!err, err);
limiter({params: {user: {id: "yay"}}}, null, function (err) {
assert(!err, err);
done();
});
});
});
it("Should work again after a delay", function (done) {
var limiter = ratelimit("username", 10, 1);
@ -73,13 +82,13 @@ describe("ratelimit", function() {
function(next) {
limiter({params: {username: "mario"}}, null, function(err) {
assert(!err, err);
setTimeout(next, 25);
setTimeout(next, 40);
});
},
function (next) {
limiter({params: {username: "mario"}}, null, function(err) {
assert(!err, err);
setTimeout(next, 40);
setTimeout(next, 45);
});
},
function (next) {
@ -88,7 +97,7 @@ describe("ratelimit", function() {
limiter({params: {username: "mario"}}, null, function(err) {
assert(err);
assert.equal(err.code, 429);
setTimeout(next, 20);
setTimeout(next, 40);
});
});
},