Skip to content

Commit 5e383e1

Browse files
committed
fix: null doc crashes aem2doc + more debug
1 parent 571eda9 commit 5e383e1

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

src/collab.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,13 @@ function removeComments(node) {
140140
return node;
141141
}
142142

143+
export const EMPTY_DOC = '<body><header></header><main><div></div></main><footer></footer></body>';
144+
143145
export function aem2doc(html, ydoc) {
146+
if (!html) {
147+
// eslint-disable-next-line no-param-reassign
148+
html = EMPTY_DOC;
149+
}
144150
const tree = fromHtml(html, { fragment: true });
145151
const main = tree.children.find((child) => child.tagName === 'main');
146152
fixImageLinks(main);

src/shareddoc.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import * as awarenessProtocol from 'y-protocols/awareness.js';
1616
import * as encoding from 'lib0/encoding.js';
1717
import * as decoding from 'lib0/decoding.js';
1818
import debounce from 'lodash/debounce.js';
19-
import { aem2doc, doc2aem } from './collab.js';
19+
import { aem2doc, doc2aem, EMPTY_DOC } from './collab.js';
2020

2121
const wsReadyStateConnecting = 0;
2222
const wsReadyStateOpen = 1;
@@ -27,7 +27,6 @@ const gcEnabled = false;
2727
// The local cache of ydocs
2828
const docs = new Map();
2929

30-
const EMPTY_DOC = '<main></main>';
3130
const messageSync = 0;
3231
const messageAwareness = 1;
3332
const MAX_STORAGE_KEYS = 128;
@@ -153,14 +152,19 @@ export const storeState = async (docName, state, storage, chunkSize = MAX_STORAG
153152
};
154153

155154
export const showError = (ydoc, err) => {
156-
const em = ydoc.getMap('error');
155+
try {
156+
const em = ydoc.getMap('error');
157157

158-
// Perform the change in a transaction to avoid seeing a partial error
159-
ydoc.transact(() => {
160-
em.set('timestamp', Date.now());
161-
em.set('message', err.message);
162-
em.set('stack', err.stack);
163-
});
158+
// Perform the change in a transaction to avoid seeing a partial error
159+
ydoc.transact(() => {
160+
em.set('timestamp', Date.now());
161+
em.set('message', err.message);
162+
em.set('stack', err.stack);
163+
});
164+
} catch (e) {
165+
// eslint-disable-next-line no-console
166+
console.error('Error showing error', e, err);
167+
}
164168
};
165169

166170
export const persistence = {
@@ -330,6 +334,8 @@ export const persistence = {
330334
// The doc was not restored from worker persistence, so read it from da-admin,
331335
// but do this async to give the ydoc some time to get synced up first. Without
332336
// this timeout, the ydoc can get confused which may result in duplicated content.
337+
// eslint-disable-next-line no-console
338+
console.log('Could not be restored, trying to restore from da-admin', docName);
333339
setTimeout(() => {
334340
if (ydoc === docs.get(docName)) {
335341
const rootType = ydoc.getXmlFragment('prosemirror');
@@ -344,7 +350,7 @@ export const persistence = {
344350
console.log('Restored from da-admin', docName);
345351
} catch (error) {
346352
// eslint-disable-next-line no-console
347-
console.error('Problem restoring state from da-admin', error);
353+
console.error('Problem restoring state from da-admin', error, current);
348354
showError(ydoc, error);
349355
}
350356
});

test/collab.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import assert from 'assert';
1313
import * as Y from 'yjs';
1414
import { readFileSync } from 'fs';
15-
import { aem2doc, doc2aem, tableToBlock } from '../src/collab.js';
15+
import { aem2doc, doc2aem, tableToBlock, EMPTY_DOC } from '../src/collab.js';
1616

1717
const collapseTagWhitespace = (str) => str.replace(/>\s+</g, '><');
1818
const collapseWhitespace = (str) => collapseTagWhitespace(str.replace(/\s+/g, ' ')).trim();
@@ -578,6 +578,22 @@ assert.equal(result, html);
578578
const result = doc2aem(yDoc);
579579
assert.equal(collapseWhitespace(result), collapseWhitespace(html));
580580
});
581+
582+
it('can parse empty doc', async () => {
583+
const html = EMPTY_DOC;
584+
const yDoc = new Y.Doc();
585+
aem2doc(html, yDoc);
586+
const result = doc2aem(yDoc);
587+
assert.equal(collapseWhitespace(result), collapseWhitespace(EMPTY_DOC));
588+
});
589+
590+
it('can parse null', async () => {
591+
const html = null;
592+
const yDoc = new Y.Doc();
593+
aem2doc(html, yDoc);
594+
const result = doc2aem(yDoc);
595+
assert.equal(collapseWhitespace(result), collapseWhitespace(EMPTY_DOC));
596+
});
581597
});
582598

583599

0 commit comments

Comments
 (0)