@mml-io/networked-dom-document 0.0.0-experimental-d240bbd-20230807

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/index.js ADDED
@@ -0,0 +1,1145 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/index.ts
30
+ var src_exports = {};
31
+ __export(src_exports, {
32
+ EditableNetworkedDOM: () => EditableNetworkedDOM,
33
+ NetworkedDOM: () => NetworkedDOM,
34
+ defaultWebsocketSubProtocol: () => defaultWebsocketSubProtocol,
35
+ networkedDOMProtocolSubProtocol_v0_1: () => networkedDOMProtocolSubProtocol_v0_1
36
+ });
37
+ module.exports = __toCommonJS(src_exports);
38
+
39
+ // src/NetworkedDOM.ts
40
+ var import_rfc6902 = require("rfc6902");
41
+
42
+ // src/diffing.ts
43
+ var rfc6902 = __toESM(require("rfc6902"));
44
+ var visibleToAttrName = "visible-to";
45
+ var hiddenFromAttrName = "hidden-from";
46
+ function diffFromApplicationOfStaticVirtualDOMMutationRecordToConnection(mutation, parentNode, connectionId, visibleNodesForConnection) {
47
+ const virtualDOMElement = mutation.target;
48
+ switch (mutation.type) {
49
+ case "snapshot": {
50
+ const nodeDescription = describeNodeWithChildrenForConnectionId(
51
+ virtualDOMElement,
52
+ connectionId,
53
+ visibleNodesForConnection
54
+ );
55
+ if (!nodeDescription) {
56
+ return null;
57
+ }
58
+ const diff = {
59
+ type: "snapshot",
60
+ snapshot: nodeDescription,
61
+ documentTime: 0
62
+ };
63
+ return diff;
64
+ }
65
+ case "attributes": {
66
+ const visible = visibleNodesForConnection.has(virtualDOMElement.nodeId);
67
+ if (!parentNode) {
68
+ throw new Error("Node has no parent");
69
+ }
70
+ const parentNodeId = parentNode.nodeId;
71
+ const shouldBeVisible = shouldShowNodeToConnectionId(virtualDOMElement, connectionId) && visibleNodesForConnection.has(parentNodeId);
72
+ const attributeName = mutation.attributeName;
73
+ if (visible && shouldBeVisible) {
74
+ let newValue = null;
75
+ if (virtualDOMElement.attributes[attributeName] !== void 0) {
76
+ newValue = virtualDOMElement.attributes[attributeName];
77
+ }
78
+ const diff = {
79
+ type: "attributeChange",
80
+ nodeId: virtualDOMElement.nodeId,
81
+ attribute: attributeName,
82
+ newValue
83
+ };
84
+ return diff;
85
+ } else if (!visible && shouldBeVisible) {
86
+ visibleNodesForConnection.add(virtualDOMElement.nodeId);
87
+ const index = parentNode.childNodes.indexOf(virtualDOMElement);
88
+ if (index === -1) {
89
+ throw new Error("Node not found in parent's children");
90
+ }
91
+ let previousNodeId = null;
92
+ if (index > 0) {
93
+ previousNodeId = getNodeIdOfPreviousVisibleSibling(
94
+ parentNode,
95
+ index - 1,
96
+ visibleNodesForConnection
97
+ );
98
+ }
99
+ const nodeDescription = describeNodeWithChildrenForConnectionId(
100
+ virtualDOMElement,
101
+ connectionId,
102
+ visibleNodesForConnection
103
+ );
104
+ if (!nodeDescription) {
105
+ throw new Error("Node description not found");
106
+ }
107
+ const diff = {
108
+ type: "childrenChanged",
109
+ nodeId: parentNodeId,
110
+ previousNodeId,
111
+ addedNodes: [nodeDescription],
112
+ removedNodes: []
113
+ };
114
+ return diff;
115
+ } else if (visible && !shouldBeVisible) {
116
+ removeNodeAndChildrenFromVisibleNodes(virtualDOMElement, visibleNodesForConnection);
117
+ const diff = {
118
+ type: "childrenChanged",
119
+ nodeId: parentNodeId,
120
+ previousNodeId: null,
121
+ addedNodes: [],
122
+ removedNodes: [virtualDOMElement.nodeId]
123
+ };
124
+ return diff;
125
+ } else if (!visible && !shouldBeVisible) {
126
+ return null;
127
+ }
128
+ break;
129
+ }
130
+ case "characterData": {
131
+ const diff = {
132
+ type: "textChanged",
133
+ nodeId: virtualDOMElement.nodeId,
134
+ text: virtualDOMElement.textContent || ""
135
+ };
136
+ return diff;
137
+ }
138
+ case "childList": {
139
+ let previousSibling = mutation.previousSibling;
140
+ let previousNodeId = null;
141
+ if (previousSibling) {
142
+ let previousIndex = virtualDOMElement.childNodes.indexOf(previousSibling);
143
+ while (previousIndex !== -1) {
144
+ previousSibling = virtualDOMElement.childNodes[previousIndex];
145
+ if (visibleNodesForConnection.has(previousSibling.nodeId)) {
146
+ previousNodeId = previousSibling.nodeId;
147
+ break;
148
+ }
149
+ previousIndex--;
150
+ }
151
+ }
152
+ const diff = {
153
+ type: "childrenChanged",
154
+ nodeId: virtualDOMElement.nodeId,
155
+ previousNodeId,
156
+ addedNodes: [],
157
+ removedNodes: []
158
+ };
159
+ mutation.addedNodes.forEach((childVirtualDOMElement) => {
160
+ const describedNode = describeNodeWithChildrenForConnectionId(
161
+ childVirtualDOMElement,
162
+ connectionId,
163
+ visibleNodesForConnection
164
+ );
165
+ if (!describedNode) {
166
+ return;
167
+ }
168
+ diff.addedNodes.push(describedNode);
169
+ });
170
+ mutation.removedNodes.forEach((childVirtualDOMElement) => {
171
+ if (visibleNodesForConnection.has(childVirtualDOMElement.nodeId)) {
172
+ removeNodeAndChildrenFromVisibleNodes(childVirtualDOMElement, visibleNodesForConnection);
173
+ diff.removedNodes.push(childVirtualDOMElement.nodeId);
174
+ }
175
+ });
176
+ if (diff.addedNodes.length > 0 || diff.removedNodes.length > 0) {
177
+ return diff;
178
+ }
179
+ return null;
180
+ }
181
+ default:
182
+ console.error("Unknown mutation type: " + mutation.type);
183
+ break;
184
+ }
185
+ return null;
186
+ }
187
+ function getNodeIdOfPreviousVisibleSibling(parentVirtualElement, candidateIndex, visibleNodesForConnection) {
188
+ if (candidateIndex > 0) {
189
+ let previousSiblingIndex = candidateIndex;
190
+ while (previousSiblingIndex >= 0) {
191
+ const previousSibling = parentVirtualElement.childNodes[previousSiblingIndex];
192
+ if (visibleNodesForConnection.has(previousSibling.nodeId)) {
193
+ return previousSibling.nodeId;
194
+ }
195
+ previousSiblingIndex--;
196
+ }
197
+ }
198
+ return null;
199
+ }
200
+ function shouldShowNodeToConnectionId(virtualDOMElement, connectionId) {
201
+ const visibleToAttr = virtualDOMElement.attributes[visibleToAttrName];
202
+ const hiddenFromAttr = virtualDOMElement.attributes[hiddenFromAttrName];
203
+ const connectionIdString = connectionId.toString();
204
+ if (visibleToAttr !== void 0) {
205
+ const visibleToList = visibleToAttr.split(" ");
206
+ const explicityVisible = visibleToList.includes(connectionIdString);
207
+ if (!explicityVisible) {
208
+ return false;
209
+ }
210
+ }
211
+ if (hiddenFromAttr !== void 0) {
212
+ const hiddenFromList = hiddenFromAttr.split(" ");
213
+ const explicityHidden = hiddenFromList.includes(connectionIdString);
214
+ if (explicityHidden) {
215
+ return false;
216
+ }
217
+ }
218
+ return true;
219
+ }
220
+ function describeNodeWithChildrenForConnectionId(virtualDOMElement, connectionId, visibleNodesForConnection) {
221
+ if (!shouldShowNodeToConnectionId(virtualDOMElement, connectionId)) {
222
+ return null;
223
+ }
224
+ let emittedTagName = virtualDOMElement.tag;
225
+ if (emittedTagName === "#document") {
226
+ emittedTagName = "DIV";
227
+ }
228
+ if (emittedTagName === "#text") {
229
+ const textNode = {
230
+ type: "text",
231
+ nodeId: virtualDOMElement.nodeId,
232
+ text: virtualDOMElement.textContent || ""
233
+ };
234
+ visibleNodesForConnection.add(textNode.nodeId);
235
+ return textNode;
236
+ } else {
237
+ const node = {
238
+ type: "element",
239
+ nodeId: virtualDOMElement.nodeId,
240
+ tag: emittedTagName,
241
+ attributes: virtualDOMElement.attributes,
242
+ children: [],
243
+ text: virtualDOMElement.textContent
244
+ };
245
+ visibleNodesForConnection.add(node.nodeId);
246
+ for (const child of virtualDOMElement.childNodes) {
247
+ const childNodeDescription = describeNodeWithChildrenForConnectionId(
248
+ child,
249
+ connectionId,
250
+ visibleNodesForConnection
251
+ );
252
+ if (childNodeDescription) {
253
+ node.children.push(childNodeDescription);
254
+ }
255
+ }
256
+ return node;
257
+ }
258
+ }
259
+ function removeNodeAndChildrenFromVisibleNodes(virtualDOMElement, visibleNodesForConnection) {
260
+ visibleNodesForConnection.delete(virtualDOMElement.nodeId);
261
+ for (const child of virtualDOMElement.childNodes) {
262
+ if (!visibleNodesForConnection.has(child.nodeId)) {
263
+ console.error("Inner child of removed element was not visible", child.nodeId);
264
+ }
265
+ removeNodeAndChildrenFromVisibleNodes(child, visibleNodesForConnection);
266
+ }
267
+ }
268
+ function findParentNodeOfNodeId(virtualDOMElement, targetNodeId) {
269
+ for (const child of virtualDOMElement.childNodes) {
270
+ if (child.nodeId === targetNodeId) {
271
+ return virtualDOMElement;
272
+ } else {
273
+ const foundParentId = findParentNodeOfNodeId(child, targetNodeId);
274
+ if (foundParentId) {
275
+ return foundParentId;
276
+ }
277
+ }
278
+ }
279
+ return null;
280
+ }
281
+ function virtualDOMDiffToVirtualDOMMutationRecord(virtualStructure, domDiff) {
282
+ const pointer = rfc6902.Pointer.fromJSON(domDiff.path);
283
+ const grandParentTokens = pointer.tokens.slice(0, pointer.tokens.length - 2);
284
+ const lastToken = pointer.tokens[pointer.tokens.length - 1];
285
+ const secondLastToken = pointer.tokens[pointer.tokens.length - 2];
286
+ if (lastToken === "textContent") {
287
+ const nodePointer = new rfc6902.Pointer(pointer.tokens.slice(0, pointer.tokens.length - 1));
288
+ const node = nodePointer.get(virtualStructure);
289
+ return [
290
+ {
291
+ type: "characterData",
292
+ target: node,
293
+ addedNodes: [],
294
+ removedNodes: [],
295
+ attributeName: null,
296
+ previousSibling: null
297
+ }
298
+ ];
299
+ }
300
+ if (secondLastToken === "attributes") {
301
+ const nodePointer = new rfc6902.Pointer(grandParentTokens);
302
+ const node = nodePointer.get(virtualStructure);
303
+ return [
304
+ {
305
+ type: "attributes",
306
+ target: node,
307
+ addedNodes: [],
308
+ removedNodes: [],
309
+ attributeName: lastToken,
310
+ previousSibling: null
311
+ }
312
+ ];
313
+ }
314
+ if (secondLastToken === "childNodes") {
315
+ const nodePointer = new rfc6902.Pointer(grandParentTokens);
316
+ const node = nodePointer.get(virtualStructure);
317
+ let previousSibling = null;
318
+ if (lastToken === "-") {
319
+ } else {
320
+ const index = parseInt(lastToken, 10);
321
+ if (index === 0) {
322
+ previousSibling = null;
323
+ } else {
324
+ previousSibling = node.childNodes[index - 1];
325
+ }
326
+ }
327
+ const addedNodes = [];
328
+ const removedNodes = [];
329
+ switch (domDiff.op) {
330
+ case "add": {
331
+ addedNodes.push(domDiff.value);
332
+ return [
333
+ {
334
+ type: "childList",
335
+ target: node,
336
+ addedNodes,
337
+ removedNodes,
338
+ previousSibling,
339
+ attributeName: null
340
+ }
341
+ ];
342
+ }
343
+ case "remove": {
344
+ const removedNode = pointer.get(virtualStructure);
345
+ removedNodes.push(removedNode);
346
+ return [
347
+ {
348
+ type: "childList",
349
+ target: node,
350
+ addedNodes,
351
+ removedNodes,
352
+ previousSibling,
353
+ attributeName: null
354
+ }
355
+ ];
356
+ }
357
+ case "replace": {
358
+ const removedNode = pointer.get(virtualStructure);
359
+ removedNodes.push(removedNode);
360
+ addedNodes.push(domDiff.value);
361
+ return [
362
+ {
363
+ type: "childList",
364
+ target: node,
365
+ addedNodes: [],
366
+ removedNodes,
367
+ previousSibling,
368
+ attributeName: null
369
+ },
370
+ {
371
+ type: "childList",
372
+ target: node,
373
+ addedNodes,
374
+ removedNodes: [],
375
+ previousSibling,
376
+ attributeName: null
377
+ }
378
+ ];
379
+ }
380
+ }
381
+ }
382
+ if (domDiff.op === "replace" && domDiff.path === "") {
383
+ const node = domDiff.value;
384
+ return [
385
+ {
386
+ type: "snapshot",
387
+ target: node,
388
+ addedNodes: [],
389
+ removedNodes: [],
390
+ previousSibling: null,
391
+ attributeName: null
392
+ }
393
+ ];
394
+ }
395
+ console.error("Unhandled JSON diff:", JSON.stringify(domDiff, null, 2));
396
+ throw new Error("Unhandled diff type");
397
+ }
398
+ function calculateStaticVirtualDOMDiff(originalState, latestState) {
399
+ const jsonPatchDiffs = rfc6902.createPatch(
400
+ originalState,
401
+ latestState,
402
+ (a, b, ptr) => {
403
+ if (a.tag !== b.tag) {
404
+ return [{ op: "replace", path: ptr.toString(), value: b }];
405
+ }
406
+ return;
407
+ }
408
+ );
409
+ const nodeIdRemappings = [];
410
+ const virtualDOMDiffs = [];
411
+ for (const diff of jsonPatchDiffs) {
412
+ if (diff.op === "replace" && diff.path.endsWith("/nodeId")) {
413
+ const pointer = rfc6902.Pointer.fromJSON(diff.path);
414
+ const value = pointer.get(originalState);
415
+ nodeIdRemappings.push({
416
+ internalNodeId: diff.value,
417
+ clientFacingNodeId: value
418
+ });
419
+ } else {
420
+ virtualDOMDiffs.push(diff);
421
+ }
422
+ }
423
+ return remapDuplicatedNodeIdsInOperations(
424
+ {
425
+ originalState,
426
+ nodeIdRemappings,
427
+ virtualDOMDiffs
428
+ },
429
+ latestState
430
+ );
431
+ }
432
+ function getHighestNodeId(node) {
433
+ let highest = node.nodeId;
434
+ for (const child of node.childNodes) {
435
+ highest = Math.max(highest, getHighestNodeId(child));
436
+ }
437
+ return highest;
438
+ }
439
+ function getRemovedNodeIds(before, diff) {
440
+ const removedIds = /* @__PURE__ */ new Set();
441
+ function addNode(node) {
442
+ removedIds.add(node.nodeId);
443
+ for (const child of node.childNodes) {
444
+ addNode(child);
445
+ }
446
+ }
447
+ if (diff.op === "replace" || diff.op === "remove") {
448
+ const removedNode = rfc6902.Pointer.fromJSON(diff.path).get(before);
449
+ addNode(removedNode);
450
+ }
451
+ return removedIds;
452
+ }
453
+ function getNodeIdsFromNodeAndChildren(node) {
454
+ const nodeIds = /* @__PURE__ */ new Set();
455
+ function addNode(node2) {
456
+ nodeIds.add(node2.nodeId);
457
+ for (const child of node2.childNodes) {
458
+ addNode(child);
459
+ }
460
+ }
461
+ addNode(node);
462
+ return nodeIds;
463
+ }
464
+ function remapDuplicatedNodeIdsInOperations(virtualDOMDiffStruct, latestState) {
465
+ const { originalState, nodeIdRemappings, virtualDOMDiffs } = virtualDOMDiffStruct;
466
+ const highestNodeIdAcrossStartAndEnd = Math.max(
467
+ getHighestNodeId(originalState),
468
+ getHighestNodeId(latestState)
469
+ );
470
+ let nextNodeId = highestNodeIdAcrossStartAndEnd + 1;
471
+ const before = JSON.parse(JSON.stringify(originalState));
472
+ function checkAndReplaceNodeIdsIfAlreadyInUse(node, addingNodeIds, removedIds) {
473
+ if (existingNodeIds.has(node.nodeId) && removedIds && !removedIds.has(node.nodeId)) {
474
+ const newNodeId = nextNodeId++;
475
+ nodeIdRemappings.push({
476
+ internalNodeId: node.nodeId,
477
+ clientFacingNodeId: newNodeId
478
+ });
479
+ node.nodeId = newNodeId;
480
+ addingNodeIds.add(newNodeId);
481
+ } else {
482
+ addingNodeIds.add(node.nodeId);
483
+ }
484
+ for (const child of node.childNodes) {
485
+ checkAndReplaceNodeIdsIfAlreadyInUse(child, addingNodeIds, removedIds);
486
+ }
487
+ }
488
+ const existingNodeIds = getNodeIdsFromNodeAndChildren(before);
489
+ for (const diff of virtualDOMDiffs) {
490
+ const pointer = rfc6902.Pointer.fromJSON(diff.path);
491
+ const secondLastToken = pointer.tokens[pointer.tokens.length - 2];
492
+ if (secondLastToken !== "childNodes") {
493
+ continue;
494
+ }
495
+ const removedIds = getRemovedNodeIds(before, diff);
496
+ const addingNodeIds = /* @__PURE__ */ new Set();
497
+ if (diff.op === "replace" || diff.op === "add") {
498
+ checkAndReplaceNodeIdsIfAlreadyInUse(diff.value, addingNodeIds, removedIds);
499
+ }
500
+ removedIds.forEach((removedId) => {
501
+ existingNodeIds.delete(removedId);
502
+ });
503
+ addingNodeIds.forEach((addingNodeId) => {
504
+ existingNodeIds.add(addingNodeId);
505
+ });
506
+ const patchErrors = rfc6902.applyPatch(before, [diff]);
507
+ if (patchErrors.length !== 1 || patchErrors[0] !== null) {
508
+ throw new Error("Patch failed");
509
+ }
510
+ }
511
+ return virtualDOMDiffStruct;
512
+ }
513
+
514
+ // src/NetworkedDOM.ts
515
+ var networkedDOMProtocolSubProtocol_v0_1 = "networked-dom-v0.1";
516
+ var defaultWebsocketSubProtocol = networkedDOMProtocolSubProtocol_v0_1;
517
+ var _NetworkedDOM = class {
518
+ constructor(observableDOMFactory, htmlPath, htmlContents, oldInstanceDocumentRoot, onLoad, params = {}, ignoreTextNodes = true, logCallback) {
519
+ // Map from the node ids that the DOM uses internally to the node ids that clients refer to.
520
+ this.internalNodeIdToClientNodeId = /* @__PURE__ */ new Map();
521
+ // Map from the node ids that clients refer to to the node ids that the DOM uses internally.
522
+ this.clientNodeIdToInternalNodeId = /* @__PURE__ */ new Map();
523
+ this.currentConnectionId = 1;
524
+ this.connectionIdToWebSocketContext = /* @__PURE__ */ new Map();
525
+ this.webSocketToConnectionId = /* @__PURE__ */ new Map();
526
+ this.visibleNodeIdsByConnectionId = /* @__PURE__ */ new Map();
527
+ this.initialLoad = true;
528
+ this.disposed = false;
529
+ this.nodeIdToNode = /* @__PURE__ */ new Map();
530
+ this.nodeIdToParentNodeId = /* @__PURE__ */ new Map();
531
+ this.documentEffectiveStartTime = Date.now();
532
+ this.latestDocumentTime = 0;
533
+ this.pingCounter = 1;
534
+ this.maximumNodeId = 0;
535
+ this.htmlPath = htmlPath;
536
+ this.ignoreTextNodes = ignoreTextNodes;
537
+ this.logCallback = logCallback || this.defaultLogCallback;
538
+ this.observableDOM = observableDOMFactory(
539
+ {
540
+ htmlPath,
541
+ htmlContents,
542
+ params,
543
+ ignoreTextNodes,
544
+ pingIntervalMilliseconds: 5e3
545
+ },
546
+ (message, observableDOM) => {
547
+ this.observableDOM = observableDOM;
548
+ if (message.documentTime) {
549
+ this.documentEffectiveStartTime = Date.now() - message.documentTime;
550
+ this.latestDocumentTime = message.documentTime;
551
+ }
552
+ if (message.snapshot) {
553
+ this.documentRoot = message.snapshot;
554
+ const clonedSnapshot = JSON.parse(JSON.stringify(message.snapshot));
555
+ if (!this.initialLoad) {
556
+ throw new Error("Received snapshot after initial load");
557
+ }
558
+ this.initialLoad = false;
559
+ let domDiff = null;
560
+ if (oldInstanceDocumentRoot) {
561
+ domDiff = calculateStaticVirtualDOMDiff(oldInstanceDocumentRoot, clonedSnapshot);
562
+ for (const remapping of domDiff.nodeIdRemappings) {
563
+ this.addRemappedNodeId(remapping.clientFacingNodeId, remapping.internalNodeId);
564
+ }
565
+ }
566
+ this.addAndRemapNodeFromInstance(this.documentRoot, -1);
567
+ onLoad(domDiff, this);
568
+ } else if (message.mutation) {
569
+ if (this.initialLoad) {
570
+ throw new Error("Received mutation before initial load");
571
+ }
572
+ const mutation = this.addKnownNodesInMutation(message.mutation);
573
+ this.processModification(mutation);
574
+ this.removeKnownNodesInMutation(mutation);
575
+ } else if (message.logMessage) {
576
+ if (this.logCallback) {
577
+ this.logCallback(message.logMessage);
578
+ }
579
+ } else {
580
+ if (message.documentTime) {
581
+ this.sendPings();
582
+ return;
583
+ }
584
+ console.error("Unknown message type from observableDOM", message);
585
+ }
586
+ }
587
+ );
588
+ }
589
+ defaultLogCallback(message) {
590
+ const getLogFn = (level) => {
591
+ switch (level) {
592
+ case "system":
593
+ return console.error;
594
+ case "error":
595
+ return console.error;
596
+ case "warn":
597
+ return console.warn;
598
+ case "log":
599
+ return console.log;
600
+ case "info":
601
+ return console.info;
602
+ default:
603
+ return console.log;
604
+ }
605
+ };
606
+ const logFn = getLogFn(message.level);
607
+ logFn(`${message.level.toUpperCase()} (${this.htmlPath}):`, ...message.content);
608
+ }
609
+ addRemappedNodeId(clientFacingNodeId, internalNodeId) {
610
+ this.internalNodeIdToClientNodeId.set(internalNodeId, clientFacingNodeId);
611
+ this.clientNodeIdToInternalNodeId.set(clientFacingNodeId, internalNodeId);
612
+ this.maximumNodeId = Math.max(this.maximumNodeId, Math.max(clientFacingNodeId, internalNodeId));
613
+ }
614
+ sendPings() {
615
+ const ping = this.pingCounter++;
616
+ if (this.pingCounter > 1e3) {
617
+ this.pingCounter = 1;
618
+ }
619
+ const pingMessage = [
620
+ {
621
+ type: "ping",
622
+ ping,
623
+ documentTime: this.getDocumentTime()
624
+ }
625
+ ];
626
+ const stringified = JSON.stringify(pingMessage);
627
+ this.connectionIdToWebSocketContext.forEach((webSocketContext) => {
628
+ webSocketContext.webSocket.send(stringified);
629
+ });
630
+ }
631
+ getInitialSnapshot(connectionId, documentVirtualDOMElement) {
632
+ const visibleNodesForConnection = this.visibleNodeIdsByConnectionId.get(connectionId);
633
+ if (!visibleNodesForConnection) {
634
+ const err = new Error(
635
+ `visibleNodesForConnection not found for connectionId in getInitialSnapshot: ${connectionId}`
636
+ );
637
+ console.error(err);
638
+ throw err;
639
+ }
640
+ const domSnapshot = describeNodeWithChildrenForConnectionId(
641
+ documentVirtualDOMElement,
642
+ connectionId,
643
+ visibleNodesForConnection
644
+ );
645
+ if (!domSnapshot) {
646
+ throw new Error(`domSnapshot was not generated`);
647
+ }
648
+ return {
649
+ type: "snapshot",
650
+ snapshot: domSnapshot,
651
+ documentTime: Date.now() - this.documentEffectiveStartTime
652
+ };
653
+ }
654
+ getDocumentTime() {
655
+ return this.latestDocumentTime;
656
+ }
657
+ addExistingWebsockets(websockets, existingWebsocketMap, domDiff) {
658
+ const connectionIds = [];
659
+ for (const websocket of websockets) {
660
+ let existingId = null;
661
+ if (existingWebsocketMap !== null) {
662
+ existingId = existingWebsocketMap.get(websocket);
663
+ }
664
+ const { connectionId } = this.registerWebsocket(websocket, existingId);
665
+ connectionIds.push(connectionId);
666
+ }
667
+ if (domDiff) {
668
+ const diffsByConnectionId = new Map(
669
+ connectionIds.map((connectionId) => [connectionId, []])
670
+ );
671
+ for (const connectionId of connectionIds) {
672
+ this.getInitialSnapshot(connectionId, domDiff.originalState);
673
+ }
674
+ for (const virtualDOMDiff of domDiff.virtualDOMDiffs) {
675
+ const mutationRecordLikes = virtualDOMDiffToVirtualDOMMutationRecord(
676
+ domDiff.originalState,
677
+ virtualDOMDiff
678
+ );
679
+ if (virtualDOMDiff.path === "" && virtualDOMDiff.op === "replace") {
680
+ } else {
681
+ const patchResults = (0, import_rfc6902.applyPatch)(domDiff.originalState, [virtualDOMDiff]);
682
+ for (const patchResult of patchResults) {
683
+ if (patchResult !== null) {
684
+ console.error("Patching virtual dom structure resulted in error", patchResult);
685
+ throw patchResult;
686
+ }
687
+ }
688
+ }
689
+ for (const mutationRecordLike of mutationRecordLikes) {
690
+ const targetNodeId = mutationRecordLike.target.nodeId;
691
+ const virtualElementParent = findParentNodeOfNodeId(domDiff.originalState, targetNodeId);
692
+ diffsByConnectionId.forEach((diffs, connectionId) => {
693
+ const mutationDiff = diffFromApplicationOfStaticVirtualDOMMutationRecordToConnection(
694
+ mutationRecordLike,
695
+ virtualElementParent,
696
+ connectionId,
697
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
698
+ this.visibleNodeIdsByConnectionId.get(connectionId)
699
+ );
700
+ if (mutationDiff) {
701
+ diffs.push(mutationDiff);
702
+ }
703
+ });
704
+ }
705
+ }
706
+ diffsByConnectionId.forEach((diffs, connectionId) => {
707
+ if (diffs.length === 0) {
708
+ diffs.push({
709
+ type: "childrenChanged",
710
+ nodeId: this.documentRoot.nodeId,
711
+ previousNodeId: null,
712
+ addedNodes: [],
713
+ removedNodes: []
714
+ });
715
+ }
716
+ const asServerMessages = diffs;
717
+ const firstDiff = diffs[0];
718
+ firstDiff.documentTime = this.getDocumentTime();
719
+ const serializedDiffs = JSON.stringify(asServerMessages, null, 4);
720
+ const webSocketContext = this.connectionIdToWebSocketContext.get(connectionId);
721
+ if (!webSocketContext) {
722
+ throw new Error(`webSocketContext not found in addExistingWebsockets`);
723
+ }
724
+ webSocketContext.webSocket.send(serializedDiffs);
725
+ });
726
+ } else {
727
+ const documentVirtualDOMElement = this.documentRoot;
728
+ if (!documentVirtualDOMElement) {
729
+ throw new Error(`documentVirtualDOMElement not found in getInitialSnapshot`);
730
+ }
731
+ for (const connectionId of connectionIds) {
732
+ const webSocketContext = this.connectionIdToWebSocketContext.get(connectionId);
733
+ if (!webSocketContext) {
734
+ throw new Error(`webSocketContext not found in addExistingWebsockets`);
735
+ }
736
+ const asServerMessages = [
737
+ this.getInitialSnapshot(connectionId, documentVirtualDOMElement)
738
+ ];
739
+ const serializedSnapshotMessage = JSON.stringify(asServerMessages);
740
+ webSocketContext.webSocket.send(serializedSnapshotMessage);
741
+ }
742
+ }
743
+ for (const connectionId of connectionIds) {
744
+ this.observableDOM.addConnectedUserId(connectionId);
745
+ }
746
+ }
747
+ findParentNodeOfNodeId(targetNodeId) {
748
+ const parentNodeId = this.nodeIdToParentNodeId.get(targetNodeId);
749
+ if (parentNodeId === void 0) {
750
+ throw new Error("Parent node ID not found");
751
+ }
752
+ return this.getStaticVirtualDOMElementByInternalNodeIdOrThrow(parentNodeId);
753
+ }
754
+ registerWebsocket(webSocket, existingConnectionId = null) {
755
+ let connectionId;
756
+ if (existingConnectionId !== null) {
757
+ connectionId = existingConnectionId;
758
+ this.currentConnectionId = Math.max(this.currentConnectionId, connectionId + 1);
759
+ } else {
760
+ connectionId = this.currentConnectionId++;
761
+ }
762
+ const webSocketContext = {
763
+ webSocket,
764
+ messageListener: (messageEvent) => {
765
+ const string = String(messageEvent.data);
766
+ let parsed;
767
+ try {
768
+ parsed = JSON.parse(string);
769
+ } catch (e) {
770
+ console.error(`Error parsing message from websocket: ${string}`);
771
+ console.trace();
772
+ return;
773
+ }
774
+ if (_NetworkedDOM.IsPongMessage(parsed)) {
775
+ return;
776
+ }
777
+ this.dispatchRemoteEvent(webSocket, parsed);
778
+ }
779
+ };
780
+ this.connectionIdToWebSocketContext.set(connectionId, webSocketContext);
781
+ this.visibleNodeIdsByConnectionId.set(connectionId, /* @__PURE__ */ new Set());
782
+ this.webSocketToConnectionId.set(webSocket, connectionId);
783
+ webSocket.addEventListener("message", webSocketContext.messageListener);
784
+ return { connectionId };
785
+ }
786
+ static handleWebsocketSubprotocol(protocols) {
787
+ const protocolsSet = new Set(protocols);
788
+ for (const protocol of _NetworkedDOM.SupportedWebsocketSubProtocolsPreferenceOrder) {
789
+ if (protocolsSet.has(protocol)) {
790
+ return protocol;
791
+ }
792
+ }
793
+ return false;
794
+ }
795
+ addWebSocket(webSocket) {
796
+ if (this.initialLoad) {
797
+ throw new Error("addWebSocket called before initial load - unsupported at this time");
798
+ }
799
+ if (this.disposed) {
800
+ console.error("addWebSocket called on disposed NetworkedDOM");
801
+ throw new Error("This NetworkedDOM has been disposed");
802
+ }
803
+ if (webSocket.protocol) {
804
+ if (_NetworkedDOM.SupportedWebsocketSubProtocolsPreferenceOrder.indexOf(webSocket.protocol) === -1) {
805
+ const errorMessageString = `Unsupported websocket subprotocol: ${webSocket.protocol}`;
806
+ const errorMessage = [
807
+ {
808
+ type: "error",
809
+ message: errorMessageString
810
+ }
811
+ ];
812
+ webSocket.send(JSON.stringify(errorMessage));
813
+ webSocket.close();
814
+ return;
815
+ }
816
+ } else {
817
+ const warningMessageString = `No websocket subprotocol specified. Please specify a subprotocol to ensure compatibility with networked-dom servers. Assuming subprotocol "${defaultWebsocketSubProtocol}" for this connection.`;
818
+ const warningMessage = [
819
+ {
820
+ type: "warning",
821
+ message: warningMessageString
822
+ }
823
+ ];
824
+ webSocket.send(JSON.stringify(warningMessage));
825
+ }
826
+ const { connectionId } = this.registerWebsocket(webSocket);
827
+ const documentVirtualDOMElement = this.documentRoot;
828
+ if (!documentVirtualDOMElement) {
829
+ throw new Error(`documentVirtualDOMElement not found in getInitialSnapshot`);
830
+ }
831
+ const asServerMessages = [
832
+ this.getInitialSnapshot(connectionId, documentVirtualDOMElement)
833
+ ];
834
+ const serializedSnapshotMessage = JSON.stringify(asServerMessages);
835
+ webSocket.send(serializedSnapshotMessage);
836
+ this.observableDOM.addConnectedUserId(connectionId);
837
+ }
838
+ removeWebSocket(webSocket) {
839
+ const connectionId = this.webSocketToConnectionId.get(webSocket);
840
+ if (!connectionId) {
841
+ return;
842
+ }
843
+ this.observableDOM.removeConnectedUserId(connectionId);
844
+ const webSocketContext = this.connectionIdToWebSocketContext.get(connectionId);
845
+ if (!webSocketContext) {
846
+ throw new Error("Missing context for websocket");
847
+ }
848
+ webSocket.removeEventListener("message", webSocketContext.messageListener);
849
+ this.connectionIdToWebSocketContext.delete(connectionId);
850
+ this.visibleNodeIdsByConnectionId.delete(connectionId);
851
+ this.webSocketToConnectionId.delete(webSocket);
852
+ }
853
+ dispose() {
854
+ this.disposed = true;
855
+ this.observableDOM.dispose();
856
+ for (const [webSocket, connectionId] of this.webSocketToConnectionId) {
857
+ const webSocketContext = this.connectionIdToWebSocketContext.get(connectionId);
858
+ if (!webSocketContext) {
859
+ throw new Error("Missing context for websocket");
860
+ }
861
+ webSocket.removeEventListener("message", webSocketContext.messageListener);
862
+ this.connectionIdToWebSocketContext.delete(connectionId);
863
+ this.visibleNodeIdsByConnectionId.delete(connectionId);
864
+ this.webSocketToConnectionId.delete(webSocket);
865
+ }
866
+ }
867
+ processModification(mutationRecord) {
868
+ const documentVirtualDOMElement = this.documentRoot;
869
+ if (!documentVirtualDOMElement) {
870
+ throw new Error(`document not created in processModification`);
871
+ }
872
+ for (const [, visibleNodesForConnection] of this.visibleNodeIdsByConnectionId) {
873
+ visibleNodesForConnection.add(documentVirtualDOMElement.nodeId);
874
+ }
875
+ const diffsByConnectionId = new Map(
876
+ Array.from(this.connectionIdToWebSocketContext.keys()).map((connectionId) => [
877
+ connectionId,
878
+ []
879
+ ])
880
+ );
881
+ diffsByConnectionId.forEach((diffs, connectionId) => {
882
+ const parentNode = this.findParentNodeOfNodeId(mutationRecord.target.nodeId);
883
+ if (mutationRecord.type === "attributes" && !parentNode) {
884
+ console.error("parentNode not found for attribute mutationRecord", mutationRecord);
885
+ console.error("this.documentRoot", JSON.stringify(this.documentRoot, null, 2));
886
+ }
887
+ const diff = diffFromApplicationOfStaticVirtualDOMMutationRecordToConnection(
888
+ mutationRecord,
889
+ parentNode,
890
+ connectionId,
891
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
892
+ this.visibleNodeIdsByConnectionId.get(connectionId)
893
+ );
894
+ if (diff) {
895
+ diffs.push(diff);
896
+ }
897
+ });
898
+ diffsByConnectionId.forEach((diffs, connectionId) => {
899
+ if (diffs.length > 0) {
900
+ const asServerMessages = diffs;
901
+ const serializedDiffs = JSON.stringify(asServerMessages, null, 4);
902
+ const webSocketContext = this.connectionIdToWebSocketContext.get(connectionId);
903
+ if (!webSocketContext) {
904
+ throw new Error(`webSocketContext not found in processModificationList`);
905
+ }
906
+ webSocketContext.webSocket.send(serializedDiffs);
907
+ }
908
+ });
909
+ }
910
+ removeKnownNodesInMutation(mutation) {
911
+ const virtualDOMElement = mutation.target;
912
+ if (mutation.type === "childList") {
913
+ mutation.removedNodes.forEach((childDOMElement) => {
914
+ this.removeVirtualDOMElement(childDOMElement);
915
+ const index = virtualDOMElement.childNodes.indexOf(childDOMElement);
916
+ virtualDOMElement.childNodes.splice(index, 1);
917
+ });
918
+ return;
919
+ }
920
+ }
921
+ removeVirtualDOMElement(virtualDOMElement) {
922
+ this.nodeIdToNode.delete(virtualDOMElement.nodeId);
923
+ this.nodeIdToParentNodeId.delete(virtualDOMElement.nodeId);
924
+ for (const child of virtualDOMElement.childNodes) {
925
+ this.removeVirtualDOMElement(child);
926
+ }
927
+ }
928
+ static IsPongMessage(message) {
929
+ return message.type === "pong";
930
+ }
931
+ dispatchRemoteEvent(webSocket, remoteEvent) {
932
+ if (this.disposed) {
933
+ console.error("Cannot dispatch remote event after dispose");
934
+ throw new Error("This NetworkedDOM has been disposed");
935
+ }
936
+ const connectionId = this.webSocketToConnectionId.get(webSocket);
937
+ if (!connectionId) {
938
+ console.error("Unknown web socket dispatched event:", webSocket);
939
+ return;
940
+ }
941
+ const visibleNodes = this.visibleNodeIdsByConnectionId.get(connectionId);
942
+ if (!visibleNodes) {
943
+ console.error("No visible nodes for connection: " + connectionId);
944
+ return;
945
+ }
946
+ if (!visibleNodes.has(remoteEvent.nodeId)) {
947
+ console.error("Node not visible for connection: " + remoteEvent.nodeId);
948
+ return;
949
+ }
950
+ const remappedNode = this.clientNodeIdToInternalNodeId.get(remoteEvent.nodeId);
951
+ if (remappedNode) {
952
+ remoteEvent.nodeId = remappedNode;
953
+ }
954
+ this.observableDOM.dispatchRemoteEventFromConnectionId(connectionId, remoteEvent);
955
+ }
956
+ getStaticVirtualDOMElementByInternalNodeIdOrThrow(internalNodeId) {
957
+ const remappedId = this.internalNodeIdToClientNodeId.get(internalNodeId);
958
+ if (remappedId !== void 0) {
959
+ const node2 = this.nodeIdToNode.get(remappedId);
960
+ if (!node2) {
961
+ throw new Error("Remapped node not found with nodeId " + remappedId);
962
+ }
963
+ return node2;
964
+ }
965
+ const node = this.nodeIdToNode.get(internalNodeId);
966
+ if (!node) {
967
+ throw new Error("Node not found with nodeId:" + internalNodeId);
968
+ }
969
+ return node;
970
+ }
971
+ addKnownNodesInMutation(mutation) {
972
+ const target = this.getStaticVirtualDOMElementByInternalNodeIdOrThrow(mutation.targetId);
973
+ if (mutation.attribute) {
974
+ if (mutation.attribute.value !== null) {
975
+ target.attributes[mutation.attribute.attributeName] = mutation.attribute.value;
976
+ } else {
977
+ delete target.attributes[mutation.attribute.attributeName];
978
+ }
979
+ }
980
+ const previousSibling = mutation.previousSiblingId ? this.getStaticVirtualDOMElementByInternalNodeIdOrThrow(mutation.previousSiblingId) : null;
981
+ if (mutation.type === "childList") {
982
+ let index = 0;
983
+ if (previousSibling) {
984
+ index = target.childNodes.indexOf(previousSibling);
985
+ if (index === -1) {
986
+ throw new Error("Previous sibling is not currently a child of the parent element");
987
+ }
988
+ index += 1;
989
+ }
990
+ mutation.addedNodes.forEach((childVirtualDOMElement) => {
991
+ this.addAndRemapNodeFromInstance(childVirtualDOMElement, target.nodeId);
992
+ if (target.childNodes.indexOf(childVirtualDOMElement) === -1) {
993
+ target.childNodes.splice(index, 0, childVirtualDOMElement);
994
+ index++;
995
+ }
996
+ });
997
+ } else if (mutation.type === "attributes") {
998
+ const attributePair = mutation.attribute;
999
+ if (attributePair.value === null) {
1000
+ delete target.attributes[attributePair.attributeName];
1001
+ } else {
1002
+ target.attributes[attributePair.attributeName] = attributePair.value;
1003
+ }
1004
+ } else if (mutation.type === "characterData") {
1005
+ throw new Error("characterData not supported");
1006
+ }
1007
+ const record = {
1008
+ type: mutation.type,
1009
+ target,
1010
+ addedNodes: mutation.addedNodes,
1011
+ removedNodes: mutation.removedNodeIds.map((nodeId) => {
1012
+ return this.getStaticVirtualDOMElementByInternalNodeIdOrThrow(nodeId);
1013
+ }),
1014
+ previousSibling: mutation.previousSiblingId ? this.getStaticVirtualDOMElementByInternalNodeIdOrThrow(mutation.previousSiblingId) : null,
1015
+ attributeName: mutation.attribute ? mutation.attribute.attributeName : null
1016
+ };
1017
+ return record;
1018
+ }
1019
+ getSnapshot() {
1020
+ return this.documentRoot;
1021
+ }
1022
+ addAndRemapNodeFromInstance(node, parentNodeId) {
1023
+ const remappedNodeId = this.internalNodeIdToClientNodeId.get(node.nodeId);
1024
+ if (remappedNodeId !== void 0) {
1025
+ node.nodeId = remappedNodeId;
1026
+ } else {
1027
+ const existingClientReference = this.clientNodeIdToInternalNodeId.get(node.nodeId);
1028
+ if (existingClientReference) {
1029
+ const newNodeId = ++this.maximumNodeId;
1030
+ this.addRemappedNodeId(newNodeId, node.nodeId);
1031
+ node.nodeId = newNodeId;
1032
+ }
1033
+ }
1034
+ if (this.nodeIdToNode.has(node.nodeId)) {
1035
+ throw new Error("Node already exists with id " + node.nodeId);
1036
+ }
1037
+ this.nodeIdToNode.set(node.nodeId, node);
1038
+ this.nodeIdToParentNodeId.set(node.nodeId, parentNodeId);
1039
+ this.maximumNodeId = Math.max(this.maximumNodeId, node.nodeId);
1040
+ for (const childNode of node.childNodes) {
1041
+ this.addAndRemapNodeFromInstance(childNode, node.nodeId);
1042
+ }
1043
+ }
1044
+ getWebsocketConnectionIdMap() {
1045
+ return new Map(this.webSocketToConnectionId);
1046
+ }
1047
+ };
1048
+ var NetworkedDOM = _NetworkedDOM;
1049
+ // First to last in order of preference
1050
+ NetworkedDOM.SupportedWebsocketSubProtocolsPreferenceOrder = [
1051
+ networkedDOMProtocolSubProtocol_v0_1
1052
+ ];
1053
+
1054
+ // src/EditableNetworkedDOM.ts
1055
+ var EditableNetworkedDOM = class {
1056
+ constructor(htmlPath, observableDOMFactory, ignoreTextNodes = true, logCallback) {
1057
+ this.params = {};
1058
+ this.websockets = /* @__PURE__ */ new Set();
1059
+ this.loadedState = null;
1060
+ this.htmlPath = htmlPath;
1061
+ this.observableDOMFactory = observableDOMFactory;
1062
+ this.ignoreTextNodes = ignoreTextNodes;
1063
+ this.logCallback = logCallback;
1064
+ }
1065
+ isLoaded() {
1066
+ return this.loadedState !== null;
1067
+ }
1068
+ load(htmlContents, params) {
1069
+ if (params !== void 0) {
1070
+ this.params = params;
1071
+ }
1072
+ let oldInstanceRoot = null;
1073
+ let existingWebsocketMap = null;
1074
+ if (this.loadedState) {
1075
+ const oldInstance = this.loadedState.networkedDOM;
1076
+ existingWebsocketMap = oldInstance.getWebsocketConnectionIdMap();
1077
+ oldInstance.dispose();
1078
+ oldInstanceRoot = oldInstance.getSnapshot();
1079
+ }
1080
+ this.loadedState = null;
1081
+ let didLoad = false;
1082
+ const networkedDOM = new NetworkedDOM(
1083
+ this.observableDOMFactory,
1084
+ this.htmlPath,
1085
+ htmlContents,
1086
+ oldInstanceRoot,
1087
+ (domDiff, networkedDOM2) => {
1088
+ didLoad = true;
1089
+ if (this.loadedState) {
1090
+ this.loadedState.loaded = true;
1091
+ }
1092
+ networkedDOM2.addExistingWebsockets(
1093
+ Array.from(this.websockets),
1094
+ existingWebsocketMap,
1095
+ domDiff
1096
+ );
1097
+ },
1098
+ this.params,
1099
+ this.ignoreTextNodes,
1100
+ this.logCallback
1101
+ );
1102
+ this.loadedState = {
1103
+ htmlContents,
1104
+ networkedDOM,
1105
+ loaded: didLoad
1106
+ };
1107
+ }
1108
+ reload() {
1109
+ if (this.loadedState) {
1110
+ this.load(this.loadedState.htmlContents, this.params);
1111
+ } else {
1112
+ console.warn("EditableNetworkedDOM.reload called whilst not loaded");
1113
+ }
1114
+ }
1115
+ dispose() {
1116
+ for (const ws of this.websockets) {
1117
+ ws.close();
1118
+ }
1119
+ this.websockets.clear();
1120
+ if (this.loadedState) {
1121
+ this.loadedState.networkedDOM.dispose();
1122
+ }
1123
+ this.loadedState = null;
1124
+ }
1125
+ addWebSocket(webSocket) {
1126
+ this.websockets.add(webSocket);
1127
+ if (this.loadedState && this.loadedState.loaded) {
1128
+ this.loadedState.networkedDOM.addWebSocket(webSocket);
1129
+ }
1130
+ }
1131
+ removeWebSocket(webSocket) {
1132
+ this.websockets.delete(webSocket);
1133
+ if (this.loadedState && this.loadedState.loaded) {
1134
+ this.loadedState.networkedDOM.removeWebSocket(webSocket);
1135
+ }
1136
+ }
1137
+ };
1138
+ // Annotate the CommonJS export names for ESM import in node:
1139
+ 0 && (module.exports = {
1140
+ EditableNetworkedDOM,
1141
+ NetworkedDOM,
1142
+ defaultWebsocketSubProtocol,
1143
+ networkedDOMProtocolSubProtocol_v0_1
1144
+ });
1145
+ //# sourceMappingURL=index.js.map