Skip to content

hoa-js/cloudflare-rate-limit

Repository files navigation

@hoajs/cloudflare-rate-limit

Cloudflare Rate Limit middleware for Hoa.

Installation

$ npm i @hoajs/cloudflare-rate-limit --save

Quick Start

RateLimiter (using Cloudflare native Rate Limiting API)

import { Hoa } from 'hoa'
import { RateLimiter } from '@hoajs/cloudflare-rate-limit'

const app = new Hoa()

app.use(RateLimiter({
  binding: 'RATE_LIMITER',
  keyGenerator: (ctx) => ctx.req.ip
}))

app.use(async (ctx) => {
  ctx.res.body = 'Hello, Hoa!'
})

export default app

KVRateLimiter (using Cloudflare KV)

import { Hoa } from 'hoa'
import { KVRateLimiter } from '@hoajs/cloudflare-rate-limit'

const app = new Hoa()

app.use(KVRateLimiter({
  binding: 'KV',
  prefix: 'ratelimit:',
  limit: 3,
  period: 60,
  interval: 10,
  keyGenerator: (ctx) => ctx.req.ip
}))

app.use(async (ctx) => {
  ctx.res.body = 'Hello, Hoa!'
})

export default app

Documentation

The documentation is available on hoa-js.com

Test (100% coverage)

$ npm test

License

MIT

About

Cloudflare rate limiter middleware for Hoa.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published