@meshagent/meshagent 0.3.1 → 0.4.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.
Files changed (84) hide show
  1. package/CHANGELOG.md +3 -0
  2. package/dist/browser/entrypoint.js +7956 -5611
  3. package/dist/browser/helpers.d.ts +10 -4
  4. package/dist/browser/helpers.js +14 -39
  5. package/dist/browser/index.d.ts +1 -0
  6. package/dist/browser/index.js +1 -0
  7. package/dist/browser/participant-token.d.ts +4 -4
  8. package/dist/browser/participant-token.js +4 -9
  9. package/dist/browser/protocol.d.ts +5 -1
  10. package/dist/browser/protocol.js +7 -2
  11. package/dist/browser/room-client.d.ts +4 -1
  12. package/dist/browser/room-client.js +2 -2
  13. package/dist/browser/sync-client.d.ts +5 -1
  14. package/dist/browser/sync-client.js +13 -2
  15. package/dist/esm/agent-client.d.ts +88 -0
  16. package/dist/esm/agent-client.js +166 -0
  17. package/dist/esm/agent.d.ts +103 -0
  18. package/dist/esm/agent.js +218 -0
  19. package/dist/esm/client.d.ts +90 -0
  20. package/dist/esm/client.js +443 -0
  21. package/dist/esm/completer.d.ts +9 -0
  22. package/dist/esm/completer.js +21 -0
  23. package/dist/esm/data-types.d.ts +44 -0
  24. package/dist/esm/data-types.js +110 -0
  25. package/dist/esm/database-client.d.ts +77 -0
  26. package/dist/esm/database-client.js +109 -0
  27. package/dist/esm/developer-client.d.ts +13 -0
  28. package/dist/esm/developer-client.js +31 -0
  29. package/dist/esm/document.d.ts +84 -0
  30. package/dist/esm/document.js +522 -0
  31. package/dist/esm/entrypoint.d.ts +49722 -0
  32. package/dist/esm/entrypoint.js +6313 -0
  33. package/dist/esm/event-emitter.d.ts +13 -0
  34. package/dist/esm/event-emitter.js +34 -0
  35. package/dist/esm/helpers.d.ts +32 -0
  36. package/dist/esm/helpers.js +46 -0
  37. package/dist/esm/index.d.ts +25 -0
  38. package/dist/esm/index.js +25 -0
  39. package/dist/esm/messaging-client.d.ts +76 -0
  40. package/dist/esm/messaging-client.js +241 -0
  41. package/dist/esm/participant-token.d.ts +36 -0
  42. package/dist/esm/participant-token.js +91 -0
  43. package/dist/esm/participant.d.ts +18 -0
  44. package/dist/esm/participant.js +36 -0
  45. package/dist/esm/protocol.d.ts +91 -0
  46. package/dist/esm/protocol.js +287 -0
  47. package/dist/esm/queues-client.d.ts +26 -0
  48. package/dist/esm/queues-client.js +42 -0
  49. package/dist/esm/requirement.d.ts +25 -0
  50. package/dist/esm/requirement.js +42 -0
  51. package/dist/esm/response.d.ts +60 -0
  52. package/dist/esm/response.js +128 -0
  53. package/dist/esm/room-client.d.ts +46 -0
  54. package/dist/esm/room-client.js +106 -0
  55. package/dist/esm/room-event.d.ts +60 -0
  56. package/dist/esm/room-event.js +72 -0
  57. package/dist/esm/room-server-client.d.ts +19 -0
  58. package/dist/esm/room-server-client.js +45 -0
  59. package/dist/esm/runtime.d.ts +6 -0
  60. package/dist/esm/runtime.js +1 -0
  61. package/dist/esm/schema.d.ts +83 -0
  62. package/dist/esm/schema.js +312 -0
  63. package/dist/esm/storage-client.d.ts +38 -0
  64. package/dist/esm/storage-client.js +79 -0
  65. package/dist/esm/stream-controller.d.ts +8 -0
  66. package/dist/esm/stream-controller.js +51 -0
  67. package/dist/esm/sync-client.d.ts +37 -0
  68. package/dist/esm/sync-client.js +125 -0
  69. package/dist/esm/utils.d.ts +14 -0
  70. package/dist/esm/utils.js +44 -0
  71. package/dist/node/entrypoint.js +9 -4
  72. package/dist/node/helpers.d.ts +10 -4
  73. package/dist/node/helpers.js +14 -39
  74. package/dist/node/index.d.ts +1 -0
  75. package/dist/node/index.js +1 -0
  76. package/dist/node/participant-token.d.ts +4 -4
  77. package/dist/node/participant-token.js +4 -9
  78. package/dist/node/protocol.d.ts +5 -1
  79. package/dist/node/protocol.js +7 -2
  80. package/dist/node/room-client.d.ts +4 -1
  81. package/dist/node/room-client.js +2 -2
  82. package/dist/node/sync-client.d.ts +5 -1
  83. package/dist/node/sync-client.js +13 -2
  84. package/package.json +4 -3
