kopia lustrzana https://github.com/c9/core
Add ratelimits to collab
rodzic
f46f266c0a
commit
cf52ebf36a
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
|
Ładowanie…
Reference in New Issue