@mml-io/networked-dom-web 0.1.3 → 0.3.0

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.
@@ -0,0 +1,7 @@
1
+ export declare class DOMSanitizer {
2
+ static sanitise(node: HTMLElement): void;
3
+ static IsASCIIDigit(c: string): boolean;
4
+ static IsASCIIAlpha(c: string): boolean;
5
+ static IsValidAttributeName(characters: string): boolean;
6
+ static shouldAcceptAttribute(attribute: string): boolean;
7
+ }
@@ -0,0 +1,42 @@
1
+ export type NetworkedDOMWebsocketFactory = (url: string) => WebSocket;
2
+ export declare enum NetworkedDOMWebsocketStatus {
3
+ Connecting = 0,
4
+ Connected = 1,
5
+ Reconnecting = 2,
6
+ Disconnected = 3
7
+ }
8
+ export declare class NetworkedDOMWebsocket {
9
+ private idToElement;
10
+ private elementToId;
11
+ private websocket;
12
+ private currentRoot;
13
+ private url;
14
+ private websocketFactory;
15
+ private parentElement;
16
+ private timeCallback;
17
+ private statusUpdateCallback;
18
+ private stopped;
19
+ private backoffTime;
20
+ private status;
21
+ static createWebSocket(url: string): WebSocket;
22
+ constructor(url: string, websocketFactory: NetworkedDOMWebsocketFactory, parentElement: HTMLElement, timeCallback?: (time: number) => void, statusUpdateCallback?: (status: NetworkedDOMWebsocketStatus) => void);
23
+ private setStatus;
24
+ private isHTMLElement;
25
+ private isText;
26
+ private createWebsocketWithTimeout;
27
+ private waitBackoffTime;
28
+ private startWebSocketConnectionAttempt;
29
+ private handleIncomingWebsocketMessage;
30
+ stop(): void;
31
+ handleEvent(element: HTMLElement, event: CustomEvent<{
32
+ element: HTMLElement;
33
+ }>): void;
34
+ private send;
35
+ private handleTextChanged;
36
+ private handleChildrenChanged;
37
+ private removeChildElementIds;
38
+ private handleSnapshot;
39
+ private handleAttributeChange;
40
+ private handleNewElement;
41
+ private clearContents;
42
+ }
package/build/index.d.ts CHANGED
@@ -1 +1,2 @@
1
- export * from "../src/index";
1
+ export * from "./NetworkedDOMWebsocket";
2
+ export * from "./DOMSanitizer";
package/build/index.js CHANGED
@@ -90,24 +90,6 @@ var NetworkedDOMWebsocketStatus = /* @__PURE__ */ ((NetworkedDOMWebsocketStatus2
90
90
  NetworkedDOMWebsocketStatus2[NetworkedDOMWebsocketStatus2["Disconnected"] = 3] = "Disconnected";
91
91
  return NetworkedDOMWebsocketStatus2;
92
92
  })(NetworkedDOMWebsocketStatus || {});
