hono rate limiter
v0.4.0
hono-rate-limiterHONO的费率限制中间件。用于将重复的请求限制为公共API和/或端点,例如密码重置。
笔记
需要定义keyGenerator函数,以使hono-rate-limiter在您的环境中正常工作。在使用库之前,请确保根据文档定义keyGenerator函数。
# Using npm/yarn/pnpm/bun
npm add hono-rate-limiter import { rateLimiter } from "hono-rate-limiter" ;
// Apply the rate limiting middleware to all requests.
app . use (
rateLimiter ( {
windowMs : 15 * 60 * 1000 , // 15 minutes
limit : 100 , // Limit each IP to 100 requests per `window` (here, per 15 minutes).
standardHeaders : "draft-6" , // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
keyGenerator : ( c ) => "<unique_key>" , // Method to generate custom identifiers for clients.
// store: ... , // Redis, MemoryStore, etc. See below.
} )
) ; import { webSocketLimiter } from "hono-rate-limiter" ;
import { upgradeWebSocket } from "hono/cloudflare-workers" ;
import { RedisStore } from "@hono-rate-limiter/redis" ;
import { Redis } from "@upstash/redis/cloudflare" ;
const limiter = webSocketLimiter ( {
windowMs : 15 * 60 * 1000 , // 15 minutes
limit : 100 , // Limit each IP to 100 requests per `window` (here, per 15 minutes).
keyGenerator : ( c ) => "<unique_key>" , // Method to generate custom identifiers for clients.
store : new RedisStore ( { client } ) , // Define your DataStore. See below.
} ) ;
// Apply the rate limiting middleware to ws requests.
app . get (
"/" ,
upgradeWebSocket (
limiter ( ( c ) => {
return {
onOpen : ( ) => {
console . log ( "Connection opened" ) ;
} ,
async onMessage ( event , ws ) {
console . log ( `Message from client: ${ event . data } ` ) ;
ws . send ( "Hello from server!" ) ;
} ,
onClose : ( ) => {
console . log ( "Connection closed" ) ;
} ,
} ;
} )
)
) ; hono-rate-limiter支持外部数据存储,以在多个过程和服务器上同步命中计数。
默认情况下,使用MemoryStore 。这不会在整个实例中同步其状态。部署很简单,通常足以预防基本的滥用,但在重新启动或具有多个过程或服务器的部署中会不一致。
需要更稳定执行的费率限制的部署应使用外部商店。
这是商店清单:
| 姓名 | 描述 |
|---|---|
| 内存店 | (默认)简单内存选项。当应用具有多个流程或服务器时,不会共享状态。 |
| @Hono-rate-limiter/redis | 一家重新支持的商店,与@vercel/kv和@upstash/redis一起使用。 |
| @Hono-rate-limiter/Cloudflare | 一家由Cloudflare支持的商店,与耐用的对象,WorkersKV和工人一起使用限制API。 |
| 费率限制 | 重新支持的商店,更适合大型或苛刻的部署。 |
| 速率限制的postgresql | 一家后QL支持的商店。 |
| 速率限制的限制 | 一家由备受支持的商店。 |
| 集群内存商店 | 一种内存储物包装器,通过节点:cluster模块在单个服务器上共享所有进程的状态。不会在多个服务器上共享状态。 |
| 精确的内存率限制 | 一个类似于内置的存储器存储器,除了它为每个键存储一个独特的时间戳。 |
| typeorm-rate-limit商店 | 通过TypeORM来支持各种数据库:MySQL,Mariadb,CockroactDB,Sqlite,Microsoft SQL Server,Oracle,SAP HANA等。 |
| @rlimit/storage | 分布式RLIMIT商店,非常适合多区域部署。 |
如果您想创建自己的商店,请查看本指南。
keyGenerator函数确定要限制请求的内容,它应代表您希望限制的用户或一系列用户的唯一特征。不错的选择包括Authorization标题,URL路径或路由中的API键,应用程序使用的特定查询参数和/或用户ID。如果这里的建议不起作用,请尝试在GitHub讨论或Hono Discord的#HELP频道上发布问题。
当使用hono-rate-limiter正式支持的软件包时,您可能会遇到与类型相关的问题。通过参考#22,#10中的讨论可以轻松解决这些问题。例子 -
rateLimiter ( {
// ...
store : new RedisStore ( {
sendCommand : ( ... args : string [ ] ) => redisClient . sendCommand ( args ) ,
} ) as unknown as Store ,
} ) ;hono-rate-limiter与Cloudflare工人或页面一起使用如果您试图在Cloudflare环境(例如工人或页面)中使用hono-rate-limiter ,则可能会遇到以下错误:
Uncaught Error: Disallowed operation called within global scope. Asynchronous I/O (ex: fetch () or connect ()), setting a timeout, and generating random values are not allowed within global scope. To fix this error, perform this operation within a handler. https://developers.cloudflare.com/workers/runtime-apis/handlers/之所以发生这种情况,是因为hono-rate-limiter使用的默认存储器存储由于其对全局异步操作的限制,因此无法在Cloudflare环境中运行。
要解决此问题,您需要将兼容的商店用于Cloudflare。您可以使用@hono-rate-limiter/cloudflare软件包,该软件包专门设计用于使用CloudFlare的基础架构。
我们希望有更多的贡献者参与!
首先,请阅读我们的贡献指南。
hono-rate-limiter项目受到快速速率限制的启发