@meshagent/meshagent 0.31.0 → 0.31.2

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 CHANGED
@@ -1,3 +1,9 @@
1
+ ## [0.31.2]
2
+ - Stability
3
+
4
+ ## [0.31.1]
5
+ - Stability
6
+
1
7
  ## [0.31.0]
2
8
  - Stability
3
9
 
@@ -21,15 +21,19 @@ export declare class MessagingClient extends EventEmitter<RoomMessageEvent> {
21
21
  room: RoomClient;
22
22
  });
23
23
  private _messageInput;
24
+ private _removeParticipant;
25
+ private _resolveMessageRecipient;
26
+ private _participantNotFound;
24
27
  createStream({ to, header }: {
25
28
  to: Participant;
26
29
  header: Record<string, any>;
27
30
  }): Promise<MessageStreamWriter>;
28
- sendMessage({ to, type, message, attachment }: {
31
+ sendMessage({ to, type, message, attachment, ignoreOffline }: {
29
32
  to: Participant;
30
33
  type: string;
31
34
  message: Record<string, any>;
32
35
  attachment?: Uint8Array;
36
+ ignoreOffline?: boolean;
33
37
  }): Promise<void>;
34
38
  enable(onStreamAccept?: (reader: MessageStreamReader) => void): Promise<void>;
35
39
  disable(): Promise<void>;
@@ -5,6 +5,7 @@ const uuid_1 = require("uuid");
5
5
  const event_emitter_1 = require("./event-emitter");
6
6
  const participant_1 = require("./participant");
7
7
  const room_event_1 = require("./room-event");
8
+ const room_server_client_1 = require("./room-server-client");
8
9
  const utils_1 = require("./utils");
9
10
  const stream_controller_1 = require("./stream-controller");
10
11
  const completer_1 = require("./completer");
@@ -51,6 +52,37 @@ class MessagingClient extends event_emitter_1.EventEmitter {
51
52
  }
52
53
  return input;
53
54
  }
55
+ _removeParticipant(participantId) {
56
+ const participant = this._participants[participantId];
57
+ if (participant === undefined) {
58
+ return undefined;
59
+ }
60
+ participant._setOnline(false);
61
+ delete this._participants[participantId];
62
+ for (const [streamId, reader] of Object.entries(this._streamReaders)) {
63
+ if (reader._to.id !== participant.id) {
64
+ continue;
65
+ }
66
+ reader._controller.close();
67
+ delete this._streamReaders[streamId];
68
+ }
69
+ return participant;
70
+ }
71
+ _resolveMessageRecipient(to) {
72
+ if (!(to instanceof participant_1.RemoteParticipant)) {
73
+ return to;
74
+ }
75
+ if (to.online === false) {
76
+ return null;
77
+ }
78
+ return this._participants[to.id] ?? null;
79
+ }
80
+ _participantNotFound(ignoreOffline) {
81
+ if (ignoreOffline) {
82
+ return null;
83
+ }
84
+ throw new room_server_client_1.RoomServerException("the participant was not found");
85
+ }
54
86
  async createStream({ to, header }) {
55
87
  const streamId = (0, uuid_1.v4)();
56
88
  const completer = new completer_1.Completer();
@@ -62,12 +94,16 @@ class MessagingClient extends event_emitter_1.EventEmitter {
62
94
  });
63
95
  return completer.fut;
64
96
  }
