init
This commit is contained in:
37
packages/next-utils/src/index.ts
Normal file
37
packages/next-utils/src/index.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
// Simple in-memory rate limiting
|
||||
const submissions: Record<string, number> = {};
|
||||
const RATE_LIMIT_WINDOW = 60 * 60 * 1000; // 1 hour
|
||||
const MAX_SUBMISSIONS_PER_WINDOW = 3;
|
||||
|
||||
export async function rateLimit(identifier: string, windowMs = RATE_LIMIT_WINDOW, maxSubmissions = MAX_SUBMISSIONS_PER_WINDOW) {
|
||||
const now = Date.now();
|
||||
|
||||
// Clean up old submissions
|
||||
Object.keys(submissions).forEach((key) => {
|
||||
if (now - submissions[key] > windowMs) {
|
||||
delete submissions[key];
|
||||
}
|
||||
});
|
||||
|
||||
// Check if identifier has exceeded submission limit
|
||||
const currentSubmissions = Object.values(submissions).filter(
|
||||
(timestamp) => now - timestamp <= windowMs
|
||||
);
|
||||
|
||||
if (currentSubmissions.length >= maxSubmissions) {
|
||||
throw new Error("Too many submissions. Please try again later.");
|
||||
}
|
||||
|
||||
// Record this submission
|
||||
submissions[identifier] = now;
|
||||
}
|
||||
|
||||
export const languages = ["en", "de"] as const;
|
||||
export type Lang = (typeof languages)[number];
|
||||
|
||||
export function isValidLang(lang: string): lang is Lang {
|
||||
return (languages as readonly string[]).includes(lang);
|
||||
}
|
||||
|
||||
export * from "./i18n";
|
||||
export * from "./env";
|
||||
Reference in New Issue
Block a user