gitflarePublicPartly agent-ready50
cf-durable-whiteboard
Realtime collaborative whiteboard on Cloudflare Durable Objects: presence cursors, WebSocket hibernation, SQLite storage, undo, PNG export.
- LicenseApache-2.0
- LanguageTypeScript
- Last landed
6eb4fbdOct 20, 2026
Keep board zoom and pan stable across reloads
6eb4fbdexport const MAX_MESSAGE_BYTES = 32 * 1024;export const KINDS = ["pen", "rect", "ellipse", "arrow", "note"] as const;export type Shape = { id: string; kind: (typeof KINDS)[number]; z?: number; [k: string]: unknown };export type Op = | { type: "add"; shape: Shape } | { type: "update"; shape: Shape } | { type: "delete"; id: string } | { type: "clear" } | { type: "batch"; ops: Op[] };export type ClientMsg = | { type: "hello"; name: string } | { type: "op"; op: Op } | { type: "undo" } | { type: "redo" } | { type: "cursor"; x: number; y: number }; const num = (v: unknown) => typeof v === "number" && Number.isFinite(v);const isObj = (v: unknown): v is Record<string, unknown> => typeof v === "object" && v !== null && !Array.isArray(v); export function validateShape(v: unknown): v is Shape { if (!isObj(v)) return false; if (typeof v.id !== "string" || !/^[\w-]{1,40}$/.test(v.id)) return false; if (!(KINDS as readonly string[]).includes(v.kind as string)) return false; if (v.z !== undefined && !num(v.z)) return false; if (v.kind === "pen") { if (!Array.isArray(v.points) || v.points.length < 1 || v.points.length > 2000) return false; if (!v.points.every(num)) return false; if (v.points.length % 2) return false; } else { if (!num(v.x) || !num(v.y)) return false; if (v.kind === "note") { if (typeof v.text !== "string" || v.text.length > 2000) return false; } else if (!num(v.w) || !num(v.h)) return false; } if (v.colour !== undefined && (typeof v.colour !== "string" || !/^#[0-9a-fA-F]{6}$/.test(v.colour))) return false; if (v.size !== undefined && !num(v.size)) return false; return true;} export function validateOp(v: unknown): v is Op { if (!isObj(v)) return false; switch (v.type) { case "add": case "update": return validateShape(v.shape); case "delete": return typeof v.id === "string" && v.id.length > 0 && v.id.length <= 40; case "clear": return true; default: return false; }} export function parseClientMessage(raw: string): ClientMsg | null { if (raw.length > MAX_MESSAGE_BYTES || new TextEncoder().encode(raw).length > MAX_MESSAGE_BYTES) return null; let m: unknown; try { m = JSON.parse(raw); } catch { return null; } if (!isObj(m)) return null; switch (m.type) { case "hello": return typeof m.name === "string" ? { type: "hello", name: m.name.trim().slice(0, 24) || "Guest" } : null; case "op": return validateOp(m.op) ? { type: "op", op: m.op } : null; case "undo": case "redo": return { type: m.type }; case "cursor": return num(m.x) && num(m.y) ? { type: "cursor", x: m.x as number, y: m.y as number } : null; default: return null; }}