oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1// scene-version.mjs — a scene's version (scenes and clips plan call R1,
2// as amended in review), for scenes.mjs: core/src/scene.rs's
3// scene_canonical and scene_version, and internal/slides/deck/scenes.go's
4// SceneCanonical and SceneVersion.
5//
6// The version is not stored. It is the SHA-256, lowercase hex, of the
7// canonical JSON of the object {code, data, words}: JSON.stringify with
8// object keys sorted by code point at every level, UTF-8. Absent code is
9// "", absent data null, absent words {}. vectors/scene-version.json holds
10// the three to one answer.
11import { digest } from 'lib0/hash/sha256'
12import { put } from './ydoc.mjs'
13
14const isObject = v => v !== null && typeof v === 'object' && !Array.isArray(v)
15
16/** Code point order: what sorting UTF-8 bytes gives, unlike JS's default UTF-16 order. */
17function byCodePoint (a, b) {
18 let i = 0
19 let j = 0
20 while (i < a.length && j < b.length) {
21 const x = a.codePointAt(i)
22 const y = b.codePointAt(j)
23 if (x !== y) return x - y
24 i += x > 0xffff ? 2 : 1
25 j += y > 0xffff ? 2 : 1
26 }
27 return (a.length - i) - (b.length - j)
28}
29
30/** v as canonical JSON: JSON.stringify with every object's keys in code point order. */
31export function canonicalJSON (v) {
32 if (v === undefined || v === null) return 'null'
33 if (Array.isArray(v)) return '[' + v.map(canonicalJSON).join(',') + ']'
34 if (typeof v === 'object') {
35 return '{' + Object.keys(v).sort(byCodePoint).map(k => JSON.stringify(k) + ':' + canonicalJSON(v[k])).join(',') + '}'
36 }
37 return JSON.stringify(v) ?? 'null'
38}
39
40/** What a scene's version hashes: the canonical JSON of its code, data and words, from the view's scene object. */
41export function sceneCanonical (scene) {
42 const s = scene || {}
43 const words = {}
44 if (isObject(s.words)) for (const k of Object.keys(s.words)) if (typeof s.words[k] === 'string') put(words, k, s.words[k])
45 return canonicalJSON({ code: typeof s.code === 'string' ? s.code : '', data: s.data, words })
46}
47
48export const utf8 = s => new TextEncoder().encode(s)
49
50/** A scene's version: SHA-256 of sceneCanonical, lowercase hex. */
51export function sceneVersion (scene) {
52 return Array.from(digest(utf8(sceneCanonical(scene))), b => b.toString(16).padStart(2, '0')).join('')
53}
54
55/**
56 * Whether a scene's stills are up to date: `missing` is the states with no still, `stale` those whose still was
57 * made from another version of the scene than the one it has now, and `current` is neither. A still for a state
58 * the scene no longer has is not counted. Given the deck's assets, a still whose ref no asset is registered under
59 * is missing too: the deck cannot draw it (deck.go's StillOf).
60 */
61export function stillsCurrent (scene, assets) {
62 const s = scene || {}
63 const states = Array.isArray(s.states) ? s.states : []
64 const stills = isObject(s.stills) ? s.stills : {}
65 const version = sceneVersion(s)
66 const registered = isObject(assets) ? new Set(Object.values(assets).map(a => a && a.ref)) : null
67 const missing = []
68 const stale = []
69 for (const state of states) {
70 const still = isObject(state) ? stills[state.id] : undefined
71 if (!isObject(still) || typeof still.ref !== 'string' || (registered && !registered.has(still.ref))) missing.push(state.id)
72 else if (still.made !== version) stale.push(state.id)
73 }
74 return { current: missing.length === 0 && stale.length === 0, missing, stale }
75}
76