@meshagent/meshagent 0.0.11 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/agent-client.d.ts +88 -0
- package/dist/browser/agent-client.js +174 -0
- package/dist/browser/agent.d.ts +103 -0
- package/dist/browser/agent.js +229 -0
- package/dist/browser/client.d.ts +90 -0
- package/dist/browser/client.js +449 -0
- package/dist/browser/completer.d.ts +9 -0
- package/dist/browser/completer.js +25 -0
- package/dist/browser/data-types.d.ts +44 -0
- package/dist/browser/data-types.js +119 -0
- package/dist/browser/database-client.d.ts +77 -0
- package/dist/browser/database-client.js +113 -0
- package/dist/browser/developer-client.d.ts +13 -0
- package/dist/browser/developer-client.js +35 -0
- package/dist/browser/document.d.ts +84 -0
- package/dist/browser/document.js +531 -0
- package/dist/browser/entrypoint.d.ts +49722 -0
- package/dist/browser/entrypoint.js +8659 -0
- package/dist/browser/event-emitter.d.ts +13 -0
- package/dist/browser/event-emitter.js +38 -0
- package/dist/browser/helpers.d.ts +26 -0
- package/dist/browser/helpers.js +79 -0
- package/dist/browser/index.d.ts +24 -0
- package/dist/browser/index.js +40 -0
- package/dist/browser/messaging-client.d.ts +76 -0
- package/dist/browser/messaging-client.js +248 -0
- package/dist/browser/participant-token.d.ts +36 -0
- package/dist/browser/participant-token.js +104 -0
- package/dist/browser/participant.d.ts +18 -0
- package/dist/browser/participant.js +42 -0
- package/dist/browser/protocol.d.ts +87 -0
- package/dist/browser/protocol.js +305 -0
- package/dist/browser/queues-client.d.ts +26 -0
- package/dist/browser/queues-client.js +47 -0
- package/dist/browser/requirement.d.ts +25 -0
- package/dist/browser/requirement.js +49 -0
- package/dist/browser/response.d.ts +60 -0
- package/dist/browser/response.js +138 -0
- package/dist/browser/room-client.d.ts +43 -0
- package/dist/browser/room-client.js +109 -0
- package/dist/browser/room-event.d.ts +60 -0
- package/dist/browser/room-event.js +82 -0
- package/dist/browser/room-server-client.d.ts +19 -0
- package/dist/browser/room-server-client.js +50 -0
- package/dist/browser/runtime.d.ts +6 -0
- package/dist/browser/runtime.js +8 -0
- package/dist/browser/schema.d.ts +83 -0
- package/dist/browser/schema.js +321 -0
- package/dist/browser/storage-client.d.ts +38 -0
- package/dist/browser/storage-client.js +86 -0
- package/dist/browser/stream-controller.d.ts +8 -0
- package/dist/browser/stream-controller.js +55 -0
- package/dist/browser/sync-client.d.ts +33 -0
- package/dist/browser/sync-client.js +123 -0
- package/dist/browser/utils.d.ts +14 -0
- package/dist/browser/utils.js +59 -0
- package/dist/node/agent-client.d.ts +88 -0
- package/dist/node/agent-client.js +174 -0
- package/dist/node/agent.d.ts +103 -0
- package/dist/node/agent.js +229 -0
- package/dist/node/client.d.ts +90 -0
- package/dist/node/client.js +449 -0
- package/dist/node/completer.d.ts +9 -0
- package/dist/node/completer.js +25 -0
- package/dist/node/data-types.d.ts +44 -0
- package/dist/node/data-types.js +119 -0
- package/dist/node/database-client.d.ts +77 -0
- package/dist/node/database-client.js +113 -0
- package/dist/node/developer-client.d.ts +13 -0
- package/dist/node/developer-client.js +35 -0
- package/dist/node/document.d.ts +84 -0
- package/dist/node/document.js +531 -0
- package/dist/node/entrypoint.d.ts +49722 -0
- package/dist/node/entrypoint.js +6321 -0
- package/dist/node/event-emitter.d.ts +13 -0
- package/dist/node/event-emitter.js +38 -0
- package/dist/node/helpers.d.ts +26 -0
- package/dist/node/helpers.js +79 -0
- package/dist/node/index.d.ts +24 -0
- package/dist/node/index.js +40 -0
- package/dist/node/messaging-client.d.ts +76 -0
- package/dist/node/messaging-client.js +248 -0
- package/dist/node/participant-token.d.ts +36 -0
- package/dist/node/participant-token.js +104 -0
- package/dist/node/participant.d.ts +18 -0
- package/dist/node/participant.js +42 -0
- package/dist/node/protocol.d.ts +87 -0
- package/dist/node/protocol.js +305 -0
- package/dist/node/queues-client.d.ts +26 -0
- package/dist/node/queues-client.js +47 -0
- package/dist/node/requirement.d.ts +25 -0
- package/dist/node/requirement.js +49 -0
- package/dist/node/response.d.ts +60 -0
- package/dist/node/response.js +138 -0
- package/dist/node/room-client.d.ts +43 -0
- package/dist/node/room-client.js +109 -0
- package/dist/node/room-event.d.ts +60 -0
- package/dist/node/room-event.js +82 -0
- package/dist/node/room-server-client.d.ts +19 -0
- package/dist/node/room-server-client.js +50 -0
- package/dist/node/runtime.d.ts +6 -0
- package/dist/node/runtime.js +8 -0
- package/dist/node/schema.d.ts +83 -0
- package/dist/node/schema.js +321 -0
- package/dist/node/storage-client.d.ts +38 -0
- package/dist/node/storage-client.js +86 -0
- package/dist/node/stream-controller.d.ts +8 -0
- package/dist/node/stream-controller.js +55 -0
- package/dist/node/sync-client.d.ts +33 -0
- package/dist/node/sync-client.js +123 -0
- package/dist/node/utils.d.ts +14 -0
- package/dist/node/utils.js +59 -0
- package/package.json +36 -22
- package/README.md +0 -0
- package/babel.config.cjs +0 -3
- package/client.js +0 -422
- package/dist/entrypoint.js +0 -11262
- package/dist/entrypoint.js.map +0 -7
- package/document-server-client.js +0 -246
- package/entrypoint.js +0 -11568
- package/index.js +0 -2
- package/protocol.js +0 -459
- package/server.js +0 -310
- package/test/assert.js +0 -19
- package/test/document-server-client.test.js +0 -33
- package/test/document.test.js +0 -313
- 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
|
-
}
|