Download this file
| 1 | // fixtures/generate-peer-updates.mjs <seed.bin> <count> <outdir> — a peer's |
| 2 | // typing, for the native realtime audit's UI probe: <count> Yjs updates |
| 3 | // from client 999, each inserting one "Z" after the previous one, in a |
| 4 | // paragraph in the middle of the seed document. Written as 0.bin, 1.bin… |
| 5 | import * as Y from 'yjs' |
| 6 | import { readFileSync, writeFileSync, mkdirSync } from 'node:fs' |
| 7 | const [seed, countArg = '30', outdir = '/tmp/docs-peer'] = process.argv.slice(2) |
| 8 | const doc = new Y.Doc() |
| 9 | doc.clientID = 999 |
| 10 | Y.applyUpdate(doc, readFileSync(seed)) |
| 11 | const root = doc.getXmlFragment('prosemirror') |
| 12 | const blocks = root.toArray() |
| 13 | let text = null |
| 14 | for (let i = Math.floor(blocks.length / 2); i < blocks.length && !text; i++) { |
| 15 | const first = blocks[i].toArray ? blocks[i].toArray()[0] : null |
| 16 | if (blocks[i].nodeName === 'paragraph' && first instanceof Y.XmlText && first.length > 40) text = first |
| 17 | } |
| 18 | if (!text) throw new Error('no paragraph to type into') |
| 19 | mkdirSync(outdir, { recursive: true }) |
| 20 | const at = 20 |
| 21 | for (let k = 0; k < Number(countArg); k++) { |
| 22 | const before = Y.encodeStateVector(doc) |
| 23 | text.insert(at + k, 'Z') |
| 24 | writeFileSync(`${outdir}/${k}.bin`, Y.encodeStateAsUpdate(doc, before)) |
| 25 | } |
| 26 | console.log(JSON.stringify({ count: Number(countArg), outdir })) |
| 27 | |