import { email } from "https://esm.town/v/std/email?v=9";
let { rateLimitData } = await import("https://esm.town/v/stevekrouse/rateLimitData");
import { msDay } from "https://esm.town/v/stevekrouse/msDay";
export function rateLimit(
key: string,
limit = 1000,
filter = (ms) => Date.now() - ms < msDay,
) {
if (!rateLimitData)
rateLimitData = {};
if (!rateLimitData[key])
rateLimitData[key] = [];
rateLimitData[key] = rateLimitData[key].filter(
filter,
);
rateLimitData[key].push(Date.now());
if (rateLimitData[key].length >= limit) {
return email({
text: rateLimitData[key],
subject: `${key} exceeded the rate limit`,
});
}
return true;
}