1.0.2 • Published 4 years ago

koa-advanced-ratelimit v1.0.2

Weekly downloads
23
License
ISC
Repository
github
Last release
4 years ago

koa-advanced-ratelimit

An advanced ratelimiter for koa.js 2.0.

Features:

  • Customisable time limit
  • Customisable amount of requests/time
  • Automatically increase ratelimit on repeated fails
  • Custom response format
  • Automatically ban users (403) on several repeated fails
  • Custom ban response format
  • Custom IP functions

Installation

npm install koa-advanced-ratelimit

Usage

const Koa = require('koa');
const app = new Koa();

const { Ratelimiter } = require('koa-advanced-ratelimiter');

const ratelimit = new Ratelimiter(options);

app.use(ratelimit.middleware());

app.use(ctx => {
	ctx.body = 'Hello World';
});

app.listen(3000);

Options

PropertyTypeExampleDefault Value
totalInteger10 // 10 requests allowed in time10
timeInteger60 // 60 seconds60
increasingLimitBooleanfalsefalse
responseFormatAny"You have been banned"{ status: 429, message: 'You have been ratelimited.' }
banUsersBooleanfalsefalse
banResponseFormatAny"You have been banned"{ status: 403, message: 'You have been banned.' }
getIPFunction(ctx) => ctx.request.ip(ctx) => ctx.get('cf-connecting-ip') || ctx.get('x-forwarded-for') || ctx.request.ip