feat: add rate limits to import/export
This commit is contained in:
parent
45941e47d6
commit
fc4f106afb
3 changed files with 32 additions and 2 deletions
18
backend/src/api/rateLimits.ts
Normal file
18
backend/src/api/rateLimits.ts
Normal 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();
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue