SPB Git

Repository Public

http.ts 1.1 KB 26 lines typescript

1import { hmacFingerprint } from "./crypto";23export function clientIp(headers: Headers): string {4  const fwd = headers.get("x-forwarded-for");5  if (fwd) return fwd.split(",")[0].trim();6  return headers.get("x-real-ip") ?? "unknown";7}8export function ipHashOf(request: Request): string {9  return hmacFingerprint(clientIp(request.headers));10}11export function json(data: unknown, status = 200, headers: Record<string, string> = {}) {12  return new Response(JSON.stringify(data), { status, headers: { "Content-Type": "application/json; charset=utf-8", "Cache-Control": "no-store", ...headers } });13}14/** Lit un corps JSON borné ; null si absent, trop gros ou invalide. */15export async function readJson(request: Request, maxBytes = 32 * 1024): Promise<unknown | null> {16  if (!(request.headers.get("content-type") ?? "").includes("application/json")) return null;17  if (Number(request.headers.get("content-length") ?? "0") > maxBytes) return null;18  try {19    const text = await request.text();20    if (text.length > maxBytes) return null;21    return JSON.parse(text);22  } catch {23    return null;24  }25}26
Files main
Menu

Theme