@meshagent/meshagent 0.0.11 → 0.0.13

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 (127) hide show
  1. package/dist/browser/agent-client.d.ts +88 -0
  2. package/dist/browser/agent-client.js +174 -0
  3. package/dist/browser/agent.d.ts +103 -0
  4. package/dist/browser/agent.js +229 -0
  5. package/dist/browser/client.d.ts +90 -0
  6. package/dist/browser/client.js +449 -0
  7. package/dist/browser/completer.d.ts +9 -0
  8. package/dist/browser/completer.js +25 -0
  9. package/dist/browser/data-types.d.ts +44 -0
  10. package/dist/browser/data-types.js +119 -0
  11. package/dist/browser/database-client.d.ts +77 -0
  12. package/dist/browser/database-client.js +113 -0
  13. package/dist/browser/developer-client.d.ts +13 -0
  14. package/dist/browser/developer-client.js +35 -0
  15. package/dist/browser/document.d.ts +84 -0
  16. package/dist/browser/document.js +531 -0
  17. package/dist/browser/entrypoint.d.ts +49722 -0
  18. package/dist/browser/entrypoint.js +8659 -0
  19. package/dist/browser/event-emitter.d.ts +13 -0
  20. package/dist/browser/event-emitter.js +38 -0
  21. package/dist/browser/helpers.d.ts +26 -0
  22. package/dist/browser/helpers.js +79 -0
  23. package/dist/browser/index.d.ts +24 -0
  24. package/dist/browser/index.js +40 -0
  25. package/dist/browser/messaging-client.d.ts +76 -0
  26. package/dist/browser/messaging-client.js +248 -0
  27. package/dist/browser/participant-token.d.ts +36 -0
  28. package/dist/browser/participant-token.js +104 -0
  29. package/dist/browser/participant.d.ts +18 -0
  30. package/dist/browser/participant.js +42 -0
  31. package/dist/browser/protocol.d.ts +87 -0
  32. package/dist/browser/protocol.js +305 -0
  33. package/dist/browser/queues-client.d.ts +26 -0
  34. package/dist/browser/queues-client.js +47 -0
  35. package/dist/browser/requirement.d.ts +25 -0
  36. package/dist/browser/requirement.js +49 -0
  37. package/dist/browser/response.d.ts +60 -0
  38. package/dist/browser/response.js +138 -0
  39. package/dist/browser/room-client.d.ts +43 -0
  40. package/dist/browser/room-client.js +109 -0
  41. package/dist/browser/room-event.d.ts +60 -0
  42. package/dist/browser/room-event.js +82 -0
  43. package/dist/browser/room-server-client.d.ts +19 -0
  44. package/dist/browser/room-server-client.js +50 -0
  45. package/dist/browser/runtime.d.ts +6 -0
  46. package/dist/browser/runtime.js +8 -0
  47. package/dist/browser/schema.d.ts +83 -0
  48. package/dist/browser/schema.js +321 -0
  49. package/dist/browser/storage-client.d.ts +38 -0
  50. package/dist/browser/storage-client.js +86 -0
  51. package/dist/browser/stream-controller.d.ts +8 -0
  52. package/dist/browser/stream-controller.js +55 -0
  53. package/dist/browser/sync-client.d.ts +33 -0
  54. package/dist/browser/sync-client.js +123 -0
  55. package/dist/browser/utils.d.ts +14 -0
  56. package/dist/browser/utils.js +59 -0
  57. package/dist/node/agent-client.d.ts +88 -0
  58. package/dist/node/agent-client.js +174 -0
  59. package/dist/node/agent.d.ts +103 -0
  60. package/dist/node/agent.js +229 -0
  61. package/dist/node/client.d.ts +90 -0
  62. package/dist/node/client.js +449 -0
  63. package/dist/node/completer.d.ts +9 -0
  64. package/dist/node/completer.js +25 -0
  65. package/dist/node/data-types.d.ts +44 -0
  66. package/dist/node/data-types.js +119 -0
  67. package/dist/node/database-client.d.ts +77 -0
  68. package/dist/node/database-client.js +113 -0
  69. package/dist/node/developer-client.d.ts +13 -0
  70. package/dist/node/developer-client.js +35 -0
  71. package/dist/node/document.d.ts +84 -0
  72. package/dist/node/document.js +531 -0
  73. package/dist/node/entrypoint.d.ts +49722 -0
  74. package/dist/node/entrypoint.js +6321 -0
  75. package/dist/node/event-emitter.d.ts +13 -0
  76. package/dist/node/event-emitter.js +38 -0
  77. package/dist/node/helpers.d.ts +26 -0
  78. package/dist/node/helpers.js +79 -0
  79. package/dist/node/index.d.ts +24 -0
  80. package/dist/node/index.js +40 -0
  81. package/dist/node/messaging-client.d.ts +76 -0
  82. package/dist/node/messaging-client.js +248 -0
  83. package/dist/node/participant-token.d.ts +36 -0
  84. package/dist/node/participant-token.js +104 -0
  85. package/dist/node/participant.d.ts +18 -0
  86. package/dist/node/participant.js +42 -0
  87. package/dist/node/protocol.d.ts +87 -0
  88. package/dist/node/protocol.js +305 -0
  89. package/dist/node/queues-client.d.ts +26 -0
  90. package/dist/node/queues-client.js +47 -0
  91. package/dist/node/requirement.d.ts +25 -0
  92. package/dist/node/requirement.js +49 -0
  93. package/dist/node/response.d.ts +60 -0
  94. package/dist/node/response.js +138 -0
  95. package/dist/node/room-client.d.ts +43 -0
  96. package/dist/node/room-client.js +109 -0
  97. package/dist/node/room-event.d.ts +60 -0
  98. package/dist/node/room-event.js +82 -0
  99. package/dist/node/room-server-client.d.ts +19 -0
  100. package/dist/node/room-server-client.js +50 -0
  101. package/dist/node/runtime.d.ts +6 -0
  102. package/dist/node/runtime.js +8 -0
  103. package/dist/node/schema.d.ts +83 -0
  104. package/dist/node/schema.js +321 -0
  105. package/dist/node/storage-client.d.ts +38 -0
  106. package/dist/node/storage-client.js +86 -0
  107. package/dist/node/stream-controller.d.ts +8 -0
  108. package/dist/node/stream-controller.js +55 -0
  109. package/dist/node/sync-client.d.ts +33 -0
  110. package/dist/node/sync-client.js +123 -0
  111. package/dist/node/utils.d.ts +14 -0
  112. package/dist/node/utils.js +59 -0
  113. package/package.json +35 -22
  114. package/README.md +0 -0
  115. package/babel.config.cjs +0 -3
  116. package/client.js +0 -422
  117. package/dist/entrypoint.js +0 -11262
  118. package/dist/entrypoint.js.map +0 -7
  119. package/document-server-client.js +0 -246
  120. package/entrypoint.js +0 -11568
  121. package/index.js +0 -2
  122. package/protocol.js +0 -459
  123. package/server.js +0 -310
  124. package/test/assert.js +0 -19
  125. package/test/document-server-client.test.js +0 -33
  126. package/test/document.test.js +0 -313
  127. package/test/protocol.test.js +0 -64
