mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
Refactored errorMessage in DDPRateLimiter
This commit is contained in:
@@ -1,19 +1,13 @@
|
||||
// Rate Limiter built into DDP
|
||||
DDPRateLimiter = {}
|
||||
// Rate Limiter built into DDP with a default error message.
|
||||
DDPRateLimiter = {
|
||||
errorMessage : function (rateLimitResult) {
|
||||
return "Error, too many requests. Please slow down. You must wait " + Math.ceil(
|
||||
rateLimitResult.timeToReset / 1000) + " seconds before trying again.";
|
||||
}
|
||||
}
|
||||
|
||||
DDPRateLimiter.rateLimiter = new RateLimiter();
|
||||
|
||||
// Add a default rule of limiting logins to 5 times per 10 seconds by IP address.
|
||||
// Override using DDPRateLimiter.config
|
||||
DDPRateLimiter.rateLimiter.addRule({
|
||||
userId: null,
|
||||
ipAddr: function (ipAddr) {
|
||||
return true;
|
||||
},
|
||||
type: 'method',
|
||||
name: 'login'
|
||||
}, 5, 10000);
|
||||
|
||||
// DDPRateLimiter.rateLimiter.addRule( {
|
||||
// userId: null,
|
||||
// ipAddr: function (ipAddr) {
|
||||
@@ -24,8 +18,14 @@ DDPRateLimiter.rateLimiter.addRule({
|
||||
// }, 5, 10000);
|
||||
|
||||
DDPRateLimiter.getErrorMessage = function (rateLimitResult) {
|
||||
return "Error, too many requests. Please slow down. You must wait " + Math.ceil(
|
||||
rateLimitResult.timeToReset / 1000) + " seconds before trying again.";
|
||||
if (typeof this.errorMessage === 'function')
|
||||
return this.errorMessage(rateLimitResult);
|
||||
else
|
||||
return this.errorMessage;
|
||||
}
|
||||
|
||||
DDPRateLimiter.setErrorMessage = function (message) {
|
||||
this.errorMessage = message;
|
||||
}
|
||||
|
||||
DDPRateLimiter.config = function (rules) {
|
||||
@@ -34,4 +34,16 @@ DDPRateLimiter.config = function (rules) {
|
||||
|
||||
DDPRateLimiter.addRule = function (rule, numRequests, intervalTime) {
|
||||
DDPRateLimiter.rateLimiter.addRule(rule, numRequests, intervalTime);
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
// Add a default rule of limiting logins to 5 times per 10 seconds by IP address.
|
||||
// Override using DDPRateLimiter.config
|
||||
DDPRateLimiter.addRule({
|
||||
userId: null,
|
||||
ipAddr: function (ipAddr) {
|
||||
return true;
|
||||
},
|
||||
type: 'method',
|
||||
name: 'login'
|
||||
}, 5, 10000);
|
||||
|
||||
Reference in New Issue
Block a user