Add Rate Limiting
A general API limit plus an exact, durable login limit on Cloudflare Workers.
Install
npm i @dapplelight/signet-server honoLimit the API
import { rateLimit } from "@dapplelight/signet-server/middleware/rate-limit"
import { Hono } from "hono"
const app = new Hono()
app.use("/api/*", rateLimit({ max: 100, windowSeconds: 60, trust: "cloudflare" }))The default store is per isolate on Workers, which is fine for general traffic. trust is required: it says which proxy header to believe.
Add an exact limit for login
import { durableObjectStore } from "@dapplelight/signet-server/middleware/rate-limit-do"
app.use("/api/auth/login", rateLimit({
scope: "login",
max: 5,
windowSeconds: 60,
trust: "cloudflare",
store: (c) => durableObjectStore(c.env.RATE_LIMIT), // bindings live on c.env
failOpen: false, // a login limit is a security control: 503 if the store is down
}))Export and bind the Durable Object
export { RateLimitCounter } from "@dapplelight/signet-server/middleware/rate-limit-do"{
"durable_objects": { "bindings": [{ "name": "RATE_LIMIT", "class_name": "RateLimitCounter" }] },
"migrations": [{ "tag": "v1", "new_sqlite_classes": ["RateLimitCounter"] }]
}Check the setup
npx signet-doctorIt checks the binding, the migration and the export, and exits 1 on a problem, so it fits in CI.
Off Cloudflare, use upstashRateLimitStore from @dapplelight/signet-server/adapters/upstash for a shared count.