Skip to content

Multi-framework rate limiting for JavaScript/TypeScript. Supports Hono, Express, H3, Nuxt, and more.

License

Notifications You must be signed in to change notification settings

rokasta12/ratelimit

Repository files navigation

@jfungus/ratelimit

Multi-framework rate limiting for JavaScript/TypeScript.

CI npm version npm downloads bundle size TypeScript License: MIT

Documentation

Features

  • Multi-Framework - Works with Hono, Express, H3/Nitro, Nuxt
  • Smart Algorithms - Fixed window and sliding window (Cloudflare-style)
  • Flexible Storage - Memory, Redis, Cloudflare KV, Vercel KV, and more
  • TypeScript First - Full type support with comprehensive definitions
  • Zero Dependencies - Core package has no external dependencies

Quick Start

1. Install

# For Hono
npm install @jfungus/ratelimit-hono

# For Express
npm install @jfungus/ratelimit-express

# For H3/Nitro
npm install @jfungus/ratelimit-h3

# For Nuxt
npm install @jfungus/ratelimit-nuxt

2. Use

import { Hono } from "hono";
import { rateLimiter } from "@jfungus/ratelimit-hono";

const app = new Hono();

app.use(
  rateLimiter({
    limit: 100, // 100 requests
    windowMs: 60 * 1000, // per 1 minute
  }),
);

app.get("/", (c) => c.text("Hello!"));

That's it! Your API is now rate limited.

Options

Option Type Default Description
limit number 100 Max requests per window
windowMs number 60 * 1000 Window size in milliseconds (1 minute)
algorithm string "sliding-window" "sliding-window" or "fixed-window"
keyGenerator function IP-based Function to identify clients
skip function - Skip rate limiting for certain requests
handler function 429 response Custom rate limit exceeded handler
store Store MemoryStore Custom storage backend

Common windowMs values

windowMs: 1000; // 1 second
windowMs: 60 * 1000; // 1 minute
windowMs: 5 * 60 * 1000; // 5 minutes
windowMs: 15 * 60 * 1000; // 15 minutes
windowMs: 60 * 60 * 1000; // 1 hour

See all options in documentation

Packages

Package Description
@jfungus/ratelimit Core library with algorithms and MemoryStore
@jfungus/ratelimit-hono Hono middleware
@jfungus/ratelimit-express Express middleware
@jfungus/ratelimit-h3 H3/Nitro middleware
@jfungus/ratelimit-nuxt Nuxt module
@jfungus/ratelimit-unstorage Adapter for Redis, Cloudflare KV, etc.

Examples

Express

import express from "express";
import { rateLimiter } from "@jfungus/ratelimit-express";

const app = express();

app.use(
  rateLimiter({
    limit: 100,
    windowMs: 15 * 60 * 1000, // 15 minutes
  }),
);

H3/Nitro

import { createApp } from "h3";
import { rateLimiter } from "@jfungus/ratelimit-h3";

const app = createApp();

app.use(
  rateLimiter({
    limit: 100,
    windowMs: 60 * 1000, // 1 minute
  }),
);

Nuxt

// nuxt.config.ts
export default defineNuxtConfig({
  modules: ["@jfungus/ratelimit-nuxt"],
  rateLimit: {
    limit: 100,
    windowMs: 60 * 1000, // 1 minute
  },
});

Custom Key Generator

app.use(
  rateLimiter({
    limit: 100,
    windowMs: 60 * 1000,
    keyGenerator: (c) => c.req.header("X-API-Key") ?? "anonymous",
  }),
);

Distributed Storage (Redis)

import { createStorage } from "unstorage";
import redisDriver from "unstorage/drivers/redis";
import { createUnstorageStore } from "@jfungus/ratelimit-unstorage";

const storage = createStorage({
  driver: redisDriver({ url: "redis://localhost:6379" }),
});

app.use(
  rateLimiter({
    limit: 100,
    windowMs: 60 * 1000,
    store: createUnstorageStore({ storage }),
  }),
);

Algorithms

Sliding Window (default)

Cloudflare-style weighted sliding window for smoother rate limiting:

rateLimiter({
  algorithm: "sliding-window",
  limit: 100,
  windowMs: 60 * 1000,
});

Fixed Window

Simple counter that resets at fixed intervals:

rateLimiter({
  algorithm: "fixed-window",
  limit: 100,
  windowMs: 60 * 1000,
});

Contributing

Contributions are welcome! Please read our Contributing Guide for details.

License

MIT

About

Multi-framework rate limiting for JavaScript/TypeScript. Supports Hono, Express, H3, Nuxt, and more.

Topics

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors