// scene-version.mjs — a scene's version (scenes and clips plan call R1, // as amended in review), for scenes.mjs: core/src/scene.rs's // scene_canonical and scene_version, and internal/slides/deck/scenes.go's // SceneCanonical and SceneVersion. // // The version is not stored. It is the SHA-256, lowercase hex, of the // canonical JSON of the object {code, data, words}: JSON.stringify with // object keys sorted by code point at every level, UTF-8. Absent code is // "", absent data null, absent words {}. vectors/scene-version.json holds // the three to one answer. import { digest } from 'lib0/hash/sha256' import { put } from './ydoc.mjs' const isObject = v => v !== null && typeof v === 'object' && !Array.isArray(v) /** Code point order: what sorting UTF-8 bytes gives, unlike JS's default UTF-16 order. */ function byCodePoint (a, b) { let i = 0 let j = 0 while (i < a.length && j < b.length) { const x = a.codePointAt(i) const y = b.codePointAt(j) if (x !== y) return x - y i += x > 0xffff ? 2 : 1 j += y > 0xffff ? 2 : 1 } return (a.length - i) - (b.length - j) } /** v as canonical JSON: JSON.stringify with every object's keys in code point order. */ export function canonicalJSON (v) { if (v === undefined || v === null) return 'null' if (Array.isArray(v)) return '[' + v.map(canonicalJSON).join(',') + ']' if (typeof v === 'object') { return '{' + Object.keys(v).sort(byCodePoint).map(k => JSON.stringify(k) + ':' + canonicalJSON(v[k])).join(',') + '}' } return JSON.stringify(v) ?? 'null' } /** What a scene's version hashes: the canonical JSON of its code, data and words, from the view's scene object. */ export function sceneCanonical (scene) { const s = scene || {} const words = {} if (isObject(s.words)) for (const k of Object.keys(s.words)) if (typeof s.words[k] === 'string') put(words, k, s.words[k]) return canonicalJSON({ code: typeof s.code === 'string' ? s.code : '', data: s.data, words }) } export const utf8 = s => new TextEncoder().encode(s) /** A scene's version: SHA-256 of sceneCanonical, lowercase hex. */ export function sceneVersion (scene) { return Array.from(digest(utf8(sceneCanonical(scene))), b => b.toString(16).padStart(2, '0')).join('') } /** * Whether a scene's stills are up to date: `missing` is the states with no still, `stale` those whose still was * made from another version of the scene than the one it has now, and `current` is neither. A still for a state * the scene no longer has is not counted. Given the deck's assets, a still whose ref no asset is registered under * is missing too: the deck cannot draw it (deck.go's StillOf). */ export function stillsCurrent (scene, assets) { const s = scene || {} const states = Array.isArray(s.states) ? s.states : [] const stills = isObject(s.stills) ? s.stills : {} const version = sceneVersion(s) const registered = isObject(assets) ? new Set(Object.values(assets).map(a => a && a.ref)) : null const missing = [] const stale = [] for (const state of states) { const still = isObject(state) ? stills[state.id] : undefined if (!isObject(still) || typeof still.ref !== 'string' || (registered && !registered.has(still.ref))) missing.push(state.id) else if (still.made !== version) stale.push(state.id) } return { current: missing.length === 0 && stale.length === 0, missing, stale } }