@@ -1,246 +0,0 @@
1
- import { ClientXmlDocument } from "./client";
2
- import { mergeUint8Arrays, ProtocolMessageStream } from "./protocol";
3
- import { v4 } from "uuid";
4
- import { TextEncoder, TextDecoder } from "@kayahr/text-encoding";
5
-
6
- const encoder = new TextEncoder('utf-8');
7
- const decoder = new TextDecoder('utf-8');
8
- /**
9
- * @param {Uint8Array} [packet] - a data packet to split
10
- * @return {Uint8Array}
11
- */
12
- export function splitMessagePayload(packet) {
13
- const dataView = new DataView(packet.buffer);
14
- const headerSize = dataView.getUint32(4, false) + dataView.getUint32(0, false) * Math.pow(2, 32);
15
-
16
- const payload = packet.subarray(8+headerSize, packet.length);
17
- return payload;
18
- }
19
-
20
- /**
21
- * @param {Uint8Array} [packet] - a data packet to split
22
- * @return {String}
23
- */
24
- export function splitMessageHeader(packet) {
25
- const dataView = new DataView(packet.buffer);
26
- const headerSize = dataView.getUint32(4, false) + dataView.getUint32(0, false) * Math.pow(2, 32);
27
- const payload = packet.subarray(8, 8+headerSize);
28
- return decoder.decode(payload);
29
- }
30
-
31
- /**
32
- * @param {Object} [request] - a javascript object with the request header
33
- * @param {Uint8Array?} [data] - An optional data packet to add to the message
34
- * @return {Uint8Array}
35
- */
36
- export function packMessage(request, data) {
37
- const jsonMessage = encoder.encode(JSON.stringify(request));
38
-
39
- const size = jsonMessage.length;
40
-
41
- const header = new Uint8Array(4*2);
42
- const dataView = new DataView(header.buffer);
43
- dataView.setUint32(0, (size & 0x000fffff00000000) / Math.pow(2, 32), false);
44
- dataView.setUint32(4, size & 0xffffffff, false);
45
-
46
- return mergeUint8Arrays(
47
- header,
48
- jsonMessage,
49
- data ? data : new Uint8Array(0)
50
- );
51
- }
52
-
53
- class Completer {
54
- constructor() {
55
- const self = this;
56
- this.fut = new Promise((resolve, reject) => {
57
- self.resolve = resolve;
58
- self.reject = reject;
59
- });
60
- }
61
- }
62
-
63
- class _PendingRequest {
64
- /**
65
- * @param {Completer} [fut] - a javascript promise that will be r
66
- */
67
- constructor() {
68
- this._completer = new Completer();
69
- }
70
-
71
- get fut() {
72
- return this._completer.fut;
73
- }
74
-
75
- }
76
-
77
-
78
- class _QueuedSync {
79
- /**
80
- * @param {Object} params - the parameters
81
- * @param {string} params.path - the path of the document
82
- * @param {string} params.base64 - base64 data representing the change
83
- */
84
- constructor({ path, base64 }) {
85
- this.path = path;
86
- this.base64 = base64;
87
- }
88
- }
89
-
90
- export class RoomClient {
91
-
92
- /**
93
- * @param {Object} params - the parameters
94
- * @param {Protocol} params.protocol - the protocol to use
95
- * @param {DocumentRuntime} params.runtime - the document runtime
96
- */
97
- constructor({ protocol, runtime }) {
98
- this.protocol = protocol;
99
-
100
- protocol.addHandler("room.sync", this._handleSync.bind(this));
101
- protocol.addHandler("__response__", this._handleResponse.bind(this));
102
- this._connectedDocuments = new Map();
103
- this._pendingRequests = new Map();
104
- this._changesToSync = new ProtocolMessageStream();
105
- this.runtime = runtime;
106
- }
107
-
108
-
109
- start() {
110
- this.protocol.start();
111
- (async () => {
112
- for await (let message of this._changesToSync.stream()) {
113
- const base64 = JSON.parse(message.base64).data;
114
- console.log(`sending changes to backend ${base64}`);
115
- await this.sendRequest("room.sync", {"path":message.path}, encoder.encode(base64));
116
- }
117
- })();
118
- }
119
-
120
- dispose() {
121
- this.protocol.dispose();
122
- this._changesToSync.close();
123
- }
124
-
125
-
126
- // send a request, optionally with a binary trailer
127
- /**
128
- * @param {string} type - the type of the request
129
- * @param {Object} request - the request header
130
- * @param {Uint8Array?} data - additional data for the request
131
- * @returns {Promise<Object>} - the response
132
- */
133
- async sendRequest(type, request, data) {
134
- const requestId = this.protocol.getNextMessageId();
135
-
136
- const pr = new _PendingRequest();
137
- this._pendingRequests.set(requestId, pr);
138
-
139
- const message = packMessage(request, data);
140
-
141
- await this.protocol.send(type, message, requestId);
142
- return await pr.fut;
143
- }
144
-
145
- /**
146
- * @param {string} path - the path of the document to connect to
147
- */
148
- async connect(path) {
149
- if (this._connectedDocuments.has(path)) {
150
- throw new Error("Already connected to"+path);
151
- }
152
-
153
- // todo: add support for state vector / partial updates
154
- // todo: initial bytes loading
155
-
156
- const doc = new ClientXmlDocument({id: v4(), sendChanges: (changes) => {
157
- this.runtime.applyChanges(changes);
158
- }});
159
- this.runtime.registerDocument(doc.id, null, (base64) => {
160
- this._changesToSync.add(new _QueuedSync({ path, base64 }))
161
- }, (changes) => {
162
- const c = JSON.parse(changes);
163
- doc.receiveChanges(c.data);
164
- });
165
- this._connectedDocuments.set(path, doc);
166
- await this.sendRequest("room.connect", {"path":path});
167
- return doc;
168
- }
169
-
170
-
171
- /**
172
- * @param {string} path - the path of the document to connect to
173
- */
174
- async disconnect(path) {
175
- await this.sendRequest("room.disconnect", {"path":path});
176
-
177
- if (!this._connectedDocuments.has(path)) {
178
- throw new Error("Not connected to "+path);
179
- }
180
-
181
- const doc = this._connectedDocuments.remove(path);
182
- this.runtime.unregisterDocument(doc.id);
183
- doc.dispose();
184
- }
185
-
186
- /**
187
- * @param {string} path - the path of the document to connect to
188
- * @param {Uint8Array?} data - the data to sync
189
- */
190
- async sync(path, data) {
191
- await this.sendRequest("room.sync", {"path":path}, data);
192
- }
193
-
194
- /**
195
- * @param {Protocol} protocol - the path of the document to connect to
196
- * @param {number} messageId - the id of the message
197
- * @param {string} type - the type of the message
198
- * @param {Uint8Array} data - the message data
199
- */
200
- async _handleResponse(protocol, messageId, type, data) {
201
- console.log("GOT RESPONSE");
202
- const decoder = new TextDecoder("utf-8");
203
- const response = JSON.parse(decoder.decode(data));
204
- const requestId = response["request_id"];
205
-
206
- if(this._pendingRequests.has(requestId)) {
207
- const pr = this._pendingRequests.get(requestId);
208
- this._pendingRequests.delete(requestId);
209
- if(response["error"] != null) {
210
- pr._completer.reject(new Error(response["error"]));
211
- } else {
212
- pr._completer.resolve(response["response"]);
213
- }
214
- } else {
215
- // warning
216
- console.log(`received a response for a request that is not pending ${requestId}`);
217
- }
218
- return;
219
- }
220
-
221
- /**
222
- * @param {Protocol} protocol - the path of the document to connect to
223
- * @param {number} messageId - the id of the message
224
- * @param {string} type - the type of the message
225
- * @param {Uint8Array} bytes - the message data
226
- */
227
- async _handleSync(protocol, messageId, type, bytes) {
228
- const headerStr = splitMessageHeader(bytes);
229
- const payload = splitMessagePayload(bytes);
230
-
231
- console.log("GOT SYNC", headerStr);
232
-
233
- const header = JSON.parse(headerStr);
234
- const path = header["path"];
235
-
236
- if(this._connectedDocuments.has(path)) {
237
- const doc = this._connectedDocuments.get(path);
238
- const base64 = new TextDecoder("utf-8").decode(payload);
239
- console.log(`GOT SYNC ${base64}`);
240
- this.runtime.applyBackendChanges(doc.id, base64);
241
- } else {
242
- throw new Error("received change for a document that is not connected:" + path);
243
- }
244
- }
245
-
246
- }