npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rate-limiter-ts

v1.0.7

Published

This library is built to offer flexibility in rate limiting strategies, supporting token bucket and IP-based methods, making it suitable for a wide range of use cases.

Downloads

12

Readme

rate-limiter-ts | Rate Limiting Library for TypeScript and JavaScript

This npm package provides a rate limiter middleware for Express applications. It helps prevent abuse of your API by limiting the number of requests that can be made from a specific IP address within a given time frame.

Features

  • Flexible Integration: rate-limiter-ts can be seamlessly integrated into different web frameworks, including Nest, Express, and other JavaScript frameworks, allowing you to implement rate limiting in various projects.

  • Storage Strategies:

    • Local Storage: The default storage strategy that uses in-memory storage. It's suitable for single-server setups.

    • Redis Storage: Use Redis as a distributed storage solution for multi-server environments. Configure Redis options to enable this strategy.

  • Built with TypeScript: rate-limit-ts is developed using TypeScript, providing strong typing and enhancing code readability.

Installation

You can install the npm i rate-limiter-ts library using npm:

npm i rate-limiter-ts

Use Rate Limiting:

Create a new instance of the RateLimit class and configure it according to your needs. specify the limits, and provide any optional configuration options:

import express from 'express';
import { TokenBucket } from 'rate-limiter-ts';

const app = express();

// Create a TokenBucket rate limiter with a specific configuration
const rateLimiter = new TokenBucket({
  capacity: 100, // Maximum number of requests allowed
  refillRate: 'minute', // Refill rate, can be 'sec', 'min', 'hr', or 'day'
  message: 'Custom Message', // Optional message to send when rate limit is exceeded
  storage: {
    strategy: 'redis',
    redisOptions: {
      host: 'custom-host', // Redis host
      port: 12345 // Redis port
    }
  }
});

// Apply the rate limiter middleware to all routes
app.use((req, res, next) => {
  rateLimiter.middleware(req, res, next);
});

const specificLimiter = new TokenBucket({
  capacity: 100,
  refillRate: 'minute',
  storage: {
    strategy: 'local'
  }
});

const specificLimiterMiddleware = (req, res, next) => {
  specificLimiter.middleware(req, res, next);
};

// Apply the rate limiter middleware to specific routes
app.use('/api/some-route', limiterMiddleware);

// Your Express route handlers here
app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Configuration Options

When creating a RateLimit instance, you can provide various configuration options to customize the rate limiting behavior. Some of the available options include:

  • capacity: The maximum number of requests allowed within the specified time frame.
  • refillRate: The rate at which the tokens (requests) are refilled, specified in milliseconds by default. You can also use one of 'sec', 'min', 'hr', or 'day'.
  • message: Optional. The message to be sent in the response when the rate limit is exceeded. The default message is 'Too Many Requests'.
  • storage: Configuration for the storage strategy. You can choose between 'redis' or 'local' storage. If using 'redis', you can provide redisOptions for custom Redis configuration.

note: The default Redis host is "localhost" and port is 6379.

Examples

For more detailed usage examples and integration with different web frameworks like Nest and Express, check out the examples directory in the repository.

Contributing

Contributions to RateLimit-ts are welcome! If you find any issues or want to enhance the library, please create an issue or submit a pull request on the GitHub repository.

License

RateLimit-TS is released under the GPL-3.0 License.

Copyright 2023, Max Base