// Native realtime audit finding 7: the native engine sends transaction-scoped // updates, as the browser does. Replays update_wire_probe's per-command // updates in Yjs (gc on and off), each alone and in order, against the // authority's snapshot after it; checks that a keystroke carries no delete // set and is the size of the browser's own update for the same keystroke; // then writes browser per-transaction updates for the native side to apply. import assert from 'node:assert/strict' import fs from 'node:fs' import path from 'node:path' import * as Y from '../../fixtures/node_modules/yjs/dist/yjs.mjs' const directory = process.argv[2] assert.ok(directory, 'usage: node native/scripts/check-update-wire.mjs OUTPUT_DIRECTORY') const read = name => fs.readFileSync(path.join(directory, name)) const manifest = JSON.parse(read('manifest.json')) const deleted = update => { let units = 0 for (const ranges of Y.decodeUpdate(update).ds.clients.values()) for (const r of ranges) units += r.len return units } // Same deletions, same items: the state vector and the delete set together // (Y.snapshot), not only the rendered text. const same = (a, b) => Y.equalSnapshots(Y.snapshot(a), Y.snapshot(b)) let seed = 7 const random = n => { seed = (seed * 1103515245 + 12345) % 2147483648; return seed % n } let comparisons = 0 for (const gc of [true, false]) { // In order: after each command's update alone, the peer equals the authority. const doc = new Y.Doc({ gc }) Y.applyUpdate(doc, read('base.bin')) for (let step = 1; step <= manifest.length; step++) { Y.applyUpdate(doc, read(step + '.bin')) const snapshot = new Y.Doc({ gc }) Y.applyUpdate(snapshot, read(step + '.snapshot')) const label = `gc=${gc}, step=${step} (${manifest[step - 1].command})` assert.equal(doc.getXmlFragment('prosemirror').toString(), snapshot.getXmlFragment('prosemirror').toString(), label) assert.ok(same(doc, snapshot), `state vector and delete set, ${label}`) comparisons++ snapshot.destroy() } // Out of order and duplicated: three more peers each get every update in // their own shuffled order, a fifth of them twice, and must converge on // the authority's final state. const final = new Y.Doc({ gc }) Y.applyUpdate(final, read(manifest.length + '.snapshot')) for (let peer = 0; peer < 3; peer++) { const updates = manifest.map((_, i) => read((i + 1) + '.bin')) for (let i = 0; i < manifest.length; i++) if (random(5) === 0) updates.push(updates[i]) for (let i = updates.length - 1; i > 0; i--) { const j = random(i + 1); [updates[i], updates[j]] = [updates[j], updates[i]] } const shuffled = new Y.Doc({ gc }) Y.applyUpdate(shuffled, read('base.bin')) for (const update of updates) Y.applyUpdate(shuffled, update) assert.equal(shuffled.getXmlFragment('prosemirror').toString(), final.getXmlFragment('prosemirror').toString(), `shuffled peer ${peer}, gc=${gc}`) assert.ok(same(shuffled, final), `shuffled peer ${peer}, gc=${gc}: state vector and delete set`) comparisons++ shuffled.destroy() } assert.ok(same(doc, final)) doc.destroy(); final.destroy() } const base = new Y.Doc() Y.applyUpdate(base, read('base.bin')) const typed = new Y.Doc() Y.applyUpdate(typed, read('base.bin')); Y.applyUpdate(typed, read('1.bin')) // What the engine used to send for the same keystroke: a state-vector diff. const legacy = Y.encodeStateAsUpdate(typed, Y.encodeStateVector(base)).length const baseDeletes = deleted(Y.encodeStateAsUpdate(base)) assert.ok(baseDeletes > 20, `the base document carries a delete set (${baseDeletes} units)`) const keystroke = read('1.bin'), deletion = read('2.bin') assert.equal(deleted(keystroke), 0, 'a typed character carries no deletions') assert.equal(deleted(deletion), 3, 'a three-unit deletion carries exactly those three units') // The browser's own update for typing one character into the same document. const browser = new Y.Doc() Y.applyUpdate(browser, read('base.bin')) const updates = [] browser.on('update', update => updates.push(update)) const firstText = (fragment) => { for (const child of fragment.toArray()) { if (child instanceof Y.XmlText && child.length > 4) return child if (child instanceof Y.XmlElement) { const found = firstText(child); if (found) return found } } } const text = firstText(browser.getXmlFragment('prosemirror')) text.insert(2, 'x') assert.equal(updates.length, 1) assert.ok(keystroke.length <= updates[0].length + 8, `native keystroke ${keystroke.length} B vs browser ${updates[0].length} B`) // Browser transactions for native to apply: an insertion, a deletion and a mark. text.delete(1, 2) text.format(0, 2, { strong: {} }) browser.transact(() => { text.insert(0, 'browser ') }) updates.forEach((update, index) => fs.writeFileSync(path.join(directory, `browser-${index}.bin`), update)) const plain = node => node instanceof Y.XmlText ? node.toDelta().map(d => typeof d.insert === 'string' ? d.insert : '').join('') : node.toArray().map(plain).join('') fs.writeFileSync(path.join(directory, 'browser-text.txt'), plain(browser.getXmlFragment('prosemirror'))) 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).`)