oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1// Native realtime audit finding 7: the native engine sends transaction-scoped
2// updates, as the browser does. Replays update_wire_probe's per-command
3// updates in Yjs (gc on and off), each alone and in order, against the
4// authority's snapshot after it; checks that a keystroke carries no delete
5// set and is the size of the browser's own update for the same keystroke;
6// then writes browser per-transaction updates for the native side to apply.
7import assert from 'node:assert/strict'
8import fs from 'node:fs'
9import path from 'node:path'
10import * as Y from '../../fixtures/node_modules/yjs/dist/yjs.mjs'
11const directory = process.argv[2]
12assert.ok(directory, 'usage: node native/scripts/check-update-wire.mjs OUTPUT_DIRECTORY')
13const read = name => fs.readFileSync(path.join(directory, name))
14const manifest = JSON.parse(read('manifest.json'))
15const deleted = update => {
16 let units = 0
17 for (const ranges of Y.decodeUpdate(update).ds.clients.values()) for (const r of ranges) units += r.len
18 return units
19}
20// Same deletions, same items: the state vector and the delete set together
21// (Y.snapshot), not only the rendered text.
22const same = (a, b) => Y.equalSnapshots(Y.snapshot(a), Y.snapshot(b))
23let seed = 7
24const random = n => { seed = (seed * 1103515245 + 12345) % 2147483648; return seed % n }
25let comparisons = 0
26for (const gc of [true, false]) {
27 // In order: after each command's update alone, the peer equals the authority.
28 const doc = new Y.Doc({ gc })
29 Y.applyUpdate(doc, read('base.bin'))
30 for (let step = 1; step <= manifest.length; step++) {
31 Y.applyUpdate(doc, read(step + '.bin'))
32 const snapshot = new Y.Doc({ gc })
33 Y.applyUpdate(snapshot, read(step + '.snapshot'))
34 const label = `gc=${gc}, step=${step} (${manifest[step - 1].command})`
35 assert.equal(doc.getXmlFragment('prosemirror').toString(), snapshot.getXmlFragment('prosemirror').toString(), label)
36 assert.ok(same(doc, snapshot), `state vector and delete set, ${label}`)
37 comparisons++
38 snapshot.destroy()
39 }
40 // Out of order and duplicated: three more peers each get every update in
41 // their own shuffled order, a fifth of them twice, and must converge on
42 // the authority's final state.
43 const final = new Y.Doc({ gc })
44 Y.applyUpdate(final, read(manifest.length + '.snapshot'))
45 for (let peer = 0; peer < 3; peer++) {
46 const updates = manifest.map((_, i) => read((i + 1) + '.bin'))
47 for (let i = 0; i < manifest.length; i++) if (random(5) === 0) updates.push(updates[i])
48 for (let i = updates.length - 1; i > 0; i--) { const j = random(i + 1); [updates[i], updates[j]] = [updates[j], updates[i]] }
49 const shuffled = new Y.Doc({ gc })
50 Y.applyUpdate(shuffled, read('base.bin'))
51 for (const update of updates) Y.applyUpdate(shuffled, update)
52 assert.equal(shuffled.getXmlFragment('prosemirror').toString(), final.getXmlFragment('prosemirror').toString(), `shuffled peer ${peer}, gc=${gc}`)
53 assert.ok(same(shuffled, final), `shuffled peer ${peer}, gc=${gc}: state vector and delete set`)
54 comparisons++
55 shuffled.destroy()
56 }
57 assert.ok(same(doc, final))
58 doc.destroy(); final.destroy()
59}
60const base = new Y.Doc()
61Y.applyUpdate(base, read('base.bin'))
62const typed = new Y.Doc()
63Y.applyUpdate(typed, read('base.bin')); Y.applyUpdate(typed, read('1.bin'))
64// What the engine used to send for the same keystroke: a state-vector diff.
65const legacy = Y.encodeStateAsUpdate(typed, Y.encodeStateVector(base)).length
66const baseDeletes = deleted(Y.encodeStateAsUpdate(base))
67assert.ok(baseDeletes > 20, `the base document carries a delete set (${baseDeletes} units)`)
68const keystroke = read('1.bin'), deletion = read('2.bin')
69assert.equal(deleted(keystroke), 0, 'a typed character carries no deletions')
70assert.equal(deleted(deletion), 3, 'a three-unit deletion carries exactly those three units')
71// The browser's own update for typing one character into the same document.
72const browser = new Y.Doc()
73Y.applyUpdate(browser, read('base.bin'))
74const updates = []
75browser.on('update', update => updates.push(update))
76const firstText = (fragment) => {
77 for (const child of fragment.toArray()) {
78 if (child instanceof Y.XmlText && child.length > 4) return child
79 if (child instanceof Y.XmlElement) { const found = firstText(child); if (found) return found }
80 }
81}
82const text = firstText(browser.getXmlFragment('prosemirror'))
83text.insert(2, 'x')
84assert.equal(updates.length, 1)
85assert.ok(keystroke.length <= updates[0].length + 8, `native keystroke ${keystroke.length} B vs browser ${updates[0].length} B`)
86// Browser transactions for native to apply: an insertion, a deletion and a mark.
87text.delete(1, 2)
88text.format(0, 2, { strong: {} })
89browser.transact(() => { text.insert(0, 'browser ') })
90updates.forEach((update, index) => fs.writeFileSync(path.join(directory, `browser-${index}.bin`), update))
91const plain = node => node instanceof Y.XmlText ? node.toDelta().map(d => typeof d.insert === 'string' ? d.insert : '').join('') : node.toArray().map(plain).join('')
92fs.writeFileSync(path.join(directory, 'browser-text.txt'), plain(browser.getXmlFragment('prosemirror')))
93console.log(`Update wire replay passed ${comparisons} comparisons (${manifest.length} commands in order, and shuffled with duplicates, gc on and off); keystroke ${keystroke.length} B (browser ${updates[0].length} B, a state-vector diff would be ${legacy} B).`)
94