Add rate limit information in header

pull/2261/head^2
Jérémy Rialland 2022-08-31 10:55:04 +02:00 committed by Jérémy Rialland
parent cada4c7c2b
commit 3fa193ec56
1 changed files with 30 additions and 2 deletions

View File

@ -1,6 +1,10 @@
import { BaseContext, Next } from 'koa'
import { RateLimiterMemory, RateLimiterRedis } from 'rate-limiter-flexible'
import IORedis from 'ioredis'
import { BaseContext, Next } from 'koa'
import {
RateLimiterMemory,
RateLimiterRedis,
RateLimiterRes,
} from 'rate-limiter-flexible'
const Redis = IORedis.default
@ -26,8 +30,32 @@ export const rateLimiterMiddleware = async (ctx: BaseContext, next: Next) => {
ctx.status = 429
ctx.body = 'Too Many Requests'
if (isRateLimiterRes(rejRes)) {
ctx.set({
'Retry-After': (rejRes.msBeforeNext / 1000).toString(),
'X-RateLimit-Limit': rateLimiter.points.toString(),
'X-RateLimit-Remaining': rejRes.remainingPoints.toString(),
'X-RateLimit-Reset': new Date(
Date.now() + rejRes.msBeforeNext
).toString(),
})
}
return
}
await next()
}
const isRateLimiterRes = (val: unknown): val is RateLimiterRes => {
return !!(
val &&
typeof val === 'object' &&
[
'msBeforeNext',
'remainingPoints',
'consumedPoints',
'isFirstInDuration',
].every((s) => s in val)
)
}