rate-limit-threshold
TypeScript icon, indicating that this package has built-in type declarations

0.1.5 • Public • Published

Node.js CI NPM version npm downloads Coverage Status Codacy Badge CodeQL Known Vulnerabilities

rate-limit-threshold

Installation

npm install rate-limit-threshold

This package is an ESM package. Your project needs to be ESM too. Read more.

Usage

A rate-limit-threshold which restricts a maximum number of call in a given time frame.

Example

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

(async () => {
  const rateLimitThreshold = new RateLimitThreshold(3, 1); // Maximum 3 requests in 1 seconds

  for (let n = 0; n < 7; ++n) {
    const delayInMs = await rateLimitThreshold.limit(); // Slow down loop to comply with 20 call in 10 seconds
    console.log('Timeout applied to coply with rate limit', delayInMs);
    // after the limit() is been aplied, you call your limitted requested
  }
})();

API

RateLimitThreshold

Constructor

new RateLimitThreshold(requests, period);

Arguments:

  • requests, number, Allowed requests within period (window)
  • period, number, Period (window) in milliseconds

limit

Call limit before the function you want call no more than requests in per period.

const timeSleptInMs await = limit()

Promise which resolved when timeout (sleep). The time in milliseconds slept is returned.

Compatibility

  1. Pure ESM package
  2. ECMAScript 2019

Package Sidebar

Install

npm i rate-limit-threshold

Weekly Downloads

287

Version

0.1.5

License

MIT

Unpacked Size

8.04 kB

Total Files

7

Last publish

Collaborators

  • borewit