optimize docker, more rate limiting, remove not needed shit in build-prod-package.

This commit is contained in:
KingKDot
2026-05-30 23:20:15 -05:00
parent 2a90053eee
commit 707ba172c0
9 changed files with 235 additions and 14 deletions
+20 -1
View File
@@ -1,7 +1,18 @@
import { wrapServerWithClientIp, type RequestServerLike } from "./client-ip";
import { consumeUnauthorizedRateLimit } from "../rateLimit";
export type RouteHandler = (req: Request, url: URL, server: unknown) => Promise<Response | null>;
function tooManyRequestsResponse(retryAfter = 60): Response {
return new Response("Too many requests", {
status: 429,
headers: {
"Content-Type": "text/plain; charset=utf-8",
"Retry-After": String(retryAfter),
},
});
}
export function createHttpFetchHandler(deps: {
metrics: { withHttpMetrics: (fn: () => Promise<Response>) => Promise<Response> };
CORS_HEADERS: Record<string, string>;
@@ -14,9 +25,17 @@ export function createHttpFetchHandler(deps: {
}
const url = new URL(req.url);
const wrapped = wrapServerWithClientIp(server as RequestServerLike);
const ip = wrapped.requestIP(req)?.address || "unknown";
for (const route of deps.routes) {
const response = await route(req, url, wrapped);
if (response) return response;
if (response) {
if (response.status === 401) {
const limited = consumeUnauthorizedRateLimit(ip);
if (limited.limited) return tooManyRequestsResponse(limited.retryAfter);
}
return response;
}
}
return new Response("Not found", { status: 404 });
});