93
- function isHTMLElement(node) {
94
- if (node instanceof HTMLElement) {
95
- return true;
96
- }
97
- if (!this.parentElement.ownerDocument.defaultView) {
98
- return false;
99
- }
100
- return node instanceof this.parentElement.ownerDocument.defaultView.HTMLElement;
101
- }
102
- function isText(node) {
103
- if (node instanceof Text) {
104
- return true;
105
- }
106
- if (!this.parentElement.ownerDocument.defaultView) {
107
- return false;
108
- }
109
- return node instanceof this.parentElement.ownerDocument.defaultView.Text;
110
- }
111
93
  var NetworkedDOMWebsocket = class {
112
94
  constructor(url, websocketFactory, parentElement, timeCallback, statusUpdateCallback) {
113
95
  this.idToElement = /* @__PURE__ */ new Map();
@@ -136,6 +118,24 @@ var NetworkedDOMWebsocket = class {
136
118
  this.statusUpdateCallback(status);
137
119
  }
138
120
  }
121
+ isHTMLElement(node) {
122
+ if (node instanceof HTMLElement) {
123
+ return true;
124
+ }
125
+ if (!this.parentElement.ownerDocument.defaultView) {
126
+ return false;
127
+ }
128
+ return node instanceof this.parentElement.ownerDocument.defaultView.HTMLElement;
129
+ }
130
+ isText(node) {
131
+ if (node instanceof Text) {
132
+ return true;
133
+ }
134
+ if (!this.parentElement.ownerDocument.defaultView) {
135
+ return false;
136
+ }
137
+ return node instanceof this.parentElement.ownerDocument.defaultView.Text;
138
+ }
139
139
  createWebsocketWithTimeout(timeout) {
140
140
  return new Promise((resolve, reject) => {
141
141
  const timeoutId = setTimeout(() => {
@@ -219,47 +219,42 @@ var NetworkedDOMWebsocket = class {
219
219
  handleIncomingWebsocketMessage(event) {
220
220
  const messages = JSON.parse(event.data);
221
221
  for (const message of messages) {
222
- if (message.type === "error") {
223
- console.error("Error from server", message);
224
- } else if (message.type === "warning") {
225
- console.warn("Warning from server", message);
226
- } else {
227
- if (message.documentTime) {
228
- if (this.timeCallback) {
229
- this.timeCallback(message.documentTime);
230
- }
231
- }
232
- if (message.type === "snapshot") {
233
- this.backoffTime = startingBackoffTimeMilliseconds;
234
- this.setStatus(1 /* Connected */);
235
- if (this.currentRoot) {
236
- this.currentRoot.remove();
237
- this.currentRoot = null;
238
- this.elementToId.clear();
239
- this.idToElement.clear();
240
- }
241
- const element = this.handleNewElement(message.snapshot);
242
- if (!element) {
243
- throw new Error("Snapshot element not created");
222
+ switch (message.type) {
223
+ case "error":
224
+ console.error("Error from server", message);
225
+ break;
226
+ case "warning":
227
+ console.warn("Warning from server", message);
228
+ break;
229
+ default: {
230
+ if (message.documentTime) {
231
+ if (this.timeCallback) {
232
+ this.timeCallback(message.documentTime);
233
+ }
244
234
  }
245
- if (!isHTMLElement(element)) {
246
- throw new Error("Snapshot element is not an HTMLElement");
235
+ switch (message.type) {
236
+ case "snapshot":
237
+ this.handleSnapshot(message);
238
+ break;
239
+ case "attributeChange":
240
+ this.handleAttributeChange(message);
241
+ break;
242
+ case "childrenChanged":
243
+ this.handleChildrenChanged(message);
244
+ break;
245
+ case "textChanged":
246
+ this.handleTextChanged(message);
247
+ break;
248
+ case "ping":
249
+ this.send({
250
+ type: "pong",
251
+ pong: message.ping
252
+ });
253
+ break;
254
+ default:
255
+ console.warn("unknown message type", message);
256
+ break;
247
257
  }
248
- this.currentRoot = element;
249
- this.parentElement.append(element);
250
- } else if (message.type === "attributeChange") {
251
- this.handleAttributeChange(message);
252
- } else if (message.type === "childrenChanged") {
253
- this.handleChildrenChanged(message);
254
- } else if (message.type === "textChanged") {
255
- this.handleTextChanged(message);
256
- } else if (message.type === "ping") {
257
- this.send({
258
- type: "pong",
259
- pong: message.ping
260
- });
261
- } else {
262
- console.warn("unknown message type", message);
263
258
  }
264
259
  }
265
260
  }
@@ -308,7 +303,7 @@ var NetworkedDOMWebsocket = class {
308
303
  if (!node) {
309
304
  throw new Error("No node found for textChanged message");
310
305
  }
311
- if (!isText(node)) {
306
+ if (!this.isText(node)) {
312
307
  throw new Error("Node for textChanged message is not a Text node");
313
308
  }
314
309
  node.textContent = text;
@@ -326,7 +321,7 @@ var NetworkedDOMWebsocket = class {
326
321
  if (!parent.isConnected) {
327
322
  console.error("Parent is not connected", parent);
328
323
  }
329
- if (!isHTMLElement(parent)) {
324
+ if (!this.isHTMLElement(parent)) {
330
325
  throw new Error("Parent is not an HTMLElement (that supports children)");
331
326
  }
332
327
  let previousElement;
@@ -359,7 +354,7 @@ var NetworkedDOMWebsocket = class {
359
354
  this.elementToId.delete(childElement);
360
355
  this.idToElement.delete(removedNode);
361
356
  parent.removeChild(childElement);
362
- if (isHTMLElement(childElement)) {
357
+ if (this.isHTMLElement(childElement)) {
363
358
  this.removeChildElementIds(childElement);
364
359
  }
365
360
  }
@@ -377,6 +372,25 @@ var NetworkedDOMWebsocket = class {
377
372
  this.removeChildElementIds(child);
378
373
  }
379
374
  }
375
+ handleSnapshot(message) {
376
+ this.backoffTime = startingBackoffTimeMilliseconds;
377
+ this.setStatus(1 /* Connected */);
378
+ if (this.currentRoot) {
379
+ this.currentRoot.remove();
380
+ this.currentRoot = null;
381
+ this.elementToId.clear();
382
+ this.idToElement.clear();
383
+ }
384
+ const element = this.handleNewElement(message.snapshot);
385
+ if (!element) {
386
+ throw new Error("Snapshot element not created");
387
+ }
388
+ if (!this.isHTMLElement(element)) {
389
+ throw new Error("Snapshot element is not an HTMLElement");
390
+ }
391
+ this.currentRoot = element;
392
+ this.parentElement.append(element);
393
+ }
380
394
  handleAttributeChange(message) {
381
395
  const { nodeId, attribute, newValue } = message;
382
396
  if (nodeId === void 0 || nodeId === null) {
@@ -385,7 +399,7 @@ var NetworkedDOMWebsocket = class {
385
399
  }
386
400
  const element = this.idToElement.get(nodeId);
387
401
  if (element) {
388
- if (isHTMLElement(element)) {
402
+ if (this.isHTMLElement(element)) {
389
403
  if (newValue === null) {
390
404
  element.removeAttribute(attribute);
391
405
  } else {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/index.ts", "../src/DOMSanitizer.ts", "../src/NetworkedDOMWebsocket.ts"],
4
- "sourcesContent": ["export * from \"./NetworkedDOMWebsocket\";\nexport * from \"./DOMSanitizer\";\n", "export class DOMSanitizer {\n static sanitise(node: HTMLElement) {\n if (node.getAttributeNames) {\n for (const attr of node.getAttributeNames()) {\n if (!DOMSanitizer.IsValidAttributeName(attr)) {\n node.removeAttribute(attr);\n }\n }\n }\n if (node.nodeName === \"SCRIPT\") {\n // set text to empty string\n node.innerText = \"\";\n } else {\n if (node.getAttributeNames) {\n for (const attr of node.getAttributeNames()) {\n if (!DOMSanitizer.shouldAcceptAttribute(attr)) {\n node.removeAttribute(attr);\n }\n }\n }\n for (let i = 0; i < node.childNodes.length; i++) {\n DOMSanitizer.sanitise(node.childNodes[i] as HTMLElement);\n }\n }\n }\n\n static IsASCIIDigit(c: string): boolean {\n return c >= \"0\" && c <= \"9\";\n }\n\n static IsASCIIAlpha(c: string) {\n return c >= \"a\" && c <= \"z\";\n }\n\n static IsValidAttributeName(characters: string): boolean {\n const c = characters[0];\n if (!(DOMSanitizer.IsASCIIAlpha(c) || c === \":\" || c === \"_\")) {\n return false;\n }\n\n for (let i = 1; i < characters.length; i++) {\n const c = characters[i];\n if (\n !(\n DOMSanitizer.IsASCIIDigit(c) ||\n DOMSanitizer.IsASCIIAlpha(c) ||\n c === \":\" ||\n c === \"_\" ||\n c === \"-\" ||\n c === \".\"\n )\n ) {\n return false;\n }\n }\n\n return true;\n }\n\n static shouldAcceptAttribute(attribute: string) {\n if (!DOMSanitizer.IsValidAttributeName(attribute)) {\n console.warn(\"Invalid attribute name\", attribute);\n return false;\n }\n\n // TODO - this might be overly restrictive - apologies to someone that finds this because you have a non-event attribute filtered by this\n return !attribute.startsWith(\"on\");\n }\n}\n", "import {\n AttributeChangedDiff,\n ChildrenChangedDiff,\n ClientMessage,\n NodeDescription,\n RemoteEvent,\n ServerMessage,\n TextChangedDiff,\n} from \"@mml-io/networked-dom-protocol\";\n\nimport { DOMSanitizer } from \"./DOMSanitizer\";\n\nconst websocketProtocol = \"networked-dom-v0.1\";\n\nconst startingBackoffTimeMilliseconds = 100;\nconst maximumBackoffTimeMilliseconds = 10000;\nconst maximumWebsocketConnectionTimeout = 5000;\n\nexport type NetworkedDOMWebsocketFactory = (url: string) => WebSocket;\n\nexport enum NetworkedDOMWebsocketStatus {\n Connecting,\n Connected,\n Reconnecting,\n Disconnected,\n}\n\nfunction isHTMLElement(node: unknown): node is HTMLElement {\n if (node instanceof HTMLElement) {\n return true;\n }\n if (!this.parentElement.ownerDocument.defaultView) {\n return false;\n }\n return node instanceof this.parentElement.ownerDocument.defaultView.HTMLElement;\n}\n\nfunction isText(node: unknown): node is Text {\n if (node instanceof Text) {\n return true;\n }\n if (!this.parentElement.ownerDocument.defaultView) {\n return false;\n }\n return node instanceof this.parentElement.ownerDocument.defaultView.Text;\n}\n\nexport class NetworkedDOMWebsocket {\n private idToElement = new Map<number, Node>();\n private elementToId = new Map<Node, number>();\n private websocket: WebSocket | null = null;\n private currentRoot: HTMLElement | null = null;\n\n private url: string;\n private websocketFactory: NetworkedDOMWebsocketFactory;\n private parentElement: HTMLElement;\n private timeCallback: (time: number) => void;\n private statusUpdateCallback: (status: NetworkedDOMWebsocketStatus) => void;\n private stopped = false;\n private backoffTime = startingBackoffTimeMilliseconds;\n private status: NetworkedDOMWebsocketStatus | null = null;\n\n public static createWebSocket(url: string): WebSocket {\n return new WebSocket(url, [websocketProtocol]);\n }\n\n constructor(\n url: string,\n websocketFactory: NetworkedDOMWebsocketFactory,\n parentElement: HTMLElement,\n timeCallback?: (time: number) => void,\n statusUpdateCallback?: (status: NetworkedDOMWebsocketStatus) => void,\n ) {\n this.url = url;\n this.websocketFactory = websocketFactory;\n this.parentElement = parentElement;\n this.timeCallback =\n timeCallback ||\n (() => {\n // no-op\n });\n this.statusUpdateCallback =\n statusUpdateCallback ||\n (() => {\n // no-op\n });\n this.setStatus(NetworkedDOMWebsocketStatus.Connecting);\n this.startWebSocketConnectionAttempt();\n }\n\n private setStatus(status: NetworkedDOMWebsocketStatus) {\n if (this.status !== status) {\n this.status = status;\n this.statusUpdateCallback(status);\n }\n }\n\n private createWebsocketWithTimeout(timeout: number): Promise<WebSocket> {\n return new Promise((resolve, reject) => {\n const timeoutId = setTimeout(() => {\n reject(new Error(\"websocket connection timed out\"));\n }, timeout);\n const websocket = this.websocketFactory(this.url);\n websocket.addEventListener(\"open\", () => {\n clearTimeout(timeoutId);\n\n this.websocket = websocket;\n\n websocket.addEventListener(\"message\", (event) => {\n if (websocket !== this.websocket) {\n console.log(\"Ignoring websocket message event because it is no longer current\");\n websocket.close();\n return;\n }\n this.handleIncomingWebsocketMessage(event);\n });\n\n const onWebsocketClose = async () => {\n const hadContents = this.currentRoot !== null;\n this.clearContents();\n if (this.stopped) {\n // This closing is expected. The client closed the websocket.\n this.setStatus(NetworkedDOMWebsocketStatus.Disconnected);\n return;\n }\n if (!hadContents) {\n // The websocket did not deliver any contents. It may have been successfully opened, but immediately closed. This client should back off to prevent this happening in a rapid loop.\n await this.waitBackoffTime();\n }\n // The websocket closed unexpectedly. Try to reconnect.\n this.setStatus(NetworkedDOMWebsocketStatus.Reconnecting);\n this.startWebSocketConnectionAttempt();\n };\n\n websocket.addEventListener(\"close\", (e) => {\n if (websocket !== this.websocket) {\n console.warn(\"Ignoring websocket close event because it is no longer current\");\n return;\n }\n console.log(\"NetworkedDOMWebsocket close\", e);\n onWebsocketClose();\n });\n websocket.addEventListener(\"error\", (e) => {\n if (websocket !== this.websocket) {\n console.log(\"Ignoring websocket error event because it is no longer current\");\n return;\n }\n console.error(\"NetworkedDOMWebsocket error\", e);\n onWebsocketClose();\n });\n\n resolve(websocket);\n });\n websocket.addEventListener(\"error\", (e) => {\n clearTimeout(timeoutId);\n reject(e);\n });\n });\n }\n\n private async waitBackoffTime(): Promise<void> {\n console.warn(`Websocket connection to '${this.url}' failed: retrying in ${this.backoffTime}ms`);\n await new Promise((resolve) => setTimeout(resolve, this.backoffTime));\n this.backoffTime = Math.min(\n // Introduce a small amount of randomness to prevent clients from retrying in lockstep\n this.backoffTime * (1.5 + Math.random() * 0.5),\n maximumBackoffTimeMilliseconds,\n );\n }\n\n private async startWebSocketConnectionAttempt() {\n if (this.stopped) {\n return;\n }\n // eslint-disable-next-line no-constant-condition\n while (true) {\n if (this.stopped) {\n return;\n }\n try {\n await this.createWebsocketWithTimeout(maximumWebsocketConnectionTimeout);\n break;\n } catch (e) {\n // Connection failed, retry with backoff\n this.setStatus(NetworkedDOMWebsocketStatus.Reconnecting);\n await this.waitBackoffTime();\n }\n }\n }\n\n private handleIncomingWebsocketMessage(event: MessageEvent) {\n const messages = JSON.parse(event.data) as Array<ServerMessage>;\n for (const message of messages) {\n if (message.type === \"error\") {\n console.error(\"Error from server\", message);\n } else if (message.type === \"warning\") {\n console.warn(\"Warning from server\", message);\n } else {\n if (message.documentTime) {\n if (this.timeCallback) {\n this.timeCallback(message.documentTime);\n }\n }\n if (message.type === \"snapshot\") {\n // This websocket is successfully connected. Reset the backoff time.\n this.backoffTime = startingBackoffTimeMilliseconds;\n this.setStatus(NetworkedDOMWebsocketStatus.Connected);\n\n if (this.currentRoot) {\n this.currentRoot.remove();\n this.currentRoot = null;\n this.elementToId.clear();\n this.idToElement.clear();\n }\n\n // create a tree of DOM elements\n // NOTE: the MLElement contructors are not executed during this stage\n const element = this.handleNewElement(message.snapshot);\n if (!element) {\n throw new Error(\"Snapshot element not created\");\n }\n if (!isHTMLElement(element)) {\n throw new Error(\"Snapshot element is not an HTMLElement\");\n }\n this.currentRoot = element;\n // appending to the tree causes MElements to be constructed\n this.parentElement.append(element);\n } else if (message.type === \"attributeChange\") {\n this.handleAttributeChange(message);\n } else if (message.type === \"childrenChanged\") {\n this.handleChildrenChanged(message);\n } else if (message.type === \"textChanged\") {\n this.handleTextChanged(message);\n } else if (message.type === \"ping\") {\n this.send({\n type: \"pong\",\n pong: message.ping,\n });\n } else {\n console.warn(\"unknown message type\", message);\n }\n }\n }\n }\n\n public stop() {\n this.stopped = true;\n if (this.websocket !== null) {\n this.websocket.close();\n this.websocket = null;\n }\n }\n\n public handleEvent(element: HTMLElement, event: CustomEvent<{ element: HTMLElement }>) {\n const nodeId = this.elementToId.get(element);\n if (nodeId === undefined || nodeId === null) {\n throw new Error(\"Element not found\");\n }\n\n console.log(\n `Sending event to websocket: \"${event.type}\" on node: ${nodeId} type: ${element.tagName}`,\n );\n\n const detailWithoutElement: Partial<typeof event.detail> = {\n ...event.detail,\n };\n delete detailWithoutElement.element;\n\n const remoteEvent: RemoteEvent = {\n type: \"event\",\n nodeId,\n name: event.type,\n bubbles: event.bubbles,\n params: detailWithoutElement,\n };\n\n this.send(remoteEvent);\n }\n\n private send(fromClientMessage: ClientMessage) {\n if (!this.websocket) {\n throw new Error(\"No websocket created\");\n }\n this.websocket.send(JSON.stringify(fromClientMessage));\n }\n\n private handleTextChanged(message: TextChangedDiff) {\n const { nodeId, text } = message;\n\n if (nodeId === undefined || nodeId === null) {\n console.warn(\"No nodeId in textChanged message\");\n return;\n }\n const node = this.idToElement.get(nodeId);\n if (!node) {\n throw new Error(\"No node found for textChanged message\");\n }\n if (!isText(node)) {\n throw new Error(\"Node for textChanged message is not a Text node\");\n }\n node.textContent = text;\n }\n\n private handleChildrenChanged(message: ChildrenChangedDiff) {\n const { nodeId, addedNodes, removedNodes, previousNodeId } = message;\n if (nodeId === undefined || nodeId === null) {\n console.warn(\"No nodeId in childrenChanged message\");\n return;\n }\n const parent = this.idToElement.get(nodeId);\n if (!parent) {\n throw new Error(\"No parent found for childrenChanged message\");\n }\n if (!parent.isConnected) {\n console.error(\"Parent is not connected\", parent);\n }\n if (!isHTMLElement(parent)) {\n throw new Error(\"Parent is not an HTMLElement (that supports children)\");\n }\n let previousElement;\n if (previousNodeId) {\n previousElement = this.idToElement.get(previousNodeId);\n if (!previousElement) {\n throw new Error(\"No previous element found for childrenChanged message\");\n }\n }\n\n for (const addedNode of addedNodes) {\n const childElement = this.handleNewElement(addedNode);\n if (childElement) {\n if (previousElement) {\n const nextElement = previousElement.nextSibling;\n if (nextElement) {\n parent.insertBefore(childElement, nextElement);\n } else {\n parent.append(childElement);\n }\n } else {\n parent.append(childElement);\n }\n }\n }\n for (const removedNode of removedNodes) {\n const childElement = this.idToElement.get(removedNode);\n if (!childElement) {\n throw new Error(`Child element not found: ${removedNode}`);\n }\n this.elementToId.delete(childElement);\n this.idToElement.delete(removedNode);\n parent.removeChild(childElement);\n if (isHTMLElement(childElement)) {\n // If child is capable of supporting children then remove any that exist\n this.removeChildElementIds(childElement);\n }\n }\n }\n\n private removeChildElementIds(parent: HTMLElement) {\n for (let i = 0; i < parent.children.length; i++) {\n const child = parent.children[i];\n const childId = this.elementToId.get(child as HTMLElement);\n if (!childId) {\n console.error(\"Inner child of removed element had no id\", child);\n } else {\n this.elementToId.delete(child);\n this.idToElement.delete(childId);\n }\n this.removeChildElementIds(child as HTMLElement);\n }\n }\n\n private handleAttributeChange(message: AttributeChangedDiff) {\n const { nodeId, attribute, newValue } = message;\n if (nodeId === undefined || nodeId === null) {\n console.warn(\"No nodeId in attributeChange message\");\n return;\n }\n const element = this.idToElement.get(nodeId);\n if (element) {\n if (isHTMLElement(element)) {\n if (newValue === null) {\n element.removeAttribute(attribute);\n } else {\n if (DOMSanitizer.shouldAcceptAttribute(attribute)) {\n element.setAttribute(attribute, newValue);\n }\n }\n } else {\n console.error(\"Element is not an HTMLElement and cannot support attributes\", element);\n }\n } else {\n console.error(\"No element found for attributeChange message\");\n }\n }\n\n private handleNewElement(message: NodeDescription): Node | null {\n if (message.type === \"text\") {\n const { nodeId, text } = message;\n const textNode = document.createTextNode(\"\");\n textNode.textContent = text;\n this.idToElement.set(nodeId, textNode);\n this.elementToId.set(textNode, nodeId);\n return textNode;\n }\n const { tag, nodeId, attributes, children, text } = message;\n if (nodeId === undefined || nodeId === null) {\n console.warn(\"No nodeId in handleNewElement message\", message);\n return null;\n }\n if (this.idToElement.has(nodeId)) {\n console.error(\n \"Received nodeId to add that is already present\",\n nodeId,\n this.idToElement.get(nodeId),\n );\n }\n if (tag === \"#text\") {\n const textNode = document.createTextNode(\"\");\n textNode.textContent = text || null;\n this.idToElement.set(nodeId, textNode);\n this.elementToId.set(textNode, nodeId);\n return textNode;\n }\n\n let element;\n try {\n element = document.createElement(tag);\n } catch (e) {\n console.error(`Error creating element: (${tag})`, e);\n element = document.createElement(\"div\");\n }\n this.idToElement.set(nodeId, element);\n this.elementToId.set(element, nodeId);\n for (const key in attributes) {\n if (DOMSanitizer.shouldAcceptAttribute(key)) {\n const value = attributes[key];\n element.setAttribute(key, value);\n }\n }\n if (children) {\n for (const child of children) {\n const childElement = this.handleNewElement(child);\n if (childElement) {\n element.append(childElement);\n }\n }\n }\n return element;\n }\n\n private clearContents() {\n this.idToElement.clear();\n this.elementToId.clear();\n if (this.currentRoot) {\n this.currentRoot.remove();\n this.currentRoot = null;\n }\n }\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,eAAN,MAAmB;AAAA,EACxB,OAAO,SAAS,MAAmB;AACjC,QAAI,KAAK,mBAAmB;AAC1B,iBAAW,QAAQ,KAAK,kBAAkB,GAAG;AAC3C,YAAI,CAAC,aAAa,qBAAqB,IAAI,GAAG;AAC5C,eAAK,gBAAgB,IAAI;AAAA,QAC3B;AAAA,MACF;AAAA,IACF;AACA,QAAI,KAAK,aAAa,UAAU;AAE9B,WAAK,YAAY;AAAA,IACnB,OAAO;AACL,UAAI,KAAK,mBAAmB;AAC1B,mBAAW,QAAQ,KAAK,kBAAkB,GAAG;AAC3C,cAAI,CAAC,aAAa,sBAAsB,IAAI,GAAG;AAC7C,iBAAK,gBAAgB,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ACvB,QAAI,KAAK,aAAa;AACpB,WAAK,YAAY,OAAO;AACxB,WAAK,cAAc;AAAA,IACrB;AAAA,EACF;AACF;",
4
+ "sourcesContent": ["export * from \"./NetworkedDOMWebsocket\";\nexport * from \"./DOMSanitizer\";\n", "export class DOMSanitizer {\n static sanitise(node: HTMLElement) {\n if (node.getAttributeNames) {\n for (const attr of node.getAttributeNames()) {\n if (!DOMSanitizer.IsValidAttributeName(attr)) {\n node.removeAttribute(attr);\n }\n }\n }\n if (node.nodeName === \"SCRIPT\") {\n // set text to empty string\n node.innerText = \"\";\n } else {\n if (node.getAttributeNames) {\n for (const attr of node.getAttributeNames()) {\n if (!DOMSanitizer.shouldAcceptAttribute(attr)) {\n node.removeAttribute(attr);\n }\n }\n }\n for (let i = 0; i < node.childNodes.length; i++) {\n DOMSanitizer.sanitise(node.childNodes[i] as HTMLElement);\n }\n }\n }\n\n static IsASCIIDigit(c: string): boolean {\n return c >= \"0\" && c <= \"9\";\n }\n\n static IsASCIIAlpha(c: string) {\n return c >= \"a\" && c <= \"z\";\n }\n\n static IsValidAttributeName(characters: string): boolean {\n const c = characters[0];\n if (!(DOMSanitizer.IsASCIIAlpha(c) || c === \":\" || c === \"_\")) {\n return false;\n }\n\n for (let i = 1; i < characters.length; i++) {\n const c = characters[i];\n if (\n !(\n DOMSanitizer.IsASCIIDigit(c) ||\n DOMSanitizer.IsASCIIAlpha(c) ||\n c === \":\" ||\n c === \"_\" ||\n c === \"-\" ||\n c === \".\"\n )\n ) {\n return false;\n }\n }\n\n return true;\n }\n\n static shouldAcceptAttribute(attribute: string) {\n if (!DOMSanitizer.IsValidAttributeName(attribute)) {\n console.warn(\"Invalid attribute name\", attribute);\n return false;\n }\n\n // TODO - this might be overly restrictive - apologies to someone that finds this because you have a non-event attribute filtered by this\n return !attribute.startsWith(\"on\");\n }\n}\n", "import {\n AttributeChangedDiff,\n ChildrenChangedDiff,\n ClientMessage,\n NodeDescription,\n RemoteEvent,\n ServerMessage,\n SnapshotMessage,\n TextChangedDiff,\n} from \"@mml-io/networked-dom-protocol\";\n\nimport { DOMSanitizer } from \"./DOMSanitizer\";\n\nconst websocketProtocol = \"networked-dom-v0.1\";\n\nconst startingBackoffTimeMilliseconds = 100;\nconst maximumBackoffTimeMilliseconds = 10000;\nconst maximumWebsocketConnectionTimeout = 5000;\n\nexport type NetworkedDOMWebsocketFactory = (url: string) => WebSocket;\n\nexport enum NetworkedDOMWebsocketStatus {\n Connecting,\n Connected,\n Reconnecting,\n Disconnected,\n}\n\nexport class NetworkedDOMWebsocket {\n private idToElement = new Map<number, Node>();\n private elementToId = new Map<Node, number>();\n private websocket: WebSocket | null = null;\n private currentRoot: HTMLElement | null = null;\n\n private url: string;\n private websocketFactory: NetworkedDOMWebsocketFactory;\n private parentElement: HTMLElement;\n private timeCallback: (time: number) => void;\n private statusUpdateCallback: (status: NetworkedDOMWebsocketStatus) => void;\n private stopped = false;\n private backoffTime = startingBackoffTimeMilliseconds;\n private status: NetworkedDOMWebsocketStatus | null = null;\n\n public static createWebSocket(url: string): WebSocket {\n return new WebSocket(url, [websocketProtocol]);\n }\n\n constructor(\n url: string,\n websocketFactory: NetworkedDOMWebsocketFactory,\n parentElement: HTMLElement,\n timeCallback?: (time: number) => void,\n statusUpdateCallback?: (status: NetworkedDOMWebsocketStatus) => void,\n ) {\n this.url = url;\n this.websocketFactory = websocketFactory;\n this.parentElement = parentElement;\n this.timeCallback =\n timeCallback ||\n (() => {\n // no-op\n });\n this.statusUpdateCallback =\n statusUpdateCallback ||\n (() => {\n // no-op\n });\n this.setStatus(NetworkedDOMWebsocketStatus.Connecting);\n this.startWebSocketConnectionAttempt();\n }\n\n private setStatus(status: NetworkedDOMWebsocketStatus) {\n if (this.status !== status) {\n this.status = status;\n this.statusUpdateCallback(status);\n }\n }\n\n private isHTMLElement(node: unknown): node is HTMLElement {\n if (node instanceof HTMLElement) {\n return true;\n }\n if (!this.parentElement.ownerDocument.defaultView) {\n return false;\n }\n return node instanceof this.parentElement.ownerDocument.defaultView.HTMLElement;\n }\n\n private isText(node: unknown): node is Text {\n if (node instanceof Text) {\n return true;\n }\n if (!this.parentElement.ownerDocument.defaultView) {\n return false;\n }\n return node instanceof this.parentElement.ownerDocument.defaultView.Text;\n }\n\n private createWebsocketWithTimeout(timeout: number): Promise<WebSocket> {\n return new Promise((resolve, reject) => {\n const timeoutId = setTimeout(() => {\n reject(new Error(\"websocket connection timed out\"));\n }, timeout);\n const websocket = this.websocketFactory(this.url);\n websocket.addEventListener(\"open\", () => {\n clearTimeout(timeoutId);\n\n this.websocket = websocket;\n\n websocket.addEventListener(\"message\", (event) => {\n if (websocket !== this.websocket) {\n console.log(\"Ignoring websocket message event because it is no longer current\");\n websocket.close();\n return;\n }\n this.handleIncomingWebsocketMessage(event);\n });\n\n const onWebsocketClose = async () => {\n const hadContents = this.currentRoot !== null;\n this.clearContents();\n if (this.stopped) {\n // This closing is expected. The client closed the websocket.\n this.setStatus(NetworkedDOMWebsocketStatus.Disconnected);\n return;\n }\n if (!hadContents) {\n // The websocket did not deliver any contents. It may have been successfully opened, but immediately closed. This client should back off to prevent this happening in a rapid loop.\n await this.waitBackoffTime();\n }\n // The websocket closed unexpectedly. Try to reconnect.\n this.setStatus(NetworkedDOMWebsocketStatus.Reconnecting);\n this.startWebSocketConnectionAttempt();\n };\n\n websocket.addEventListener(\"close\", (e) => {\n if (websocket !== this.websocket) {\n console.warn(\"Ignoring websocket close event because it is no longer current\");\n return;\n }\n console.log(\"NetworkedDOMWebsocket close\", e);\n onWebsocketClose();\n });\n websocket.addEventListener(\"error\", (e) => {\n if (websocket !== this.websocket) {\n console.log(\"Ignoring websocket error event because it is no longer current\");\n return;\n }\n console.error(\"NetworkedDOMWebsocket error\", e);\n onWebsocketClose();\n });\n\n resolve(websocket);\n });\n websocket.addEventListener(\"error\", (e) => {\n clearTimeout(timeoutId);\n reject(e);\n });\n });\n }\n\n private async waitBackoffTime(): Promise<void> {\n console.warn(`Websocket connection to '${this.url}' failed: retrying in ${this.backoffTime}ms`);\n await new Promise((resolve) => setTimeout(resolve, this.backoffTime));\n this.backoffTime = Math.min(\n // Introduce a small amount of randomness to prevent clients from retrying in lockstep\n this.backoffTime * (1.5 + Math.random() * 0.5),\n maximumBackoffTimeMilliseconds,\n );\n }\n\n private async startWebSocketConnectionAttempt() {\n if (this.stopped) {\n return;\n }\n // eslint-disable-next-line no-constant-condition\n while (true) {\n if (this.stopped) {\n return;\n }\n try {\n await this.createWebsocketWithTimeout(maximumWebsocketConnectionTimeout);\n break;\n } catch (e) {\n // Connection failed, retry with backoff\n this.setStatus(NetworkedDOMWebsocketStatus.Reconnecting);\n await this.waitBackoffTime();\n }\n }\n }\n\n private handleIncomingWebsocketMessage(event: MessageEvent) {\n const messages = JSON.parse(event.data) as Array<ServerMessage>;\n for (const message of messages) {\n switch (message.type) {\n case \"error\":\n console.error(\"Error from server\", message);\n break;\n case \"warning\":\n console.warn(\"Warning from server\", message);\n break;\n default: {\n if (message.documentTime) {\n if (this.timeCallback) {\n this.timeCallback(message.documentTime);\n }\n }\n switch (message.type) {\n case \"snapshot\":\n this.handleSnapshot(message);\n break;\n case \"attributeChange\":\n this.handleAttributeChange(message);\n break;\n case \"childrenChanged\":\n this.handleChildrenChanged(message);\n break;\n case \"textChanged\":\n this.handleTextChanged(message);\n break;\n case \"ping\":\n this.send({\n type: \"pong\",\n pong: message.ping,\n });\n break;\n default:\n console.warn(\"unknown message type\", message);\n break;\n }\n }\n }\n }\n }\n\n public stop() {\n this.stopped = true;\n if (this.websocket !== null) {\n this.websocket.close();\n this.websocket = null;\n }\n }\n\n public handleEvent(element: HTMLElement, event: CustomEvent<{ element: HTMLElement }>) {\n const nodeId = this.elementToId.get(element);\n if (nodeId === undefined || nodeId === null) {\n throw new Error(\"Element not found\");\n }\n\n console.log(\n `Sending event to websocket: \"${event.type}\" on node: ${nodeId} type: ${element.tagName}`,\n );\n\n const detailWithoutElement: Partial<typeof event.detail> = {\n ...event.detail,\n };\n delete detailWithoutElement.element;\n\n const remoteEvent: RemoteEvent = {\n type: \"event\",\n nodeId,\n name: event.type,\n bubbles: event.bubbles,\n params: detailWithoutElement,\n };\n\n this.send(remoteEvent);\n }\n\n private send(fromClientMessage: ClientMessage) {\n if (!this.websocket) {\n throw new Error(\"No websocket created\");\n }\n this.websocket.send(JSON.stringify(fromClientMessage));\n }\n\n private handleTextChanged(message: TextChangedDiff) {\n const { nodeId, text } = message;\n\n if (nodeId === undefined || nodeId === null) {\n console.warn(\"No nodeId in textChanged message\");\n return;\n }\n const node = this.idToElement.get(nodeId);\n if (!node) {\n throw new Error(\"No node found for textChanged message\");\n }\n if (!this.isText(node)) {\n throw new Error(\"Node for textChanged message is not a Text node\");\n }\n node.textContent = text;\n }\n\n private handleChildrenChanged(message: ChildrenChangedDiff) {\n const { nodeId, addedNodes, removedNodes, previousNodeId } = message;\n if (nodeId === undefined || nodeId === null) {\n console.warn(\"No nodeId in childrenChanged message\");\n return;\n }\n const parent = this.idToElement.get(nodeId);\n if (!parent) {\n throw new Error(\"No parent found for childrenChanged message\");\n }\n if (!parent.isConnected) {\n console.error(\"Parent is not connected\", parent);\n }\n if (!this.isHTMLElement(parent)) {\n throw new Error(\"Parent is not an HTMLElement (that supports children)\");\n }\n let previousElement;\n if (previousNodeId) {\n previousElement = this.idToElement.get(previousNodeId);\n if (!previousElement) {\n throw new Error(\"No previous element found for childrenChanged message\");\n }\n }\n\n for (const addedNode of addedNodes) {\n const childElement = this.handleNewElement(addedNode);\n if (childElement) {\n if (previousElement) {\n const nextElement = previousElement.nextSibling;\n if (nextElement) {\n parent.insertBefore(childElement, nextElement);\n } else {\n parent.append(childElement);\n }\n } else {\n parent.append(childElement);\n }\n }\n }\n for (const removedNode of removedNodes) {\n const childElement = this.idToElement.get(removedNode);\n if (!childElement) {\n throw new Error(`Child element not found: ${removedNode}`);\n }\n this.elementToId.delete(childElement);\n this.idToElement.delete(removedNode);\n parent.removeChild(childElement);\n if (this.isHTMLElement(childElement)) {\n // If child is capable of supporting children then remove any that exist\n this.removeChildElementIds(childElement);\n }\n }\n }\n\n private removeChildElementIds(parent: HTMLElement) {\n for (let i = 0; i < parent.children.length; i++) {\n const child = parent.children[i];\n const childId = this.elementToId.get(child as HTMLElement);\n if (!childId) {\n console.error(\"Inner child of removed element had no id\", child);\n } else {\n this.elementToId.delete(child);\n this.idToElement.delete(childId);\n }\n this.removeChildElementIds(child as HTMLElement);\n }\n }\n\n private handleSnapshot(message: SnapshotMessage) {\n // This websocket is successfully connected. Reset the backoff time.\n this.backoffTime = startingBackoffTimeMilliseconds;\n this.setStatus(NetworkedDOMWebsocketStatus.Connected);\n\n if (this.currentRoot) {\n this.currentRoot.remove();\n this.currentRoot = null;\n this.elementToId.clear();\n this.idToElement.clear();\n }\n\n // create a tree of DOM elements\n // NOTE: the MElement constructors are not executed during this stage\n const element = this.handleNewElement(message.snapshot);\n if (!element) {\n throw new Error(\"Snapshot element not created\");\n }\n if (!this.isHTMLElement(element)) {\n throw new Error(\"Snapshot element is not an HTMLElement\");\n }\n this.currentRoot = element;\n // appending to the tree causes MElements to be constructed\n this.parentElement.append(element);\n }\n\n private handleAttributeChange(message: AttributeChangedDiff) {\n const { nodeId, attribute, newValue } = message;\n if (nodeId === undefined || nodeId === null) {\n console.warn(\"No nodeId in attributeChange message\");\n return;\n }\n const element = this.idToElement.get(nodeId);\n if (element) {\n if (this.isHTMLElement(element)) {\n if (newValue === null) {\n element.removeAttribute(attribute);\n } else {\n if (DOMSanitizer.shouldAcceptAttribute(attribute)) {\n element.setAttribute(attribute, newValue);\n }\n }\n } else {\n console.error(\"Element is not an HTMLElement and cannot support attributes\", element);\n }\n } else {\n console.error(\"No element found for attributeChange message\");\n }\n }\n\n private handleNewElement(message: NodeDescription): Node | null {\n if (message.type === \"text\") {\n const { nodeId, text } = message;\n const textNode = document.createTextNode(\"\");\n textNode.textContent = text;\n this.idToElement.set(nodeId, textNode);\n this.elementToId.set(textNode, nodeId);\n return textNode;\n }\n const { tag, nodeId, attributes, children, text } = message;\n if (nodeId === undefined || nodeId === null) {\n console.warn(\"No nodeId in handleNewElement message\", message);\n return null;\n }\n if (this.idToElement.has(nodeId)) {\n console.error(\n \"Received nodeId to add that is already present\",\n nodeId,\n this.idToElement.get(nodeId),\n );\n }\n if (tag === \"#text\") {\n const textNode = document.createTextNode(\"\");\n textNode.textContent = text || null;\n this.idToElement.set(nodeId, textNode);\n this.elementToId.set(textNode, nodeId);\n return textNode;\n }\n\n let element;\n try {\n element = document.createElement(tag);\n } catch (e) {\n console.error(`Error creating element: (${tag})`, e);\n element = document.createElement(\"div\");\n }\n this.idToElement.set(nodeId, element);\n this.elementToId.set(element, nodeId);\n for (const key in attributes) {\n if (DOMSanitizer.shouldAcceptAttribute(key)) {\n const value = attributes[key];\n element.setAttribute(key, value);\n }\n }\n if (children) {\n for (const child of children) {\n const childElement = this.handleNewElement(child);\n if (childElement) {\n element.append(childElement);\n }\n }\n }\n return element;\n }\n\n private clearContents() {\n this.idToElement.clear();\n this.elementToId.clear();\n if (this.currentRoot) {\n this.currentRoot.remove();\n this.currentRoot = null;\n }\n }\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,eAAN,MAAmB;AAAA,EACxB,OAAO,SAAS,MAAmB;AACjC,QAAI,KAAK,mBAAmB;AAC1B,iBAAW,QAAQ,KAAK,kBAAkB,GAAG;AAC3C,YAAI,CAAC,aAAa,qBAAqB,IAAI,GAAG;AAC5C,eAAK,gBAAgB,IAAI;AAAA,QAC3B;AAAA,MACF;AAAA,IACF;AACA,QAAI,KAAK,aAAa,UAAU;AAE9B,WAAK,YAAY;AAAA,IACnB,OAAO;AACL,UAAI,KAAK,mBAAmB;AAC1B,mBAAW,QAAQ,KAAK,kBAAkB,GAAG;AAC3C,cAAI,CAAC,aAAa,sBAAsB,IAAI,GAAG;AAC7C,iBAAK,gBAAgB,IAAI;AAAA,UAC3B;AAAA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AAK,YAAY,MAAM;AACvB,SAAK,YAAY,MAAM;AACvB,QAAI,KAAK,aAAa;AACpB,WAAK,YAAY,OAAO;AACxB,WAAK,cAAc;AAAA,IACrB;AAAA,EACF;AACF;",
6
6
  "names": ["c", "NetworkedDOMWebsocketStatus", "nodeId", "text"]
7
7
  }
@@ -0,0 +1 @@
1
+ {"program":{"fileNames":["../../../node_modules/typescript/lib/lib.es5.d.ts","../../../node_modules/typescript/lib/lib.es2015.d.ts","../../../node_modules/typescript/lib/lib.es2016.d.ts","../../../node_modules/typescript/lib/lib.es2017.d.ts","../../../node_modules/typescript/lib/lib.es2018.d.ts","../../../node_modules/typescript/lib/lib.es2019.d.ts","../../../node_modules/typescript/lib/lib.es2020.d.ts","../../../node_modules/typescript/lib/lib.dom.d.ts","../../../node_modules/typescript/lib/lib.es2015.core.d.ts","../../../node_modules/typescript/lib/lib.es2015.collection.d.ts","../../../node_modules/typescript/lib/lib.es2015.generator.d.ts","../../../node_modules/typescript/lib/lib.es2015.iterable.d.ts","../../../node_modules/typescript/lib/lib.es2015.promise.d.ts","../../../node_modules/typescript/lib/lib.es2015.proxy.d.ts","../../../node_modules/typescript/lib/lib.es2015.reflect.d.ts","../../../node_modules/typescript/lib/lib.es2015.symbol.d.ts","../../../node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts","../../../node_modules/typescript/lib/lib.es2016.array.include.d.ts","../../../node_modules/typescript/lib/lib.es2017.object.d.ts","../../../node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts","../../../node_modules/typescript/lib/lib.es2017.string.d.ts","../../../node_modules/typescript/lib/lib.es2017.intl.d.ts","../../../node_modules/typescript/lib/lib.es2017.typedarrays.d.ts","../../../node_modules/typescript/lib/lib.es2018.asyncgenerator.d.ts","../../../node_modules/typescript/lib/lib.es2018.asynciterable.d.ts","../../../node_modules/typescript/lib/lib.es2018.intl.d.ts","../../../node_modules/typescript/lib/lib.es2018.promise.d.ts","../../../node_modules/typescript/lib/lib.es2018.regexp.d.ts","../../../node_modules/typescript/lib/lib.es2019.array.d.ts","../../../node_modules/typescript/lib/lib.es2019.object.d.ts","../../../node_modules/typescript/lib/lib.es2019.string.d.ts","../../../node_modules/typescript/lib/lib.es2019.symbol.d.ts","../../../node_modules/typescript/lib/lib.es2019.intl.d.ts","../../../node_modules/typescript/lib/lib.es2020.bigint.d.ts","../../../node_modules/typescript/lib/lib.es2020.date.d.ts","../../../node_modules/typescript/lib/lib.es2020.promise.d.ts","../../../node_modules/typescript/lib/lib.es2020.sharedmemory.d.ts","../../../node_modules/typescript/lib/lib.es2020.string.d.ts","../../../node_modules/typescript/lib/lib.es2020.symbol.wellknown.d.ts","../../../node_modules/typescript/lib/lib.es2020.intl.d.ts","../../../node_modules/typescript/lib/lib.es2020.number.d.ts","../../../node_modules/typescript/lib/lib.esnext.intl.d.ts","../../../node_modules/typescript/lib/lib.decorators.d.ts","../../../node_modules/typescript/lib/lib.decorators.legacy.d.ts","../../../node_modules/jest-expect-message/types/index.d.ts","../src/domsanitizer.ts","../../networked-dom-protocol/build/networked-dom-v0.1/from-client.d.ts","../../networked-dom-protocol/build/networked-dom-v0.1/from-server.d.ts","../../networked-dom-protocol/build/networked-dom-v0.1/index.d.ts","../../networked-dom-protocol/build/index.d.ts","../src/networkeddomwebsocket.ts","../src/index.ts","../../../node_modules/@types/node/assert.d.ts","../../../node_modules/@types/node/assert/strict.d.ts","../../../node_modules/@types/node/globals.d.ts","../../../node_modules/@types/node/async_hooks.d.ts","../../../node_modules/@types/node/buffer.d.ts","../../../node_modules/@types/node/child_process.d.ts","../../../node_modules/@types/node/cluster.d.ts","../../../node_modules/@types/node/console.d.ts","../../../node_modules/@types/node/constants.d.ts","../../../node_modules/@types/node/crypto.d.ts","../../../node_modules/@types/node/dgram.d.ts","../../../node_modules/@types/node/diagnostics_channel.d.ts","../../../node_modules/@types/node/dns.d.ts","../../../node_modules/@types/node/dns/promises.d.ts","../../../node_modules/@types/node/domain.d.ts","../../../node_modules/@types/node/dom-events.d.ts","../../../node_modules/@types/node/events.d.ts","../../../node_modules/@types/node/fs.d.ts","../../../node_modules/@types/node/fs/promises.d.ts","../../../node_modules/@types/node/http.d.ts","../../../node_modules/@types/node/http2.d.ts","../../../node_modules/@types/node/https.d.ts","../../../node_modules/@types/node/inspector.d.ts","../../../node_modules/@types/node/module.d.ts","../../../node_modules/@types/node/net.d.ts","../../../node_modules/@types/node/os.d.ts","../../../node_modules/@types/node/path.d.ts","../../../node_modules/@types/node/perf_hooks.d.ts","../../../node_modules/@types/node/process.d.ts","../../../node_modules/@types/node/punycode.d.ts","../../../node_modules/@types/node/querystring.d.ts","../../../node_modules/@types/node/readline.d.ts","../../../node_modules/@types/node/readline/promises.d.ts","../../../node_modules/@types/node/repl.d.ts","../../../node_modules/@types/node/stream.d.ts","../../../node_modules/@types/node/stream/promises.d.ts","../../../node_modules/@types/node/stream/consumers.d.ts","../../../node_modules/@types/node/stream/web.d.ts","../../../node_modules/@types/node/string_decoder.d.ts","../../../node_modules/@types/node/test.d.ts","../../../node_modules/@types/node/timers.d.ts","../../../node_modules/@types/node/timers/promises.d.ts","../../../node_modules/@types/node/tls.d.ts","../../../node_modules/@types/node/trace_events.d.ts","../../../node_modules/@types/node/tty.d.ts","../../../node_modules/@types/node/url.d.ts","../../../node_modules/@types/node/util.d.ts","../../../node_modules/@types/node/v8.d.ts","../../../node_modules/@types/node/vm.d.ts","../../../node_modules/@types/node/wasi.d.ts","../../../node_modules/@types/node/worker_threads.d.ts","../../../node_modules/@types/node/zlib.d.ts","../../../node_modules/@types/node/globals.global.d.ts","../../../node_modules/@types/node/index.d.ts","../../../node_modules/@jest/expect-utils/build/index.d.ts","../../../node_modules/chalk/index.d.ts","../../../node_modules/@sinclair/typebox/typebox.d.ts","../../../node_modules/@jest/schemas/build/index.d.ts","../../../node_modules/pretty-format/build/index.d.ts","../../../node_modules/jest-diff/build/index.d.ts","../../../node_modules/jest-matcher-utils/build/index.d.ts","../../../node_modules/expect/build/index.d.ts","../../../node_modules/@types/jest/index.d.ts"],"fileInfos":[{"version":"6a6b471e7e43e15ef6f8fe617a22ce4ecb0e34efa6c3dfcfe7cebd392bcca9d2","affectsGlobalScope":true},"45b7ab580deca34ae9729e97c13cfd999df04416a79116c3bfb483804f85ded4","dc48272d7c333ccf58034c0026162576b7d50ea0e69c3b9292f803fc20720fd5","27147504487dc1159369da4f4da8a26406364624fa9bc3db632f7d94a5bae2c3","5e1c4c362065a6b95ff952c0eab010f04dcd2c3494e813b493ecfd4fcb9fc0d8","68d73b4a11549f9c0b7d352d10e91e5dca8faa3322bfb77b661839c42b1ddec7","5efce4fc3c29ea84e8928f97adec086e3dc876365e0982cc8479a07954a3efd4",{"version":"fcd3ecc9f764f06f4d5c467677f4f117f6abf49dee6716283aa204ff1162498b","affectsGlobalScope":true},{"version":"f296963760430fb65b4e5d91f0ed770a91c6e77455bacf8fa23a1501654ede0e","affectsGlobalScope":true},{"version":"5114a95689b63f96b957e00216bc04baf9e1a1782aa4d8ee7e5e9acbf768e301","affectsGlobalScope":true},{"version":"4443e68b35f3332f753eacc66a04ac1d2053b8b035a0e0ac1d455392b5e243b3","affectsGlobalScope":true},{"version":"ab22100fdd0d24cfc2cc59d0a00fc8cf449830d9c4030dc54390a46bd562e929","affectsGlobalScope":true},{"version":"f7bd636ae3a4623c503359ada74510c4005df5b36de7f23e1db8a5c543fd176b","affectsGlobalScope":true},{"version":"ce691fb9e5c64efb9547083e4a34091bcbe5bdb41027e310ebba8f7d96a98671","affectsGlobalScope":true},{"version":"8d697a2a929a5fcb38b7a65594020fcef05ec1630804a33748829c5ff53640d0","affectsGlobalScope":true},{"version":"0c20f4d2358eb679e4ae8a4432bdd96c857a2960fd6800b21ec4008ec59d60ea","affectsGlobalScope":true},{"version":"36ae84ccc0633f7c0787bc6108386c8b773e95d3b052d9464a99cd9b8795fbec","affectsGlobalScope":true},{"version":"82d0d8e269b9eeac02c3bd1c9e884e85d483fcb2cd168bccd6bc54df663da031","affectsGlobalScope":true},{"version":"b8deab98702588840be73d67f02412a2d45a417a3c097b2e96f7f3a42ac483d1","affectsGlobalScope":true},{"version":"4738f2420687fd85629c9efb470793bb753709c2379e5f85bc1815d875ceadcd","affectsGlobalScope":true},{"version":"2f11ff796926e0832f9ae148008138ad583bd181899ab7dd768a2666700b1893","affectsGlobalScope":true},{"version":"376d554d042fb409cb55b5cbaf0b2b4b7e669619493c5d18d5fa8bd67273f82a","affectsGlobalScope":true},{"version":"9fc46429fbe091ac5ad2608c657201eb68b6f1b8341bd6d670047d32ed0a88fa","affectsGlobalScope":true},{"version":"61c37c1de663cf4171e1192466e52c7a382afa58da01b1dc75058f032ddf0839","affectsGlobalScope":true},{"version":"c4138a3dd7cd6cf1f363ca0f905554e8d81b45844feea17786cdf1626cb8ea06","affectsGlobalScope":true},{"version":"6ff3e2452b055d8f0ec026511c6582b55d935675af67cdb67dd1dc671e8065df","affectsGlobalScope":true},{"version":"03de17b810f426a2f47396b0b99b53a82c1b60e9cba7a7edda47f9bb077882f4","affectsGlobalScope":true},{"version":"8184c6ddf48f0c98429326b428478ecc6143c27f79b79e85740f17e6feb090f1","affectsGlobalScope":true},{"version":"261c4d2cf86ac5a89ad3fb3fafed74cbb6f2f7c1d139b0540933df567d64a6ca","affectsGlobalScope":true},{"version":"6af1425e9973f4924fca986636ac19a0cf9909a7e0d9d3009c349e6244e957b6","affectsGlobalScope":true},{"version":"576711e016cf4f1804676043e6a0a5414252560eb57de9faceee34d79798c850","affectsGlobalScope":true},{"version":"89c1b1281ba7b8a96efc676b11b264de7a8374c5ea1e6617f11880a13fc56dc6","affectsGlobalScope":true},{"version":"15a630d6817718a2ddd7088c4f83e4673fde19fa992d2eae2cf51132a302a5d3","affectsGlobalScope":true},{"version":"b7e9f95a7387e3f66be0ed6db43600c49cec33a3900437ce2fd350d9b7cb16f2","affectsGlobalScope":true},{"version":"01e0ee7e1f661acedb08b51f8a9b7d7f959e9cdb6441360f06522cc3aea1bf2e","affectsGlobalScope":true},{"version":"ac17a97f816d53d9dd79b0d235e1c0ed54a8cc6a0677e9a3d61efb480b2a3e4e","affectsGlobalScope":true},{"version":"bf14a426dbbf1022d11bd08d6b8e709a2e9d246f0c6c1032f3b2edb9a902adbe","affectsGlobalScope":true},{"version":"ec0104fee478075cb5171e5f4e3f23add8e02d845ae0165bfa3f1099241fa2aa","affectsGlobalScope":true},{"version":"2b72d528b2e2fe3c57889ca7baef5e13a56c957b946906d03767c642f386bbc3","affectsGlobalScope":true},{"version":"9cc66b0513ad41cb5f5372cca86ef83a0d37d1c1017580b7dace3ea5661836df","affectsGlobalScope":true},{"version":"368af93f74c9c932edd84c58883e736c9e3d53cec1fe24c0b0ff451f529ceab1","affectsGlobalScope":true},{"version":"307c8b7ebbd7f23a92b73a4c6c0a697beca05b06b036c23a34553e5fe65e4fdc","affectsGlobalScope":true},{"version":"189c0703923150aa30673fa3de411346d727cc44a11c75d05d7cf9ef095daa22","affectsGlobalScope":true},{"version":"782dec38049b92d4e85c1585fbea5474a219c6984a35b004963b00beb1aab538","affectsGlobalScope":true},{"version":"406a5a771d97867cba07e7fbd3cd1fb2dd1004558b973733205737e9dd7ed929","affectsGlobalScope":true},"eb1786acd76610acb0cdb940449c66f8e945ac512eb7c0723e468b8b4c949887","328c09d87a15cde5936230f085c6888e6dc5f31a7c600a8a222419e28ea02812","0e902771dd21ea683172d5b5dd5fb192a166a9e3071b674695ac9013edb4b4fc","660b97032e7c3a5a094b60e3de0bf602626931fe0167dbad07e9ada9f683161e","28e083d1a14be292588f05b70cf7852deb63a3e0da35a0156bcbe361b5072562","54f9cb96075590738161547adc4c651c5a0ca89f78cb7b65163b9a49e6921b04","eed070a577657243e4f7d5c8a0d4cfacbd6e6518ad809ffd21d10721c84ed0af","587f13f1e8157bd8cec0adda0de4ef558bb8573daa9d518d1e2af38e87ecc91f","a69c09dbea52352f479d3e7ac949fde3d17b195abe90b045d619f747b38d6d1a",{"version":"bce910d9164785c9f0d4dcea4be359f5f92130c7c7833dea6138ab1db310a1f9","affectsGlobalScope":true},"7a435e0c814f58f23e9a0979045ec0ef5909aac95a70986e8bcce30c27dff228",{"version":"a7534271773a27ff7d136d550e86b41894d8090fa857ba4c02b5bb18d2eb1c8e","affectsGlobalScope":true},"db71be322f07f769200108aa19b79a75dd19a187c9dca2a30c4537b233aa2863","57135ce61976a8b1dadd01bb412406d1805b90db6e8ecb726d0d78e0b5f76050",{"version":"49479e21a040c0177d1b1bc05a124c0383df7a08a0726ad4d9457619642e875a","affectsGlobalScope":true},"82408ed3e959ddc60d3e9904481b5a8dc16469928257af22a3f7d1a3bc7fd8c4","b8e431e9b9bb2dc832b23d4e3e02774e953d5537998923f215ea446169e9a61e","3690133deae19c8127c5505fcb67b04bdc9eb053796008538a9b9abbb70d85aa","5b1c0a23f464f894e7c2b2b6c56df7b9afa60ed48c5345f8618d389a636b2108","be2b092f2765222757c6441b86c53a5ea8dfed47bbc43eab4c5fe37942c866b3","8e6b05abc98adba15e1ac78e137c64576c74002e301d682e66feb77a23907ab8","1ca735bb3d407b2af4fbee7665f3a0a83be52168c728cc209755060ba7ed67bd",{"version":"6b526a5ec4a401ca7c26cfe6a48e641d8f30af76673bad3b06a1b4504594a960","affectsGlobalScope":true},{"version":"b85c02e14ecb2a873dad5a1de72319b265160ba48f1b83661aeb3bba1366c1bc","affectsGlobalScope":true},"7a2ba0c9af860ac3e77b35ed01fd96d15986f17aa22fe40f188ae556fb1070df","fc3764040518a1008dd04bdc80964591b566b896283e00df85c95851c1f46237","55709608060f77965c270ac10ac646286589f1bd1cb174fff1778a2dd9a7ef31","790623a47c5eda62910098884ecb154dc0e5f3a23fc36c1bfb3b5b9ed44e2c2d","42b40e40f2a358cda332456214fad311e1806a6abf3cebaaac72496e07556642","354612fe1d49ecc9551ea3a27d94eef2887b64ef4a71f72ca444efe0f2f0ba80",{"version":"125af9d85cb9d5e508353f10a8d52f01652d2d48b2cea54789a33e5b4d289c1c","affectsGlobalScope":true},"f5490f53d40291cc8607f5463434d1ac6c5564bc4fbb03abceb03a8f6b014457","5e2b91328a540a0933ab5c2203f4358918e6f0fe7505d22840a891a6117735f1","3abc3512fa04aa0230f59ea1019311fd8667bd935d28306311dccc8b17e79d5d",{"version":"14a50dafe3f45713f7f27cb6320dff07c6ac31678f07959c2134260061bf91ff","affectsGlobalScope":true},{"version":"19da7150ca062323b1db6311a6ef058c9b0a39cc64d836b5e9b75d301869653b","affectsGlobalScope":true},"1349077576abb41f0e9c78ec30762ff75b710208aff77f5fdcc6a8c8ce6289dd","e2ce82603102b5c0563f59fb40314cc1ff95a4d521a66ad14146e130ea80d89c","a3e0395220255a350aa9c6d56f882bfcb5b85c19fddf5419ec822cf22246a26d","c27b01e8ddff5cd280711af5e13aecd9a3228d1c256ea797dd64f8fdec5f7df5","898840e876dfd21843db9f2aa6ae38ba2eab550eb780ff62b894b9fbfebfae6b","0cab4d7d4edc40cd3af9eea7c3ed6d1016910c0954c49c4297e479bf3822a625","1b952304137851e45bc009785de89ada562d9376177c97e37702e39e60c2f1ff","785e5be57d4f20f290a20e7b0c6263f6c57fd6e51283050756cef07d6d651c68","44b8b584a338b190a59f4f6929d072431950c7bd92ec2694821c11bce180c8a5","164deb2409ac5f4da3cd139dbcee7f7d66753d90363a4d7e2db8d8874f272270","1fb6c5ec52332a8b531a8d7a5300ac9301f98c4fe62f68e744e0841ccba65e7e",{"version":"ab294c4b7279318ee2a8fdf681305457ecc05970c94108d304933f18823eeac1","affectsGlobalScope":true},"ad08154d9602429522cac965a715fde27d421d69b24756c5d291877dda75353e","bbda6ea452a2386093a1eda18a6e26a989e98869f1b9f37e46f510a986d2e740","812b25f798033c202baedf386a1ccc41f9191b122f089bffd10fdccce99fba11","993325544790073f77e945bee046d53988c0bc3ac5695c9cf8098166feb82661",{"version":"75dd741ca6a6c8d2437a6ca8349b64b816421dbf9fe82dd026afaba965576962","affectsGlobalScope":true},{"version":"8799401a7ab57764f0d464513a7fa7c72e1d70a226b172ec60fff534ea94d108","affectsGlobalScope":true},"2ce2210032ccaff7710e2abf6a722e62c54960458e73e356b6a365c93ab6ca66","92db194ef7d208d5e4b6242a3434573fd142a621ff996d84cc9dbba3553277d0","16a3080e885ed52d4017c902227a8d0d8daf723d062bec9e45627c6fdcd6699b",{"version":"0bd9543cd8fc0959c76fb8f4f5a26626c2ed62ef4be98fd857bce268066db0a2","affectsGlobalScope":true},"1ca6858a0cbcd74d7db72d7b14c5360a928d1d16748a55ecfa6bfaff8b83071b",{"version":"ab9b9a36e5284fd8d3bf2f7d5fcbc60052f25f27e4d20954782099282c60d23e","affectsGlobalScope":true},"247aa3419c98713231952b33801d4f46563fe542e03604acd8c63ac45a32409c","6c1e688f95fcaf53b1e41c0fdadf2c1cfc96fa924eaf7f9fdb60f96deb0a4986","0d14fa22c41fdc7277e6f71473b20ebc07f40f00e38875142335d5b63cdfc9d2","c085e9aa62d1ae1375794c1fb927a445fa105fed891a7e24edbb1c3300f7384a","f315e1e65a1f80992f0509e84e4ae2df15ecd9ef73df975f7c98813b71e4c8da","5b9586e9b0b6322e5bfbd2c29bd3b8e21ab9d871f82346cb71020e3d84bae73e","3e70a7e67c2cb16f8cd49097360c0309fe9d1e3210ff9222e9dac1f8df9d4fb6","ab68d2a3e3e8767c3fba8f80de099a1cfc18c0de79e42cb02ae66e22dfe14a66","6d969939c4a63f70f2aa49e88da6f64b655c8e6799612807bef41ccff6ea0da9",{"version":"240c702fb4b3bd54d83ee167d80fa7f0cd7300fef7eea0b32cef33129740893c","affectsGlobalScope":true}],"root":[45,46,51,52],"options":{"allowSyntheticDefaultImports":true,"downlevelIteration":true,"jsx":2,"module":99,"noImplicitAny":true,"outDir":"./","sourceMap":true,"strictNullChecks":true,"target":2},"fileIdsList":[[99],[99,109],[99,111,114],[53,99],[56,99],[57,62,90,99],[58,69,70,77,87,98,99],[58,59,69,77,99],[60,99],[61,62,70,78,99],[62,87,95,99],[63,65,69,77,99],[64,99],[65,66,99],[69,99],[67,69,99],[69,70,71,87,98,99],[69,70,71,84,87,90,99],[99,103],[65,69,72,77,87,98,99],[69,70,72,73,77,87,95,98,99],[72,74,87,95,98,99],[53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105],[69,75,99],[76,98,99],[65,69,77,87,99],[78,99],[79,99],[56,80,99],[81,97,99,103],[82,99],[83,99],[69,84,85,99],[84,86,99,101],[57,69,87,88,89,90,99],[57,87,89,99],[87,88,99],[90,99],[91,99],[87,99],[69,93,94,99],[93,94,99],[62,77,87,95,99],[96,99],[77,97,99],[57,72,83,98,99],[62,99],[87,99,100],[99,101],[99,102],[57,62,69,71,80,87,98,99,101,103],[87,99,104],[56,99,106,107,113],[99,111],[99,108,112],[99,110],[49,99],[47,48,99],[46,51,99],[46,50,99]],"referencedMap":[[107,1],[110,2],[109,1],[115,3],[53,4],[54,4],[56,5],[57,6],[58,7],[59,8],[60,9],[61,10],[62,11],[63,12],[64,13],[65,14],[66,14],[68,15],[67,16],[69,15],[70,17],[71,18],[55,19],[105,1],[72,20],[73,21],[74,22],[106,23],[75,24],[76,25],[77,26],[78,27],[79,28],[80,29],[81,30],[82,31],[83,32],[84,33],[85,33],[86,34],[87,35],[89,36],[88,37],[90,38],[91,39],[92,40],[93,41],[94,42],[95,43],[96,44],[97,45],[98,46],[99,47],[100,48],[101,49],[102,50],[103,51],[104,52],[108,1],[114,53],[112,54],[45,1],[113,55],[111,56],[43,1],[44,1],[8,1],[10,1],[9,1],[2,1],[11,1],[12,1],[13,1],[14,1],[15,1],[16,1],[17,1],[18,1],[3,1],[4,1],[22,1],[19,1],[20,1],[21,1],[23,1],[24,1],[25,1],[5,1],[26,1],[27,1],[28,1],[29,1],[6,1],[33,1],[30,1],[31,1],[32,1],[34,1],[7,1],[35,1],[40,1],[41,1],[36,1],[37,1],[38,1],[39,1],[1,1],[42,1],[50,57],[47,1],[48,1],[49,58],[46,1],[52,59],[51,60]],"exportedModulesMap":[[107,1],[110,2],[109,1],[115,3],[53,4],[54,4],[56,5],[57,6],[58,7],[59,8],[60,9],[61,10],[62,11],[63,12],[64,13],[65,14],[66,14],[68,15],[67,16],[69,15],[70,17],[71,18],[55,19],[105,1],[72,20],[73,21],[74,22],[106,23],[75,24],[76,25],[77,26],[78,27],[79,28],[80,29],[81,30],[82,31],[83,32],[84,33],[85,33],[86,34],[87,35],[89,36],[88,37],[90,38],[91,39],[92,40],[93,41],[94,42],[95,43],[96,44],[97,45],[98,46],[99,47],[100,48],[101,49],[102,50],[103,51],[104,52],[108,1],[114,53],[112,54],[45,1],[113,55],[111,56],[43,1],[44,1],[8,1],[10,1],[9,1],[2,1],[11,1],[12,1],[13,1],[14,1],[15,1],[16,1],[17,1],[18,1],[3,1],[4,1],[22,1],[19,1],[20,1],[21,1],[23,1],[24,1],[25,1],[5,1],[26,1],[27,1],[28,1],[29,1],[6,1],[33,1],[30,1],[31,1],[32,1],[34,1],[7,1],[35,1],[40,1],[41,1],[36,1],[37,1],[38,1],[39,1],[1,1],[42,1],[50,57],[47,1],[48,1],[49,58],[46,1],[52,59],[51,60]],"semanticDiagnosticsPerFile":[107,110,109,115,53,54,56,57,58,59,60,61,62,63,64,65,66,68,67,69,70,71,55,105,72,73,74,106,75,76,77,78,79,80,81,82,83,84,85,86,87,89,88,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,108,114,112,45,113,111,43,44,8,10,9,2,11,12,13,14,15,16,17,18,3,4,22,19,20,21,23,24,25,5,26,27,28,29,6,33,30,31,32,34,7,35,40,41,36,37,38,39,1,42,50,47,48,49,46,52,51],"affectedFilesPendingEmit":[46,52,51]},"version":"5.0.4"}
package/package.json CHANGED
@@ -1,11 +1,10 @@
1
1
  {
2
2
  "name": "@mml-io/networked-dom-web",
3
- "version": "0.1.3",
3
+ "version": "0.3.0",
4
4
  "main": "./build/index.js",
5
5
  "types": "./build/index.d.ts",
6
6
  "files": [
7
- "/build",
8
- "/src"
7
+ "/build"
9
8
  ],
10
9
  "scripts": {
11
10
  "type-check": "tsc --noEmit",
@@ -18,7 +17,7 @@
18
17
  "test-iterate": "jest --watch"
19
18
  },
20
19
  "dependencies": {
21
- "@mml-io/networked-dom-protocol": "^0.1.3"
20
+ "@mml-io/networked-dom-protocol": "^0.3.0"
22
21
  },
23
22
  "devDependencies": {
24
23
  "jest-canvas-mock": "2.5.1",
@@ -1,69 +0,0 @@
1
- export class DOMSanitizer {
2
- static sanitise(node: HTMLElement) {
3
- if (node.getAttributeNames) {
4
- for (const attr of node.getAttributeNames()) {
5
- if (!DOMSanitizer.IsValidAttributeName(attr)) {
6
- node.removeAttribute(attr);
7
- }
8
- }
9
- }
10
- if (node.nodeName === "SCRIPT") {
11
- // set text to empty string
12
- node.innerText = "";
13
- } else {
14
- if (node.getAttributeNames) {
15
- for (const attr of node.getAttributeNames()) {
16
- if (!DOMSanitizer.shouldAcceptAttribute(attr)) {
17
- node.removeAttribute(attr);
18
- }
19
- }
20
- }
21
- for (let i = 0; i < node.childNodes.length; i++) {
22
- DOMSanitizer.sanitise(node.childNodes[i] as HTMLElement);
23
- }
24
- }
25
- }
26
-
27
- static IsASCIIDigit(c: string): boolean {
28
- return c >= "0" && c <= "9";
29
- }
30
-
31
- static IsASCIIAlpha(c: string) {
32
- return c >= "a" && c <= "z";
33
- }
34
-
35
- static IsValidAttributeName(characters: string): boolean {
36
- const c = characters[0];
37
- if (!(DOMSanitizer.IsASCIIAlpha(c) || c === ":" || c === "_")) {
38
- return false;
39
- }
40
-
41
- for (let i = 1; i < characters.length; i++) {
42
- const c = characters[i];
43
- if (
44
- !(
45
- DOMSanitizer.IsASCIIDigit(c) ||
46
- DOMSanitizer.IsASCIIAlpha(c) ||
47
- c === ":" ||
48
- c === "_" ||
49
- c === "-" ||
50
- c === "."
51
- )
52
- ) {
53
- return false;
54
- }
55
- }
56
-
57
- return true;
58
- }
59
-
60
- static shouldAcceptAttribute(attribute: string) {
61
- if (!DOMSanitizer.IsValidAttributeName(attribute)) {
62
- console.warn("Invalid attribute name", attribute);
63
- return false;
64
- }
65
-
66
- // TODO - this might be overly restrictive - apologies to someone that finds this because you have a non-event attribute filtered by this
67
- return !attribute.startsWith("on");
68
- }
69
- }
@@ -1,459 +0,0 @@
1
- import {
2
- AttributeChangedDiff,
3
- ChildrenChangedDiff,
4
- ClientMessage,
5
- NodeDescription,
6
- RemoteEvent,
7
- ServerMessage,
8
- TextChangedDiff,
9
- } from "@mml-io/networked-dom-protocol";
10
-
11
- import { DOMSanitizer } from "./DOMSanitizer";
12
-
13
- const websocketProtocol = "networked-dom-v0.1";
14
-
15
- const startingBackoffTimeMilliseconds = 100;
16
- const maximumBackoffTimeMilliseconds = 10000;
17
- const maximumWebsocketConnectionTimeout = 5000;
18
-
19
- export type NetworkedDOMWebsocketFactory = (url: string) => WebSocket;
20
-
21
- export enum NetworkedDOMWebsocketStatus {
22
- Connecting,
23
- Connected,
24
- Reconnecting,
25
- Disconnected,
26
- }
27
-
28
- function isHTMLElement(node: unknown): node is HTMLElement {
29
- if (node instanceof HTMLElement) {
30
- return true;
31
- }
32
- if (!this.parentElement.ownerDocument.defaultView) {
33
- return false;
34
- }
35
- return node instanceof this.parentElement.ownerDocument.defaultView.HTMLElement;
36
- }
37
-
38
- function isText(node: unknown): node is Text {
39
- if (node instanceof Text) {
40
- return true;
41
- }
42
- if (!this.parentElement.ownerDocument.defaultView) {
43
- return false;
44
- }
45
- return node instanceof this.parentElement.ownerDocument.defaultView.Text;
46
- }
47
-
48
- export class NetworkedDOMWebsocket {
49
- private idToElement = new Map<number, Node>();
50
- private elementToId = new Map<Node, number>();
51
- private websocket: WebSocket | null = null;
52
- private currentRoot: HTMLElement | null = null;
53
-
54
- private url: string;
55
- private websocketFactory: NetworkedDOMWebsocketFactory;
56
- private parentElement: HTMLElement;
57
- private timeCallback: (time: number) => void;
58
- private statusUpdateCallback: (status: NetworkedDOMWebsocketStatus) => void;
59
- private stopped = false;
60
- private backoffTime = startingBackoffTimeMilliseconds;
61
- private status: NetworkedDOMWebsocketStatus | null = null;
62
-
63
- public static createWebSocket(url: string): WebSocket {
64
- return new WebSocket(url, [websocketProtocol]);
65
- }
66
-
67
- constructor(
68
- url: string,
69
- websocketFactory: NetworkedDOMWebsocketFactory,
70
- parentElement: HTMLElement,
71
- timeCallback?: (time: number) => void,
72
- statusUpdateCallback?: (status: NetworkedDOMWebsocketStatus) => void,
73
- ) {
74
- this.url = url;
75
- this.websocketFactory = websocketFactory;
76
- this.parentElement = parentElement;
77
- this.timeCallback =
78
- timeCallback ||
79
- (() => {
80
- // no-op
81
- });
82
- this.statusUpdateCallback =
83
- statusUpdateCallback ||
84
- (() => {
85
- // no-op
86
- });
87
- this.setStatus(NetworkedDOMWebsocketStatus.Connecting);
88
- this.startWebSocketConnectionAttempt();
89
- }
90
-
91
- private setStatus(status: NetworkedDOMWebsocketStatus) {
92
- if (this.status !== status) {
93
- this.status = status;
94
- this.statusUpdateCallback(status);
95
- }
96
- }
97
-
98
- private createWebsocketWithTimeout(timeout: number): Promise<WebSocket> {
99
- return new Promise((resolve, reject) => {
100
- const timeoutId = setTimeout(() => {
101
- reject(new Error("websocket connection timed out"));
102
- }, timeout);
103
- const websocket = this.websocketFactory(this.url);
104
- websocket.addEventListener("open", () => {
105
- clearTimeout(timeoutId);
106
-
107
- this.websocket = websocket;
108
-
109
- websocket.addEventListener("message", (event) => {
110
- if (websocket !== this.websocket) {
111
- console.log("Ignoring websocket message event because it is no longer current");
112
- websocket.close();
113
- return;
114
- }
115
- this.handleIncomingWebsocketMessage(event);
116
- });
117
-
118
- const onWebsocketClose = async () => {
119
- const hadContents = this.currentRoot !== null;
120
- this.clearContents();
121
- if (this.stopped) {
122
- // This closing is expected. The client closed the websocket.
123
- this.setStatus(NetworkedDOMWebsocketStatus.Disconnected);
124
- return;
125
- }
126
- if (!hadContents) {
127
- // The websocket did not deliver any contents. It may have been successfully opened, but immediately closed. This client should back off to prevent this happening in a rapid loop.
128
- await this.waitBackoffTime();
129
- }
130
- // The websocket closed unexpectedly. Try to reconnect.
131
- this.setStatus(NetworkedDOMWebsocketStatus.Reconnecting);
132
- this.startWebSocketConnectionAttempt();
133
- };
134
-
135
- websocket.addEventListener("close", (e) => {
136
- if (websocket !== this.websocket) {
137
- console.warn("Ignoring websocket close event because it is no longer current");
138
- return;
139
- }
140
- console.log("NetworkedDOMWebsocket close", e);
141
- onWebsocketClose();
142
- });
143
- websocket.addEventListener("error", (e) => {
144
- if (websocket !== this.websocket) {
145
- console.log("Ignoring websocket error event because it is no longer current");
146
- return;
147
- }
148
- console.error("NetworkedDOMWebsocket error", e);
149
- onWebsocketClose();
150
- });
151
-
152
- resolve(websocket);
153
- });
154
- websocket.addEventListener("error", (e) => {
155
- clearTimeout(timeoutId);
156
- reject(e);
157
- });
158
- });
159
- }
160
-
161
- private async waitBackoffTime(): Promise<void> {
162
- console.warn(`Websocket connection to '${this.url}' failed: retrying in ${this.backoffTime}ms`);
163
- await new Promise((resolve) => setTimeout(resolve, this.backoffTime));
164
- this.backoffTime = Math.min(
165
- // Introduce a small amount of randomness to prevent clients from retrying in lockstep
166
- this.backoffTime * (1.5 + Math.random() * 0.5),
167
- maximumBackoffTimeMilliseconds,
168
- );
169
- }
170
-
171
- private async startWebSocketConnectionAttempt() {
172
- if (this.stopped) {
173
- return;
174
- }
175
- // eslint-disable-next-line no-constant-condition
176
- while (true) {
177
- if (this.stopped) {
178
- return;
179
- }
180
- try {
181
- await this.createWebsocketWithTimeout(maximumWebsocketConnectionTimeout);
182
- break;
183
- } catch (e) {
184
- // Connection failed, retry with backoff
185
- this.setStatus(NetworkedDOMWebsocketStatus.Reconnecting);
186
- await this.waitBackoffTime();
187
- }
188
- }
189
- }
190
-
191
- private handleIncomingWebsocketMessage(event: MessageEvent) {
192
- const messages = JSON.parse(event.data) as Array<ServerMessage>;
193
- for (const message of messages) {
194
- if (message.type === "error") {
195
- console.error("Error from server", message);
196
- } else if (message.type === "warning") {
197
- console.warn("Warning from server", message);
198
- } else {
199
- if (message.documentTime) {
200
- if (this.timeCallback) {
201
- this.timeCallback(message.documentTime);
202
- }
203
- }
204
- if (message.type === "snapshot") {
205
- // This websocket is successfully connected. Reset the backoff time.
206
- this.backoffTime = startingBackoffTimeMilliseconds;
207
- this.setStatus(NetworkedDOMWebsocketStatus.Connected);
208
-
209
- if (this.currentRoot) {
210
- this.currentRoot.remove();
211
- this.currentRoot = null;
212
- this.elementToId.clear();
213
- this.idToElement.clear();
214
- }
215
-
216
- // create a tree of DOM elements
217
- // NOTE: the MLElement contructors are not executed during this stage
218
- const element = this.handleNewElement(message.snapshot);
219
- if (!element) {
220
- throw new Error("Snapshot element not created");
221
- }
222
- if (!isHTMLElement(element)) {
223
- throw new Error("Snapshot element is not an HTMLElement");
224
- }
225
- this.currentRoot = element;
226
- // appending to the tree causes MElements to be constructed
227
- this.parentElement.append(element);
228
- } else if (message.type === "attributeChange") {
229
- this.handleAttributeChange(message);
230
- } else if (message.type === "childrenChanged") {
231
- this.handleChildrenChanged(message);
232
- } else if (message.type === "textChanged") {
233
- this.handleTextChanged(message);
234
- } else if (message.type === "ping") {
235
- this.send({
236
- type: "pong",
237
- pong: message.ping,
238
- });
239
- } else {
240
- console.warn("unknown message type", message);
241
- }
242
- }
243
- }
244
- }
245
-
246
- public stop() {
247
- this.stopped = true;
248
- if (this.websocket !== null) {
249
- this.websocket.close();
250
- this.websocket = null;
251
- }
252
- }
253
-
254
- public handleEvent(element: HTMLElement, event: CustomEvent<{ element: HTMLElement }>) {
255
- const nodeId = this.elementToId.get(element);
256
- if (nodeId === undefined || nodeId === null) {
257
- throw new Error("Element not found");
258
- }
259
-
260
- console.log(
261
- `Sending event to websocket: "${event.type}" on node: ${nodeId} type: ${element.tagName}`,
262
- );
263
-
264
- const detailWithoutElement: Partial<typeof event.detail> = {
265
- ...event.detail,
266
- };
267
- delete detailWithoutElement.element;
268
-
269
- const remoteEvent: RemoteEvent = {
270
- type: "event",
271
- nodeId,
272
- name: event.type,
273
- bubbles: event.bubbles,
274
- params: detailWithoutElement,
275
- };
276
-
277
- this.send(remoteEvent);
278
- }
279
-
280
- private send(fromClientMessage: ClientMessage) {
281
- if (!this.websocket) {
282
- throw new Error("No websocket created");
283
- }
284
- this.websocket.send(JSON.stringify(fromClientMessage));
285
- }
286
-
287
- private handleTextChanged(message: TextChangedDiff) {
288
- const { nodeId, text } = message;
289
-
290
- if (nodeId === undefined || nodeId === null) {
291
- console.warn("No nodeId in textChanged message");
292
- return;
293
- }
294
- const node = this.idToElement.get(nodeId);
295
- if (!node) {
296
- throw new Error("No node found for textChanged message");
297
- }
298
- if (!isText(node)) {
299
- throw new Error("Node for textChanged message is not a Text node");
300
- }
301
- node.textContent = text;
302
- }
303
-
304
- private handleChildrenChanged(message: ChildrenChangedDiff) {
305
- const { nodeId, addedNodes, removedNodes, previousNodeId } = message;
306
- if (nodeId === undefined || nodeId === null) {
307
- console.warn("No nodeId in childrenChanged message");
308
- return;
309
- }
310
- const parent = this.idToElement.get(nodeId);
311
- if (!parent) {
312
- throw new Error("No parent found for childrenChanged message");
313
- }
314
- if (!parent.isConnected) {
315
- console.error("Parent is not connected", parent);
316
- }
317
- if (!isHTMLElement(parent)) {
318
- throw new Error("Parent is not an HTMLElement (that supports children)");
319
- }
320
- let previousElement;
321
- if (previousNodeId) {
322
- previousElement = this.idToElement.get(previousNodeId);
323
- if (!previousElement) {
324
- throw new Error("No previous element found for childrenChanged message");
325
- }
326
- }
327
-
328
- for (const addedNode of addedNodes) {
329
- const childElement = this.handleNewElement(addedNode);
330
- if (childElement) {
331
- if (previousElement) {
332
- const nextElement = previousElement.nextSibling;
333
- if (nextElement) {
334
- parent.insertBefore(childElement, nextElement);
335
- } else {
336
- parent.append(childElement);
337
- }
338
- } else {
339
- parent.append(childElement);
340
- }
341
- }
342
- }
343
- for (const removedNode of removedNodes) {
344
- const childElement = this.idToElement.get(removedNode);
345
- if (!childElement) {
346
- throw new Error(`Child element not found: ${removedNode}`);
347
- }
348
- this.elementToId.delete(childElement);
349
- this.idToElement.delete(removedNode);
350
- parent.removeChild(childElement);
351
- if (isHTMLElement(childElement)) {
352
- // If child is capable of supporting children then remove any that exist
353
- this.removeChildElementIds(childElement);
354
- }
355
- }
356
- }
357
-
358
- private removeChildElementIds(parent: HTMLElement) {
359
- for (let i = 0; i < parent.children.length; i++) {
360
- const child = parent.children[i];
361
- const childId = this.elementToId.get(child as HTMLElement);
362
- if (!childId) {
363
- console.error("Inner child of removed element had no id", child);
364
- } else {
365
- this.elementToId.delete(child);
366
- this.idToElement.delete(childId);
367
- }
368
- this.removeChildElementIds(child as HTMLElement);
369
- }
370
- }
371
-
372
- private handleAttributeChange(message: AttributeChangedDiff) {
373
- const { nodeId, attribute, newValue } = message;
374
- if (nodeId === undefined || nodeId === null) {
375
- console.warn("No nodeId in attributeChange message");
376
- return;
377
- }
378
- const element = this.idToElement.get(nodeId);
379
- if (element) {
380
- if (isHTMLElement(element)) {
381
- if (newValue === null) {
382
- element.removeAttribute(attribute);
383
- } else {
384
- if (DOMSanitizer.shouldAcceptAttribute(attribute)) {
385
- element.setAttribute(attribute, newValue);
386
- }
387
- }
388
- } else {
389
- console.error("Element is not an HTMLElement and cannot support attributes", element);
390
- }
391
- } else {
392
- console.error("No element found for attributeChange message");
393
- }
394
- }
395
-
396
- private handleNewElement(message: NodeDescription): Node | null {
397
- if (message.type === "text") {
398
- const { nodeId, text } = message;
399
- const textNode = document.createTextNode("");
400
- textNode.textContent = text;
401
- this.idToElement.set(nodeId, textNode);
402
- this.elementToId.set(textNode, nodeId);
403
- return textNode;
404
- }
405
- const { tag, nodeId, attributes, children, text } = message;
406
- if (nodeId === undefined || nodeId === null) {
407
- console.warn("No nodeId in handleNewElement message", message);
408
- return null;
409
- }
410
- if (this.idToElement.has(nodeId)) {
411
- console.error(
412
- "Received nodeId to add that is already present",
413
- nodeId,
414
- this.idToElement.get(nodeId),
415
- );
416
- }
417
- if (tag === "#text") {
418
- const textNode = document.createTextNode("");
419
- textNode.textContent = text || null;
420
- this.idToElement.set(nodeId, textNode);
421
- this.elementToId.set(textNode, nodeId);
422
- return textNode;
423
- }
424
-
425
- let element;
426
- try {
427
- element = document.createElement(tag);
428
- } catch (e) {
429
- console.error(`Error creating element: (${tag})`, e);
430
- element = document.createElement("div");
431
- }
432
- this.idToElement.set(nodeId, element);
433
- this.elementToId.set(element, nodeId);
434
- for (const key in attributes) {
435
- if (DOMSanitizer.shouldAcceptAttribute(key)) {
436
- const value = attributes[key];
437
- element.setAttribute(key, value);
438
- }
439
- }
440
- if (children) {
441
- for (const child of children) {
442
- const childElement = this.handleNewElement(child);
443
- if (childElement) {
444
- element.append(childElement);
445
- }
446
- }
447
- }
448
- return element;
449
- }
450
-
451
- private clearContents() {
452
- this.idToElement.clear();
453
- this.elementToId.clear();
454
- if (this.currentRoot) {
455
- this.currentRoot.remove();
456
- this.currentRoot = null;
457
- }
458
- }
459
- }
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from "./NetworkedDOMWebsocket";
2
- export * from "./DOMSanitizer";