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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@fortify-ts/rate-limit

v0.3.1

Published

Token bucket rate limiter for @fortify-ts

Downloads

122

Readme

@fortify-ts/rate-limit

Token bucket rate limiter for the Fortify-TS resilience library.

Installation

npm install @fortify-ts/rate-limit
# or
pnpm add @fortify-ts/rate-limit

Features

  • Token Bucket Algorithm: Smooth rate limiting with burst support
  • Per-Key Limiting: Rate limit by user ID, IP, or custom key
  • External Storage: Support for Redis, DynamoDB, or custom storage
  • Sync and Async APIs: Both allow() and allowAsync() methods
  • Wait Support: Block until tokens available with wait()

Usage

Basic Usage

import { RateLimiter } from '@fortify-ts/rate-limit';

const limiter = new RateLimiter({
  rate: 100,      // 100 requests
  interval: 1000, // per second
});

// Check if request is allowed
if (limiter.allow('user-123')) {
  // Process request
} else {
  // Rate limited
}

With Burst

const limiter = new RateLimiter({
  rate: 10,       // 10 requests per second steady state
  burst: 50,      // Allow bursts up to 50 requests
  interval: 1000,
});

Wait for Token

// Block until token available (with timeout via signal)
await limiter.wait('user-123', signal);
// Token acquired, process request

Execute with Rate Limiting

// Throws RateLimitExceededError if rate limited
const result = await limiter.execute(
  async (signal) => fetch('/api/data', { signal }),
  'user-123'
);

External Storage (Redis)

import { RateLimiter, type RateLimitStorage } from '@fortify-ts/rate-limit';
import Redis from 'ioredis';

const redis = new Redis();

const storage: RateLimitStorage = {
  async get(key) {
    const data = await redis.get(`ratelimit:${key}`);
    return data ? JSON.parse(data) : null;
  },
  async set(key, state) {
    await redis.set(`ratelimit:${key}`, JSON.stringify(state), 'EX', 3600);
  },
  async delete(key) {
    await redis.del(`ratelimit:${key}`);
  },
};

const limiter = new RateLimiter({
  rate: 100,
  interval: 1000,
  storage,
});

Configuration Options

const limiter = new RateLimiter({
  // Requests per interval
  rate: 100,

  // Interval in milliseconds
  interval: 1000,

  // Maximum burst size (defaults to rate)
  burst: 200,

  // Tokens consumed per request
  tokensPerRequest: 1,

  // Maximum buckets in memory
  maxBuckets: 10000,

  // External storage adapter
  storage: myRedisStorage,

  // Storage timeout
  storageTimeoutMs: 1000,

  // Failure mode: 'fail-open' | 'fail-closed' | 'throw'
  storageFailureMode: 'fail-open',

  // Sanitize keys (prevents injection)
  sanitizeKeys: true,

  // Rate limit exceeded callback
  onLimit: (key) => console.log(`Rate limited: ${key}`),

  // Optional logger
  logger: myLogger,
});

API Reference

| Method | Description | |--------|-------------| | allow(key) | Sync check if request allowed | | allowAsync(key) | Async check with external storage | | wait(key, signal?) | Wait for token availability | | execute(op, key, signal?) | Execute with rate limiting | | getTokens(key) | Get current token count | | reset(key) | Reset bucket for key | | close() | Clean up resources |

Configuration Reference

| Option | Type | Default | Description | |--------|------|---------|-------------| | rate | number | 100 | Requests per interval | | interval | number | 1000 | Interval (ms) | | burst | number | rate | Maximum burst | | tokensPerRequest | number | 1 | Tokens per request | | maxBuckets | number | 10000 | Max memory buckets | | storage | RateLimitStorage | - | External storage | | storageTimeoutMs | number | 1000 | Storage timeout | | storageFailureMode | string | 'fail-open' | Failure handling | | sanitizeKeys | boolean | true | Sanitize keys | | onLimit | function | - | Rate limit callback | | logger | FortifyLogger | - | Optional logger |

License

MIT