@dxos/messaging 0.1.56-main.c787da3 → 0.1.56-main.cf6bb41

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.
@@ -39,9 +39,9 @@ __export(src_exports, {
39
39
  module.exports = __toCommonJS(src_exports);
40
40
 
41
41
  // packages/core/mesh/messaging/src/messenger.ts
42
- var import_tiny_invariant = __toESM(require("tiny-invariant"));
43
42
  var import_async = require("@dxos/async");
44
43
  var import_context = require("@dxos/context");
44
+ var import_invariant = require("@dxos/invariant");
45
45
  var import_keys = require("@dxos/keys");
46
46
  var import_log = require("@dxos/log");
47
47
  var import_protocols = require("@dxos/protocols");
@@ -76,14 +76,14 @@ var Messenger = class {
76
76
  id: traceId
77
77
  }), {
78
78
  F: __dxlog_file,
79
- L: 63,
79
+ L: 62,
80
80
  S: this,
81
81
  C: (f, a) => f(...a)
82
82
  });
83
83
  this._ctx = new import_context.Context({
84
84
  onError: (err) => import_log.log.catch(err, void 0, {
85
85
  F: __dxlog_file,
86
- L: 65,
86
+ L: 64,
87
87
  S: this,
88
88
  C: (f, a) => f(...a)
89
89
  })
@@ -93,7 +93,7 @@ var Messenger = class {
93
93
  from: message.author
94
94
  }, {
95
95
  F: __dxlog_file,
96
- L: 69,
96
+ L: 68,
97
97
  S: this,
98
98
  C: (f, a) => f(...a)
99
99
  });
@@ -104,7 +104,7 @@ var Messenger = class {
104
104
  id: traceId
105
105
  }), {
106
106
  F: __dxlog_file,
107
- L: 74,
107
+ L: 73,
108
108
  S: this,
109
109
  C: (f, a) => f(...a)
110
110
  });
@@ -117,20 +117,36 @@ var Messenger = class {
117
117
  await this._ctx.dispose();
118
118
  }
119
119
  async sendMessage({ author, recipient, payload }) {
120
- (0, import_tiny_invariant.default)(!this._closed, "Closed");
120
+ (0, import_invariant.invariant)(!this._closed, "Closed", {
121
+ F: __dxlog_file,
122
+ L: 85,
123
+ S: this,
124
+ A: [
125
+ "!this._closed",
126
+ "'Closed'"
127
+ ]
128
+ });
121
129
  const messageContext = this._ctx.derive();
122
130
  const reliablePayload = {
123
131
  messageId: import_keys.PublicKey.random(),
124
132
  payload
125
133
  };
126
- (0, import_tiny_invariant.default)(!this._onAckCallbacks.has(reliablePayload.messageId));
134
+ (0, import_invariant.invariant)(!this._onAckCallbacks.has(reliablePayload.messageId), void 0, {
135
+ F: __dxlog_file,
136
+ L: 92,
137
+ S: this,
138
+ A: [
139
+ "!this._onAckCallbacks.has(reliablePayload.messageId!)",
140
+ ""
141
+ ]
142
+ });
127
143
  (0, import_log.log)("send message", {
128
144
  messageId: reliablePayload.messageId,
129
145
  author,
130
146
  recipient
131
147
  }, {
132
148
  F: __dxlog_file,
133
- L: 94,
149
+ L: 93,
134
150
  S: this,
135
151
  C: (f, a) => f(...a)
136
152
  });
@@ -145,7 +161,7 @@ var Messenger = class {
145
161
  messageId: reliablePayload.messageId
146
162
  }, {
147
163
  F: __dxlog_file,
148
- L: 108,
164
+ L: 107,
149
165
  S: this,
150
166
  C: (f, a) => f(...a)
151
167
  });
@@ -157,7 +173,7 @@ var Messenger = class {
157
173
  err
158
174
  }, {
159
175
  F: __dxlog_file,
160
- L: 110,
176
+ L: 109,
161
177
  S: this,
162
178
  C: (f, a) => f(...a)
163
179
  }));
@@ -167,7 +183,7 @@ var Messenger = class {
167
183
  messageId: reliablePayload.messageId
168
184
  }, {
169
185
  F: __dxlog_file,
170
- L: 119,
186
+ L: 118,
171
187
  S: this,
172
188
  C: (f, a) => f(...a)
173
189
  });
@@ -192,7 +208,15 @@ var Messenger = class {
192
208
  * @param payloadType if not specified, onMessage will be subscribed to all types of messages.
193
209
  */
194
210
  async listen({ peerId, payloadType, onMessage }) {
195
- (0, import_tiny_invariant.default)(!this._closed, "Closed");
211
+ (0, import_invariant.invariant)(!this._closed, "Closed", {
212
+ F: __dxlog_file,
213
+ L: 150,
214
+ S: this,
215
+ A: [
216
+ "!this._closed",
217
+ "'Closed'"
218
+ ]
219
+ });
196
220
  await this._signalManager.subscribeMessages(peerId);
197
221
  let listeners;
198
222
  if (!payloadType) {
@@ -248,7 +272,15 @@ var Messenger = class {
248
272
  }
249
273
  }
250
274
  async _handleReliablePayload({ author, recipient, payload }) {
251
- (0, import_tiny_invariant.default)(payload.type_url === "dxos.mesh.messaging.ReliablePayload");
275
+ (0, import_invariant.invariant)(payload.type_url === "dxos.mesh.messaging.ReliablePayload", void 0, {
276
+ F: __dxlog_file,
277
+ L: 211,
278
+ S: this,
279
+ A: [
280
+ "payload.type_url === 'dxos.mesh.messaging.ReliablePayload'",
281
+ ""
282
+ ]
283
+ });
252
284
  const reliablePayload = ReliablePayload.decode(payload.value, {
253
285
  preserveAny: true
254
286
  });
@@ -256,7 +288,7 @@ var Messenger = class {
256
288
  messageId: reliablePayload.messageId
257
289
  }, {
258
290
  F: __dxlog_file,
259
- L: 215,
291
+ L: 214,
260
292
  S: this,
261
293
  C: (f, a) => f(...a)
262
294
  });
@@ -276,9 +308,16 @@ var Messenger = class {
276
308
  });
277
309
  }
