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-limit-threshold

v0.2.0

Published

Rate limiter with a maximum number of calls per given period of time

Downloads

1,284

Readme

Node.js CI CodeQL NPM version npm downloads Coverage Status Codacy Badge Known Vulnerabilities DeepScan grade bundlejs.com badge

rate-limit-threshold

Module designed to handle rate-limiting by allowing developers to set thresholds for the maximum number of requests that can be made within a specified time period. This helps to prevent exceeding the rate limits imposed by APIs or services. The module provides configurable options and is useful for managing API consumption in a controlled manner.

Installation

npm install rate-limit-threshold

This package is an ESM (ECMAScript Module) package. Therefore, your project must also use the ESM format. For more details, refer to this guide.

Compatibility

This package is compatible with:

  1. Pure ESM environments
  2. ECMAScript 2020 (11th Edition) standard.

Sponsor

If you find this package useful and would like to support the development of open-source projects, please consider sponsoring or making a contribution. Your support helps sustain ongoing development and improvements.

Some of my other projects you may want to support include:

Become a sponsor to Borewit

or

Usage

A rate-limit-threshold helps enforce rate limits by restricting the maximum number of calls allowed within a specified time frame. More information about rate limiting can be found here.

Example

import { RateLimitThreshold } from 'rate-limit-threshold';

(async () => {
    const rateLimitThreshold = new RateLimitThreshold(3, 1); // Allow a maximum of 3 requests per second

    for (let n = 0; n < 7; ++n) {
        const delayInMs = await rateLimitThreshold.limit(); // Apply delay to comply with the rate limit
        console.log('Timeout applied to comply with rate limit:', delayInMs);
        // After the limit() has been applied, proceed with your rate-limited request
    }
})();

API Documentation

RateLimitThreshold

Constructor

Create an instance of RateLimitThreshold with the following syntax:

new RateLimitThreshold(requests, period);
Parameters:
  • requests (number): The maximum number of requests allowed within the specified period.
  • period (number): The time period (in seconds) within which the specified number of requests are allowed.

limit

The limit() method ensures that the number of function calls does not exceed the specified requests within the given period. This method should be called before the function you wish to rate limit.

const timeSleptInMs = await rateLimitThreshold.limit();
Returns:

A promise that resolves after a delay, with the time (in milliseconds) that the execution was paused.