hono-throttle
v1.0.0
Published
Lightweight sliding-window rate limiter middleware for Hono
Maintainers
Readme
hono-rate-limiter
A lightweight rate limiter middleware for Hono using a sliding-window algorithm with an in-memory store.
Installation
npm install hono-throttleUsage
import { Hono } from "hono";
import { rateLimiter } from "hono-throttle";
const app = new Hono();
app.use(rateLimiter({
maxRequests: 100,
windowMs: 60_000, // 1 minute
whitelist: [],
}));
app.get("/", (c) => c.text("Hello!"));Options
| Option | Type | Description |
| ------------- | ---------- | ---------------------------------------------------- |
| maxRequests | number | Maximum number of requests allowed within the window |
| windowMs | number | Duration of the sliding window in milliseconds |
| whitelist | string[] | List of IPs that bypass rate limiting |
Behavior
- IP addresses are resolved from the
X-Forwarded-Forheader (first entry) orX-Real-IPas a fallback. - When the limit is exceeded, the middleware returns
429 Too Many Requestswith aRetry-Afterheader indicating how many seconds to wait. - Stale entries are automatically cleaned up every 5 minutes.
