SPB Git

Repository Public

format.ts 6.5 KB 93 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Note SPBoucher AI8 * File             : src/lib/canvas/format.ts9 * Description      : the canvas document `spn-canvas-1` (SPEC §3.4): shape,10 *                    empty value, structural validation and limits. Pure11 *                    (shared by the server, the web client and mirrored by12 *                    SPNotesCore). Unknown node kinds and unknown fields are13 *                    kept untouched.14 * Module           : lib15 * Repository       : https://git.spboucher.ai/note-spboucher-ai16 * Project Website  : https://note.spboucher.ai17 * Created          : 2026-11-1918 * Last Modified    : 2026-11-1919 *20 * Author           : Simon-Pierre Boucher21 * Position         : Professor of Finance22 * Institution      : Université du Québec en Outaouais (UQO)23 * Organization     : SP Boucher24 * Website          : https://spboucher.ai25 * Git              : https://git.spboucher.ai26 *27 * Preferred Contact: contact@spboucher.ai28 * Development      : dev@spboucher.ai29 * Support          : support@spboucher.ai30 * Security/Admin   : admin@spboucher.ai31 *32 * Copyright (c) 202633 * Simon-Pierre Boucher34 * All rights reserved where applicable. This file is governed by the35 * applicable project license, copyright notices and relevant law.36 * Existing third-party rights and notices remain unaffected.37 *38 * License          : None specified (no license file in this repository)39 *40 * Do not embed credentials, private keys, access tokens,41 * certificates or confidential personal information.42 *43 * =============================================================================44 * END OF SP BOUCHER SOURCE HEADER45 * =============================================================================46 */4748export const CANVAS_FORMAT = "spn-canvas-1";49export const CANVAS_NODE_KINDS = ["text", "page", "image", "pdf", "link", "drawing", "shape", "reference", "math", "table"] as const;50export interface CanvasNode { id: string; kind: string; x: number; y: number; w: number; h: number; z?: number; text?: string; pageId?: string; attachmentId?: string; url?: string; refId?: string; latex?: string; color?: string; shape?: string; ink?: string; [k: string]: unknown }51export interface CanvasEdge { id: string; from: string; to: string; label?: string; kind?: string; color?: string; [k: string]: unknown }52export interface CanvasData { format: typeof CANVAS_FORMAT; nodes: CanvasNode[]; edges: CanvasEdge[]; viewport: { x: number; y: number; zoom: number }; [k: string]: unknown }53export const EMPTY_CANVAS = (): CanvasData => ({ format: CANVAS_FORMAT, nodes: [], edges: [], viewport: { x: 0, y: 0, zoom: 1 } });54export const CANVAS_LIMITS = { nodes: 5_000, edges: 10_000, bytes: 4 * 1024 * 1024, textChars: 100_000, idChars: 64 } as const;5556const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;57const num = (v: unknown) => typeof v === "number" && Number.isFinite(v);58const idOk = (v: unknown) => typeof v === "string" && v.length >= 1 && v.length <= CANVAS_LIMITS.idChars;59/** Returns a human message when `data` is not a valid spn-canvas-1 document, else null. */60export function canvasProblem(data: unknown): string | null {61  const d = data as CanvasData | null;62  if (!d || typeof d !== "object" || d.format !== CANVAS_FORMAT) return `data.format must be "${CANVAS_FORMAT}"`;63  if (!Array.isArray(d.nodes) || !Array.isArray(d.edges)) return "data.nodes and data.edges must be arrays";64  if (d.nodes.length > CANVAS_LIMITS.nodes) return `a canvas holds at most ${CANVAS_LIMITS.nodes} nodes`;65  if (d.edges.length > CANVAS_LIMITS.edges) return `a canvas holds at most ${CANVAS_LIMITS.edges} edges`;66  const v = d.viewport; if (!v || typeof v !== "object" || !num(v.x) || !num(v.y) || !num(v.zoom) || v.zoom <= 0) return "data.viewport must be {x, y, zoom > 0}";67  const ids = new Set<string>();68  for (const n of d.nodes) {69    if (!n || typeof n !== "object" || !idOk(n.id)) return "every node needs a string id (1–64 characters)";70    if (ids.has(n.id)) return `duplicate node id ${n.id}`; ids.add(n.id);71    if (typeof n.kind !== "string" || !n.kind || n.kind.length > 32) return `node ${n.id}: kind must be a short string`;72    if (!num(n.x) || !num(n.y) || !num(n.w) || !num(n.h) || n.w < 0 || n.h < 0) return `node ${n.id}: x, y, w, h must be finite numbers (w, h ≥ 0)`;73    if (n.z !== undefined && !num(n.z)) return `node ${n.id}: z must be a number`;74    for (const k of ["pageId", "attachmentId", "refId", "ink"] as const) if (n[k] !== undefined && n[k] !== null && !(typeof n[k] === "string" && UUID.test(n[k] as string))) return `node ${n.id}: ${k} must be a uuid`;75    for (const k of ["text", "latex"] as const) if (n[k] !== undefined && n[k] !== null && !(typeof n[k] === "string" && (n[k] as string).length <= CANVAS_LIMITS.textChars)) return `node ${n.id}: ${k} must be a string of at most ${CANVAS_LIMITS.textChars} characters`;76    if (n.url !== undefined && n.url !== null && !(typeof n.url === "string" && n.url.length <= 2000 && !/^\s*(javascript|vbscript|data:text\/html)/i.test(n.url))) return `node ${n.id}: unsafe or oversized url`;77  }78  const eids = new Set<string>();79  for (const e of d.edges) {80    if (!e || typeof e !== "object" || !idOk(e.id)) return "every edge needs a string id (1–64 characters)";81    if (eids.has(e.id)) return `duplicate edge id ${e.id}`; eids.add(e.id);82    if (!ids.has(e.from as string) || !ids.has(e.to as string)) return `edge ${e.id}: from and to must be ids of nodes of this canvas`;83    if (e.label !== undefined && e.label !== null && !(typeof e.label === "string" && e.label.length <= 500)) return `edge ${e.id}: label must be a string of at most 500 characters`;84  }85  if (JSON.stringify(d).length > CANVAS_LIMITS.bytes) return "the canvas is too large (4 MB max)";86  return null;87}88/** Pages and references a canvas points to (cards of kind page / reference): used for the graph and the time machine. */89export function canvasRefs(d: CanvasData): { pages: string[]; references: string[]; attachments: string[] } {90  const pick = (k: "pageId" | "refId" | "attachmentId" | "ink") => [...new Set(d.nodes.map((n) => n[k]).filter((x): x is string => typeof x === "string" && UUID.test(x)).map((x) => x.toLowerCase()))];91  return { pages: pick("pageId"), references: pick("refId"), attachments: [...new Set([...pick("attachmentId"), ...pick("ink")])] };92}93
Files main
Menu

Theme