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

@cch137/rate-limiter

v0.1.1

Published

`@cch137/rate-limiter` is a module designed to help manage rate limiting for various applications. It allows you to define rules for how many points an identifier (e.g., a user or an IP address) can accumulate within a specified time window. The `RateLimi

Downloads

22

Readme

@cch137/rate-limiter

Overview

@cch137/rate-limiter is a module designed to help manage rate limiting for various applications. It allows you to define rules for how many points an identifier (e.g., a user or an IP address) can accumulate within a specified time window. The RateLimiter class is the primary export of this module.

Installation

You can install the @cch137/rate-limiter module via npm:

npm install @cch137/rate-limiter

Usage

Here's a basic example to get you started with the RateLimiter class.

import RateLimiter from "@cch137/rate-limiter";

// Create a rate limiter with a rule that allows a maximum of 5 points per second
const rl = new RateLimiter([RateLimiter.rule(1000, 5)]);

// Consume points for identifier "a"
rl.consume("a", 5);
rl.consume("a", 5);

// Check if identifier "a" exceeds the rate limit
console.log(rl.check("a")); // false, because 10 points are consumed within 1 second

// Check again after 900 milliseconds
setTimeout(() => console.log(rl.check("a")), 900); // false, because 10 points are still within 1 second

// Check after 1000 milliseconds
setTimeout(() => console.log(rl.check("a")), 1000); // true, because the initial points are now outside the 1-second window

// Check after 1100 milliseconds
setTimeout(() => console.log(rl.check("a")), 1100); // true, because the points are outside the 1-second window

// Log the current state of the rate limiter
setTimeout(() => console.log(rl), 1200);

API

RateLimiter

constructor(rules: RateRule[])

Creates a new RateLimiter instance with the specified rules.

  • rules: An array of RateRule objects defining the rate limiting rules.

static rule(milliseconds: number, maxPoints: number): RateRule

Creates a new RateRule instance.

  • milliseconds: The time window in milliseconds.
  • maxPoints: The maximum points allowed within the specified time window.

check(id: string): boolean

Checks if the given identifier is within the rate limit based on the defined rules.

  • id: The identifier to check (e.g., a user ID or IP address).
  • Returns true if the identifier is within the rate limit, false otherwise.

consume(id: string, points: number = 1): this

Consumes points for the given identifier. If the identifier does not exist, it is created.

  • id: The identifier to consume points for.
  • points: The number of points to consume (default is 1).
  • Returns the RateLimiter instance for chaining.

trim()

Trims expired logs from all records based on the maximum time window defined in the rules.