globalThis.process ??= {};
globalThis.process.env ??= {};
import { l as jsonError } from "./crypto_CY2zBtG3.mjs";
//#region src/lib/rate-limit.ts
var ADMIN_PREFIX = "/api/admin/";
var LOGIN_PATH = "/api/admin/session";
async function enforceAdminRateLimit(env, request) {
	const { pathname } = new URL(request.url);
	if (!pathname.startsWith(ADMIN_PREFIX)) return null;
	const isLogin = pathname === LOGIN_PATH && request.method === "POST";
	const limiter = isLogin ? env.ADMIN_LOGIN_LIMITER : env.ADMIN_API_LIMITER;
	if (!limiter) return null;
	const ip = request.headers.get("CF-Connecting-IP") ?? "unknown";
	const key = `${isLogin ? "login" : "api"}:${ip}`;
	const { success } = await limiter.limit({ key });
	if (success) return null;
	const response = jsonError(429, "rate_limited", "Too many requests. Please retry shortly.");
	response.headers.set("Retry-After", "60");
	return response;
}
/** Public comment POST rate-limit — keyed comment:<ip>; fail-open when binding absent. */
async function enforcePublicCommentRateLimit(env, request) {
	const limiter = env.COMMENTS_LIMITER;
	if (!limiter) return null;
	const ip = request.headers.get("CF-Connecting-IP") ?? "unknown";
	const { success } = await limiter.limit({ key: `comment:${ip}` });
	if (success) return null;
	const response = jsonError(429, "rate_limited", "Too many requests. Please retry shortly.");
	response.headers.set("Retry-After", "60");
	return response;
}
/** Public contact form POST rate-limit — keyed form:<ip>; fail-open when binding absent. */
async function enforcePublicFormRateLimit(env, request) {
	const limiter = env.FORMS_LIMITER;
	if (!limiter) return null;
	const ip = request.headers.get("CF-Connecting-IP") ?? "unknown";
	const { success } = await limiter.limit({ key: `form:${ip}` });
	if (success) return null;
	const response = jsonError(429, "rate_limited", "Too many requests. Please retry shortly.");
	response.headers.set("Retry-After", "60");
	return response;
}
//#endregion
export { enforcePublicCommentRateLimit as n, enforcePublicFormRateLimit as r, enforceAdminRateLimit as t };
