A relatively simple utility for abstract rate limiting. This library uses memory storage (i.e. does not rely on external database or writing data on your file system). Rate limits are reset if the process is restarted.
npm i cldn-ratelimitHere is a very simple demo demonstrating very basic limiting of login attempts.
import {RateLimit} from 'cldn-ratelimit';
const rateLimit = new RateLimit("login-attempts", 3, 60); // max 3 requests per 60 seconds
const attemptLogin = (username, password) => {
if (!rateLimit.attempt(username).allow) return "rate-limited";
if (username === "john.doe" && password === "password123") return "success";
return "wrong-password";
}
attemptLogin("john.doe", "wrongpass"); //-> "wrong-password"
attemptLogin("john.doe", "wrongpass2"); //-> "wrong-password"
attemptLogin("john.doe", "wrongpass"); //-> "wrong-password"
attemptLogin("john.doe", "password123"); //-> "rate-limited"
// wait 60 seconds
attemptLogin("john.doe", "password123"); //-> "success"If you want to reset the rate limit after a successful login, call rateLimit.reset(username).
Table of contents
- Class:
RateLimit- Static method:
RateLimit.attempt(name, source, [attempts], [callback]) - Static method:
RateLimit.check(name, source, [callback]) - Static method:
RateLimit.clear(name) - Static method:
RateLimit.create(name, limit, timeWindow) - Static method:
RateLimit.delete(name) - Static method:
RateLimit.get(name) - Static method:
RateLimit.reset(name, source) - Static method:
RateLimit.setRemaining(name, source, remaining) new RateLimit(name, limit, timeWindow)rateLimit.attempt(source, [attempts], [callback])rateLimit.check(source, [callback])rateLimit.clear()rateLimit.delete()rateLimit.limitrateLimit.namerateLimit.reset(source)rateLimit.setRemaining(source, remaining)rateLimit.timeWindow
- Static method:
- Interface:
AttemptResult
Rate limit
Make an attempt with a source ID
namestringThe name of the rate limitsourcestringUnique source identifier (e.g. username, IP, etc.)attemptsnumberThe number of attempts to make. Default:1callbackfunctionCallback function. Default:undefinedresultAttemptResultThe result of the attempt- Returns:
void
- Returns:
AttemptResult - Throws:
ErrorIf the rate limit does not exist
Check the attempt state for a source ID without decrementing the remaining attempts
namestringThe name of the rate limitsourcestringUnique source identifier (e.g. username, IP, etc.)callbackfunctionCallback function. Default:undefinedresultAttemptResultThe result of the attempt- Returns:
void
- Returns:
AttemptResult - Throws:
ErrorIf the rate limit does not exist
Clear rate limit attempts storage. This is equivalent to resetting all rate limits.
Create a new rate limit
namestringThe name of the rate limitlimitnumberThe number of attempts allowed per time window (e.g. 60)timeWindownumberThe time window in seconds (e.g. 60)- Returns:
RateLimit
Delete the rate limit instance. After it is deleted, it should not be used any further without constructing a new instance.
Get a rate limit instance
Reset limit for a source ID. The storage entry will be deleted and a new one will be created on the next attempt.
namestringThe name of the rate limitsourcestringUnique source identifier (e.g. username, IP, etc.)- Returns:
void - Throws:
ErrorIf the rate limit does not exist
Set the remaining attempts for a source ID.
Warning: This is not recommended as the remaining attempts depend on the limit of the instance.
namestringThe name of the rate limitsourcestringUnique source identifier (e.g. username, IP, etc.)remainingnumberThe number of remaining attempts- Returns:
void - Throws:
ErrorIf the rate limit does not exist
Create a new rate limit
namestringThe name of the rate limitlimitnumberThe number of attempts allowed per time window (e.g. 60)timeWindownumberThe time window in seconds (e.g. 60)- Throws:
ErrorIf the rate limit already exists
Make an attempt with a source ID
sourcestringUnique source identifier (e.g. username, IP, etc.)attemptsnumberThe number of attempts to make. Default:1callbackfunctionCallback functionresultAttemptResultThe result of the attempt- Returns:
void
- Returns:
AttemptResult
Check the attempt state for a source ID without decrementing the remaining attempts
sourcestringUnique source identifier (e.g. username, IP, etc.)callbackfunctionCallback functionresultAttemptResultThe result of the attempt- Returns:
void
- Returns:
AttemptResult
Clear rate limit attempts storage. This is equivalent to resetting all rate limits.
- Returns:
void
Delete the rate limit instance. After it is deleted, it should not be used any further without constructing a new instance.
- Returns:
void
The number of requests allowed per time window
- Type:
number
Get rate limit name
- Type:
string - Readonly
Reset limit for a source ID. The storage entry will be deleted and a new one will be created on the next attempt.
Set the remaining attempts for a source ID.
Warning: This is not recommended as the remaining attempts depend on the limit of the instance.
sourcestringUnique source identifier (e.g. username, IP, etc.)remainingnumberThe number of remaining attempts- Returns:
void
The time window in seconds (e.g. 60)
- Type:
number
The result from a rate limit attempt
The number of requests this rate limit allows per time window
- Type:
number - Readonly
The number of requests remaining in the current time window
- Type:
number - Readonly
The number of seconds until the current time window resets
- Type:
number - Readonly
The rate limit that this attempt was made on
- Type:
RateLimit - Readonly
Whether this attempt should be allowed to proceed. If false, the attempt is rate limited.
- Type:
boolean - Readonly