@meshagent/meshagent 0.42.2 → 0.43.1
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/CHANGELOG.md +13 -0
- package/dist/browser/agent-client.d.ts +4 -4
- package/dist/browser/agent-client.js +16 -16
- package/dist/browser/agent.d.ts +6 -6
- package/dist/browser/agent.js +57 -57
- package/dist/browser/api_keys.js +7 -3
- package/dist/browser/containers-client.d.ts +3 -3
- package/dist/browser/containers-client.js +76 -76
- package/dist/browser/datasets-client.d.ts +1 -1
- package/dist/browser/datasets-client.js +83 -83
- package/dist/browser/developer-client.d.ts +3 -3
- package/dist/browser/developer-client.js +11 -11
- package/dist/browser/document.d.ts +2 -2
- package/dist/browser/document.js +3 -3
- package/dist/browser/helpers.d.ts +4 -4
- package/dist/browser/helpers.js +9 -9
- package/dist/browser/index.d.ts +35 -35
- package/dist/browser/index.js +35 -35
- package/dist/browser/memory-client.d.ts +1 -1
- package/dist/browser/memory-client.js +6 -6
- package/dist/browser/meshagent-client.d.ts +1 -1
- package/dist/browser/meshagent-client.js +78 -78
- package/dist/browser/messaging-client.d.ts +4 -4
- package/dist/browser/messaging-client.js +33 -33
- package/dist/browser/participant-token.d.ts +1 -1
- package/dist/browser/participant-token.js +5 -5
- package/dist/browser/participant.d.ts +1 -1
- package/dist/browser/protocol.d.ts +7 -4
- package/dist/browser/protocol.js +36 -20
- package/dist/browser/queues-client.d.ts +1 -1
- package/dist/browser/queues-client.js +10 -10
- package/dist/browser/response.js +11 -11
- package/dist/browser/room-client.d.ts +23 -17
- package/dist/browser/room-client.js +138 -130
- package/dist/browser/room-server-client.d.ts +2 -2
- package/dist/browser/room-server-client.js +10 -10
- package/dist/browser/secrets-client.d.ts +3 -3
- package/dist/browser/secrets-client.js +30 -30
- package/dist/browser/services-client.d.ts +2 -2
- package/dist/browser/services-client.js +8 -8
- package/dist/browser/storage-client.d.ts +5 -5
- package/dist/browser/storage-client.js +30 -30
- package/dist/browser/sync-client.d.ts +4 -4
- package/dist/browser/sync-client.js +38 -38
- package/dist/browser/toolkit-config.d.ts +3 -3
- package/dist/esm/agent-client.d.ts +4 -4
- package/dist/esm/agent-client.js +19 -25
- package/dist/esm/agent.d.ts +6 -6
- package/dist/esm/agent.js +73 -93
- package/dist/esm/api_keys.js +12 -21
- package/dist/esm/client.js +7 -13
- package/dist/esm/completer.js +1 -5
- package/dist/esm/containers-client.d.ts +3 -3
- package/dist/esm/containers-client.js +82 -87
- package/dist/esm/datasets-client.d.ts +1 -1
- package/dist/esm/datasets-client.js +101 -111
- package/dist/esm/developer-client.d.ts +3 -3
- package/dist/esm/developer-client.js +11 -15
- package/dist/esm/document.d.ts +2 -2
- package/dist/esm/document.js +11 -20
- package/dist/esm/entrypoint.js +1 -12
- package/dist/esm/event-emitter.js +1 -5
- package/dist/esm/helpers.d.ts +4 -4
- package/dist/esm/helpers.js +15 -23
- package/dist/esm/index.d.ts +35 -35
- package/dist/esm/index.js +35 -51
- package/dist/esm/memory-client.d.ts +1 -1
- package/dist/esm/memory-client.js +7 -11
- package/dist/esm/meshagent-client.d.ts +1 -1
- package/dist/esm/meshagent-client.js +79 -83
- package/dist/esm/messaging-client.d.ts +4 -4
- package/dist/esm/messaging-client.js +33 -37
- package/dist/esm/oauth-scopes.js +2 -5
- package/dist/esm/participant-token.d.ts +1 -1
- package/dist/esm/participant-token.js +34 -62
- package/dist/esm/participant.d.ts +1 -1
- package/dist/esm/participant.js +3 -9
- package/dist/esm/protocol.d.ts +7 -4
- package/dist/esm/protocol.js +48 -46
- package/dist/esm/queues-client.d.ts +1 -1
- package/dist/esm/queues-client.js +12 -17
- package/dist/esm/requirement.js +4 -11
- package/dist/esm/response.js +22 -34
- package/dist/esm/room-client.d.ts +23 -17
- package/dist/esm/room-client.js +140 -137
- package/dist/esm/room-event.js +9 -21
- package/dist/esm/room-server-client.d.ts +2 -2
- package/dist/esm/room-server-client.js +13 -18
- package/dist/esm/runtime.js +1 -10
- package/dist/esm/schema.js +9 -18
- package/dist/esm/secrets-client.d.ts +3 -3
- package/dist/esm/secrets-client.js +31 -35
- package/dist/esm/services-client.d.ts +2 -2
- package/dist/esm/services-client.js +9 -13
- package/dist/esm/storage-client.d.ts +5 -5
- package/dist/esm/storage-client.js +32 -38
- package/dist/esm/stream-controller.js +1 -5
- package/dist/esm/sync-client.d.ts +4 -4
- package/dist/esm/sync-client.js +38 -42
- package/dist/esm/tool-content-type.js +1 -5
- package/dist/esm/toolkit-config.d.ts +3 -3
- package/dist/esm/toolkit-config.js +5 -13
- package/dist/esm/utils.js +10 -20
- package/dist/esm/version.js +1 -4
- package/dist/node/agent-client.d.ts +4 -4
- package/dist/node/agent-client.js +16 -16
- package/dist/node/agent.d.ts +6 -6
- package/dist/node/agent.js +57 -57
- package/dist/node/api_keys.js +7 -3
- package/dist/node/containers-client.d.ts +3 -3
- package/dist/node/containers-client.js +76 -76
- package/dist/node/datasets-client.d.ts +1 -1
- package/dist/node/datasets-client.js +83 -83
- package/dist/node/developer-client.d.ts +3 -3
- package/dist/node/developer-client.js +11 -11
- package/dist/node/document.d.ts +2 -2
- package/dist/node/document.js +3 -3
- package/dist/node/helpers.d.ts +4 -4
- package/dist/node/helpers.js +9 -9
- package/dist/node/index.d.ts +35 -35
- package/dist/node/index.js +35 -35
- package/dist/node/memory-client.d.ts +1 -1
- package/dist/node/memory-client.js +6 -6
- package/dist/node/meshagent-client.d.ts +1 -1
- package/dist/node/meshagent-client.js +78 -78
- package/dist/node/messaging-client.d.ts +4 -4
- package/dist/node/messaging-client.js +33 -33
- package/dist/node/participant-token.d.ts +1 -1
- package/dist/node/participant-token.js +5 -5
- package/dist/node/participant.d.ts +1 -1
- package/dist/node/protocol.d.ts +7 -4
- package/dist/node/protocol.js +36 -20
- package/dist/node/queues-client.d.ts +1 -1
- package/dist/node/queues-client.js +10 -10
- package/dist/node/response.js +11 -11
- package/dist/node/room-client.d.ts +23 -17
- package/dist/node/room-client.js +138 -130
- package/dist/node/room-server-client.d.ts +2 -2
- package/dist/node/room-server-client.js +10 -10
- package/dist/node/secrets-client.d.ts +3 -3
- package/dist/node/secrets-client.js +30 -30
- package/dist/node/services-client.d.ts +2 -2
- package/dist/node/services-client.js +8 -8
- package/dist/node/storage-client.d.ts +5 -5
- package/dist/node/storage-client.js +30 -30
- package/dist/node/sync-client.d.ts +4 -4
- package/dist/node/sync-client.js +38 -38
- package/dist/node/toolkit-config.d.ts +3 -3
- package/package.json +11 -5
- package/dist/browser/lk-client.d.ts +0 -18
- package/dist/browser/lk-client.js +0 -33
- package/dist/browser/lk-protocol.d.ts +0 -22
- package/dist/browser/lk-protocol.js +0 -37
- package/dist/esm/lk-client.d.ts +0 -18
- package/dist/esm/lk-client.js +0 -33
- package/dist/esm/lk-protocol.d.ts +0 -22
- package/dist/esm/lk-protocol.js +0 -37
- package/dist/node/lk-client.d.ts +0 -18
- package/dist/node/lk-client.js +0 -33
- package/dist/node/lk-protocol.d.ts +0 -22
- package/dist/node/lk-protocol.js +0 -37
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MessagingClient = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
4
|
+
const completer_js_1 = require("./completer.js");
|
|
5
|
+
const event_emitter_js_1 = require("./event-emitter.js");
|
|
6
|
+
const participant_js_1 = require("./participant.js");
|
|
7
|
+
const response_js_1 = require("./response.js");
|
|
8
|
+
const room_event_js_1 = require("./room-event.js");
|
|
9
|
+
const room_server_client_js_1 = require("./room-server-client.js");
|
|
10
|
+
const utils_js_1 = require("./utils.js");
|
|
11
11
|
const globalScope = globalThis;
|
|
12
12
|
function bytesToBase64(bytes) {
|
|
13
13
|
if (globalScope.Buffer != null) {
|
|
@@ -22,7 +22,7 @@ function bytesToBase64(bytes) {
|
|
|
22
22
|
}
|
|
23
23
|
return globalScope.btoa(binary);
|
|
24
24
|
}
|
|
25
|
-
class MessagingClient extends
|
|
25
|
+
class MessagingClient extends event_emitter_js_1.EventEmitter {
|
|
26
26
|
constructor({ room }) {
|
|
27
27
|
super();
|
|
28
28
|
this._messageHandler = this._handleMessageSend.bind(this);
|
|
@@ -63,14 +63,14 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
63
63
|
await this.client.invoke({
|
|
64
64
|
toolkit: "messaging",
|
|
65
65
|
tool: operation,
|
|
66
|
-
input: new
|
|
66
|
+
input: new response_js_1.JsonContent({ json: input }),
|
|
67
67
|
});
|
|
68
68
|
}
|
|
69
69
|
_invokeNowait({ operation, input, }) {
|
|
70
70
|
this.client.invokeNowait({
|
|
71
71
|
toolkit: "messaging",
|
|
72
72
|
tool: operation,
|
|
73
|
-
input: new
|
|
73
|
+
input: new response_js_1.JsonContent({ json: input }),
|
|
74
74
|
});
|
|
75
75
|
}
|
|
76
76
|
start() {
|
|
@@ -104,7 +104,7 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
104
104
|
if (this._messageQueueClosed) {
|
|
105
105
|
return null;
|
|
106
106
|
}
|
|
107
|
-
this._messageQueued ?? (this._messageQueued = new
|
|
107
|
+
this._messageQueued ?? (this._messageQueued = new completer_js_1.Completer());
|
|
108
108
|
await this._messageQueued.fut;
|
|
109
109
|
}
|
|
110
110
|
}
|
|
@@ -117,7 +117,7 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
117
117
|
}
|
|
118
118
|
_queueMessage(message) {
|
|
119
119
|
if (this._messageQueueClosed) {
|
|
120
|
-
throw new
|
|
120
|
+
throw new room_server_client_js_1.RoomServerException("Cannot send messages because messaging has been stopped");
|
|
121
121
|
}
|
|
122
122
|
this._messageQueue.push(message);
|
|
123
123
|
this._wakeMessageQueue();
|
|
@@ -173,7 +173,7 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
173
173
|
return participant;
|
|
174
174
|
}
|
|
175
175
|
_markParticipantOffline(participant) {
|
|
176
|
-
if (!(participant instanceof
|
|
176
|
+
if (!(participant instanceof participant_js_1.RemoteParticipant)) {
|
|
177
177
|
return;
|
|
178
178
|
}
|
|
179
179
|
participant._setOnline(false);
|
|
@@ -185,7 +185,7 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
185
185
|
if (to == null) {
|
|
186
186
|
return null;
|
|
187
187
|
}
|
|
188
|
-
if (!(to instanceof
|
|
188
|
+
if (!(to instanceof participant_js_1.RemoteParticipant)) {
|
|
189
189
|
return to;
|
|
190
190
|
}
|
|
191
191
|
if (to.online === false) {
|
|
@@ -219,12 +219,12 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
219
219
|
}
|
|
220
220
|
}
|
|
221
221
|
catch (error) {
|
|
222
|
-
if (error instanceof
|
|
222
|
+
if (error instanceof room_server_client_js_1.RoomServerException) {
|
|
223
223
|
this._dropQueuedMessage({ message, error });
|
|
224
224
|
this._drainQueuedMessages({ error });
|
|
225
225
|
}
|
|
226
226
|
else {
|
|
227
|
-
const wrapped = new
|
|
227
|
+
const wrapped = new room_server_client_js_1.RoomServerException(String(error));
|
|
228
228
|
this._dropQueuedMessage({ message, error: wrapped });
|
|
229
229
|
this._drainQueuedMessages({ error: wrapped });
|
|
230
230
|
}
|
|
@@ -234,7 +234,7 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
234
234
|
if (resolvedTo == null) {
|
|
235
235
|
this._dropQueuedMessage({
|
|
236
236
|
message,
|
|
237
|
-
error: new
|
|
237
|
+
error: new room_server_client_js_1.RoomServerException("the participant was not found"),
|
|
238
238
|
});
|
|
239
239
|
continue;
|
|
240
240
|
}
|
|
@@ -253,7 +253,7 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
253
253
|
}
|
|
254
254
|
}
|
|
255
255
|
catch (error) {
|
|
256
|
-
if (error instanceof
|
|
256
|
+
if (error instanceof room_server_client_js_1.RoomServerException) {
|
|
257
257
|
const wrapped = this.client._coerceMessageSendError(error);
|
|
258
258
|
if (wrapped.message === "the participant was not found") {
|
|
259
259
|
this._markParticipantOffline(message.to);
|
|
@@ -271,7 +271,7 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
271
271
|
}
|
|
272
272
|
async sendMessage({ to, type, message, attachment, ignoreOffline = false, }) {
|
|
273
273
|
if (this._sendTask == null) {
|
|
274
|
-
throw new
|
|
274
|
+
throw new room_server_client_js_1.RoomServerException("Cannot send messages because messaging has not been started");
|
|
275
275
|
}
|
|
276
276
|
const queued = {
|
|
277
277
|
to,
|
|
@@ -279,7 +279,7 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
279
279
|
message,
|
|
280
280
|
attachment,
|
|
281
281
|
dropIfOffline: ignoreOffline,
|
|
282
|
-
completer: ignoreOffline ? undefined : new
|
|
282
|
+
completer: ignoreOffline ? undefined : new completer_js_1.Completer(),
|
|
283
283
|
};
|
|
284
284
|
this._queueMessage(queued);
|
|
285
285
|
if (queued.completer != null) {
|
|
@@ -311,7 +311,7 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
311
311
|
}
|
|
312
312
|
async broadcastMessage({ type, message, attachment, }) {
|
|
313
313
|
if (this._sendTask == null) {
|
|
314
|
-
throw new
|
|
314
|
+
throw new room_server_client_js_1.RoomServerException("Cannot send messages because messaging has not been started");
|
|
315
315
|
}
|
|
316
316
|
await this.client._waitUntilConnectedForMessages();
|
|
317
317
|
if (this._desiredEnabled) {
|
|
@@ -324,7 +324,7 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
324
324
|
});
|
|
325
325
|
}
|
|
326
326
|
catch (error) {
|
|
327
|
-
if (error instanceof
|
|
327
|
+
if (error instanceof room_server_client_js_1.RoomServerException) {
|
|
328
328
|
throw this.client._coerceMessageSendError(error);
|
|
329
329
|
}
|
|
330
330
|
throw error;
|
|
@@ -348,10 +348,10 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
348
348
|
if (!this.client.isActiveProtocol(protocol)) {
|
|
349
349
|
return;
|
|
350
350
|
}
|
|
351
|
-
const headerStr = (0,
|
|
352
|
-
const payload = (0,
|
|
351
|
+
const headerStr = (0, utils_js_1.splitMessageHeader)(bytes);
|
|
352
|
+
const payload = (0, utils_js_1.splitMessagePayload)(bytes);
|
|
353
353
|
const header = JSON.parse(headerStr);
|
|
354
|
-
const message = new
|
|
354
|
+
const message = new room_event_js_1.RoomMessage({
|
|
355
355
|
fromParticipantId: header["from_participant_id"],
|
|
356
356
|
type: header["type"],
|
|
357
357
|
message: header["message"],
|
|
@@ -373,16 +373,16 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
373
373
|
default:
|
|
374
374
|
break;
|
|
375
375
|
}
|
|
376
|
-
const event = new
|
|
376
|
+
const event = new room_event_js_1.RoomMessageEvent({ message });
|
|
377
377
|
this.client.emit(event);
|
|
378
378
|
this.emit("message", event);
|
|
379
379
|
}
|
|
380
380
|
_onParticipantEnabled(message) {
|
|
381
381
|
const data = message.message;
|
|
382
|
-
const participant = new
|
|
382
|
+
const participant = new participant_js_1.RemoteParticipant(this.client, data.id, data.role, true);
|
|
383
383
|
participant._setAttributes(data.attributes ?? {});
|
|
384
384
|
this._participants[data.id] = participant;
|
|
385
|
-
this.emit("participant_added", new
|
|
385
|
+
this.emit("participant_added", new room_event_js_1.RoomMessageEvent({ message }));
|
|
386
386
|
}
|
|
387
387
|
_onParticipantAttributes(message) {
|
|
388
388
|
const participant = this._participants[message.fromParticipantId];
|
|
@@ -390,12 +390,12 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
390
390
|
return;
|
|
391
391
|
}
|
|
392
392
|
participant._setAttributes(message.message["attributes"]);
|
|
393
|
-
this.emit("participant_attributes_updated", new
|
|
393
|
+
this.emit("participant_attributes_updated", new room_event_js_1.RoomMessageEvent({ message }));
|
|
394
394
|
}
|
|
395
395
|
_onParticipantDisabled(message) {
|
|
396
396
|
const removed = this._removeParticipant(String(message.message["id"]));
|
|
397
397
|
if (removed != null) {
|
|
398
|
-
this.emit("participant_removed", new
|
|
398
|
+
this.emit("participant_removed", new room_event_js_1.RoomMessageEvent({ message }));
|
|
399
399
|
}
|
|
400
400
|
}
|
|
401
401
|
_onMessagingEnabled(message) {
|
|
@@ -405,7 +405,7 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
405
405
|
}
|
|
406
406
|
const participants = message.message["participants"];
|
|
407
407
|
for (const data of participants) {
|
|
408
|
-
const participant = new
|
|
408
|
+
const participant = new participant_js_1.RemoteParticipant(this.client, data.id, data.role, true);
|
|
409
409
|
participant._setAttributes(data.attributes ?? {});
|
|
410
410
|
this._participants[data.id] = participant;
|
|
411
411
|
}
|
|
@@ -415,10 +415,10 @@ class MessagingClient extends event_emitter_1.EventEmitter {
|
|
|
415
415
|
this._clearCurrentConnectionState();
|
|
416
416
|
return;
|
|
417
417
|
}
|
|
418
|
-
this.emit("messaging_enabled", new
|
|
418
|
+
this.emit("messaging_enabled", new room_event_js_1.RoomMessageEvent({ message }));
|
|
419
419
|
}
|
|
420
420
|
dispose() {
|
|
421
|
-
const error = new
|
|
421
|
+
const error = new room_server_client_js_1.RoomServerException("messaging client disposed");
|
|
422
422
|
this._messageQueueClosed = true;
|
|
423
423
|
this._wakeMessageQueue();
|
|
424
424
|
this._drainQueuedMessages({ error });
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ParticipantToken = exports.ParticipantGrant = exports.ApiScope = exports.LLMGrant = exports.ServicesGrant = exports.TunnelsGrant = exports.SecretsGrant = exports.OAuthEndpoint = exports.AdminGrant = exports.DeveloperGrant = exports.ContainersGrant = exports.ContainerRegistryGrant = exports.StorageGrant = exports.StoragePathGrant = exports.SyncGrant = exports.SyncPathGrant = exports.MemoryGrant = exports.MemoryEntryGrant = exports.MemoryPermissions = exports.DatasetGrant = exports.TableGrant = exports.MessagingGrant = exports.QueuesGrant = exports.LivekitGrant = exports.AgentsGrant = void 0;
|
|
4
4
|
const jose_1 = require("jose");
|
|
5
|
-
const
|
|
6
|
-
const
|
|
5
|
+
const api_keys_js_1 = require("./api_keys.js");
|
|
6
|
+
const version_js_1 = require("./version.js");
|
|
7
7
|
function matchesGrantPattern(patterns, value, allowIfUnset) {
|
|
8
8
|
if (!patterns) {
|
|
9
9
|
return allowIfUnset;
|
|
@@ -1112,7 +1112,7 @@ class ParticipantToken {
|
|
|
1112
1112
|
this.name = name;
|
|
1113
1113
|
this.projectId = projectId;
|
|
1114
1114
|
this.apiKeyId = apiKeyId;
|
|
1115
|
-
this.version = version ??
|
|
1115
|
+
this.version = version ?? version_js_1.__version__;
|
|
1116
1116
|
this.extra = extra ?? {};
|
|
1117
1117
|
this.grants = grants ?? [];
|
|
1118
1118
|
}
|
|
@@ -1196,7 +1196,7 @@ class ParticipantToken {
|
|
|
1196
1196
|
const resolvedApiKey = apiKey ?? getEnvValue("MESHAGENT_API_KEY");
|
|
1197
1197
|
let resolvedSecret = token;
|
|
1198
1198
|
if (resolvedApiKey !== undefined) {
|
|
1199
|
-
const parsed = (0,
|
|
1199
|
+
const parsed = (0, api_keys_js_1.parseApiKey)(resolvedApiKey);
|
|
1200
1200
|
resolvedSecret = parsed.secret;
|
|
1201
1201
|
payload["kid"] = parsed.id;
|
|
1202
1202
|
payload["sub"] = parsed.projectId;
|
|
@@ -1231,7 +1231,7 @@ class ParticipantToken {
|
|
|
1231
1231
|
delete data.sub;
|
|
1232
1232
|
const apiKeyId = typeof data.kid === "string" ? data.kid : undefined;
|
|
1233
1233
|
delete data.kid;
|
|
1234
|
-
const version = typeof data.version === "string" ? data.version :
|
|
1234
|
+
const version = typeof data.version === "string" ? data.version : version_js_1.__version__;
|
|
1235
1235
|
delete data.version;
|
|
1236
1236
|
return new ParticipantToken({
|
|
1237
1237
|
name,
|
package/dist/node/protocol.d.ts
CHANGED
|
@@ -49,7 +49,7 @@ export declare class StreamProtocolChannel implements ProtocolChannel {
|
|
|
49
49
|
}
|
|
50
50
|
export declare class WebSocketProtocolChannel implements ProtocolChannel {
|
|
51
51
|
readonly url: string;
|
|
52
|
-
readonly jwt: string;
|
|
52
|
+
readonly jwt: string | null;
|
|
53
53
|
webSocket: WebSocket | null;
|
|
54
54
|
private _opened;
|
|
55
55
|
private _finished;
|
|
@@ -59,7 +59,7 @@ export declare class WebSocketProtocolChannel implements ProtocolChannel {
|
|
|
59
59
|
private readonly _onUnexpectedResponse;
|
|
60
60
|
constructor({ url, jwt }: {
|
|
61
61
|
url: string;
|
|
62
|
-
jwt: string;
|
|
62
|
+
jwt: string | null;
|
|
63
63
|
});
|
|
64
64
|
start(onDataReceived: (data: Uint8Array) => void, { onDone, onError }: {
|
|
65
65
|
onDone?: () => void;
|
|
@@ -141,8 +141,11 @@ export declare class WebSocketClientProtocol extends Protocol<WebSocketProtocolC
|
|
|
141
141
|
private readonly _token;
|
|
142
142
|
constructor({ url, token }: {
|
|
143
143
|
url: string;
|
|
144
|
-
token: string;
|
|
144
|
+
token: string | null;
|
|
145
145
|
});
|
|
146
|
+
static withIAP({ url }?: {
|
|
147
|
+
url?: string;
|
|
148
|
+
}): WebSocketClientProtocol;
|
|
146
149
|
get url(): string;
|
|
147
|
-
get token(): string;
|
|
150
|
+
get token(): string | null;
|
|
148
151
|
}
|
package/dist/node/protocol.js
CHANGED
|
@@ -5,14 +5,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.WebSocketClientProtocol = exports.Protocol = exports.ProtocolMessageStream = exports.WebSocketProtocolChannel = exports.StreamProtocolChannel = exports.ProtocolHandshakeException = exports.ProtocolCloseException = exports.ProtocolReconnectUnsupportedException = exports.ProtocolCloseKind = void 0;
|
|
7
7
|
const isomorphic_ws_1 = __importDefault(require("isomorphic-ws"));
|
|
8
|
-
const
|
|
9
|
-
const
|
|
8
|
+
const completer_js_1 = require("./completer.js");
|
|
9
|
+
const utils_js_1 = require("./utils.js");
|
|
10
10
|
class ProtocolMessage {
|
|
11
11
|
constructor({ id, type, data }) {
|
|
12
12
|
this.id = id;
|
|
13
13
|
this.type = type;
|
|
14
14
|
this.data = data;
|
|
15
|
-
this.sent = new
|
|
15
|
+
this.sent = new completer_js_1.Completer();
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
var ProtocolCloseKind;
|
|
@@ -52,6 +52,17 @@ exports.ProtocolHandshakeException = ProtocolHandshakeException;
|
|
|
52
52
|
function isNodeRuntime() {
|
|
53
53
|
return typeof process !== "undefined" && process.release?.name === "node";
|
|
54
54
|
}
|
|
55
|
+
function resolveWebSocketUrl(url) {
|
|
56
|
+
const baseUrl = globalThis.location?.href;
|
|
57
|
+
const resolved = baseUrl == null ? new URL(url) : new URL(url, baseUrl);
|
|
58
|
+
if (resolved.protocol === "https:") {
|
|
59
|
+
resolved.protocol = "wss:";
|
|
60
|
+
}
|
|
61
|
+
else if (resolved.protocol === "http:") {
|
|
62
|
+
resolved.protocol = "ws:";
|
|
63
|
+
}
|
|
64
|
+
return resolved;
|
|
65
|
+
}
|
|
55
66
|
class StreamProtocolChannel {
|
|
56
67
|
constructor({ input, output, }) {
|
|
57
68
|
this.started = false;
|
|
@@ -94,7 +105,7 @@ exports.StreamProtocolChannel = StreamProtocolChannel;
|
|
|
94
105
|
class WebSocketProtocolChannel {
|
|
95
106
|
constructor({ url, jwt }) {
|
|
96
107
|
this.webSocket = null;
|
|
97
|
-
this._opened = new
|
|
108
|
+
this._opened = new completer_js_1.Completer();
|
|
98
109
|
this._finished = false;
|
|
99
110
|
this._onUnexpectedResponse = (_request, response) => {
|
|
100
111
|
const statusCode = response.statusCode;
|
|
@@ -121,7 +132,7 @@ class WebSocketProtocolChannel {
|
|
|
121
132
|
return;
|
|
122
133
|
}
|
|
123
134
|
if (typeof data === "string") {
|
|
124
|
-
this._onDataReceived?.(
|
|
135
|
+
this._onDataReceived?.(utils_js_1.encoder.encode(data));
|
|
125
136
|
return;
|
|
126
137
|
}
|
|
127
138
|
if (data instanceof ArrayBuffer) {
|
|
@@ -151,16 +162,18 @@ class WebSocketProtocolChannel {
|
|
|
151
162
|
this.jwt = jwt;
|
|
152
163
|
}
|
|
153
164
|
start(onDataReceived, { onDone, onError }) {
|
|
154
|
-
const url =
|
|
155
|
-
|
|
156
|
-
this._opened = new completer_1.Completer();
|
|
165
|
+
const url = resolveWebSocketUrl(this.url);
|
|
166
|
+
this._opened = new completer_js_1.Completer();
|
|
157
167
|
this._finished = false;
|
|
158
168
|
this._onDataReceived = onDataReceived;
|
|
159
169
|
this._doneHandler = onDone;
|
|
160
170
|
this._errorHandler = onError;
|
|
161
171
|
const socket = isNodeRuntime()
|
|
162
|
-
? new isomorphic_ws_1.default(url.toString(), [], {
|
|
163
|
-
|
|
172
|
+
? new isomorphic_ws_1.default(url.toString(), [], {
|
|
173
|
+
headers: this.jwt == null ? undefined : { Authorization: `Bearer ${this.jwt}` },
|
|
174
|
+
perMessageDeflate: true,
|
|
175
|
+
})
|
|
176
|
+
: new isomorphic_ws_1.default(url.toString(), this.jwt == null ? [] : [`meshagent-room.${this.jwt}`]);
|
|
164
177
|
this.webSocket = socket;
|
|
165
178
|
if (isNodeRuntime()) {
|
|
166
179
|
socket.on("unexpected-response", this._onUnexpectedResponse);
|
|
@@ -212,7 +225,7 @@ exports.WebSocketProtocolChannel = WebSocketProtocolChannel;
|
|
|
212
225
|
class ProtocolMessageStream {
|
|
213
226
|
constructor() {
|
|
214
227
|
this._messages = [];
|
|
215
|
-
this._messageAdded = new
|
|
228
|
+
this._messageAdded = new completer_js_1.Completer();
|
|
216
229
|
this._closed = false;
|
|
217
230
|
}
|
|
218
231
|
add(message) {
|
|
@@ -230,7 +243,7 @@ class ProtocolMessageStream {
|
|
|
230
243
|
async *stream() {
|
|
231
244
|
while (!this._closed) {
|
|
232
245
|
await this._messageAdded.fut;
|
|
233
|
-
this._messageAdded = new
|
|
246
|
+
this._messageAdded = new completer_js_1.Completer();
|
|
234
247
|
while (this._messages.length > 0 && !this._closed) {
|
|
235
248
|
const message = this._messages.shift();
|
|
236
249
|
if (message !== undefined) {
|
|
@@ -246,7 +259,7 @@ class Protocol {
|
|
|
246
259
|
this.handlers = {};
|
|
247
260
|
this._id = 0;
|
|
248
261
|
this._send = new ProtocolMessageStream();
|
|
249
|
-
this._done = new
|
|
262
|
+
this._done = new completer_js_1.Completer();
|
|
250
263
|
this._sendLoop = null;
|
|
251
264
|
this._open = false;
|
|
252
265
|
this._closed = false;
|
|
@@ -325,7 +338,7 @@ class Protocol {
|
|
|
325
338
|
async handleMessage(messageId, type, data) {
|
|
326
339
|
const handler = this.handlers[type] ?? this.handlers["*"];
|
|
327
340
|
if (handler == null) {
|
|
328
|
-
const unpacked = (0,
|
|
341
|
+
const unpacked = (0, utils_js_1.unpackMessage)(data);
|
|
329
342
|
console.warn(`No handler for message type ${type}; data:`, unpacked);
|
|
330
343
|
return;
|
|
331
344
|
}
|
|
@@ -357,7 +370,7 @@ class Protocol {
|
|
|
357
370
|
await message.sent.fut;
|
|
358
371
|
}
|
|
359
372
|
async sendJson(object) {
|
|
360
|
-
await this.send("application/json",
|
|
373
|
+
await this.send("application/json", utils_js_1.encoder.encode(JSON.stringify(object)));
|
|
361
374
|
}
|
|
362
375
|
start({ onMessage, onDone, onError, } = {}) {
|
|
363
376
|
if (this._sendLoop != null) {
|
|
@@ -419,14 +432,14 @@ class Protocol {
|
|
|
419
432
|
headerView.setUint32(4, message.id & 0xffffffff, false);
|
|
420
433
|
headerView.setUint32(8, 0, false);
|
|
421
434
|
headerView.setUint32(12, packets, false);
|
|
422
|
-
await this.channel.sendData((0,
|
|
435
|
+
await this.channel.sendData((0, utils_js_1.mergeUint8Arrays)(header, utils_js_1.encoder.encode(message.type)));
|
|
423
436
|
for (let i = 0; i < packets; i += 1) {
|
|
424
437
|
const packetHeader = new Uint8Array(12);
|
|
425
438
|
const packetHeaderView = new DataView(packetHeader.buffer);
|
|
426
439
|
packetHeaderView.setUint32(0, Math.floor(message.id / 2 ** 32), false);
|
|
427
440
|
packetHeaderView.setUint32(4, message.id & 0xffffffff, false);
|
|
428
441
|
packetHeaderView.setUint32(8, i + 1, false);
|
|
429
|
-
await this.channel.sendData((0,
|
|
442
|
+
await this.channel.sendData((0, utils_js_1.mergeUint8Arrays)(packetHeader, message.data.subarray(i * 1024, Math.min((i + 1) * 1024, message.data.length))));
|
|
430
443
|
}
|
|
431
444
|
if (!message.sent.completed) {
|
|
432
445
|
message.sent.complete();
|
|
@@ -461,10 +474,10 @@ class Protocol {
|
|
|
461
474
|
if (this._recvState === "ready" || this._recvState === "error") {
|
|
462
475
|
this._recvPacketTotal = dataView.getUint32(12, false);
|
|
463
476
|
this._recvMessageId = messageId;
|
|
464
|
-
this._recvType =
|
|
477
|
+
this._recvType = utils_js_1.decoder.decode(dataPacket.subarray(16));
|
|
465
478
|
if (this._recvPacketTotal === 0) {
|
|
466
479
|
try {
|
|
467
|
-
const merged = (0,
|
|
480
|
+
const merged = (0, utils_js_1.mergeUint8Arrays)(...this._recvPackets);
|
|
468
481
|
this._recvPackets.length = 0;
|
|
469
482
|
this._dispatchMessage({ messageId, type: this._recvType, data: merged });
|
|
470
483
|
}
|
|
@@ -498,7 +511,7 @@ class Protocol {
|
|
|
498
511
|
this._recvPackets.push(dataPacket.subarray(12));
|
|
499
512
|
if (this._recvPacketTotal === this._recvPacketId) {
|
|
500
513
|
try {
|
|
501
|
-
const merged = (0,
|
|
514
|
+
const merged = (0, utils_js_1.mergeUint8Arrays)(...this._recvPackets);
|
|
502
515
|
this._recvPackets.length = 0;
|
|
503
516
|
this._dispatchMessage({ messageId, type: this._recvType, data: merged });
|
|
504
517
|
}
|
|
@@ -527,6 +540,9 @@ class WebSocketClientProtocol extends Protocol {
|
|
|
527
540
|
this._url = url;
|
|
528
541
|
this._token = token;
|
|
529
542
|
}
|
|
543
|
+
static withIAP({ url = "./.well-known/meshagent/room/connect" } = {}) {
|
|
544
|
+
return new WebSocketClientProtocol({ url, token: null });
|
|
545
|
+
}
|
|
530
546
|
get url() {
|
|
531
547
|
return this._url;
|
|
532
548
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.QueuesClient = exports.Queue = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
4
|
+
const response_js_1 = require("./response.js");
|
|
5
|
+
const room_server_client_js_1 = require("./room-server-client.js");
|
|
6
6
|
class Queue {
|
|
7
7
|
constructor({ name, size }) {
|
|
8
8
|
this.name = name;
|
|
@@ -15,7 +15,7 @@ class QueuesClient {
|
|
|
15
15
|
this.client = room;
|
|
16
16
|
}
|
|
17
17
|
_unexpectedResponseError(operation) {
|
|
18
|
-
return new
|
|
18
|
+
return new room_server_client_js_1.RoomServerException(`unexpected return type from queues.${operation}`);
|
|
19
19
|
}
|
|
20
20
|
async _invoke(operation, arguments_) {
|
|
21
21
|
return await this.client.invoke({
|
|
@@ -26,7 +26,7 @@ class QueuesClient {
|
|
|
26
26
|
}
|
|
27
27
|
async list() {
|
|
28
28
|
const response = await this._invoke("list", {});
|
|
29
|
-
if (!(response instanceof
|
|
29
|
+
if (!(response instanceof response_js_1.JsonContent)) {
|
|
30
30
|
throw this._unexpectedResponseError("list");
|
|
31
31
|
}
|
|
32
32
|
const queues = response.json["queues"];
|
|
@@ -34,25 +34,25 @@ class QueuesClient {
|
|
|
34
34
|
}
|
|
35
35
|
async open(name) {
|
|
36
36
|
const response = await this._invoke("open", { name });
|
|
37
|
-
if (!(response instanceof
|
|
37
|
+
if (!(response instanceof response_js_1.EmptyContent)) {
|
|
38
38
|
throw this._unexpectedResponseError("open");
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
async drain(name) {
|
|
42
42
|
const response = await this._invoke("drain", { name });
|
|
43
|
-
if (!(response instanceof
|
|
43
|
+
if (!(response instanceof response_js_1.EmptyContent)) {
|
|
44
44
|
throw this._unexpectedResponseError("drain");
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
async close(name) {
|
|
48
48
|
const response = await this._invoke("close", { name });
|
|
49
|
-
if (!(response instanceof
|
|
49
|
+
if (!(response instanceof response_js_1.EmptyContent)) {
|
|
50
50
|
throw this._unexpectedResponseError("close");
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
async send(name, message, { create = true }) {
|
|
54
54
|
const response = await this._invoke("send", { name, create, message });
|
|
55
|
-
if (!(response instanceof
|
|
55
|
+
if (!(response instanceof response_js_1.EmptyContent)) {
|
|
56
56
|
throw this._unexpectedResponseError("send");
|
|
57
57
|
}
|
|
58
58
|
}
|
|
@@ -62,10 +62,10 @@ class QueuesClient {
|
|
|
62
62
|
create,
|
|
63
63
|
wait,
|
|
64
64
|
});
|
|
65
|
-
if (response instanceof
|
|
65
|
+
if (response instanceof response_js_1.EmptyContent) {
|
|
66
66
|
return null;
|
|
67
67
|
}
|
|
68
|
-
if (response instanceof
|
|
68
|
+
if (response instanceof response_js_1.JsonContent) {
|
|
69
69
|
return response.json;
|
|
70
70
|
}
|
|
71
71
|
throw this._unexpectedResponseError("receive");
|
package/dist/node/response.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ControlContent = exports.ControlCloseStatus = exports.EmptyContent = exports.ErrorContent = exports.JsonContent = exports.TextContent = exports.FileContent = exports.LinkContent = exports.BinaryContent = void 0;
|
|
4
4
|
exports.unpackContent = unpackContent;
|
|
5
|
-
const
|
|
5
|
+
const utils_js_1 = require("./utils.js");
|
|
6
6
|
class BinaryContent {
|
|
7
7
|
constructor({ data, headers = {} }) {
|
|
8
8
|
this.data = data;
|
|
@@ -15,7 +15,7 @@ class BinaryContent {
|
|
|
15
15
|
});
|
|
16
16
|
}
|
|
17
17
|
pack() {
|
|
18
|
-
return (0,
|
|
18
|
+
return (0, utils_js_1.packMessage)({
|
|
19
19
|
type: "binary",
|
|
20
20
|
headers: this.headers,
|
|
21
21
|
}, this.data);
|
|
@@ -37,7 +37,7 @@ class LinkContent {
|
|
|
37
37
|
});
|
|
38
38
|
}
|
|
39
39
|
pack() {
|
|
40
|
-
return (0,
|
|
40
|
+
return (0, utils_js_1.packMessage)({
|
|
41
41
|
type: "link",
|
|
42
42
|
name: this.name,
|
|
43
43
|
url: this.url,
|
|
@@ -62,7 +62,7 @@ class FileContent {
|
|
|
62
62
|
});
|
|
63
63
|
}
|
|
64
64
|
pack() {
|
|
65
|
-
return (0,
|
|
65
|
+
return (0, utils_js_1.packMessage)({
|
|
66
66
|
type: "file",
|
|
67
67
|
name: this.name,
|
|
68
68
|
mime_type: this.mimeType,
|
|
@@ -83,7 +83,7 @@ class TextContent {
|
|
|
83
83
|
});
|
|
84
84
|
}
|
|
85
85
|
pack() {
|
|
86
|
-
return (0,
|
|
86
|
+
return (0, utils_js_1.packMessage)({
|
|
87
87
|
type: "text",
|
|
88
88
|
text: this.text,
|
|
89
89
|
});
|
|
@@ -101,7 +101,7 @@ class JsonContent {
|
|
|
101
101
|
return new JsonContent({ json: header["json"] });
|
|
102
102
|
}
|
|
103
103
|
pack() {
|
|
104
|
-
return (0,
|
|
104
|
+
return (0, utils_js_1.packMessage)({
|
|
105
105
|
type: "json",
|
|
106
106
|
json: this.json,
|
|
107
107
|
});
|
|
@@ -138,7 +138,7 @@ class ErrorContent {
|
|
|
138
138
|
if (this.code !== undefined) {
|
|
139
139
|
header["code"] = this.code;
|
|
140
140
|
}
|
|
141
|
-
return (0,
|
|
141
|
+
return (0, utils_js_1.packMessage)(header);
|
|
142
142
|
}
|
|
143
143
|
toString() {
|
|
144
144
|
return this.code !== undefined
|
|
@@ -152,7 +152,7 @@ class EmptyContent {
|
|
|
152
152
|
return new EmptyContent();
|
|
153
153
|
}
|
|
154
154
|
pack() {
|
|
155
|
-
return (0,
|
|
155
|
+
return (0, utils_js_1.packMessage)({ type: "empty" });
|
|
156
156
|
}
|
|
157
157
|
toString() {
|
|
158
158
|
return `EmptyContent`;
|
|
@@ -203,7 +203,7 @@ class ControlContent {
|
|
|
203
203
|
header["message"] = this.message;
|
|
204
204
|
}
|
|
205
205
|
}
|
|
206
|
-
return (0,
|
|
206
|
+
return (0, utils_js_1.packMessage)(header);
|
|
207
207
|
}
|
|
208
208
|
toString() {
|
|
209
209
|
return `ControlContent: ${this.method}`;
|
|
@@ -221,8 +221,8 @@ const _contentTypes = {
|
|
|
221
221
|
text: TextContent.unpack,
|
|
222
222
|
};
|
|
223
223
|
function unpackContent(data) {
|
|
224
|
-
const header = JSON.parse((0,
|
|
225
|
-
const payload = (0,
|
|
224
|
+
const header = JSON.parse((0, utils_js_1.splitMessageHeader)(data));
|
|
225
|
+
const payload = (0, utils_js_1.splitMessagePayload)(data);
|
|
226
226
|
const typeKey = header["type"];
|
|
227
227
|
if (!_contentTypes[typeKey]) {
|
|
228
228
|
throw new Error(`Unknown content type: ${typeKey}`);
|