@@ -0,0 +1,522 @@
1
+ import { v4 as uuid } from "uuid";
2
+ import { EventEmitter } from "./event-emitter";
3
+ export class RuntimeDocument extends EventEmitter {
4
+ constructor({ id, schema, sendChanges, sendChangesToBackend }) {
5
+ super();
6
+ this._changeSubscribers = [];
7
+ this.id = id;
8
+ this.schema = schema;
9
+ this.sendChanges = sendChanges;
10
+ this.sendChangesToBackend = sendChangesToBackend;
11
+ }
12
+ listen(onData) {
13
+ this._changeSubscribers.push(onData);
14
+ return {
15
+ unsubscribe: () => {
16
+ const idx = this._changeSubscribers.indexOf(onData);
17
+ if (idx >= 0) {
18
+ this._changeSubscribers.splice(idx, 1);
19
+ }
20
+ },
21
+ };
22
+ }
23
+ get root() {
24
+ if (!this._root) {
25
+ this._root = new Element({
26
+ parent: null,
27
+ tagName: this.schema.root.tagName,
28
+ attributes: {},
29
+ doc: this,
30
+ elementType: this.schema.root,
31
+ });
32
+ }
33
+ return this._root;
34
+ }
35
+ _createNode(parent, data) {
36
+ if (data["element"] != null) {
37
+ const elementData = data["element"];
38
+ const tagName = elementData["tagName"];
39
+ const elementType = this.schema.element(tagName);
40
+ const element = new Element({
41
+ parent,
42
+ tagName,
43
+ attributes: elementData["attributes"] || {},
44
+ doc: this,
45
+ elementType,
46
+ });
47
+ if (elementData["children"] != null) {
48
+ for (const child of elementData["children"]) {
49
+ element.children.push(this._createNode(element, child));
50
+ }
51
+ }
52
+ return element;
53
+ }
54
+ else if (data["text"] != null) {
55
+ const delta = Array.isArray(data["text"]["delta"])
56
+ ? data["text"]["delta"]
57
+ : [];
58
+ return new TextElement({
59
+ parent: parent,
60
+ delta,
61
+ doc: this,
62
+ });
63
+ }
64
+ else {
65
+ throw new Error("Unsupported node type");
66
+ }
67
+ }
68
+ receiveChanges(message) {
69
+ for (const sub of this._changeSubscribers) {
70
+ sub(message);
71
+ }
72
+ console.log("Applying changes to doc:", JSON.stringify(message));
73
+ const nodeID = message["target"];
74
+ const target = message["root"] === true ? this.root : this.root.getNodeByID(nodeID);
75
+ if (!target) {
76
+ throw new Error(`Target node not found: ${nodeID}`);
77
+ }
78
+ let retain = 0;
79
+ const elements = message["elements"] || [];
80
+ for (const delta of elements) {
81
+ if (delta["retain"] != null) {
82
+ retain += delta["retain"];
83
+ }
84
+ if (delta["insert"] != null) {
85
+ for (const insert of delta["insert"]) {
86
+ if (insert["element"] != null || insert["text"] != null) {
87
+ target.children.splice(retain, 0, this._createNode(target, insert));
88
+ retain++;
89
+ }
90
+ else {
91
+ throw new Error("Unsupported element delta");
92
+ }
93
+ }
94
+ }
95
+ else if (delta["delete"] != null) {
96
+ target.children.splice(retain, delta["delete"]);
97
+ retain -= delta["delete"];
98
+ }
99
+ }
100
+ const text = message["text"];
101
+ if (text && text.length > 0) {
102
+ if (target.tagName !== "text") {
103
+ throw new Error("Node is not a text node: " + target.tagName);
104
+ }
105
+ const textNode = target.children[0];
106
+ let retainInner = 0;
107
+ let i = 0;
108
+ let offset = 0;
109
+ const targetDelta = textNode.delta;
110
+ for (const delta of text) {
111
+ if (delta["insert"] != null) {
112
+ const insStr = delta["insert"];
113
+ if (i === targetDelta.length) {
114
+ targetDelta.push({
115
+ insert: insStr,
116
+ attributes: delta["attributes"] ?? {},
117
+ });
118
+ i++;
119
+ offset += insStr.length;
120
+ retainInner += insStr.length;
121
+ }
122
+ else {
123
+ const str = targetDelta[i]["insert"];
124
+ const pos = (retainInner - offset) | 0;
125
+ targetDelta[i]["insert"] = str.slice(0, pos) + insStr + str.slice(pos);
126
+ retainInner += insStr.length;
127
+ }
128
+ }
129
+ else if (delta["delete"] != null) {
130
+ let deleted = 0;
131
+ while (delta["delete"] > deleted) {
132
+ const remaining = delta["delete"] - deleted;
133
+ const str = targetDelta[i]?.["insert"];
134
+ if (typeof str !== "string") {
135
+ break;
136
+ }
137
+ if (retainInner > offset) {
138
+ const startPos = (retainInner - offset) | 0;
139
+ const start = str.slice(0, startPos);
140
+ const end = str.slice(startPos);
141
+ if (remaining >= end.length) {
142
+ targetDelta[i]["insert"] = start;
143
+ deleted += end.length;
144
+ i++;
145
+ offset += str.length;
146
+ }
147
+ else {
148
+ targetDelta[i]["insert"] = start + end.slice(remaining);
149
+ deleted += remaining;
150
+ }
151
+ }
152
+ else if (remaining >= str.length) {
153
+ deleted += str.length;
154
+ targetDelta.splice(i, 1);
155
+ }
156
+ else {
157
+ const start = str.slice(0, remaining);
158
+ const end = str.slice(remaining);
159
+ targetDelta[i]["insert"] = end;
160
+ deleted += start.length;
161
+ }
162
+ }
163
+ }
164
+ else if (delta["attributes"] != null) {
165
+ let formatted = 0;
166
+ const retainVal = delta["retain"] ?? 0;
167
+ while (retainVal > formatted) {
168
+ const remaining = (retainVal - formatted) | 0;
169
+ if (!targetDelta[i]["attributes"]) {
170
+ targetDelta[i]["attributes"] = {};
171
+ }
172
+ const str = targetDelta[i]["insert"];
173
+ const startPos = (retainInner - offset) | 0;
174
+ const start = str.slice(0, startPos);
175
+ const end = str.slice(startPos);
176
+ if (retainInner > offset) {
177
+ if (remaining >= end.length) {
178
+ targetDelta[i]["insert"] = start;
179
+ targetDelta.splice(i + 1, 0, {
180
+ insert: end,
181
+ attributes: {
182
+ ...targetDelta[i]["attributes"],
183
+ ...delta["attributes"],
184
+ },
185
+ });
186
+ formatted += end.length;
187
+ i += 2;
188
+ offset += str.length;
189
+ }
190
+ else {
191
+ targetDelta[i]["insert"] = start;
192
+ const middle = end.slice(0, remaining);
193
+ const tail = end.slice(remaining);
194
+ targetDelta.splice(i + 1, 0, {
195
+ insert: middle,
196
+ attributes: {
197
+ ...targetDelta[i]["attributes"],
198
+ ...delta["attributes"],
199
+ },
200
+ });
201
+ targetDelta.splice(i + 2, 0, {
202
+ insert: tail,
203
+ attributes: { ...targetDelta[i]["attributes"] },
204
+ });
205
+ formatted += remaining;
206
+ i += 3;
207
+ offset += start.length + remaining;
208
+ }
209
+ }
210
+ else if (remaining >= str.length) {
211
+ formatted += str.length;
212
+ Object.assign(targetDelta[i]["attributes"], delta["attributes"]);
213
+ offset += str.length;
214
+ i++;
215
+ }
216
+ else {
217
+ const startChunk = str.slice(0, remaining);
218
+ const endChunk = str.slice(remaining);
219
+ targetDelta[i]["insert"] = startChunk;
220
+ targetDelta.push({
221
+ insert: endChunk,
222
+ attributes: { ...targetDelta[i]["attributes"] },
223
+ });
224
+ Object.assign(targetDelta[i]["attributes"], delta["attributes"]);
225
+ formatted += remaining;
226
+ }
227
+ }
228
+ retainInner += retainVal;
229
+ }
230
+ else if (delta["retain"] != null) {
231
+ retainInner += delta["retain"];
232
+ while (i < targetDelta.length &&
233
+ retainInner > offset + (targetDelta[i]["insert"]?.length ?? 0)) {
234
+ offset += targetDelta[i]["insert"].length;
235
+ i++;
236
+ }
237
+ }
238
+ }
239
+ }
240
+ const attr = message["attributes"];
241
+ if (attr) {
242
+ const setList = attr["set"] || [];
243
+ for (const change of setList) {
244
+ target.attributes[change["name"]] = change["value"];
245
+ target.emit("updated", { type: "change", node: target });
246
+ }
247
+ const delList = attr["delete"] || [];
248
+ for (const name of delList) {
249
+ delete target.attributes[name];
250
+ target.emit("updated", { type: "change", node: target });
251
+ }
252
+ }
253
+ this.emit("updated", { type: "change", doc: this });
254
+ }
255
+ }
256
+ export class Node extends EventEmitter {
257
+ constructor({ parent, doc }) {
258
+ super();
259
+ this.parent = parent;
260
+ this.doc = doc;
261
+ }
262
+ }
263
+ export class Element extends Node {
264
+ constructor({ parent, tagName, attributes, doc, elementType }) {
265
+ super({ parent, doc });
266
+ this.children = [];
267
+ this.tagName = tagName;
268
+ this.attributes = attributes;
269
+ this.elementType = elementType;
270
+ }
271
+ getNodeByID(id) {
272
+ if (id === this.id) {
273
+ return this;
274
+ }
275
+ for (const child of this.getChildren()) {
276
+ if (child instanceof Element) {
277
+ const found = child.getNodeByID(id);
278
+ if (found) {
279
+ return found;
280
+ }
281
+ }
282
+ }
283
+ return null;
284
+ }
285
+ get id() {
286
+ return this.getAttribute("$id");
287
+ }
288
+ getAttribute(name) {
289
+ return this.attributes[name];
290
+ }
291
+ setAttribute(name, value) {
292
+ this.doc.sendChanges({
293
+ documentID: this.doc.id,
294
+ changes: [
295
+ {
296
+ nodeID: this.id,
297
+ setAttributes: { [name]: value },
298
+ },
299
+ ],
300
+ });
301
+ }
302
+ removeAttribute(name) {
303
+ this.doc.sendChanges({
304
+ documentID: this.doc.id,
305
+ changes: [
306
+ {
307
+ nodeID: this.id,
308
+ removeAttributes: [name],
309
+ },
310
+ ],
311
+ });
312
+ }
313
+ _ensureChildValid(tagName) {
314
+ const childName = this.elementType.childPropertyName;
315
+ if (!childName) {
316
+ throw new Error(`Children are not allowed on this element: ${this.tagName}`);
317
+ }
318
+ const childProp = this.elementType.property(childName);
319
+ if (!childProp.isTagAllowed(tagName)) {
320
+ throw new Error(`Cannot add ${tagName} to ${this.tagName}`);
321
+ }
322
+ return this.doc.schema.element(tagName);
323
+ }
324
+ _validateElementAttributes(elType, attributes) {
325
+ for (const k of Object.keys(attributes)) {
326
+ elType.property(k);
327
+ }
328
+ }
329
+ createChildElement(tagName, attributes, opts) {
330
+ const childElementType = this._ensureChildValid(tagName);
331
+ this._validateElementAttributes(childElementType, attributes);
332
+ const newId = opts?.id ?? uuid();
333
+ const elementData = {
334
+ name: tagName,
335
+ attributes: {
336
+ $id: newId,
337
+ ...attributes,
338
+ },
339
+ children: this._defaultChildren(tagName),
340
+ };
341
+ this.doc.sendChanges({
342
+ documentID: this.doc.id,
343
+ changes: [
344
+ {
345
+ nodeID: this.id,
346
+ insertChildren: {
347
+ children: [{ element: elementData }],
348
+ },
349
+ },
350
+ ],
351
+ });
352
+ return this.getNodeByID(newId);
353
+ }
354
+ createChildElementAt(index, tagName, attributes, opts) {
355
+ const childElementType = this._ensureChildValid(tagName);
356
+ this._validateElementAttributes(childElementType, attributes);
357
+ const newId = opts?.id ?? uuid();
358
+ const elementData = {
359
+ name: tagName,
360
+ attributes: {
361
+ $id: newId,
362
+ ...attributes,
363
+ },
364
+ children: this._defaultChildren(tagName),
365
+ };
366
+ this.doc.sendChanges({
367
+ documentID: this.doc.id,
368
+ changes: [
369
+ {
370
+ nodeID: this.id,
371
+ insertChildren: {
372
+ index,
373
+ children: [{ element: elementData }],
374
+ },
375
+ },
376
+ ],
377
+ });
378
+ return this.getNodeByID(newId);
379
+ }
380
+ createChildElementAfter(element, tagName, attributes, opts) {
381
+ const childElementType = this._ensureChildValid(tagName);
382
+ this._validateElementAttributes(childElementType, attributes);
383
+ if (element.parent?.id !== this.id) {
384
+ throw new Error("Element does not belong to this node");
385
+ }
386
+ const newId = opts?.id ?? uuid();
387
+ const elementData = {
388
+ name: tagName,
389
+ attributes: {
390
+ $id: newId,
391
+ ...attributes,
392
+ },
393
+ children: this._defaultChildren(tagName),
394
+ };
395
+ this.doc.sendChanges({
396
+ documentID: this.doc.id,
397
+ changes: [
398
+ {
399
+ nodeID: this.id,
400
+ insertChildren: {
401
+ after: element.id,
402
+ children: [{ element: elementData }],
403
+ },
404
+ },
405
+ ],
406
+ });
407
+ return this.getNodeByID(newId);
408
+ }
409
+ _defaultChildren(tagName) {
410
+ if (tagName === "text") {
411
+ return [
412
+ {
413
+ text: { delta: [] },
414
+ },
415
+ ];
416
+ }
417
+ return [];
418
+ }
419
+ delete() {
420
+ this.doc.sendChanges({
421
+ documentID: this.doc.id,
422
+ changes: [
423
+ {
424
+ nodeID: this.id,
425
+ delete: {},
426
+ },
427
+ ],
428
+ });
429
+ }
430
+ getChildren() {
431
+ return this.children;
432
+ }
433
+ appendJson(json) {
434
+ const name = tagNameFromJson(json);
435
+ const attrs = attributesFromJson(json);
436
+ const elType = this.doc.schema.element(name);
437
+ if (elType.childPropertyName && attrs.hasOwnProperty(elType.childPropertyName)) {
438
+ const childArr = attrs[elType.childPropertyName];
439
+ delete attrs[elType.childPropertyName];
440
+ const elem = this.createChildElement(name, attrs);
441
+ if (Array.isArray(childArr)) {
442
+ for (const c of childArr) {
443
+ elem.appendJson(c);
444
+ }
445
+ }
446
+ return elem;
447
+ }
448
+ else {
449
+ return this.createChildElement(name, attrs);
450
+ }
451
+ }
452
+ }
453
+ export class TextElement extends Node {
454
+ constructor({ parent, delta, doc }) {
455
+ super({ parent, doc });
456
+ this.delta = delta;
457
+ }
458
+ insert(index, text, attributes) {
459
+ this.doc.sendChanges({
460
+ documentID: this.doc.id,
461
+ changes: [
462
+ {
463
+ nodeID: this.parent?.id,
464
+ insertText: {
465
+ index,
466
+ text,
467
+ attributes: attributes ?? {},
468
+ },
469
+ },
470
+ ],
471
+ });
472
+ }
473
+ format(from, length, attributes) {
474
+ this.doc.sendChanges({
475
+ documentID: this.doc.id,
476
+ changes: [
477
+ {
478
+ nodeID: this.parent?.id,
479
+ formatText: {
480
+ from,
481
+ length,
482
+ attributes,
483
+ },
484
+ },
485
+ ],
486
+ });
487
+ }
488
+ delete(index, length) {
489
+ this.doc.sendChanges({
490
+ documentID: this.doc.id,
491
+ changes: [
492
+ {
493
+ nodeID: this.parent?.id,
494
+ deleteText: {
495
+ index,
496
+ length,
497
+ },
498
+ },
499
+ ],
500
+ });
501
+ }
502
+ }
503
+ export function tagNameFromJson(json) {
504
+ const keys = Object.keys(json);
505
+ if (keys.length !== 1) {
506
+ throw new Error("JSON element must have a single key");
507
+ }
508
+ return keys[0];
509
+ }
510
+ export function attributesFromJson(json) {
511
+ const keys = Object.keys(json);
512
+ if (keys.length !== 1) {
513
+ throw new Error("JSON element must have a single key");
514
+ }
515
+ const val = json[keys[0]];
516
+ if (typeof val === "object" && val !== null && !Array.isArray(val)) {
517
+ return { ...val };
518
+ }
519
+ else {
520
+ throw new Error("JSON element value must be an object");
521
+ }
522
+ }