65
- async sendMessage({ to, type, message, attachment }) {
97
+ async sendMessage({ to, type, message, attachment, ignoreOffline = false }) {
98
+ const resolvedTo = this._resolveMessageRecipient(to) ?? this._participantNotFound(ignoreOffline);
99
+ if (resolvedTo === null) {
100
+ return;
101
+ }
66
102
  await this.client.invoke({
67
103
  toolkit: "messaging",
68
104
  tool: "send",
69
105
  input: this._messageInput({
70
- toParticipantId: to.id,
106
+ toParticipantId: resolvedTo.id,
71
107
  type,
72
108
  message,
73
109
  attachment,
@@ -144,7 +180,7 @@ class MessagingClient extends event_emitter_1.EventEmitter {
144
180
  }
145
181
  _onParticipantEnabled(message) {
146
182
  const data = message.message;
147
- const p = new participant_1.RemoteParticipant(this.client, data["id"], data["role"]);
183
+ const p = new participant_1.RemoteParticipant(this.client, data["id"], data["role"], true);
148
184
  for (const [k, v] of Object.entries(data["attributes"] || {})) {
149
185
  p._attributes[k] = v;
150
186
  }
@@ -162,16 +198,15 @@ class MessagingClient extends event_emitter_1.EventEmitter {
162
198
  this.emit("participant_attributes_updated", { message });
163
199
  }
164
200
  _onParticipantDisabled(message) {
165
- const part = this._participants[message.message["id"]];
201
+ const part = this._removeParticipant(message.message["id"]);
166
202
  if (part) {
167
- delete this._participants[message.message["id"]];
168
203
  this.emit("participant_removed", { message });
169
204
  }
170
205
  }
171
206
  _onMessagingEnabled(message) {
172
207
  const participants = message.message["participants"];
173
208
  for (const data of participants) {
174
- const rp = new participant_1.RemoteParticipant(this.client, data["id"], data["role"]);
209
+ const rp = new participant_1.RemoteParticipant(this.client, data["id"], data["role"], true);
175
210
  for (const [k, v] of Object.entries(data["attributes"] || {})) {
176
211
  rp._attributes[k] = v;
177
212
  }
@@ -197,17 +232,23 @@ class MessagingClient extends event_emitter_1.EventEmitter {
197
232
  throw new Error("streams are not allowed by this client");
198
233
  }
199
234
  this._onStreamAcceptCallback(reader);
200
- this.sendMessage({
235
+ void this.sendMessage({
201
236
  to: from,
202
237
  type: "stream.accept",
203
238
  message: { stream_id: streamId },
239
+ ignoreOffline: true,
240
+ }).catch((error) => {
241
+ console.warn("unable to send stream response", error);
204
242
  });
205
243
  }
206
244
  catch (e) {
207
- this.sendMessage({
245
+ void this.sendMessage({
208
246
  to: from,
209
247
  type: "stream.reject",
210
248
  message: { stream_id: streamId, error: String(e) },
249
+ ignoreOffline: true,
250
+ }).catch((error) => {
251
+ console.warn("unable to send stream response", error);
211
252
  });
212
253
  }
213
254
  this._streamReaders[streamId] = reader;
@@ -10,7 +10,9 @@ export declare abstract class Participant {
10
10
  }
11
11
  export declare class RemoteParticipant extends Participant {
12
12
  readonly role: string;
13
- constructor(client: RoomClient, id: string, role: string);
13
+ online?: boolean;
14
+ constructor(client: RoomClient, id: string, role: string, online?: boolean);
15
+ _setOnline(online: boolean): void;
14
16
  }
15
17
  export declare class LocalParticipant extends Participant {
16
18
  constructor(client: RoomClient, id: string);
@@ -18,9 +18,13 @@ class Participant {
18
18
  }
19
19
  exports.Participant = Participant;
20
20
  class RemoteParticipant extends Participant {
21
- constructor(client, id, role) {
21
+ constructor(client, id, role, online) {
22
22
  super(client, id);
23
23
  this.role = role;
24
+ this.online = online;
25
+ }
26
+ _setOnline(online) {
27
+ this.online = online;
24
28
  }
25
29
  }
26
30
  exports.RemoteParticipant = RemoteParticipant;
@@ -21,15 +21,19 @@ export declare class MessagingClient extends EventEmitter<RoomMessageEvent> {
21
21
  room: RoomClient;
22
22
  });
23
23
  private _messageInput;
24
+ private _removeParticipant;
25
+ private _resolveMessageRecipient;
26
+ private _participantNotFound;
24
27
  createStream({ to, header }: {
25
28
  to: Participant;
26
29
  header: Record<string, any>;
27
30
  }): Promise<MessageStreamWriter>;
28
- sendMessage({ to, type, message, attachment }: {
31
+ sendMessage({ to, type, message, attachment, ignoreOffline }: {
29
32
  to: Participant;
30
33
  type: string;
31
34
  message: Record<string, any>;
32
35
  attachment?: Uint8Array;
36
+ ignoreOffline?: boolean;
33
37
  }): Promise<void>;
34
38
  enable(onStreamAccept?: (reader: MessageStreamReader) => void): Promise<void>;
35
39
  disable(): Promise<void>;
@@ -2,6 +2,7 @@ import { v4 as uuidV4 } from "uuid";
2
2
  import { EventEmitter } from "./event-emitter";
3
3
  import { RemoteParticipant } from "./participant";
4
4
  import { RoomMessage, RoomMessageEvent } from "./room-event";
5
+ import { RoomServerException } from "./room-server-client";
5
6
  import { splitMessageHeader, splitMessagePayload } from "./utils";
6
7
  import { StreamController } from "./stream-controller";
7
8
  import { Completer } from "./completer";
@@ -47,6 +48,37 @@ export class MessagingClient extends EventEmitter {
47
48
  }
48
49
  return input;
49
50
  }
51
+ _removeParticipant(participantId) {
52
+ const participant = this._participants[participantId];
53
+ if (participant === undefined) {
54
+ return undefined;
55
+ }
56
+ participant._setOnline(false);
57
+ delete this._participants[participantId];
58
+ for (const [streamId, reader] of Object.entries(this._streamReaders)) {
59
+ if (reader._to.id !== participant.id) {
60
+ continue;
61
+ }
62
+ reader._controller.close();
63
+ delete this._streamReaders[streamId];
64
+ }
65
+ return participant;
66
+ }
67
+ _resolveMessageRecipient(to) {
68
+ if (!(to instanceof RemoteParticipant)) {
69
+ return to;
70
+ }
71
+ if (to.online === false) {
72
+ return null;
73
+ }
74
+ return this._participants[to.id] ?? null;
75
+ }
76
+ _participantNotFound(ignoreOffline) {
77
+ if (ignoreOffline) {
78
+ return null;
79
+ }
80
+ throw new RoomServerException("the participant was not found");
81
+ }
50
82
  async createStream({ to, header }) {
51
83
  const streamId = uuidV4();
52
84
  const completer = new Completer();
@@ -58,12 +90,16 @@ export class MessagingClient extends EventEmitter {
58
90
  });
59
91
  return completer.fut;
60
92
  }
61
- async sendMessage({ to, type, message, attachment }) {
93
+ async sendMessage({ to, type, message, attachment, ignoreOffline = false }) {
94
+ const resolvedTo = this._resolveMessageRecipient(to) ?? this._participantNotFound(ignoreOffline);
95
+ if (resolvedTo === null) {
96
+ return;
97
+ }
62
98
  await this.client.invoke({
63
99
  toolkit: "messaging",
64
100
  tool: "send",
65
101
  input: this._messageInput({
66
- toParticipantId: to.id,
102
+ toParticipantId: resolvedTo.id,
67
103
  type,
68
104
  message,
69
105
  attachment,
@@ -140,7 +176,7 @@ export class MessagingClient extends EventEmitter {
140
176
  }
141
177
  _onParticipantEnabled(message) {
142
178
  const data = message.message;
143
- const p = new RemoteParticipant(this.client, data["id"], data["role"]);
179
+ const p = new RemoteParticipant(this.client, data["id"], data["role"], true);
144
180
  for (const [k, v] of Object.entries(data["attributes"] || {})) {
145
181
  p._attributes[k] = v;
146
182
  }
@@ -158,16 +194,15 @@ export class MessagingClient extends EventEmitter {
158
194
  this.emit("participant_attributes_updated", { message });
159
195
  }
160
196
  _onParticipantDisabled(message) {
161
- const part = this._participants[message.message["id"]];
197
+ const part = this._removeParticipant(message.message["id"]);
162
198
  if (part) {
163
- delete this._participants[message.message["id"]];
164
199
  this.emit("participant_removed", { message });
165
200
  }
166
201
  }
167
202
  _onMessagingEnabled(message) {
168
203
  const participants = message.message["participants"];
169
204
  for (const data of participants) {
170
- const rp = new RemoteParticipant(this.client, data["id"], data["role"]);
205
+ const rp = new RemoteParticipant(this.client, data["id"], data["role"], true);
171
206
  for (const [k, v] of Object.entries(data["attributes"] || {})) {
172
207
  rp._attributes[k] = v;
173
208
  }
@@ -193,17 +228,23 @@ export class MessagingClient extends EventEmitter {
193
228
  throw new Error("streams are not allowed by this client");
194
229
  }
195
230
  this._onStreamAcceptCallback(reader);
196
- this.sendMessage({
231
+ void this.sendMessage({
197
232
  to: from,
198
233
  type: "stream.accept",
199
234
  message: { stream_id: streamId },
235
+ ignoreOffline: true,
236
+ }).catch((error) => {
237
+ console.warn("unable to send stream response", error);
200
238
  });
201
239
  }
202
240
  catch (e) {
203
- this.sendMessage({
241
+ void this.sendMessage({
204
242
  to: from,
205
243
  type: "stream.reject",
206
244
  message: { stream_id: streamId, error: String(e) },
245
+ ignoreOffline: true,
246
+ }).catch((error) => {
247
+ console.warn("unable to send stream response", error);
207
248
  });
208
249
  }
209
250
  this._streamReaders[streamId] = reader;
@@ -10,7 +10,9 @@ export declare abstract class Participant {
10
10
  }
11
11
  export declare class RemoteParticipant extends Participant {
12
12
  readonly role: string;
13
- constructor(client: RoomClient, id: string, role: string);
13
+ online?: boolean;
14
+ constructor(client: RoomClient, id: string, role: string, online?: boolean);
15
+ _setOnline(online: boolean): void;
14
16
  }
15
17
  export declare class LocalParticipant extends Participant {
16
18
  constructor(client: RoomClient, id: string);
@@ -14,9 +14,13 @@ export class Participant {
14
14
  }
15
15
  }
16
16
  export class RemoteParticipant extends Participant {
17
- constructor(client, id, role) {
17
+ constructor(client, id, role, online) {
18
18
  super(client, id);
19
19
  this.role = role;
20
+ this.online = online;
21
+ }
22
+ _setOnline(online) {
23
+ this.online = online;
20
24
  }
21
25
  }
22
26
  export class LocalParticipant extends Participant {
@@ -21,15 +21,19 @@ export declare class MessagingClient extends EventEmitter<RoomMessageEvent> {
21
21
  room: RoomClient;
22
22
  });
23
23
  private _messageInput;
24
+ private _removeParticipant;
25
+ private _resolveMessageRecipient;
26
+ private _participantNotFound;
24
27
  createStream({ to, header }: {
25
28
  to: Participant;
26
29
  header: Record<string, any>;
27
30
  }): Promise<MessageStreamWriter>;
28
- sendMessage({ to, type, message, attachment }: {
31
+ sendMessage({ to, type, message, attachment, ignoreOffline }: {
29
32
  to: Participant;
30
33
  type: string;
31
34
  message: Record<string, any>;
32
35
  attachment?: Uint8Array;
36
+ ignoreOffline?: boolean;
33
37
  }): Promise<void>;
34
38
  enable(onStreamAccept?: (reader: MessageStreamReader) => void): Promise<void>;
35
39
  disable(): Promise<void>;
@@ -5,6 +5,7 @@ const uuid_1 = require("uuid");
5
5
  const event_emitter_1 = require("./event-emitter");
6
6
  const participant_1 = require("./participant");
7
7
  const room_event_1 = require("./room-event");
8
+ const room_server_client_1 = require("./room-server-client");
8
9
  const utils_1 = require("./utils");
9
10
  const stream_controller_1 = require("./stream-controller");
10
11
  const completer_1 = require("./completer");
@@ -51,6 +52,37 @@ class MessagingClient extends event_emitter_1.EventEmitter {
51
52
  }
52
53
  return input;
53
54
  }
55
+ _removeParticipant(participantId) {
56
+ const participant = this._participants[participantId];
57
+ if (participant === undefined) {
58
+ return undefined;
59
+ }
60
+ participant._setOnline(false);
61
+ delete this._participants[participantId];
62
+ for (const [streamId, reader] of Object.entries(this._streamReaders)) {
63
+ if (reader._to.id !== participant.id) {
64
+ continue;
65
+ }
66
+ reader._controller.close();
67
+ delete this._streamReaders[streamId];
68
+ }
69
+ return participant;
70
+ }
71
+ _resolveMessageRecipient(to) {
72
+ if (!(to instanceof participant_1.RemoteParticipant)) {
73
+ return to;
74
+ }
75
+ if (to.online === false) {
76
+ return null;
77
+ }
78
+ return this._participants[to.id] ?? null;
79
+ }
80
+ _participantNotFound(ignoreOffline) {
81
+ if (ignoreOffline) {
82
+ return null;
83
+ }
84
+ throw new room_server_client_1.RoomServerException("the participant was not found");
85
+ }
54
86
  async createStream({ to, header }) {
55
87
  const streamId = (0, uuid_1.v4)();
56
88
  const completer = new completer_1.Completer();
@@ -62,12 +94,16 @@ class MessagingClient extends event_emitter_1.EventEmitter {
62
94
  });
63
95
  return completer.fut;
64
96
  }
65
- async sendMessage({ to, type, message, attachment }) {
97
+ async sendMessage({ to, type, message, attachment, ignoreOffline = false }) {
98
+ const resolvedTo = this._resolveMessageRecipient(to) ?? this._participantNotFound(ignoreOffline);
99
+ if (resolvedTo === null) {
100
+ return;
101
+ }
66
102
  await this.client.invoke({
67
103
  toolkit: "messaging",
68
104
  tool: "send",
69
105
  input: this._messageInput({
70
- toParticipantId: to.id,
106
+ toParticipantId: resolvedTo.id,
71
107
  type,
72
108
  message,
73
109
  attachment,
@@ -144,7 +180,7 @@ class MessagingClient extends event_emitter_1.EventEmitter {
144
180
  }
145
181
  _onParticipantEnabled(message) {
146
182
  const data = message.message;
147
- const p = new participant_1.RemoteParticipant(this.client, data["id"], data["role"]);
183
+ const p = new participant_1.RemoteParticipant(this.client, data["id"], data["role"], true);
148
184
  for (const [k, v] of Object.entries(data["attributes"] || {})) {
149
185
  p._attributes[k] = v;
150
186
  }
@@ -162,16 +198,15 @@ class MessagingClient extends event_emitter_1.EventEmitter {
162
198
  this.emit("participant_attributes_updated", { message });
163
199
  }
164
200
  _onParticipantDisabled(message) {
165
- const part = this._participants[message.message["id"]];
201
+ const part = this._removeParticipant(message.message["id"]);
166
202
  if (part) {
167
- delete this._participants[message.message["id"]];
168
203
  this.emit("participant_removed", { message });
169
204
  }
170
205
  }
171
206
  _onMessagingEnabled(message) {
172
207
  const participants = message.message["participants"];
173
208
  for (const data of participants) {
174
- const rp = new participant_1.RemoteParticipant(this.client, data["id"], data["role"]);
209
+ const rp = new participant_1.RemoteParticipant(this.client, data["id"], data["role"], true);
175
210
  for (const [k, v] of Object.entries(data["attributes"] || {})) {
176
211
  rp._attributes[k] = v;
177
212
  }
@@ -197,17 +232,23 @@ class MessagingClient extends event_emitter_1.EventEmitter {
197
232
  throw new Error("streams are not allowed by this client");
198
233
  }
199
234
  this._onStreamAcceptCallback(reader);
200
- this.sendMessage({
235
+ void this.sendMessage({
201
236
  to: from,
202
237
  type: "stream.accept",
203
238
  message: { stream_id: streamId },
239
+ ignoreOffline: true,
240
+ }).catch((error) => {
241
+ console.warn("unable to send stream response", error);
204
242
  });
205
243
  }
206
244
  catch (e) {
207
- this.sendMessage({
245
+ void this.sendMessage({
208
246
  to: from,
209
247
  type: "stream.reject",
210
248
  message: { stream_id: streamId, error: String(e) },
249
+ ignoreOffline: true,
250
+ }).catch((error) => {
251
+ console.warn("unable to send stream response", error);
211
252
  });
212
253
  }
213
254
  this._streamReaders[streamId] = reader;
@@ -10,7 +10,9 @@ export declare abstract class Participant {
10
10
  }
11
11
  export declare class RemoteParticipant extends Participant {
12
12
  readonly role: string;
13
- constructor(client: RoomClient, id: string, role: string);
13
+ online?: boolean;
14
+ constructor(client: RoomClient, id: string, role: string, online?: boolean);
15
+ _setOnline(online: boolean): void;
14
16
  }
15
17
  export declare class LocalParticipant extends Participant {
16
18
  constructor(client: RoomClient, id: string);
@@ -18,9 +18,13 @@ class Participant {
18
18
  }
19
19
  exports.Participant = Participant;
20
20
  class RemoteParticipant extends Participant {
21
- constructor(client, id, role) {
21
+ constructor(client, id, role, online) {
22
22
  super(client, id);
23
23
  this.role = role;
24
+ this.online = online;
25
+ }
26
+ _setOnline(online) {
27
+ this.online = online;
24
28
  }
25
29
  }
26
30
  exports.RemoteParticipant = RemoteParticipant;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meshagent/meshagent",
3
- "version": "0.31.0",
3
+ "version": "0.31.2",
4
4
  "description": "Meshagent Client",
5
5
  "homepage": "https://github.com/meshagent/meshagent-ts",
6
6
  "scripts": {