New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

ac-ratelimiter

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ac-ratelimiter

Simple ratelimiter for express

latest
Source
npmnpm
Version
2.0.10
Version published
Maintainers
1
Created
Source

ac-ratelimiter

This tool provides rate-limiter that can be used as middleware with ExpressJs.

For huge production load it is recommended that you use it with Redis. However, for smaller applications you can use the build in Node Cache.

Node.js CI

Breaking changes for version 2

Version 2 is a complete re-write of this module. It is now a class and uses async/await.

// Migration example

// Version 1
const acrl = require('ac-ratelimiter')

const init = {
  routes: [
    { route: 'user/find', throttleLimit: 1, limit: 2, expires: 3, delay: 250 },
  ],
  redis: REDIS INSTANCE
  logger: winston.log INSTANCE
}

acrl.init(init)

// req.rateLimitCounter should have already the current count
acrl.limiter(req, {}, err => {
  // err.status === 900 => throttling is active
  // err.status === 429 => limiter is active
  return res.json({ status: _.get(err, 'status') })
})


// Version 2
const acrl = require('ac-ratelimiter')

const init = {
  routes: [
    { route: 'user/find', throttleLimit: 1, limit: 2, expires: 3, delay: 250 },
  ],
  redis: REDIS INSTANCE
  logger: winston.log INSTANCE
}

const rateLimiter = new acrl(init)

try {
  await rateLimiter.limiter(req)
}
catch(e) {
  // e.status === 900 => throttling is active
  // e.status === 429 => limiter is active
}

Usage

Without any dependencies

This example initiates the rate limiter with NodeCache (instead of Redis) and console.log (instead of Winston). Default limits are 150 requests within 3 seconds. Starting at 50 request, requests will be throttled by 250ms.

const acrl = require('ac-ratelimiter')

const rateLimiter = new acrl()

try {
  await rateLimiter.limiter(req)
}
catch(e) {
  // e.status === 900 => throttling is active
  // e.status === 429 => limiter is active
}

Prerequisites

Init

The ac-ratelimiter can use Redis as storage for the rate-limiter keys. By default and to run out-of-the-box it uses Node Cache.

Additionally, for logging purposes, we use Winston. But you can also use any other logger that provides logging for "warn" and "error".

Last but not least, provide an array of objects with rate limiter instructions. Each object has the following properties:

PropertyTypeDefaultsRemarks
routesstringA combination of controller and action (express) or any other identifier you can provide
throttleLimit5020Number of calls before throttling starts
delayinteger250Number of milliseconds a throttle request is delayed (on purpose)
limitinteger150Number of calls before the limiter kicks in
expiresinteger3Number of seconds before the rate-limiter resets

RateLimiter

The actual rateLimiter function takes two arguments, the Express request object (req) and an options object with the following optional properties:

PropertyTypeExampleRemarks
nameStringmyNameIdentifier for the route - falls back to controller/action
redisKeyStringmyKeyOptional RedisKey to use for rate limiter
fallbackRouteStringfbrouteOptional fallback route identifier
expiresInteger3Expire time for rate limiter - see above
throttleLimitInteger20Throttle limit for rate limiter - see above
delayInteger250Delay for throttled calls for rate limiter - see above

Good practice

It is recommended to put the determined IP to the request object as req.determinedIP.

Additionally, you can put the rateLimitCounter to the request object as req.rateLimitCounter. This way, the rate limiter does not have to fetch that value.

Both values might be retrieved prior to the rate limiter so there is no need to retrieve it once again here.

  • Website
  • Facebook

Run tests

yarn run test

License

MIT License Copyright © 2009-present, AdmiralCloud AG, Mark Poepping

FAQs

Package last updated on 21 Mar 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts