Gathering detailed insights and metrics for cldn-ratelimit
Gathering detailed insights and metrics for cldn-ratelimit
Gathering detailed insights and metrics for cldn-ratelimit
Gathering detailed insights and metrics for cldn-ratelimit
npm install cldn-ratelimit
Typescript
Module System
Node Version
NPM Version
JavaScript (53.58%)
TypeScript (46.42%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
GPL-3.0 License
2 Stars
107 Commits
5 Branches
2 Contributors
Updated on Apr 15, 2025
Latest Version
1.2.3
Package Id
cldn-ratelimit@1.2.3
Unpacked Size
116.55 kB
Size
25.15 kB
File Count
24
NPM Version
10.0.0
Node Version
18.17.1
Published on
Sep 07, 2023
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
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
1npm i cldn-ratelimit
Here is a very simple demo demonstrating very basic limiting of login attempts.
1import {RateLimit} from 'cldn-ratelimit'; 2 3const rateLimit = new RateLimit("login-attempts", 3, 60); // max 3 requests per 60 seconds 4 5const attemptLogin = (username, password) => { 6 if (!rateLimit.attempt(username).allow) return "rate-limited"; 7 if (username === "john.doe" && password === "password123") return "success"; 8 return "wrong-password"; 9} 10 11attemptLogin("john.doe", "wrongpass"); //-> "wrong-password" 12attemptLogin("john.doe", "wrongpass2"); //-> "wrong-password" 13attemptLogin("john.doe", "wrongpass"); //-> "wrong-password" 14attemptLogin("john.doe", "password123"); //-> "rate-limited" 15// wait 60 seconds 16attemptLogin("john.doe", "password123"); //-> "success"
If you want to reset the rate limit after a successful login, call rateLimit.reset(username)
.
RateLimit
RateLimit.attempt(name, source, [attempts], [callback])
RateLimit.check(name, source, [callback])
RateLimit.clear(name)
RateLimit.create(name, limit, timeWindow)
RateLimit.delete(name)
RateLimit.get(name)
RateLimit.reset(name, source)
RateLimit.setRemaining(name, source, remaining)
new RateLimit(name, limit, timeWindow)
rateLimit.attempt(source, [attempts], [callback])
rateLimit.check(source, [callback])
rateLimit.clear()
rateLimit.delete()
rateLimit.limit
rateLimit.name
rateLimit.reset(source)
rateLimit.setRemaining(source, remaining)
rateLimit.timeWindow
AttemptResult
RateLimit
Rate limit
RateLimit.attempt(name, source, [attempts], [callback])
Make an attempt with a source ID
name
string
The name of the rate limitsource
string
Unique source identifier (e.g. username, IP, etc.)attempts
number
The number of attempts to make. Default: 1
callback
function
Callback function. Default: undefined
result
AttemptResult
The result of the attemptvoid
AttemptResult
Error
If the rate limit does not existRateLimit.check(name, source, [callback])
Check the attempt state for a source ID without decrementing the remaining attempts
name
string
The name of the rate limitsource
string
Unique source identifier (e.g. username, IP, etc.)callback
function
Callback function. Default: undefined
result
AttemptResult
The result of the attemptvoid
AttemptResult
Error
If the rate limit does not existRateLimit.clear(name)
Clear rate limit attempts storage. This is equivalent to resetting all rate limits.
RateLimit.create(name, limit, timeWindow)
Create a new rate limit
name
string
The name of the rate limitlimit
number
The number of attempts allowed per time window (e.g. 60)timeWindow
number
The time window in seconds (e.g. 60)RateLimit
RateLimit.delete(name)
Delete the rate limit instance. After it is deleted, it should not be used any further without constructing a new instance.
RateLimit.get(name)
Get a rate limit instance
RateLimit.reset(name, source)
Reset limit for a source ID. The storage entry will be deleted and a new one will be created on the next attempt.
name
string
The name of the rate limitsource
string
Unique source identifier (e.g. username, IP, etc.)void
Error
If the rate limit does not existRateLimit.setRemaining(name, source, remaining)
Set the remaining attempts for a source ID.
Warning: This is not recommended as the remaining attempts depend on the limit of the instance.
name
string
The name of the rate limitsource
string
Unique source identifier (e.g. username, IP, etc.)remaining
number
The number of remaining attemptsvoid
Error
If the rate limit does not existnew RateLimit(name, limit, timeWindow)
Create a new rate limit
name
string
The name of the rate limitlimit
number
The number of attempts allowed per time window (e.g. 60)timeWindow
number
The time window in seconds (e.g. 60)Error
If the rate limit already existsrateLimit.attempt(source, [attempts], [callback])
Make an attempt with a source ID
source
string
Unique source identifier (e.g. username, IP, etc.)attempts
number
The number of attempts to make. Default: 1
callback
function
Callback function
result
AttemptResult
The result of the attemptvoid
AttemptResult
rateLimit.check(source, [callback])
Check the attempt state for a source ID without decrementing the remaining attempts
source
string
Unique source identifier (e.g. username, IP, etc.)callback
function
Callback function
result
AttemptResult
The result of the attemptvoid
AttemptResult
rateLimit.clear()
Clear rate limit attempts storage. This is equivalent to resetting all rate limits.
void
rateLimit.delete()
Delete the rate limit instance. After it is deleted, it should not be used any further without constructing a new instance.
void
rateLimit.limit
The number of requests allowed per time window
number
rateLimit.name
Get rate limit name
string
rateLimit.reset(source)
Reset limit for a source ID. The storage entry will be deleted and a new one will be created on the next attempt.
rateLimit.setRemaining(source, remaining)
Set the remaining attempts for a source ID.
Warning: This is not recommended as the remaining attempts depend on the limit of the instance.
source
string
Unique source identifier (e.g. username, IP, etc.)remaining
number
The number of remaining attemptsvoid
rateLimit.timeWindow
The time window in seconds (e.g. 60)
number
AttemptResult
The result from a rate limit attempt
attemptResult.limit
The number of requests this rate limit allows per time window
number
attemptResult.remaining
The number of requests remaining in the current time window
number
attemptResult.reset
The number of seconds until the current time window resets
number
attemptResult.rateLimit
The rate limit that this attempt was made on
RateLimit
attemptResult.allow
Whether this attempt should be allowed to proceed. If false, the attempt is rate limited.
boolean
No vulnerabilities found.
No security vulnerabilities found.