18 lines
1.1 KiB
TypeScript
18 lines
1.1 KiB
TypeScript
import { z } from "zod"
|
|
import { appendEventSchema } from "@mmapp/contracts/events"
|
|
export const eventsBatchSchema = z.object({ events: z.array(appendEventSchema.strict()).min(1).max(200) }).strict()
|
|
/** Bound opaque JSON before encryption, SQL, preview and hashing. */
|
|
export function assertEventBatchBudget(value: unknown) {
|
|
let nodes = 0
|
|
const visit = (item: unknown, depth: number) => {
|
|
if (++nodes > 20000 || depth > 12) throw new Error("Event JSON complexity limit")
|
|
if (item === null || typeof item === "string" || typeof item === "boolean") return
|
|
if (typeof item === "number" && Number.isFinite(item)) return
|
|
if (Array.isArray(item)) { for (const entry of item) visit(entry, depth + 1); return }
|
|
if (item && typeof item === "object" && Object.getPrototypeOf(item) === Object.prototype) { for (const entry of Object.values(item)) if (entry !== undefined) visit(entry, depth + 1); return }
|
|
throw new Error("Event payload must be JSON")
|
|
}
|
|
visit(value, 0)
|
|
if (Buffer.byteLength(JSON.stringify(value), "utf8") > 65536) throw new Error("Event JSON byte limit")
|
|
}
|