| 1 | // fixtures/generate-large-doc.mjs <pages> <out.bin> [edits] — a long, |
| 2 | // realistic document for the native realtime audit and for the native |
| 3 | // gate's long-document check (native/scripts/check-native.sh, which opens |
| 4 | // and types in a 200-page edited one through the bounded host). A "page" is about 3,000 characters |
| 5 | // (~500 words): five paragraphs, a heading every second page, a bold and |
| 6 | // an italic run in most paragraphs, and a five-item bullet list every |
| 7 | // fifth page. Seeded, so every run produces the same bytes. |
| 8 | // |
| 9 | // [edits] (default 0) then applies that many small edits from twenty |
| 10 | // other clients (insert 1-8 characters, or delete 1-4, at random places), |
| 11 | // so the update carries the fragmentation and tombstones a document that |
| 12 | // has been typed into for a while has. A document built in one |
| 13 | // transaction is the best case for every encoder; this is closer to the |
| 14 | // one people actually open. |
| 15 | import * as Y from 'yjs' |
| 16 | import { prosemirrorToYXmlFragment } from 'y-prosemirror' |
| 17 | import { writeFileSync } from 'node:fs' |
| 18 | import { schema } from './schema.mjs' |
| 19 | |
| 20 | const [pagesArg = '50', out = '/tmp/docs-large.bin', editsArg = '0'] = process.argv.slice(2) |
| 21 | const pages = Number(pagesArg) |
| 22 | const edits = Number(editsArg) |
| 23 | |
| 24 | let state = 12345 |
| 25 | const rand = () => { |
| 26 | state = (state + 0x6d2b79f5) >>> 0 |
| 27 | let t = state |
| 28 | t = Math.imul(t ^ (t >>> 15), t | 1) |
| 29 | t ^= t + Math.imul(t ^ (t >>> 7), t | 61) |
| 30 | return ((t ^ (t >>> 14)) >>> 0) / 4294967296 |
| 31 | } |
| 32 | const WORDS = 'the of and to in is that for it as was with be by on not he this are or his from at which but have an they you were her she there would their we him been has when who will no more if out so up said what its about than into them can only other time new some could these two may first then do any like my now over such our man me even most made after also did many off before must well back through years much where your way down should because each just those people how too little state good very make world still see own men work long here get both between life being under never day same another know while last us might great old year come since against go came right used take three'.split(' ') |
| 33 | const sentence = () => { |
| 34 | const n = 8 + Math.floor(rand() * 14) |
| 35 | const words = Array.from({ length: n }, () => WORDS[Math.floor(rand() * WORDS.length)]) |
| 36 | words[0] = words[0][0].toUpperCase() + words[0].slice(1) |
| 37 | return words.join(' ') + '.' |
| 38 | } |
| 39 | const paragraphText = () => { |
| 40 | let text = '' |
| 41 | while (text.length < 560) text += (text ? ' ' : '') + sentence() |
| 42 | return text |
| 43 | } |
| 44 | const { paragraph, heading, bullet_list, list_item, doc: docType } = schema.nodes |
| 45 | const strong = schema.marks.strong, em = schema.marks.em |
| 46 | function richParagraph() { |
| 47 | const text = paragraphText() |
| 48 | if (rand() < 0.3) return paragraph.create(null, schema.text(text)) |
| 49 | const a = Math.floor(rand() * (text.length - 80)), b = a + 10 + Math.floor(rand() * 30) |
| 50 | const c = Math.min(text.length - 1, b + 20 + Math.floor(rand() * 40)), d = Math.min(text.length, c + 5 + Math.floor(rand() * 20)) |
| 51 | const spans = [[0, a], [a, b, strong], [b, c], [c, d, em], [d, text.length]] |
| 52 | const parts = spans.filter(([from, to]) => to > from).map(([from, to, mark]) => schema.text(text.slice(from, to), mark ? [mark.create()] : undefined)) |
| 53 | return paragraph.create(null, parts) |
| 54 | } |
| 55 | const blocks = [] |
| 56 | for (let page = 0; page < pages; page++) { |
| 57 | if (page % 2 === 0) blocks.push(heading.create({ level: 2 }, schema.text(`Section ${page / 2 + 1}: ${sentence().slice(0, 40)}`))) |
| 58 | for (let i = 0; i < 5; i++) blocks.push(richParagraph()) |
| 59 | if (page % 5 === 4) { |
| 60 | blocks.push(bullet_list.create(null, Array.from({ length: 5 }, () => list_item.create(null, paragraph.create(null, schema.text(sentence())))))) |
| 61 | } |
| 62 | } |
| 63 | const pm = docType.create(null, blocks) |
| 64 | pm.check() |
| 65 | const ydoc = new Y.Doc() |
| 66 | ydoc.clientID = 1 |
| 67 | prosemirrorToYXmlFragment(pm, ydoc.getXmlFragment('prosemirror')) |
| 68 | |
| 69 | if (edits > 0) { |
| 70 | // Collect every XmlText once; edits land inside existing text, as typing does. |
| 71 | const texts = [] |
| 72 | const walk = (node) => { |
| 73 | for (const child of node.toArray()) { |
| 74 | if (child instanceof Y.XmlText) texts.push(child) |
| 75 | else if (child instanceof Y.XmlElement) walk(child) |
| 76 | } |
| 77 | } |
| 78 | walk(ydoc.getXmlFragment('prosemirror')) |
| 79 | const peers = Array.from({ length: 20 }, (_, i) => { const d = new Y.Doc(); d.clientID = 100 + i; return d }) |
| 80 | // Each peer edits its own replica and the change is merged back, so the |
| 81 | // structs carry twenty client IDs, as a shared document's would. |
| 82 | for (let i = 0; i < edits; i++) { |
| 83 | const text = texts[Math.floor(rand() * texts.length)] |
| 84 | const len = text.length |
| 85 | const peer = peers[i % peers.length] |
| 86 | const before = Y.encodeStateVector(ydoc) |
| 87 | Y.applyUpdate(peer, Y.encodeStateAsUpdate(ydoc, Y.encodeStateVector(peer))) |
| 88 | // Find the same XmlText in the peer by path. |
| 89 | const path = [] |
| 90 | let node = text |
| 91 | while (node.parent && node.parent !== ydoc.getXmlFragment('prosemirror')) { path.unshift(node.parent.toArray().indexOf(node)); node = node.parent } |
| 92 | path.unshift(ydoc.getXmlFragment('prosemirror').toArray().indexOf(node)) |
| 93 | let target = peer.getXmlFragment('prosemirror') |
| 94 | for (const index of path) target = target.toArray()[index] |
| 95 | if (rand() < 0.7 || len < 8) { |
| 96 | const at = Math.floor(rand() * (len + 1)) |
| 97 | target.insert(at, WORDS[Math.floor(rand() * WORDS.length)].slice(0, 1 + Math.floor(rand() * 8))) |
| 98 | } else { |
| 99 | const at = Math.floor(rand() * (len - 4)) |
| 100 | target.delete(at, 1 + Math.floor(rand() * 4)) |
| 101 | } |
| 102 | Y.applyUpdate(ydoc, Y.encodeStateAsUpdate(peer, before)) |
| 103 | } |
| 104 | } |
| 105 | const update = Y.encodeStateAsUpdate(ydoc) |
| 106 | writeFileSync(out, update) |
| 107 | const chars = ydoc.getXmlFragment('prosemirror').toArray().reduce((n, b) => n + b.toString().replace(/<[^>]*>/g, '').length, 0) |
| 108 | console.log(JSON.stringify({ pages, edits, blocks: blocks.length, chars, bytes: update.length })) |
| 109 | |