Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rate-limiter-flexible

Package Overview
Dependencies
Maintainers
0
Versions
164
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rate-limiter-flexible

Node.js rate limiter by key and protection from DDoS and Brute-Force attacks in process Memory, Redis, MongoDb, Memcached, MySQL, PostgreSQL, Cluster or PM

  • 5.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
317K
decreased by-49.21%
Maintainers
0
Weekly downloads
 
Created

What is rate-limiter-flexible?

The rate-limiter-flexible npm package is a powerful and flexible rate limiting library for Node.js. It supports various backends like Redis, MongoDB, and in-memory storage, making it suitable for distributed systems. It helps in controlling the rate of requests to APIs, preventing abuse, and ensuring fair usage.

What are rate-limiter-flexible's main functionalities?

Basic Rate Limiting

This feature allows you to set up basic rate limiting using in-memory storage. The example limits a user to 5 requests per second.

const { RateLimiterMemory } = require('rate-limiter-flexible');

const rateLimiter = new RateLimiterMemory({
  points: 5, // 5 points
  duration: 1, // Per second
});

rateLimiter.consume('user-key')
  .then(() => {
    // Allowed
  })
  .catch(() => {
    // Blocked
  });

Rate Limiting with Redis

This feature demonstrates how to use Redis as a backend for rate limiting. The example limits a user to 10 requests per minute.

const { RateLimiterRedis } = require('rate-limiter-flexible');
const Redis = require('ioredis');

const redisClient = new Redis();
const rateLimiter = new RateLimiterRedis({
  storeClient: redisClient,
  points: 10, // 10 points
  duration: 60, // Per minute
});

rateLimiter.consume('user-key')
  .then(() => {
    // Allowed
  })
  .catch(() => {
    // Blocked
  });

Rate Limiting with MongoDB

This feature shows how to use MongoDB as a backend for rate limiting. The example limits a user to 5 requests per minute.

const { RateLimiterMongo } = require('rate-limiter-flexible');
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/rate-limiter', { useNewUrlParser: true, useUnifiedTopology: true });

const rateLimiter = new RateLimiterMongo({
  storeClient: mongoose.connection,
  points: 5, // 5 points
  duration: 60, // Per minute
});

rateLimiter.consume('user-key')
  .then(() => {
    // Allowed
  })
  .catch(() => {
    // Blocked
  });

Rate Limiting with Bursts

This feature allows for burst handling by blocking the user for a specified duration if they exceed the rate limit. The example blocks a user for 10 seconds if they exceed 10 requests per second.

const { RateLimiterMemory } = require('rate-limiter-flexible');

const rateLimiter = new RateLimiterMemory({
  points: 10, // 10 points
  duration: 1, // Per second
  blockDuration: 10, // Block for 10 seconds if consumed more than points
});

rateLimiter.consume('user-key')
  .then(() => {
    // Allowed
  })
  .catch(() => {
    // Blocked
  });

Other packages similar to rate-limiter-flexible

Keywords

FAQs

Package last updated on 20 Oct 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc