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

hono-cloudflare-rate-limit

v0.0.1

Published

Hono rate limit middleware for Cloudflare Workers using the native Rate Limiting binding. Throttle requests per IP, user, or API key and return 429 Too Many Requests.

Readme

hono-cloudflare-rate-limit

npm license

A tiny, env-agnostic Hono rate limit middleware for Cloudflare Workers, built on the native Workers Rate Limiting binding (ratelimits in wrangler.jsonc / wrangler.toml). Throttle API requests per IP, per user, or per API key and return 429 Too Many Requests when the limit is exceeded — useful for protecting login, signup, password reset, and OTP endpoints from brute-force attacks and abuse at the edge.

The middleware only calls the binding, throws on overflow, and exposes an on/off hook. You decide which binding to use, how to build the counter key, and when to skip limiting — it never reads a fixed env shape, so it works in any Hono app on Cloudflare Workers.

Install

npm install hono-cloudflare-rate-limit
# pnpm add hono-cloudflare-rate-limit

hono and @cloudflare/workers-types are peer dependencies.

Runtime requirement

The limit() binding is a Cloudflare Workers (workerd) only API. It does not exist on Node.js, Bun, Deno, or other regular servers, so this middleware won't work there.

To rate limit an origin server through Cloudflare instead:

  • Use WAF Rate Limiting Rules at the edge, or put a thin Worker in front of the origin.
  • Lock the origin down to Cloudflare IP ranges + Authenticated Origin Pulls (mTLS). Only then can you trust CF-Connecting-IP — requests that bypass the edge can omit or spoof that header.

Usage

Declare a rate limit binding in wrangler.jsonc:

{
  "ratelimits": [
    {
      "name": "RL",
      "namespace_id": "1001",
      "simple": { "limit": 100, "period": 60 }
    }
  ]
}

Then apply the middleware:

import { Hono } from "hono";
import type { RateLimit } from "@cloudflare/workers-types";
import { rateLimit, cfConnectingIp } from "hono-cloudflare-rate-limit";

type AppEnv = {
  Bindings: { RL: RateLimit; APP_URL: string };
  Variables: {};
};

const app = new Hono<AppEnv>();

const guard = rateLimit<AppEnv>({
  // Return false to skip limiting, e.g. in local development.
  enabled: (c) => !c.env.APP_URL.startsWith("http://localhost"),
  limiter: (c) => c.env.RL,
  key: (c) => {
    const ip = cfConnectingIp(c);
    if (!ip) throw new Error("unknown client");
    return `api:${ip}`;
  },
});

app.use("/api/*", guard);

By default an exceeded limit throws HTTPException(429, "too many requests"). Pass onExceeded to return a custom response.

Use cases

  • Per-IP API limit — key by api:${ip} on /api/*.
  • Stricter limits on sensitive routes — bind a separate, tighter limiter to login, signup, password reset, or OTP endpoints to slow down brute-force and spam.
  • Per-user or per-API-key quotas — key by user:${userId} or key:${apiKey} for plan-based limits.
  • Disable in development — return false from enabled for localhost.
  • Custom 429 response — use onExceeded to send a JSON error body or a Retry-After header:
rateLimit<AppEnv>({
  limiter: (c) => c.env.RL,
  key: (c) => `login:${cfConnectingIp(c) ?? "unknown"}`,
  onExceeded: (c) => {
    c.header("Retry-After", "60");
    return c.json({ error: "rate_limited" }, 429);
  },
});

API

rateLimit(options): MiddlewareHandler

| Option | Type | Description | | ------------ | --------------------------------------------- | -------------------------------------------------------------------------------------------------------- | | limiter | (c) => RateLimit | Picks the rate limit binding from env. | | key | (c) => string | Builds the counter key. If it throws (e.g. client can't be identified), the error propagates as-is. | | enabled? | (c) => boolean | Return false to skip limiting for this request. Defaults to always on. | | onExceeded? | (c) => Response \| Promise<Response> | Response to send when the limit is exceeded. Defaults to throwing HTTPException(429). |

cfConnectingIp(c): string | null

Returns the CF-Connecting-IP header set by the Cloudflare edge. It is null for requests that didn't pass through the edge (local dev, service bindings, etc.).

License

MIT