// fixtures/import-interop.mjs — reads an imported document's update, as // the server's import path (internal/docs/import.go) wrote it, on stdin, // and checks the editor can take it as it is: // // - Yjs decodes it at all. lib0 refuses a client id past // Number.MAX_SAFE_INTEGER, which yrs writes without complaint; an // import written under one applied on the server and in no browser. // - y-prosemirror builds the editor's own schema's document from it // without changing the Yjs document. y-prosemirror deletes, and then // saves the deletion of, any node the schema refuses; an import it // had to clean up would lose text the first time it was opened. // - That document passes the schema's full check, attribute validators // included (a heading level must be a number). // // Prints {"clients": [...], "doc": } for the Go side // (internal/docs/import_interop_test.go) to compare with what the // reader meant. Exits non-zero, saying why, on any failure. import * as Y from 'yjs' import { yXmlFragmentToProseMirrorRootNode } from 'y-prosemirror' import { schema } from './schema.mjs' const chunks = [] for await (const chunk of process.stdin) chunks.push(chunk) const update = new Uint8Array(Buffer.concat(chunks)) function fail (why) { console.error('import-interop: ' + why) process.exit(1) } if (update.length === 0) fail('empty update on stdin') const doc = new Y.Doc() try { Y.applyUpdate(doc, update) } catch (err) { fail('Yjs cannot decode the update: ' + err.message) } const clients = [...Y.decodeStateVector(Y.encodeStateVector(doc)).keys()] for (const c of clients) { if (!Number.isSafeInteger(c)) fail('a client id is not a safe integer') } let changed = false doc.on('update', () => { changed = true }) let root try { root = yXmlFragmentToProseMirrorRootNode(doc.getXmlFragment('prosemirror'), schema) } catch (err) { fail('y-prosemirror cannot build the document: ' + err.message) } if (changed) fail('y-prosemirror changed the document while reading it (schema clean-up)') try { root.check() } catch (err) { fail('the editor schema refuses the document: ' + err.message) } process.stdout.write(JSON.stringify({ clients, doc: root.toJSON() }))