46 lines
4.1 KiB
TypeScript
46 lines
4.1 KiB
TypeScript
/**
|
|
* 流式 canonical 捕获的范围证据校验:每个 canvas 的直属成员数量、全部活元素 ID→owner 和拥有树。
|
|
* 这些头部证据用于判断某 owner 是否完整,不能替代尾部 payload/schema/端点的完整文档校验。
|
|
*/
|
|
import type {Id} from '../canvas-core/types.ts';
|
|
import {fault} from '../canvas-core/values.ts';
|
|
|
|
/** Snapshot-bound direct membership counts, including nodes and connections. */
|
|
/** 当前捕获快照的直属数量,包含节点和边;不是递归子树大小或 DOM 已挂载数量。 */
|
|
export type CanvasElementCounts=Readonly<Record<Id,number>>;
|
|
/** 要求 counts 精确覆盖 canvasIds、每项非负安全整数,且总和等于声明 total。 */
|
|
export function validateCanvasElementCounts(value:unknown,canvasIds:readonly Id[],total:number):asserts value is CanvasElementCounts {
|
|
if(!value||typeof value!=='object'||Array.isArray(value))throw fault('invalid_model_coverage');
|
|
const counts=value as Record<Id,unknown>,keys=Object.keys(counts);let sum=0;
|
|
if(keys.length!==canvasIds.length||canvasIds.some(id=>!Object.hasOwn(counts,id)))throw fault('invalid_model_coverage');
|
|
for(const count of Object.values(counts)){if(typeof count!=='number'||!Number.isSafeInteger(count)||count<0)throw fault('invalid_model_coverage');sum+=count;if(!Number.isSafeInteger(sum))throw fault('invalid_model_coverage');}
|
|
if(sum!==total)throw fault('invalid_model_coverage');
|
|
}
|
|
|
|
/** All active IDs in the capture, including records not yet received. */
|
|
/** 完整活 ID 命名空间,即使部分记录尚未传到客户端,也预先知道其直接 owner。 */
|
|
export type ElementOwners=Readonly<Record<Id,Id>>;
|
|
/** 核验每个活 ID 的 owner 合法,并按 owner 反算数量与 counts 一致,不靠已读前缀猜完整性。 */
|
|
export function validateElementOwners(value:unknown,counts:CanvasElementCounts,total:number):asserts value is ElementOwners {
|
|
if(!value||typeof value!=='object'||Array.isArray(value)||Object.keys(value).length!==total)throw fault('invalid_model_identity');
|
|
// 无原型计数表避免特殊 owner 键污染计数;空 canvas 由缺省 0 与 counts 比较。
|
|
const actual:Record<Id,number>=Object.create(null);
|
|
for(const [id,owner]of Object.entries(value)){if(!id||typeof owner!=='string'||!Object.hasOwn(counts,owner))throw fault('invalid_model_identity');actual[owner]=(actual[owner]??0)+1;}
|
|
if(Object.keys(counts).some(id=>(actual[id]??0)!==counts[id]))throw fault('invalid_model_identity');
|
|
}
|
|
|
|
/** Full captured topology, independent of which entry payloads have arrived. */
|
|
/** 用完整 topology+owner 证明验证单根、父存在、入口唯一/归属正确和无环。
|
|
* 此时入口记录可能尚未收到,入口是否确为 node 仍需 payload 到达后的文档校验。 */
|
|
export function validateCanvasTopology(canvases:readonly import('../canvas-core/types.ts').CanvasRecord[],owners:ElementOwners,projectId:Id):void {
|
|
if(!Array.isArray(canvases)||!canvases.length)throw fault('invalid_model_topology');
|
|
const table=new Map(canvases.map(canvas=>[canvas?.id,canvas]));if(table.size!==canvases.length||!table.has(projectId))throw fault('invalid_model_topology');
|
|
// 每个入口最多绑定一个子 canvas;逐条父链用 seen 检测环,根 ID 必须等于 projectId。
|
|
let roots=0;const entries=new Set<Id>();
|
|
for(const canvas of canvases){if(!canvas||typeof canvas.id!=='string'||!canvas.id||!Object.hasOwn(canvas,'parent'))throw fault('invalid_model_topology');if(canvas.parent===null){roots++;if(canvas.id!==projectId)throw fault('invalid_model_topology');continue;}
|
|
const parent=canvas.parent;if(!parent||!table.has(parent.canvasId)||typeof parent.entryElementId!=='string'||!parent.entryElementId||entries.has(parent.entryElementId)||owners[parent.entryElementId]!==parent.canvasId)throw fault('invalid_model_topology');entries.add(parent.entryElementId);
|
|
const seen=new Set<Id>();let current=canvas;while(current.parent){if(seen.has(current.id))throw fault('invalid_model_topology');seen.add(current.id);const next=table.get(current.parent.canvasId);if(!next)throw fault('invalid_model_topology');current=next;}
|
|
}
|
|
if(roots!==1)throw fault('invalid_model_topology');
|
|
}
|