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

@mahdi.golzar/ratelimiter

v1.0.0

Published

RateLimiter is a simple tool to limit the number of requests to a server from a specific IP address within a defined time window. It helps prevent abuse and overloading of the server.

Downloads

3

Readme

RateLimiter

RateLimiter is a simple tool to limit the number of requests to a server from a specific IP address within a defined time window. It helps prevent abuse and overloading of the server.

Features

  • Limits the number of requests from an IP address
  • Configurable time window and request limit
  • Easy to integrate with Node.js HTTP server

Installation

This project does not require any external dependencies and can be run with Node.js.

Usage

  1. Copy the Code First, save the following code in a file named rateLimiter.js:
class RateLimiter {
constructor({ windowSizeInSeconds, maxRequests }) {
this.windowSizeInMillis = windowSizeInSeconds * 1000;
this.maxRequests = maxRequests;
this.requests = new Map();
}

    isAllowed(ip) {
        const currentTime = Date.now();
        if (!this.requests.has(ip)) {
            this.requests.set(ip, []);
        }

        const requestTimes = this.requests.get(ip);
        // Remove outdated request timestamps
        while (requestTimes.length > 0 && requestTimes[0] <= currentTime - this.windowSizeInMillis) {
            requestTimes.shift();
        }

        if (requestTimes.length < this.maxRequests) {
            requestTimes.push(currentTime);
            return true;
        } else {
            return false;
        }
    }

    handleRequest(req, res, next) {
        const ip = req.connection.remoteAddress || req.socket.remoteAddress;
        if (this.isAllowed(ip)) {
            next();
        } else {
            res.writeHead(429, { 'Content-Type': 'text/plain' });
            res.end('Too Many Requests');
        }
    }
}

// Usage Example with Node.js HTTP server

const http = require('http');
const rateLimiter = new RateLimiter({ windowSizeInSeconds: 60, maxRequests: 10 });

http.createServer((req, res) => {
rateLimiter.handleRequest(req, res, () => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Request allowed');
});
}).listen(3000, () => {
console.log('Server running at http://localhost:3000');
});
  1. Run the Server To start the server, run the following command in your terminal:
node rateLimiter.js
The server will run on port 3000, and you can access it in your browser at http://localhost:3000.

Adding New Routes

To add new routes and handlers, modify the example server code. Simply add new routes and their corresponding handlers to the server's request handling logic.

Method Descriptions

isAllowed(ip): Checks if a request from the given IP address is allowed based on the rate limit. handleRequest(req, res, next): Middleware function to handle incoming requests, checking the rate limit and either allowing the request or returning a "Too Many Requests" response.