feat: add rate limits to import/export

This commit is contained in:
Dragory 2021-11-03 00:49:36 +02:00
parent 45941e47d6
commit fc4f106afb
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
3 changed files with 32 additions and 2 deletions

View file

@ -0,0 +1,18 @@
import { Request, Response } from "express";
import { error } from "./responses";
const lastRequestsByKey: Map<string, number> = new Map();
export function rateLimit(getKey: (req: Request) => string, limitMs: number, message = "Rate limited") {
return async (req: Request, res: Response, next) => {
const key = getKey(req);
if (lastRequestsByKey.has(key)) {
if (lastRequestsByKey.get(key)! > Date.now() - limitMs) {
return error(res, message, 429);
}
}
lastRequestsByKey.set(key, Date.now());
next();
};
}