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

rateguardjs

v1.0.0

Published

A simple JavaScript module for time-based rate limiting to control request flow in REST APIs efficiently.

Downloads

5

Readme

RateGuardJS

RateGuardJS is a simple and efficient JavaScript library for implementing time-based rate limiting for REST APIs. It helps to control the number of requests made to a specific URL within a defined time window, ensuring that your API usage remains within safe limits.

Features

  • Time-based rate limiting for REST APIs.
  • Supports localStorage for persistent request tracking across sessions.
  • Simple and intuitive API.

Installation

You can install RateGuardJS via npm:

npm install rateguardjs

Usage

Here's an example of how to use RateGuardJS in your project:

import RateGuardJS from 'rateguardjs';

const rateGuard = new RateGuardJS(5, 1, 0.5); // Allow 5 requests per 1 minute window with a delay of 30 seconds if the limit is exceeded

async function requestHandler(url, params) {
    const response = await fetch(url, params);
    return response.json();
}

// Usage example
(async () => {
    const url = 'http://example.com/api';
    const params = { method: 'GET' };
    try {
        const data = await rateGuard.sendRequest(url, params, requestHandler);
        console.log(data);
    } catch (error) {
        console.error('Request failed:', error);
    }
})();

Project Structure

src/
├── RateGuard.js
├── RateGuard.test.js
└── index.js
  • RateGuard.js: The main library file containing the RateGuardJS class.
  • RateGuard.test.js: Unit tests for the RateGuardJS class.
  • index.js: Entry point for the library.

API

Constructor

new RateGuardJS(totalAllowedRequests, totalRequestsWindowMins, timeoutMinutes)
  • totalAllowedRequests: The maximum number of requests allowed within the time window.
  • totalRequestsWindowMins: The time window duration in minutes.
  • timeoutMinutes: The delay duration in minutes if the request limit is exceeded.

sendRequest(url, params, requestHandler)

  • url: The URL to which the request is made.
  • params: The parameters for the request.
  • requestHandler: The function to handle the request. It should return a promise.

Unit Tests

Jest is included in the package.json. To run the unit tests, simply use:

npm test

Example Test

Here's an example test for the sendRequest function:

const RateGuardJS = require("./RateGuard");

describe('RateGuardJS', () => {
    test('should allow requests within the limit', async () => {
        const rateGuard = new RateGuardJS(2, 1, 0.5);
        const requestHandler = jest.fn(() => Promise.resolve('success'));

        const response1 = await rateGuard.sendRequest('http://example.com', {}, requestHandler);
        const response2 = await rateGuard.sendRequest('http://example.com', {}, requestHandler);

        expect(response1).toBe('success');
        expect(response2).toBe('success');
        expect(requestHandler).toHaveBeenCalledTimes(2);
    });

    test('should delay requests when the limit is exceeded', async () => {
        const rateGuard = new RateGuardJS(2, 1, 0.001);
        const requestHandler = jest.fn(() => Promise.resolve('success'));

        // Send first two requests
        await rateGuard.sendRequest('http://example.com', {}, requestHandler);
        await rateGuard.sendRequest('http://example.com', {}, requestHandler);

        // Send third request, which should trigger the delay
        const responsePromise = rateGuard.sendRequest('http://example.com', {}, requestHandler);

        // Wait for the delay to pass
        await new Promise(resolve => setTimeout(resolve, 1000 * 60 * 0.001));

        const response = await responsePromise;
        expect(response).toBe('success');
        expect(requestHandler).toHaveBeenCalledTimes(3);
    });

    test('should reset the counter after the window time has passed', async () => {
        const rateGuard = new RateGuardJS(2, 0.1, 0.001);
        const requestHandler = jest.fn(() => Promise.resolve('success'));

        // Send first two requests within the window
        await rateGuard.sendRequest('http://example.com', {}, requestHandler);
        await rateGuard.sendRequest('http://example.com', {}, requestHandler);

        // Wait for the window to pass
        await new Promise(resolve => setTimeout(resolve, 1000 * 60 * 0.1));

        // The third request should be allowed since the window has reset
        const response = await rateGuard.sendRequest('http://example.com', {}, requestHandler);
        expect(response).toBe('success');
        expect(requestHandler).toHaveBeenCalledTimes(3);
    });
});

Contributing

Contributions are welcome! Please open an issue or submit a pull request if you have any improvements or new features to add.

License

This project is licensed under the MIT License. See the LICENSE file for more details.