278
310
  async _handleAcknowledgement({ payload }) {
279
- var _a;
280
- (0, import_tiny_invariant.default)(payload.type_url === "dxos.mesh.messaging.Acknowledgement");
281
- (_a = this._onAckCallbacks.get(Acknowledgement.decode(payload.value).messageId)) == null ? void 0 : _a();
311
+ (0, import_invariant.invariant)(payload.type_url === "dxos.mesh.messaging.Acknowledgement", void 0, {
312
+ F: __dxlog_file,
313
+ L: 236,
314
+ S: this,
315
+ A: [
316
+ "payload.type_url === 'dxos.mesh.messaging.Acknowledgement'",
317
+ ""
318
+ ]
319
+ });
320
+ this._onAckCallbacks.get(Acknowledgement.decode(payload.value).messageId)?.();
282
321
  }
283
322
  async _sendAcknowledgement({ author, recipient, messageId }) {
284
323
  (0, import_log.log)("sending ACK", {
@@ -287,7 +326,7 @@ var Messenger = class {
287
326
  to: author
288
327
  }, {
289
328
  F: __dxlog_file,
290
- L: 250,
329
+ L: 249,
291
330
  S: this,
292
331
  C: (f, a) => f(...a)
293
332
  });
@@ -326,9 +365,9 @@ var Messenger = class {
326
365
  };
327
366
 
328
367
  // packages/core/mesh/messaging/src/signal-client/signal-client.ts
329
- var import_tiny_invariant3 = __toESM(require("tiny-invariant"));
330
368
  var import_async3 = require("@dxos/async");
331
369
  var import_context2 = require("@dxos/context");
370
+ var import_invariant3 = require("@dxos/invariant");
332
371
  var import_keys3 = require("@dxos/keys");
333
372
  var import_log3 = require("@dxos/log");
334
373
  var import_protocols3 = require("@dxos/protocols");
@@ -337,8 +376,8 @@ var import_util2 = require("@dxos/util");
337
376
 
338
377
  // packages/core/mesh/messaging/src/signal-client/signal-rpc-client.ts
339
378
  var import_isomorphic_ws = __toESM(require("isomorphic-ws"));
340
- var import_tiny_invariant2 = __toESM(require("tiny-invariant"));
341
379
  var import_async2 = require("@dxos/async");
380
+ var import_invariant2 = require("@dxos/invariant");
342
381
  var import_keys2 = require("@dxos/keys");
343
382
  var import_log2 = require("@dxos/log");
344
383
  var import_protocols2 = require("@dxos/protocols");
@@ -396,7 +435,6 @@ var SignalRPCClient = class {
396
435
  }
397
436
  });
