import type { Annotation, AnnotationList } from '@embedpdf/engine-core'; import type { Engine } from '@embedpdf/engine-core/runtime'; /** * What we collect from a single engine for the annotations fixture: the * whole-document list plus each page's own list, for every page in the * document. Both shapes are what the v3 conformance * harness exercises, so by running the same probe against local + cloud * we get a parity check that mirrors what tests already enforce. */ export interface AnnotationsDemoResult { label: string; docId: string; /** Total elapsed ms, including open + the document list + each page's list + close. */ elapsedMs: number; all: AnnotationList; byPage: Record; } export async function runAnnotationsDemo( label: string, engine: Engine, pdfBytes: Uint8Array, docId = `annot-demo-${label}`, ): Promise { const started = Date.now(); const doc = await engine.open({ kind: 'bytes', id: docId, bytes: pdfBytes }); try { const all = await doc.annotations.list(); const byPage: Record = {}; for (const page of all.pages) { byPage[page.objectNumber] = await doc.page(page).annotations.list(); } return { label, docId: doc.id, elapsedMs: Date.now() - started, all, byPage }; } finally { await doc.close(); } } /** A compact human summary of a list, for console output. */ export interface AnnotationsSummary { pages: Array<{ pageObjectNumber: number; pageIndex: number; annotations: Array<{ index: number; subtype: string; ref: string; nm: string | null; }>; }>; } export function summarizeList(list: AnnotationList): AnnotationsSummary { return { pages: list.pages.map((page, pageIndex) => ({ pageObjectNumber: page.objectNumber, pageIndex, annotations: list.annotations .filter((a) => a.page.objectNumber === page.objectNumber) .map((a, index) => ({ index, subtype: a.subtype, ref: describeRef(a), nm: a.nm, })), })), }; } function describeRef(a: Annotation): string { switch (a.ref.kind) { case 'objectNumber': return `objectNumber=${a.ref.objectNumber}`; case 'baseIndex': return `baseIndex=${a.ref.baseIndex}`; } }