Registry / web-framework / hono-rate-limiter

hono-rate-limiter

JSON →
library0.5.3jsnpmunverified

hono-rate-limiter is a middleware library designed for the Hono web framework, providing robust rate-limiting capabilities for both HTTP API endpoints and WebSocket connections. Its current stable version is 0.5.3, with minor releases occurring relatively frequently, indicating active development. The library is inspired by the widely-used `express-rate-limit` and aims to bring similar comprehensive functionality to Hono applications, focusing on developer experience and flexibility. A key differentiator is its flexible storage mechanism, now supporting Unstorage, which allows developers to integrate various backends like Redis, Cloudflare KV, or file systems for persistent rate limit tracking. It simplifies the process of protecting Hono routes from abuse and ensures API stability under high traffic, offering fine-grained control over rate limits and responses.

npm install hono-rate-limiter
INSTALL
IMPORT
SIG · HONO-RATE-LIMITER
H
hono-rate-limiter
web-frameworkjavascriptv0.5.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

rateLimiter
import { rateLimiter } from 'hono-rate-limiter';
const rateLimiter = require('hono-rate-limiter');
The primary named export for creating the HTTP rate limit middleware. Hono is primarily ESM-focused, so CJS `require` is generally incorrect.
WebSocketLimiter
import { WebSocketLimiter } from 'hono-rate-limiter';
import WebSocketLimiter from 'hono-rate-limiter';
Named export for WebSocket-specific rate limiting, introduced in v0.4.0.
MemoryStore
import { MemoryStore } from 'hono-rate-limiter/stores';
import { MemoryStore } from 'hono-rate-limiter';
Basic in-memory store; concrete store implementations are typically imported from the `/stores` subpath or a dedicated storage package.
Store
import type { Store } from 'hono-rate-limiter';
import { Store } from 'hono-rate-limiter';
Type-only import for implementing custom storage solutions, ensuring type compatibility.

Demonstrates how to import and apply the `rateLimiter` middleware to a Hono application, configure basic limits, and use an in-memory store.

import { Hono } from 'hono'; import { rateLimiter } from 'hono-rate-limiter'; import { MemoryStore } from 'hono-rate-limiter/stores'; const app = new Hono(); // Configure the rate limiter middleware const limiter = rateLimiter({ windowMs: 60 * 1000, // 1 minute limit: 5, // Limit each IP to 5 requests per minute standardHeaders: 'draft-7', // Set standard rate limit headers legacyHeaders: false, // Disable X-RateLimit-* headers // keyGenerator: (c) => c.req.ip, // Uses client IP by default if not provided store: new MemoryStore(), // In-memory store (not recommended for production) message: 'You are making too many requests. Please try again soon.', handler: (c) => { return c.json({ status: 429, message: 'Too many requests, please try again after some time.' }, 429) } }); // Apply the rate limiter globally or to specific routes app.use(limiter); app.get('/', (c) => { return c.text('Welcome to the Hono Rate Limited API!'); }); app.get('/protected', (c) => { return c.json({ data: 'This is protected data.' }); }); export default app;
Debug
Known issues
breakingAs of `v0.5.0`, specialized Cloudflare KV and Durable Object stores were removed from the main `hono-rate-limiter` package. If you were using these, you now need to explicitly use the `@hono-rate-limiter/cloudflare` package.
fix
Migrate to using `@hono-rate-limiter/cloudflare` for Cloudflare-specific stores. Review documentation for updated configuration.
affects: >=0.5.0
breakingIn `core-0.3.0`, the `keyGenerator` option became a required parameter for the `rateLimiter` configuration. Omitting it will lead to runtime errors.
fix
Ensure `keyGenerator` is explicitly provided in your `rateLimiter` configuration, e.g., `keyGenerator: (c) => c.req.ip`.
affects: >=0.3.0
breakingSupport for built-in Redis stores was dropped in `core-0.2.0`. Users who relied on this feature must now use a separate data store package, such as `@hono-rate-limiter/redis` or an `unstorage` compatible store.
fix
Install and configure a dedicated Redis store package (e.g., `@hono-rate-limiter/redis`) or an `unstorage` Redis driver, and pass an instance to the `store` option.
affects: >=0.2.0
gotchaThe package is still actively in development. While functional, it might undergo further API changes or feature adjustments in future minor versions. Feedback is highly appreciated by the maintainers.
fix
Keep an eye on the changelog for new releases, report issues, and be prepared for potential minor breaking changes. Pinning to specific minor versions might be advisable in production.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: keyGenerator is not a function or is missing
The `keyGenerator` option was omitted from the `rateLimiter` configuration after it became a required parameter in `v0.3.0`.
fix
Add `keyGenerator: (c) => c.req.ip` or a custom function to your `rateLimiter` options.
Error: Invalid store. Please provide a valid store implementation.
The `store` option for `rateLimiter` was either not provided or an invalid object was passed, preventing the middleware from initializing properly.
fix
Ensure you are passing a valid store instance, e.g., `store: new MemoryStore()` or `store: new RedisStore(...)` after installing the appropriate package.
TypeError: rateLimiter is not a function
Attempting to use `require()` for importing `hono-rate-limiter` in an ESM context, or incorrectly trying to import it as a default export.
fix
Use ESM named imports: `import { rateLimiter } from 'hono-rate-limiter';`.
Upgrade
Version history
0.5.3latest on npm
Audit
Dependencies
honorequiredCore web framework peer dependency, as this is a Hono middleware.
unstoragerequiredPeer dependency for storage abstraction, used for types and recommended for persistent stores.
Agent activity
10 hits · last 30 days
node
10
Resources
hono-rate-limiter — npm install hono-rate-limiter · libregistry