398
437
  this._socket.onopen = async () => {
399
- var _a, _b, _c, _d;
400
438
  try {
401
439
  await this._rpc.open();
402
440
  (0, import_log2.log)(`RPC open ${this._url}`, void 0, {
@@ -405,32 +443,30 @@ var SignalRPCClient = class {
405
443
  S: this,
406
444
  C: (f, a) => f(...a)
407
445
  });
408
- (_b = (_a = this._callbacks).onConnected) == null ? void 0 : _b.call(_a);
446
+ this._callbacks.onConnected?.();
409
447
  this._connectTrigger.wake();
410
448
  } catch (err) {
411
- (_d = (_c = this._callbacks).onError) == null ? void 0 : _d.call(_c, err);
449
+ this._callbacks.onError?.(err);
412
450
  }
413
451
  };
414
452
  this._socket.onclose = async () => {
415
- var _a, _b;
416
453
  (0, import_log2.log)(`Disconnected ${this._url}`, void 0, {
417
454
  F: __dxlog_file2,
418
455
  L: 98,
419
456
  S: this,
420
457
  C: (f, a) => f(...a)
421
458
  });
422
- (_b = (_a = this._callbacks).onDisconnected) == null ? void 0 : _b.call(_a);
459
+ this._callbacks.onDisconnected?.();
423
460
  await this.close();
424
461
  };
425
462
  this._socket.onerror = async (event) => {
426
- var _a, _b, _c, _d, _e;
427
463
  if (this._closed) {
428
464
  return;
429
465
  }
430
- (_c = (_b = this._callbacks).onError) == null ? void 0 : _c.call(_b, (_a = event.error) != null ? _a : new Error(event.message));
466
+ this._callbacks.onError?.(event.error ?? new Error(event.message));
431
467
  this._connectTrigger.reset();
432
468
  try {
433
- await ((_d = this._rpc) == null ? void 0 : _d.close());
469
+ await this._rpc?.close();
434
470
  } catch (err) {
435
471
  import_log2.log.catch(err, void 0, {
436
472
  F: __dxlog_file2,
@@ -440,7 +476,7 @@ var SignalRPCClient = class {
440
476
  });
441
477
  }
442
478
  this._closed = true;
443
- import_log2.log.warn((_e = event.message) != null ? _e : "Socket error", {
479
+ import_log2.log.warn(event.message ?? "Socket error", {
444
480
  url: this._url
445
481
  }, {
446
482
  F: __dxlog_file2,
@@ -459,11 +495,10 @@ var SignalRPCClient = class {
459
495
  });
460
496
  }
461
497
  async close() {
462
- var _a, _b;
463
498
  this._closed = true;
464
499
  try {
465
- await ((_a = this._rpc) == null ? void 0 : _a.close());
466
- (_b = this._socket) == null ? void 0 : _b.close();
500
+ await this._rpc?.close();
501
+ this._socket?.close();
467
502
  } catch (err) {
468
503
  import_log2.log.warn("close error", err, {
469
504
  F: __dxlog_file2,
@@ -484,8 +519,24 @@ var SignalRPCClient = class {
484
519
  C: (f, a) => f(...a)
485
520
  });
486
521
  await this._connectTrigger.wait();
487
- (0, import_tiny_invariant2.default)(!this._closed, "SignalRPCClient is closed");
488
- (0, import_tiny_invariant2.default)(this._rpc, "Rpc is not initialized");
522
+ (0, import_invariant2.invariant)(!this._closed, "SignalRPCClient is closed", {
523
+ F: __dxlog_file2,
524
+ L: 138,
525
+ S: this,
526
+ A: [
527
+ "!this._closed",
528
+ "'SignalRPCClient is closed'"
529
+ ]
530
+ });
531
+ (0, import_invariant2.invariant)(this._rpc, "Rpc is not initialized", {
532
+ F: __dxlog_file2,
533
+ L: 139,
534
+ S: this,
535
+ A: [
536
+ "this._rpc",
537
+ "'Rpc is not initialized'"
538
+ ]
539
+ });
489
540
  const swarmStream = this._rpc.rpc.Signal.join({
490
541
  swarm: topic.asUint8Array(),
491
542
  peer: peerId.asUint8Array()
@@ -502,9 +553,25 @@ var SignalRPCClient = class {
502
553
  S: this,
503
554
  C: (f, a) => f(...a)
504
555
  });
505
- (0, import_tiny_invariant2.default)(!this._closed, "SignalRPCClient is closed");
556
+ (0, import_invariant2.invariant)(!this._closed, "SignalRPCClient is closed", {
557
+ F: __dxlog_file2,
558
+ L: 150,
559
+ S: this,
560
+ A: [
561
+ "!this._closed",
562
+ "'SignalRPCClient is closed'"
563
+ ]
564
+ });
506
565
  await this._connectTrigger.wait();
507
- (0, import_tiny_invariant2.default)(this._rpc, "Rpc is not initialized");
566
+ (0, import_invariant2.invariant)(this._rpc, "Rpc is not initialized", {
567
+ F: __dxlog_file2,
568
+ L: 152,
569
+ S: this,
570
+ A: [
571
+ "this._rpc",
572
+ "'Rpc is not initialized'"
573
+ ]
574
+ });
508
575
  const messageStream = this._rpc.rpc.Signal.receiveMessages({
509
576
  peer: peerId.asUint8Array()
510
577
  });
@@ -522,9 +589,25 @@ var SignalRPCClient = class {
522
589
  S: this,
523
590
  C: (f, a) => f(...a)
524
591
  });
525
- (0, import_tiny_invariant2.default)(!this._closed, "SignalRPCClient is closed");
592
+ (0, import_invariant2.invariant)(!this._closed, "SignalRPCClient is closed", {
593
+ F: __dxlog_file2,
594
+ L: 162,
595
+ S: this,
596
+ A: [
597
+ "!this._closed",
598
+ "'SignalRPCClient is closed'"
599
+ ]
600
+ });
526
601
  await this._connectTrigger.wait();
527
- (0, import_tiny_invariant2.default)(this._rpc, "Rpc is not initialized");
602
+ (0, import_invariant2.invariant)(this._rpc, "Rpc is not initialized", {
603
+ F: __dxlog_file2,
604
+ L: 164,
605
+ S: this,
606
+ A: [
607
+ "this._rpc",
608
+ "'Rpc is not initialized'"
609
+ ]
610
+ });
528
611
  await this._rpc.rpc.Signal.sendMessage({
529
612
  author: author.asUint8Array(),
530
613
  recipient: recipient.asUint8Array(),
@@ -550,8 +633,8 @@ var SignalClient = class {
550
633
  this._onSwarmEvent = _onSwarmEvent;
551
634
  this._state = import_signal.SignalState.CLOSED;
552
635
  this._reconnectAfter = DEFAULT_RECONNECT_TIMEOUT;
553
- this._connectionStarted = new Date();
554
- this._lastStateChange = new Date();
636
+ this._connectionStarted = /* @__PURE__ */ new Date();
637
+ this._lastStateChange = /* @__PURE__ */ new Date();
555
638
  this._clientReady = new import_async3.Trigger();
556
639
  this.statusChanged = new import_async3.Event();
557
640
  this.commandTrace = new import_async3.Event();
@@ -577,7 +660,7 @@ var SignalClient = class {
577
660
  id: this._instanceId
578
661
  }), {
579
662
  F: __dxlog_file3,
580
- L: 136,
663
+ L: 135,
581
664
  S: this,
582
665
  C: (f, a) => f(...a)
583
666
  });
@@ -589,14 +672,13 @@ var SignalClient = class {
589
672
  }
590
673
  this._ctx = new import_context2.Context({
591
674
  onError: (err) => {
592
- var _a;
593
- if (this._state === import_signal.SignalState.CLOSED || ((_a = this._ctx) == null ? void 0 : _a.disposed)) {
675
+ if (this._state === import_signal.SignalState.CLOSED || this._ctx?.disposed) {
594
676
  return;
595
677
  }
596
678
  if (this._state === import_signal.SignalState.CONNECTED) {
597
679
  import_log3.log.warn("SignalClient error:", err, {
598
680
  F: __dxlog_file3,
599
- L: 148,
681
+ L: 147,
600
682
  S: this,
601
683
  C: (f, a) => f(...a)
602
684
  });
@@ -623,16 +705,15 @@ var SignalClient = class {
623
705
  id: this._instanceId
624
706
  }), {
625
707
  F: __dxlog_file3,
626
- L: 177,
708
+ L: 176,
627
709
  S: this,
628
710
  C: (f, a) => f(...a)
629
711
  });
630
712
  }
631
713
  async close() {
632
- var _a, _b;
633
714
  (0, import_log3.log)("closing...", void 0, {
634
715
  F: __dxlog_file3,
635
- L: 181,
716
+ L: 180,
636
717
  S: this,
637
718
  C: (f, a) => f(...a)
638
719
  });
@@ -641,24 +722,23 @@ var SignalClient = class {
641
722
  ].includes(this._state)) {
642
723
  return;
643
724
  }
644
- await ((_a = this._ctx) == null ? void 0 : _a.dispose());
725
+ await this._ctx?.dispose();
645
726
  this._clientReady.reset();
646
- await ((_b = this._client) == null ? void 0 : _b.close());
727
+ await this._client?.close();
647
728
  this._client = void 0;
648
729
  this._setState(import_signal.SignalState.CLOSED);
649
730
  (0, import_log3.log)("closed", void 0, {
650
731
  F: __dxlog_file3,
651
- L: 192,
732
+ L: 191,
652
733
  S: this,
653
734
  C: (f, a) => f(...a)
654
735
  });
655
736
  }
656
737
  getStatus() {
657
- var _a;
658
738
  return {
659
739
  host: this._host,
660
740
  state: this._state,
661
- error: (_a = this._lastError) == null ? void 0 : _a.message,
741
+ error: this._lastError?.message,
662
742
  reconnectIn: this._reconnectAfter,
663
743
  connectionStarted: this._connectionStarted,
664
744
  lastStateChange: this._lastStateChange
@@ -670,7 +750,7 @@ var SignalClient = class {
670
750
  peerId
671
751
  }, {
672
752
  F: __dxlog_file3,
673
- L: 207,
753
+ L: 206,
674
754
  S: this,
675
755
  C: (f, a) => f(...a)
676
756
  });
@@ -682,21 +762,20 @@ var SignalClient = class {
682
762
  this._reconcileTask.schedule();
683
763
  }
684
764
  async leave({ topic, peerId }) {
685
- var _a;
686
765
  this._performance.leaveCounter++;
687
766
  (0, import_log3.log)("leaving", {
688
767
  topic,
689
768
  peerId
690
769
  }, {
691
770
  F: __dxlog_file3,
692
- L: 215,
771
+ L: 214,
693
772
  S: this,
694
773
  C: (f, a) => f(...a)
695
774
  });
696
- (_a = this._swarmStreams.get({
775
+ this._swarmStreams.get({
697
776
  topic,
698
777
  peerId
699
- })) == null ? void 0 : _a.close();
778
+ })?.close();
700
779
  this._swarmStreams.delete({
701
780
  topic,
702
781
  peerId
@@ -709,7 +788,15 @@ var SignalClient = class {
709
788
  async sendMessage(msg) {
710
789
  this._performance.sentMessages++;
711
790
  await this._clientReady.wait();
712
- (0, import_tiny_invariant3.default)(this._state === import_signal.SignalState.CONNECTED, "Not connected to Signal Server");
791
+ (0, import_invariant3.invariant)(this._state === import_signal.SignalState.CONNECTED, "Not connected to Signal Server", {
792
+ F: __dxlog_file3,
793
+ L: 224,
794
+ S: this,
795
+ A: [
796
+ "this._state === SignalState.CONNECTED",
797
+ "'Not connected to Signal Server'"
798
+ ]
799
+ });
713
800
  await this._client.sendMessage(msg);
714
801
  }
715
802
  async subscribeMessages(peerId) {
@@ -717,7 +804,7 @@ var SignalClient = class {
717
804
  peerId
718
805
  }, {
719
806
  F: __dxlog_file3,
720
- L: 230,
807
+ L: 229,
721
808
  S: this,
722
809
  C: (f, a) => f(...a)
723
810
  });
@@ -727,19 +814,18 @@ var SignalClient = class {
727
814
  this._reconcileTask.schedule();
728
815
  }
729
816
  async unsubscribeMessages(peerId) {
730
- var _a;
731
817
  (0, import_log3.log)("unsubscribing from messages", {
732
818
  peerId
733
819
  }, {
734
820
  F: __dxlog_file3,
735
- L: 236,
821
+ L: 235,
736
822
  S: this,
737
823
  C: (f, a) => f(...a)
738
824
  });
739
825
  this._subscribedMessages.delete({
740
826
  peerId
741
827
  });
742
- (_a = this._messageStreams.get(peerId)) == null ? void 0 : _a.close();
828
+ this._messageStreams.get(peerId)?.close();
743
829
  this._messageStreams.delete(peerId);
744
830
  }
745
831
  _scheduleReconcileAfterError() {
@@ -749,12 +835,12 @@ var SignalClient = class {
749
835
  }
750
836
  _setState(newState) {
751
837
  this._state = newState;
752
- this._lastStateChange = new Date();
838
+ this._lastStateChange = /* @__PURE__ */ new Date();
753
839
  (0, import_log3.log)("signal state changed", {
754
840
  status: this.getStatus()
755
841
  }, {
756
842
  F: __dxlog_file3,
757
- L: 255,
843
+ L: 254,
758
844
  S: this,
759
845
  C: (f, a) => f(...a)
760
846
  });
@@ -766,17 +852,25 @@ var SignalClient = class {
766
852
  state: this._state
767
853
  }, {
768
854
  F: __dxlog_file3,
769
- L: 260,
855
+ L: 259,
770
856
  S: this,
771
857
  C: (f, a) => f(...a)
772
858
  });
773
- (0, import_tiny_invariant3.default)(!this._client, "Client already created");
774
- this._connectionStarted = new Date();
859
+ (0, import_invariant3.invariant)(!this._client, "Client already created", {
860
+ F: __dxlog_file3,
861
+ L: 260,
862
+ S: this,
863
+ A: [
864
+ "!this._client",
865
+ "'Client already created'"
866
+ ]
867
+ });
868
+ this._connectionStarted = /* @__PURE__ */ new Date();
775
869
  this._connectionCtx = this._ctx.derive();
776
870
  this._connectionCtx.onDispose(() => {
777
871
  (0, import_log3.log)("connection context disposed", void 0, {
778
872
  F: __dxlog_file3,
779
- L: 268,
873
+ L: 267,
780
874
  S: this,
781
875
  C: (f, a) => f(...a)
782
876
  });
@@ -792,7 +886,7 @@ var SignalClient = class {
792
886
  onConnected: () => {
793
887
  (0, import_log3.log)("socket connected", void 0, {
794
888
  F: __dxlog_file3,
795
- L: 280,
889
+ L: 279,
796
890
  S: this,
797
891
  C: (f, a) => f(...a)
798
892
  });
@@ -807,7 +901,7 @@ var SignalClient = class {
807
901
  state: this._state
808
902
  }, {
809
903
  F: __dxlog_file3,
810
- L: 289,
904
+ L: 288,
811
905
  S: this,
812
906
  C: (f, a) => f(...a)
813
907
  });
@@ -827,7 +921,7 @@ var SignalClient = class {
827
921
  state: this._state
828
922
  }, {
829
923
  F: __dxlog_file3,
830
- L: 304,
924
+ L: 303,
831
925
  S: this,
832
926
  C: (f, a) => f(...a)
833
927
  });
@@ -854,12 +948,11 @@ var SignalClient = class {
854
948
  this._reconnectAfter = Math.min(this._reconnectAfter, MAX_RECONNECT_TIMEOUT);
855
949
  }
856
950
  async _reconnect() {
857
- var _a, _b;
858
951
  (0, import_log3.log)(`reconnecting in ${this._reconnectAfter}ms`, {
859
952
  state: this._state
860
953
  }, {
861
954
  F: __dxlog_file3,
862
- L: 331,
955
+ L: 330,
863
956
  S: this,
864
957
  C: (f, a) => f(...a)
865
958
  });
@@ -867,7 +960,7 @@ var SignalClient = class {
867
960
  if (this._state === import_signal.SignalState.RECONNECTING) {
868
961
  import_log3.log.warn("Signal api already reconnecting.", void 0, {
869
962
  F: __dxlog_file3,
870
- L: 335,
963
+ L: 334,
871
964
  S: this,
872
965
  C: (f, a) => f(...a)
873
966
  });
@@ -877,8 +970,8 @@ var SignalClient = class {
877
970
  return;
878
971
  }
879
972
  this._clientReady.reset();
880
- await ((_a = this._connectionCtx) == null ? void 0 : _a.dispose());
881
- (_b = this._client) == null ? void 0 : _b.close().catch(() => {
973
+ await this._connectionCtx?.dispose();
974
+ this._client?.close().catch(() => {
882
975
  });
883
976
  this._client = void 0;
884
977
  await (0, import_context2.cancelWithContext)(this._ctx, (0, import_async3.sleep)(this._reconnectAfter));
@@ -886,10 +979,17 @@ var SignalClient = class {
886
979
  this._createClient();
887
980
  }
888
981
  async _reconcileSwarmSubscriptions() {
889
- var _a;
890
982
  await (0, import_async3.asyncTimeout)((0, import_context2.cancelWithContext)(this._connectionCtx, this._clientReady.wait()), 5e3);
891
983
  const client = this._client;
892
- (0, import_tiny_invariant3.default)(this._state === import_signal.SignalState.CONNECTED, "Not connected to Signal Server");
984
+ (0, import_invariant3.invariant)(this._state === import_signal.SignalState.CONNECTED, "Not connected to Signal Server", {
985
+ F: __dxlog_file3,
986
+ L: 359,
987
+ S: this,
988
+ A: [
989
+ "this._state === SignalState.CONNECTED",
990
+ "'Not connected to Signal Server'"
991
+ ]
992
+ });
893
993
  for (const { topic, peerId } of this._swarmStreams.keys()) {
894
994
  if (this._joinedTopics.has({
895
995
  topic,
@@ -897,10 +997,10 @@ var SignalClient = class {
897
997
  })) {
898
998
  continue;
899
999
  }
900
- (_a = this._swarmStreams.get({
1000
+ this._swarmStreams.get({
901
1001
  topic,
902
1002
  peerId
903
- })) == null ? void 0 : _a.close();
1003
+ })?.close();
904
1004
  this._swarmStreams.delete({
905
1005
  topic,
906
1006
  peerId
@@ -922,7 +1022,7 @@ var SignalClient = class {
922
1022
  swarmEvent
923
1023
  }, {
924
1024
  F: __dxlog_file3,
925
- L: 387,
1025
+ L: 386,
926
1026
  S: this,
927
1027
  C: (f, a) => f(...a)
928
1028
  });
@@ -938,17 +1038,24 @@ var SignalClient = class {
938
1038
  }
939
1039
  }
940
1040
  async _reconcileMessageSubscriptions() {
941
- var _a;
942
1041
  await (0, import_async3.asyncTimeout)((0, import_context2.cancelWithContext)(this._connectionCtx, this._clientReady.wait()), 5e3);
943
1042
  const client = this._client;
944
- (0, import_tiny_invariant3.default)(this._state === import_signal.SignalState.CONNECTED, "Not connected to Signal Server");
1043
+ (0, import_invariant3.invariant)(this._state === import_signal.SignalState.CONNECTED, "Not connected to Signal Server", {
1044
+ F: __dxlog_file3,
1045
+ L: 399,
1046
+ S: this,
1047
+ A: [
1048
+ "this._state === SignalState.CONNECTED",
1049
+ "'Not connected to Signal Server'"
1050
+ ]
1051
+ });
945
1052
  for (const peerId of this._messageStreams.keys()) {
946
1053
  if (this._subscribedMessages.has({
947
1054
  peerId
948
1055
  })) {
949
1056
  continue;
950
1057
  }
951
- (_a = this._messageStreams.get(peerId)) == null ? void 0 : _a.close();
1058
+ this._messageStreams.get(peerId)?.close();
952
1059
  this._messageStreams.delete(peerId);
953
1060
  }
954
1061
  for (const { peerId } of this._subscribedMessages.values()) {
@@ -970,11 +1077,12 @@ var SignalClient = class {
970
1077
  };
971
1078
 
972
1079
  // packages/core/mesh/messaging/src/signal-manager/memory-signal-manager.ts
973
- var import_tiny_invariant4 = __toESM(require("tiny-invariant"));
974
1080
  var import_async4 = require("@dxos/async");
975
1081
  var import_context3 = require("@dxos/context");
1082
+ var import_invariant4 = require("@dxos/invariant");
976
1083
  var import_keys4 = require("@dxos/keys");
977
1084
  var import_log4 = require("@dxos/log");
1085
+ var import_protocols4 = require("@dxos/protocols");
978
1086
  var import_util3 = require("@dxos/util");
979
1087
  var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/mesh/messaging/src/signal-manager/memory-signal-manager.ts";
980
1088
  var MemorySignalManagerContext = class {
@@ -996,6 +1104,7 @@ var MemorySignalManager = class {
996
1104
  this.swarmEvent = new import_async4.Event();
997
1105
  this.onMessage = new import_async4.Event();
998
1106
  this._joinedSwarms = new import_util3.ComplexSet(({ topic, peerId }) => topic.toHex() + peerId.toHex());
1107
+ this._freezeTrigger = new import_async4.Trigger().wake();
999
1108
  this._ctx = new import_context3.Context();
1000
1109
  this._ctx.onDispose(this._context.swarmEvent.on((data) => this.swarmEvent.emit(data)));
1001
1110
  }
@@ -1026,7 +1135,15 @@ var MemorySignalManager = class {
1026
1135
  return [];
1027
1136
  }
1028
1137
  async join({ topic, peerId }) {
1029
- (0, import_tiny_invariant4.default)(!this._ctx.disposed, "Closed");
1138
+ (0, import_invariant4.invariant)(!this._ctx.disposed, "Closed", {
1139
+ F: __dxlog_file4,
1140
+ L: 104,
1141
+ S: this,
1142
+ A: [
1143
+ "!this._ctx.disposed",
1144
+ "'Closed'"
1145
+ ]
1146
+ });
1030
1147
  this._joinedSwarms.add({
1031
1148
  topic,
1032
1149
  peerId
@@ -1040,7 +1157,7 @@ var MemorySignalManager = class {
1040
1157
  swarmEvent: {
1041
1158
  peerAvailable: {
1042
1159
  peer: peerId.asUint8Array(),
1043
- since: new Date()
1160
+ since: /* @__PURE__ */ new Date()
1044
1161
  }
1045
1162
  }
1046
1163
  });
@@ -1051,7 +1168,7 @@ var MemorySignalManager = class {
1051
1168
  swarmEvent: {
1052
1169
  peerAvailable: {
1053
1170
  peer: peerId2.asUint8Array(),
1054
- since: new Date()
1171
+ since: /* @__PURE__ */ new Date()
1055
1172
  }
1056
1173
  }
1057
1174
  });
@@ -1059,7 +1176,15 @@ var MemorySignalManager = class {
1059
1176
  }
1060
1177
  }
1061
1178
  async leave({ topic, peerId }) {
1062
- (0, import_tiny_invariant4.default)(!this._ctx.disposed, "Closed");
1179
+ (0, import_invariant4.invariant)(!this._ctx.disposed, "Closed", {
1180
+ F: __dxlog_file4,
1181
+ L: 140,
1182
+ S: this,
1183
+ A: [
1184
+ "!this._ctx.disposed",
1185
+ "'Closed'"
1186
+ ]
1187
+ });
1063
1188
  this._joinedSwarms.delete({
1064
1189
  topic,
1065
1190
  peerId
@@ -1079,35 +1204,105 @@ var MemorySignalManager = class {
1079
1204
  });
1080
1205
  }
1081
1206
  async sendMessage({ author, recipient, payload }) {
1082
- var _a;
1083
- (0, import_tiny_invariant4.default)(recipient);
1084
- (0, import_tiny_invariant4.default)(!this._ctx.disposed, "Closed");
1085
- if (!this._context.connections.has(recipient)) {
1207
+ (0, import_log4.log)("send message", {
1208
+ author,
1209
+ recipient,
1210
+ ...dec(payload)
1211
+ }, {
1212
+ F: __dxlog_file4,
1213
+ L: 160,
1214
+ S: this,
1215
+ C: (f, a) => f(...a)
1216
+ });
1217
+ (0, import_invariant4.invariant)(recipient, void 0, {
1218
+ F: __dxlog_file4,
1219
+ L: 162,
1220
+ S: this,
1221
+ A: [
1222
+ "recipient",
1223
+ ""
1224
+ ]
1225
+ });
1226
+ (0, import_invariant4.invariant)(!this._ctx.disposed, "Closed", {
1227
+ F: __dxlog_file4,
1228
+ L: 163,
1229
+ S: this,
1230
+ A: [
1231
+ "!this._ctx.disposed",
1232
+ "'Closed'"
1233
+ ]
1234
+ });
1235
+ await this._freezeTrigger.wait();
1236
+ const remote = this._context.connections.get(recipient);
1237
+ if (!remote) {
1086
1238
  import_log4.log.warn("recipient is not subscribed for messages", {
1087
1239
  author,
1088
1240
  recipient
1089
1241
  }, {
1090
1242
  F: __dxlog_file4,
1091
- L: 160,
1243
+ L: 169,
1092
1244
  S: this,
1093
1245
  C: (f, a) => f(...a)
1094
1246
  });
1095
1247
  return;
1096
1248
  }
1097
- if (!((_a = this._context.connections.get(recipient)) == null ? void 0 : _a._ctx.disposed)) {
1098
- this._context.connections.get(recipient).onMessage.emit({
1249
+ if (remote._ctx.disposed) {
1250
+ import_log4.log.warn("recipient is disposed", {
1251
+ author,
1252
+ recipient
1253
+ }, {
1254
+ F: __dxlog_file4,
1255
+ L: 174,
1256
+ S: this,
1257
+ C: (f, a) => f(...a)
1258
+ });
1259
+ return;
1260
+ }
1261
+ remote._freezeTrigger.wait().then(() => {
1262
+ if (remote._ctx.disposed) {
1263
+ import_log4.log.warn("recipient is disposed", {
1264
+ author,
1265
+ recipient
1266
+ }, {
1267
+ F: __dxlog_file4,
1268
+ L: 182,
1269
+ S: this,
1270
+ C: (f, a) => f(...a)
1271
+ });
1272
+ return;
1273
+ }
1274
+ (0, import_log4.log)("receive message", {
1275
+ author,
1276
+ recipient,
1277
+ ...dec(payload)
1278
+ }, {
1279
+ F: __dxlog_file4,
1280
+ L: 186,
1281
+ S: this,
1282
+ C: (f, a) => f(...a)
1283
+ });
1284
+ remote.onMessage.emit({
1099
1285
  author,
1100
1286
  recipient,
1101
1287
  payload
1102
1288
  });
1103
- }
1289
+ }).catch((err) => {
1290
+ import_log4.log.error("error while waiting for freeze", {
1291
+ err
1292
+ }, {
1293
+ F: __dxlog_file4,
1294
+ L: 191,
1295
+ S: this,
1296
+ C: (f, a) => f(...a)
1297
+ });
1298
+ });
1104
1299
  }
1105
1300
  async subscribeMessages(peerId) {
1106
1301
  (0, import_log4.log)("subscribing", {
1107
1302
  peerId
1108
1303
  }, {
1109
1304
  F: __dxlog_file4,
1110
- L: 170,
1305
+ L: 196,
1111
1306
  S: this,
1112
1307
  C: (f, a) => f(...a)
1113
1308
  });
@@ -1118,21 +1313,40 @@ var MemorySignalManager = class {
1118
1313
  peerId
1119
1314
  }, {
1120
1315
  F: __dxlog_file4,
1121
- L: 175,
1316
+ L: 201,
1122
1317
  S: this,
1123
1318
  C: (f, a) => f(...a)
1124
1319
  });
1125
1320
  this._context.connections.delete(peerId);
1126
1321
  }
1322
+ freeze() {
1323
+ this._freezeTrigger.reset();
1324
+ }
1325
+ unfreeze() {
1326
+ this._freezeTrigger.wake();
1327
+ }
1328
+ };
1329
+ var dec = (payload) => {
1330
+ if (!payload.type_url.endsWith("ReliablePayload")) {
1331
+ return {};
1332
+ }
1333
+ const relPayload = import_protocols4.schema.getCodecForType("dxos.mesh.messaging.ReliablePayload").decode(payload.value);
1334
+ if (typeof relPayload?.payload?.data === "object") {
1335
+ return {
1336
+ payload: Object.keys(relPayload?.payload?.data)[0],
1337
+ sessionId: relPayload?.payload?.sessionId
1338
+ };
1339
+ }
1340
+ return {};
1127
1341
  };
1128
1342
 
1129
1343
  // packages/core/mesh/messaging/src/signal-manager/websocket-signal-manager.ts
1130
- var import_tiny_invariant5 = __toESM(require("tiny-invariant"));
1131
1344
  var import_async5 = require("@dxos/async");
1132
1345
  var import_context4 = require("@dxos/context");
1346
+ var import_invariant5 = require("@dxos/invariant");
1133
1347
  var import_keys5 = require("@dxos/keys");
1134
1348
  var import_log5 = require("@dxos/log");
1135
- var import_protocols4 = require("@dxos/protocols");
1349
+ var import_protocols5 = require("@dxos/protocols");
1136
1350
  function _ts_decorate(decorators, target, key, desc) {
1137
1351
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1138
1352
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -1159,7 +1373,7 @@ var WebsocketSignalManager = class {
1159
1373
  hosts: this._hosts
1160
1374
  }, {
1161
1375
  F: __dxlog_file5,
1162
- L: 47,
1376
+ L: 46,
1163
1377
  S: this,
1164
1378
  C: (f, a) => f(...a)
1165
1379
  });
@@ -1181,15 +1395,15 @@ var WebsocketSignalManager = class {
1181
1395
  hosts: this._hosts
1182
1396
  }, {
1183
1397
  F: __dxlog_file5,
1184
- L: 69,
1398
+ L: 68,
1185
1399
  S: this,
1186
1400
  C: (f, a) => f(...a)
1187
1401
  });
1188
- import_log5.log.trace("dxos.mesh.websocket-signal-manager.open", import_protocols4.trace.begin({
1402
+ import_log5.log.trace("dxos.mesh.websocket-signal-manager.open", import_protocols5.trace.begin({
1189
1403
  id: this._instanceId
1190
1404
  }), {
1191
1405
  F: __dxlog_file5,
1192
- L: 70,
1406
+ L: 69,
1193
1407
  S: this,
1194
1408
  C: (f, a) => f(...a)
1195
1409
  });
@@ -1198,11 +1412,11 @@ var WebsocketSignalManager = class {
1198
1412
  ...this._servers.values()
1199
1413
  ].forEach((server) => server.open());
1200
1414
  this._opened = true;
1201
- import_log5.log.trace("dxos.mesh.websocket-signal-manager.open", import_protocols4.trace.end({
1415
+ import_log5.log.trace("dxos.mesh.websocket-signal-manager.open", import_protocols5.trace.end({
1202
1416
  id: this._instanceId
1203
1417
  }), {
1204
1418
  F: __dxlog_file5,
1205
- L: 77,
1419
+ L: 76,
1206
1420
  S: this,
1207
1421
  C: (f, a) => f(...a)
1208
1422
  });
@@ -1224,11 +1438,19 @@ var WebsocketSignalManager = class {
1224
1438
  peerId
1225
1439
  }, {
1226
1440
  F: __dxlog_file5,
1227
- L: 97,
1441
+ L: 96,
1228
1442
  S: this,
1229
1443
  C: (f, a) => f(...a)
1230
1444
  });
1231
- (0, import_tiny_invariant5.default)(this._opened, "Closed");
1445
+ (0, import_invariant5.invariant)(this._opened, "Closed", {
1446
+ F: __dxlog_file5,
1447
+ L: 97,
1448
+ S: this,
1449
+ A: [
1450
+ "this._opened",
1451
+ "'Closed'"
1452
+ ]
1453
+ });
1232
1454
  await this._forEachServer((server) => server.join({
1233
1455
  topic,
1234
1456
  peerId
@@ -1240,11 +1462,19 @@ var WebsocketSignalManager = class {
1240
1462
  peerId
1241
1463
  }, {
1242
1464
  F: __dxlog_file5,
1243
- L: 104,
1465
+ L: 103,
1244
1466
  S: this,
1245
1467
  C: (f, a) => f(...a)
1246
1468
  });
1247
- (0, import_tiny_invariant5.default)(this._opened, "Closed");
1469
+ (0, import_invariant5.invariant)(this._opened, "Closed", {
1470
+ F: __dxlog_file5,
1471
+ L: 104,
1472
+ S: this,
1473
+ A: [
1474
+ "this._opened",
1475
+ "'Closed'"
1476
+ ]
1477
+ });
1248
1478
  await this._forEachServer((server) => server.leave({
1249
1479
  topic,
1250
1480
  peerId
@@ -1253,11 +1483,19 @@ var WebsocketSignalManager = class {
1253
1483
  async sendMessage({ author, recipient, payload }) {
1254
1484
  (0, import_log5.log)(`Signal ${recipient}`, void 0, {
1255
1485
  F: __dxlog_file5,
1256
- L: 119,
1486
+ L: 118,
1257
1487
  S: this,
1258
1488
  C: (f, a) => f(...a)
1259
1489
  });
1260
- (0, import_tiny_invariant5.default)(this._opened, "Closed");
1490
+ (0, import_invariant5.invariant)(this._opened, "Closed", {
1491
+ F: __dxlog_file5,
1492
+ L: 119,
1493
+ S: this,
1494
+ A: [
1495
+ "this._opened",
1496
+ "'Closed'"
1497
+ ]
1498
+ });
1261
1499
  void this._forEachServer(async (server) => {
1262
1500
  void server.sendMessage({
1263
1501
  author,
@@ -1265,7 +1503,7 @@ var WebsocketSignalManager = class {
1265
1503
  payload
1266
1504
  }).catch((err) => (0, import_log5.log)(err, void 0, {
1267
1505
  F: __dxlog_file5,
1268
- L: 123,
1506
+ L: 122,
1269
1507
  S: this,
1270
1508
  C: (f, a) => f(...a)
1271
1509
  }));
@@ -1274,28 +1512,44 @@ var WebsocketSignalManager = class {
1274
1512
  async subscribeMessages(peerId) {
1275
1513
  (0, import_log5.log)(`Subscribed for message stream peerId=${peerId}`, void 0, {
1276
1514
  F: __dxlog_file5,
1277
- L: 128,
1515
+ L: 127,
1278
1516
  S: this,
1279
1517
  C: (f, a) => f(...a)
1280
1518
  });
1281
- (0, import_tiny_invariant5.default)(this._opened, "Closed");
1519
+ (0, import_invariant5.invariant)(this._opened, "Closed", {
1520
+ F: __dxlog_file5,
1521
+ L: 128,
1522
+ S: this,
1523
+ A: [
1524
+ "this._opened",
1525
+ "'Closed'"
1526
+ ]
1527
+ });
1282
1528
  await this._forEachServer(async (server) => server.subscribeMessages(peerId));
1283
1529
  }
1284
1530
  async unsubscribeMessages(peerId) {
1285
1531
  (0, import_log5.log)(`Subscribed for message stream peerId=${peerId}`, void 0, {
1286
1532
  F: __dxlog_file5,
1287
- L: 135,
1533
+ L: 134,
1288
1534
  S: this,
1289
1535
  C: (f, a) => f(...a)
1290
1536
  });
1291
- (0, import_tiny_invariant5.default)(this._opened, "Closed");
1537
+ (0, import_invariant5.invariant)(this._opened, "Closed", {
1538
+ F: __dxlog_file5,
1539
+ L: 135,
1540
+ S: this,
1541
+ A: [
1542
+ "this._opened",
1543
+ "'Closed'"
1544
+ ]
1545
+ });
1292
1546
  await this._forEachServer(async (server) => server.unsubscribeMessages(peerId));
1293
1547
  }
1294
1548
  _initContext() {
1295
1549
  this._ctx = new import_context4.Context({
1296
1550
  onError: (err) => import_log5.log.catch(err, void 0, {
1297
1551
  F: __dxlog_file5,
1298
- L: 143,
1552
+ L: 142,
1299
1553
  S: this,
1300
1554
  C: (f, a) => f(...a)
1301
1555
  })