| 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. |
| 7 | import assert from 'node:assert/strict' |
| 8 | import fs from 'node:fs' |
| 9 | import path from 'node:path' |
| 10 | import * as Y from '../../fixtures/node_modules/yjs/dist/yjs.mjs' |
| 11 | const directory = process.argv[2] |
| 12 | assert.ok(directory, 'usage: node native/scripts/check-update-wire.mjs OUTPUT_DIRECTORY') |
| 13 | const read = name => fs.readFileSync(path.join(directory, name)) |
| 14 | const manifest = JSON.parse(read('manifest.json')) |
| 15 | const 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. |
| 22 | const same = (a, b) => Y.equalSnapshots(Y.snapshot(a), Y.snapshot(b)) |
| 23 | let seed = 7 |
| 24 | const random = n => { seed = (seed * 1103515245 + 12345) % 2147483648; return seed % n } |
| 25 | let comparisons = 0 |
| 26 | for (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 | } |
| 60 | const base = new Y.Doc() |
| 61 | Y.applyUpdate(base, read('base.bin')) |
| 62 | const typed = new Y.Doc() |
| 63 | Y.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. |
| 65 | const legacy = Y.encodeStateAsUpdate(typed, Y.encodeStateVector(base)).length |
| 66 | const baseDeletes = deleted(Y.encodeStateAsUpdate(base)) |
| 67 | assert.ok(baseDeletes > 20, `the base document carries a delete set (${baseDeletes} units)`) |
| 68 | const keystroke = read('1.bin'), deletion = read('2.bin') |
| 69 | assert.equal(deleted(keystroke), 0, 'a typed character carries no deletions') |
| 70 | assert.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. |
| 72 | const browser = new Y.Doc() |
| 73 | Y.applyUpdate(browser, read('base.bin')) |
| 74 | const updates = [] |
| 75 | browser.on('update', update => updates.push(update)) |
| 76 | const 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 | } |
| 82 | const text = firstText(browser.getXmlFragment('prosemirror')) |
| 83 | text.insert(2, 'x') |
| 84 | assert.equal(updates.length, 1) |
| 85 | assert.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. |
| 87 | text.delete(1, 2) |
| 88 | text.format(0, 2, { strong: {} }) |
| 89 | browser.transact(() => { text.insert(0, 'browser ') }) |
| 90 | updates.forEach((update, index) => fs.writeFileSync(path.join(directory, `browser-${index}.bin`), update)) |
| 91 | const plain = node => node instanceof Y.XmlText ? node.toDelta().map(d => typeof d.insert === 'string' ? d.insert : '').join('') : node.toArray().map(plain).join('') |
| 92 | fs.writeFileSync(path.join(directory, 'browser-text.txt'), plain(browser.getXmlFragment('prosemirror'))) |
| 93 | console.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 | |