2022-08-30 19:58:35 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
import cache from "memory-cache";
|
|
|
|
|
2023-03-10 23:45:24 +00:00
|
|
|
import { ErrorCode } from "@calcom/features/auth/lib/ErrorCode";
|
2022-08-30 19:58:35 +00:00
|
|
|
|
|
|
|
const rateLimit = (options: { intervalInMs: number }) => {
|
|
|
|
return {
|
|
|
|
check: (requestLimit: number, uniqueIdentifier: string) => {
|
|
|
|
const count = cache.get(uniqueIdentifier) || [0];
|
|
|
|
if (count[0] === 0) {
|
|
|
|
cache.put(uniqueIdentifier, count, options.intervalInMs);
|
|
|
|
}
|
|
|
|
count[0] += 1;
|
|
|
|
|
|
|
|
const currentUsage = count[0];
|
|
|
|
const isRateLimited = currentUsage >= requestLimit;
|
|
|
|
|
|
|
|
if (isRateLimited) {
|
|
|
|
throw new Error(ErrorCode.RateLimitExceeded);
|
|
|
|
}
|
|
|
|
|
|
|
|
return { isRateLimited, requestLimit, remaining: isRateLimited ? 0 : requestLimit - currentUsage };
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default rateLimit;
|