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.
- LicenseApache-2.0
- LanguageTypeScript
- Last landed
8a65841Oct 20, 2026
Build the full D1 + Queues pulse app: dashboard, load generator, dead letters, percentiles, docs
8a65841import { describe, it, expect } from "vitest";import worker from "../src/index";import { percentile, reservoir } from "../src/lib/percentile";import { clampPlan, eventsForTick, splitBatches, pickType, makeEvent } from "../src/lib/loadgen"; function fakeEnv(dead: { id: number; body: string }[] = []) { const sent: unknown[] = []; const stmt = (sql: string) => ({ args: [] as unknown[], bind(...a: unknown[]) { this.args = a; return this; }, async all() { if (sql.includes("FROM dead_letters")) return { results: sql.includes("WHERE id") ? dead.filter((d) => d.id === this.args[0]) : dead }; if (sql.includes("value_samples")) return { results: [{ value: 1 }, { value: 3 }] }; return { results: [] }; }, async first() { return sql.includes("COUNT") ? { n: 4 } : { m: null }; }, async run() { return { meta: { changes: 0 } }; }, }); const env = { DB: { prepare: stmt, batch: async (s: unknown[]) => s }, EVENTS: { sendBatch: async (m: unknown[]) => { sent.push(...m); } }, ASSETS: { fetch: async () => new Response("<html>", { headers: { "content-type": "text/html" } }) }, }; return { env: env as never, sent };}const call = (env: never, path: string, init?: RequestInit) => worker.fetch(new Request("http://x" + path, init), env); describe("percentile", () => { it("interpolates", () => { expect(percentile([1, 2, 3, 4], 50)).toBe(2.5); expect(percentile([], 50)).toBeNull(); expect(percentile([5], 95)).toBe(5); }); it("reservoir caps", () => { expect(reservoir([1, 2, 3, 4, 5], 2, () => 0.5)).toHaveLength(2); });});describe("loadgen", () => { it("plans", () => { expect(clampPlan({ rate: 9999 }).rate).toBe(500); expect(eventsForTick(10, 250, 0)).toEqual({ n: 2, carry: 0.5 }); expect(splitBatches(250)).toEqual([100, 100, 50]); expect(pickType({ a: 1, b: 1 }, 0.9)).toBe("b"); expect(makeEvent({ ...clampPlan({}), poisonPct: 100 }, () => 0, 1)).toHaveProperty("poison", true); });});describe("api", () => { it("stats", async () => { const { env } = fakeEnv(); const j: any = await (await call(env, "/api/stats")).json(); expect(j.percentiles.p50).toBe(2); expect(j.batches).toBe(4); expect(j.series).toHaveLength(60); }); it("events accept and 413", async () => { const { env, sent } = fakeEnv(); const r = await call(env, "/api/events", { method: "POST", body: JSON.stringify([{ type: "a" }, {}]) }); expect(r.status).toBe(202); expect(sent).toHaveLength(1); const big = await call(env, "/api/events", { method: "POST", body: "x".repeat(300000) }); expect(big.status).toBe(413); expect(((await big.json()) as any).error.code).toBe("payload_too_large"); }); it("dlq list, replay", async () => { const { env, sent } = fakeEnv([{ id: 1, body: '{"type":"a"}' }]); expect(((await (await call(env, "/api/dlq")).json()) as any).count).toBe(1); expect((await call(env, "/api/dlq/replay", { method: "POST", body: '{"id":9}' })).status).toBe(404); const ok = await call(env, "/api/dlq/replay", { method: "POST", body: "{}" }); expect(((await ok.json()) as any).replayed).toBe(1); expect(sent).toHaveLength(1); expect((await call(env, "/api/dlq", { method: "DELETE" })).status).toBe(200); }); it("html headers", async () => { const { env } = fakeEnv(); expect((await call(env, "/")).headers.get("x-frame-options")).toBe("DENY"); });});