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

stopdude

v1.0.2

Published

Another Redis rate-limiter

Downloads

124

Readme

stopdude

Another Redis rate limiter.

Features

  • Supports minute, hour, day, week, and month-based rate limiting.
  • Built on top of Redis for high performance and scalability.

Install

To include StopDude in your project, run:

npm install stopdude --save

Usage

Here's a basic example of how to use StopDude:

const StopDude = require('stopdude');

// Initialize with custom options if needed
const options = {
  redis: yourRedisClient, // Pass in a configured Redis client
  prefix: 'yourPrefix',   // Optional: to namespace your keys in Redis
};

const rateLimiter = new StopDude(options);

// Example of creating a rate-limiting rule
rateLimiter.create({ key: 'api_user_123', max: 100, time: 'hour' })
  .then(() => {
    console.log('Rule created successfully');
  })
  .catch(err => {
    console.error('Error creating rule:', err);
  });

API

create(options)

  • Description: Creates a new rate-limiting rule.
  • Parameters:
    • options: Object containing key, max, and time.
    • key: String, the unique identifier for the rule.
    • max: Number, the maximum number of allowed requests in the specified time.
    • time: String, one of ['minute', 'hour', 'day', 'week', 'month'].
  • Returns: Promise that resolves with the created rule details.

find(key)

  • Description: Finds an existing rate limit rule by key.
  • Parameters:
    • key: String, the unique identifier for the rule.
  • Returns: Promise that resolves with the rule details or false if not found.

update(key, properties)

  • Description: Updates an existing rate limit rule.
  • Parameters:
    • key: String, the rule key to update.
    • properties: Object containing properties to update (max, time).
  • Returns: Promise that resolves with true if update was successful.

incr(key, amount)

  • Description: Increments the counter for the specified rule key.
  • Parameters:
    • key: String, the rule key to increment.
    • amount: Number, the amount to increase by (default 1).
  • Returns: Promise that resolves with true if the increment was successful.

stats(key)

  • Description: Retrieves usage statistics for a rule.
  • Parameters:
    • key: String, the rule key to retrieve stats for.
  • Returns: Promise that resolves with an object containing usage stats such as counters, allowed, and percent utilization.

remove(key)

  • Description: Removes a rate-limiting rule completely.
  • Parameters:
    • key: String, the rule key to remove.
  • Returns: Promise that resolves with true if the rule was successfully removed.

Development

  • yarn: Install project dependencies.
  • yarn test: Run the test suite to ensure all functionality works as expected.
  • yarn build: Compile CoffeeScript source code to JavaScript for distribution.
  StopDude
    create
      ✔ should create a new rule
      ✔ should throw an error for invalid time segment
    find
      ✔ should find an existing rule
      ✔ should return false for non-existent rule
    update
      ✔ should update an existing rule
    incr
      ✔ should increment the counter
    clear
      ✔ should clear the counters
    remove
      ✔ should remove a rule
    stats
      ✔ should return stats for a rule
      ✔ should return not allowed when max is reached
    generateUUID
      ✔ should generate a valid UUID
    getExpires
      ✔ should return a future timestamp for each time segment
      ✔ should throw an error for invalid time segment
    secsToTime
      ✔ should convert a string to seconds
    getTime
      ✔ should return current unix timestamp
    getMinute
      ✔ should return the start of the current minute
    getHour
      ✔ should return the start of the current hour
    getType
      ✔ should return the type of an object


  18 passing (19ms)

Done in 0.34s.