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

limitkeeper

v1.0.0

Published

LimitKeeper is a flexible and robust middleware that allows you to control the rate of incoming requests to your server. It supports three rate-limiting techniques: Sliding Window Counter, Leaky Bucket, and Token Bucket. This middleware is particularly us

Downloads

5

Readme

LimitKeeper

Introduction

LimitKeeper is a flexible and robust middleware that allows you to control the rate of incoming requests to your server. It supports three rate-limiting techniques: Sliding Window Counter, Leaky Bucket, and Token Bucket. This middleware is particularly useful for preventing abuse, ensuring fair usage, and protecting your server from being overwhelmed by excessive requests.

When to Use It

Use this API Rate Limiter when you need to:

  • Prevent brute-force attacks.
  • Throttle requests to an API or service.
  • Enforce rate limits per user, IP, or any other identifier.
  • Ensure fair usage among users.
  • Protect your server resources by limiting request rates.

How to Use It

1. Installation

First, install the package via npm:

npm install limitkeeper

2. Importing and Setting Up

Import the rate limiter and configure it with your desired options:

const rateLimiter = require('limitkeeper');

const options = {
    technique: 'sliding-window', // Options: 'sliding-window', 'leaky-bucket', 'token-bucket'
    points: 100, // Only for 'sliding-window'
    duration: 60, // Only for 'sliding-window'
    blockDuration: 600, // Only for 'sliding-window'
    capacity: 10, // Only for 'leaky-bucket' and 'token-bucket'
    interval: 1, // Only for 'leaky-bucket'
    refillRate: 1, // Only for 'token-bucket'
    refillInterval: 1 // Only for 'token-bucket'
};

const limiter = rateLimiter(options);

app.use(limiter);

By default, if no technique is specified, the rate limiter will use the Sliding Window Counter technique.

Techniques Overview

  • Sliding Window Counter: This technique limits the number of requests within a specified duration. It's useful for simple, straightforward rate limiting, where you want to allow a certain number of requests per time unit (e.g., 100 requests per minute).

  • Leaky Bucket: This technique smooths out bursts of traffic by allowing requests at a steady rate. It's ideal for scenarios where you want to enforce a consistent request rate, regardless of sudden spikes.

  • Token Bucket: This technique is more flexible than the Leaky Bucket, allowing bursts of requests up to a certain limit (bucket capacity) and then refilling the bucket at a constant rate. It's useful when you want to allow occasional bursts but maintain overall control over the request rate.

Fields Explanation

  • technique: The rate-limiting technique to use. Options: 'sliding-window', 'leaky-bucket', 'token-bucket'. Default: 'sliding-window'.

  • points: Maximum number of requests allowed in the sliding window duration. Only used for the Sliding Window technique. Default: 100.

  • duration: Time in seconds for the sliding window duration. Only used for the Sliding Window technique. Default: 60.

  • blockDuration: Time in seconds to block requests once the limit is exceeded. Only used for the Sliding Window technique. Can be disabled when set 0. Default: 120.

  • capacity: Maximum number of requests or tokens the bucket can hold. Used for both Leaky Bucket and Token Bucket techniques. Default: 10 (Leaky Bucket), 100 (Token Bucket).

  • interval: Time in seconds between each request leak in the Leaky Bucket technique. Default: 1.

  • refillRate: Number of tokens added to the bucket per refill interval in the Token Bucket technique. Default: 1.

  • refillInterval: Time in seconds for each refill in the Token Bucket technique. Default: 1.

Example Usage

const express = require('express');
const rateLimiter = require('limitkeeper');

const app = express();

const limiter = rateLimiter({
    technique: 'token-bucket',
    capacity: 50,
    refillRate: 5,
    refillInterval: 2
});

// global use: limits tha rate of all requests
app.use(limiter);

app.get('/', (req, res) => {
    res.send('Welcome to the rate-limited API!');
});

// route-specific limiting
app.post('/api/data',
    limiter,
    (req, res) => {
    res.send('Route specific rate-limitin');
});

app.listen(3000, () => {
    console.log('Server is running on port 3000');
});

Conclusion

This API Rate Limiter provides a simple yet powerful way to control incoming request rates. By selecting the appropriate technique and configuring the relevant options, you can protect your API from excessive usage while maintaining a smooth user experience. Feel free to experiment with different configurations to find what works best for your specific needs.