Skip to content

gitflarePublicPartly agent-ready50

cf-d1-queues-pulse

Event analytics on Cloudflare D1 and Queues: ingestion API, batched consumer, rollups, live dashboard, load generator, dead-letter view.

Build the full D1 + Queues pulse app: dashboard, load generator, dead letters, percentiles, docs8a65841
TypeScript31 lines1.4 KB
export interface PulseEvent { type: string; path: string; country: string; value: number; ts: number } export function validateEvent(raw: unknown, now: number): PulseEvent | null {  if (!raw || typeof raw !== "object") return null;  const r = raw as Record<string, unknown>;  if (typeof r.type !== "string" || !/^[\w.:-]{1,64}$/.test(r.type)) return null;  if (r.path !== undefined && (typeof r.path !== "string" || r.path.length > 256)) return null;  if (r.country !== undefined && (typeof r.country !== "string" || !/^[A-Za-z]{2}$/.test(r.country))) return null;  if (r.value !== undefined && (typeof r.value !== "number" || !Number.isFinite(r.value))) return null;  if (r.ts !== undefined && (typeof r.ts !== "number" || !Number.isFinite(r.ts) || r.ts <= 0)) return null;  return {    type: r.type,    path: (r.path as string | undefined) ?? "/",    country: ((r.country as string | undefined) ?? "XX").toUpperCase(),    value: (r.value as number | undefined) ?? 0,    ts: (r.ts as number | undefined) ?? now,  };} export function validateBatch(body: unknown, now: number) {  const items = Array.isArray(body) ? body : [body];  if (items.length > 100) return { error: "too many events (max 100)" as const };  const accepted: PulseEvent[] = [];  let rejected = 0;  for (const i of items) {    const e = validateEvent(i, now);    if (e) accepted.push(e); else rejected++;  }  return { accepted, rejected };}