@dxos/network-manager 0.1.56-main.d9d1c3e → 0.1.56-main.da2eab1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/dist/lib/browser/{chunk-FC6CWDES.mjs → chunk-YADPADXG.mjs} +127 -138
  2. package/dist/lib/browser/chunk-YADPADXG.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +1 -1
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/browser/testing/index.mjs +23 -16
  6. package/dist/lib/browser/testing/index.mjs.map +3 -3
  7. package/dist/lib/node/index.cjs +125 -136
  8. package/dist/lib/node/index.cjs.map +3 -3
  9. package/dist/lib/node/meta.json +1 -1
  10. package/dist/lib/node/testing/index.cjs +147 -151
  11. package/dist/lib/node/testing/index.cjs.map +3 -3
  12. package/dist/types/src/connection-log.d.ts.map +1 -1
  13. package/dist/types/src/signal/swarm-messenger.d.ts +1 -1
  14. package/dist/types/src/signal/swarm-messenger.d.ts.map +1 -1
  15. package/dist/types/src/swarm/connection.d.ts +2 -1
  16. package/dist/types/src/swarm/connection.d.ts.map +1 -1
  17. package/dist/types/src/swarm/peer.d.ts +1 -1
  18. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  19. package/dist/types/src/swarm/swarm.d.ts.map +1 -1
  20. package/dist/types/src/testing/test-builder.d.ts +4 -3
  21. package/dist/types/src/testing/test-builder.d.ts.map +1 -1
  22. package/dist/types/src/testing/test-wire-protocol.d.ts +13 -2
  23. package/dist/types/src/testing/test-wire-protocol.d.ts.map +1 -1
  24. package/dist/types/src/transport/webrtc-transport-proxy.d.ts +1 -1
  25. package/dist/types/src/transport/webrtc-transport-proxy.d.ts.map +1 -1
  26. package/dist/types/src/transport/webrtc-transport.d.ts +3 -3
  27. package/dist/types/src/transport/webrtc-transport.d.ts.map +1 -1
  28. package/package.json +18 -18
  29. package/src/connection-log.ts +2 -0
  30. package/src/signal/swarm-messenger.ts +2 -2
  31. package/src/swarm/connection.ts +8 -1
  32. package/src/swarm/peer.ts +8 -6
  33. package/src/swarm/swarm.ts +5 -5
  34. package/src/testing/test-builder.ts +9 -5
  35. package/src/testing/test-wire-protocol.ts +22 -2
  36. package/src/tests/memory-transport.test.ts +1 -1
  37. package/src/transport/webrtc-transport-proxy.ts +4 -4
  38. package/src/transport/webrtc-transport.ts +20 -6
  39. package/dist/lib/browser/chunk-FC6CWDES.mjs.map +0 -7
@@ -145,7 +145,7 @@ var Connection = class {
145
145
  initiator: this.initiator
146
146
  }, {
147
147
  F: __dxlog_file,
148
- L: 95,
148
+ L: 96,
149
149
  S: this,
150
150
  C: (f, a) => f(...a)
151
151
  });
@@ -165,7 +165,7 @@ var Connection = class {
165
165
  async openConnection() {
166
166
  (0, import_invariant.invariant)(this._state === ConnectionState.INITIAL, "Invalid state.", {
167
167
  F: __dxlog_file,
168
- L: 120,
168
+ L: 121,
169
169
  S: this,
170
170
  A: [
171
171
  "this._state === ConnectionState.INITIAL",
@@ -176,7 +176,7 @@ var Connection = class {
176
176
  id: this._instanceId
177
177
  }), {
178
178
  F: __dxlog_file,
179
- L: 121,
179
+ L: 122,
180
180
  S: this,
181
181
  C: (f, a) => f(...a)
182
182
  });
@@ -188,7 +188,7 @@ var Connection = class {
188
188
  initiator: this.initiator
189
189
  }, {
190
190
  F: __dxlog_file,
191
- L: 122,
191
+ L: 123,
192
192
  S: this,
193
193
  C: (f, a) => f(...a)
194
194
  });
@@ -199,7 +199,7 @@ var Connection = class {
199
199
  this._protocol.stream.on("close", () => {
200
200
  (0, import_log.log)("protocol stream closed", void 0, {
201
201
  F: __dxlog_file,
202
- L: 139,
202
+ L: 140,
203
203
  S: this,
204
204
  C: (f, a) => f(...a)
205
205
  });
@@ -207,7 +207,7 @@ var Connection = class {
207
207
  });
208
208
  (0, import_invariant.invariant)(!this._transport, void 0, {
209
209
  F: __dxlog_file,
210
- L: 143,
210
+ L: 144,
211
211
  S: this,
212
212
  A: [
213
213
  "!this._transport",
@@ -227,6 +227,9 @@ var Connection = class {
227
227
  this.close().catch((err) => this.errors.raise(err));
228
228
  });
229
229
  this._transport.errors.handle((err) => {
230
+ if (!this.closeReason) {
231
+ this.closeReason = err?.message;
232
+ }
230
233
  if (this._state !== ConnectionState.CLOSED && this._state !== ConnectionState.CLOSING) {
231
234
  this.errors.raise(err);
232
235
  }
@@ -239,13 +242,15 @@ var Connection = class {
239
242
  id: this._instanceId
240
243
  }), {
241
244
  F: __dxlog_file,
242
- L: 172,
245
+ L: 176,
243
246
  S: this,
244
247
  C: (f, a) => f(...a)
245
248
  });
246
249
  }
247
- async close() {
248
- var _a;
250
+ async close(err) {
251
+ if (!this.closeReason) {
252
+ this.closeReason = err?.message;
253
+ }
249
254
  if (this._state === ConnectionState.CLOSED) {
250
255
  return;
251
256
  }
@@ -255,26 +260,26 @@ var Connection = class {
255
260
  peerId: this.ownId
256
261
  }, {
257
262
  F: __dxlog_file,
258
- L: 184,
263
+ L: 191,
259
264
  S: this,
260
265
  C: (f, a) => f(...a)
261
266
  });
262
267
  try {
263
268
  await this._protocol.destroy();
264
- } catch (err) {
265
- import_log.log.catch(err, void 0, {
269
+ } catch (err2) {
270
+ import_log.log.catch(err2, void 0, {
266
271
  F: __dxlog_file,
267
- L: 190,
272
+ L: 197,
268
273
  S: this,
269
274
  C: (f, a) => f(...a)
270
275
  });
271
276
  }
272
277
  try {
273
- await ((_a = this._transport) == null ? void 0 : _a.destroy());
274
- } catch (err) {
275
- import_log.log.catch(err, void 0, {
278
+ await this._transport?.destroy();
279
+ } catch (err2) {
280
+ import_log.log.catch(err2, void 0, {
276
281
  F: __dxlog_file,
277
- L: 197,
282
+ L: 204,
278
283
  S: this,
279
284
  C: (f, a) => f(...a)
280
285
  });
@@ -283,7 +288,7 @@ var Connection = class {
283
288
  peerId: this.ownId
284
289
  }, {
285
290
  F: __dxlog_file,
286
- L: 200,
291
+ L: 207,
287
292
  S: this,
288
293
  C: (f, a) => f(...a)
289
294
  });
@@ -323,7 +328,7 @@ var Connection = class {
323
328
  err
324
329
  }, {
325
330
  F: __dxlog_file,
326
- L: 234,
331
+ L: 241,
327
332
  S: this,
328
333
  C: (f, a) => f(...a)
329
334
  });
@@ -334,10 +339,9 @@ var Connection = class {
334
339
  * Receive a signal from the remote peer.
335
340
  */
336
341
  async signal(msg) {
337
- var _a, _b, _c;
338
342
  (0, import_invariant.invariant)(msg.sessionId, void 0, {
339
343
  F: __dxlog_file,
340
- L: 243,
344
+ L: 250,
341
345
  S: this,
342
346
  A: [
343
347
  "msg.sessionId",
@@ -347,7 +351,7 @@ var Connection = class {
347
351
  if (!msg.sessionId.equals(this.sessionId)) {
348
352
  (0, import_log.log)("dropping signal for incorrect session id", void 0, {
349
353
  F: __dxlog_file,
350
- L: 245,
354
+ L: 252,
351
355
  S: this,
352
356
  C: (f, a) => f(...a)
353
357
  });
@@ -355,32 +359,32 @@ var Connection = class {
355
359
  }
356
360
  (0, import_invariant.invariant)(msg.data.signal || msg.data.signalBatch, void 0, {
357
361
  F: __dxlog_file,
358
- L: 248,
362
+ L: 255,
359
363
  S: this,
360
364
  A: [
361
365
  "msg.data.signal || msg.data.signalBatch",
362
366
  ""
363
367
  ]
364
368
  });
365
- (0, import_invariant.invariant)((_a = msg.author) == null ? void 0 : _a.equals(this.remoteId), void 0, {
369
+ (0, import_invariant.invariant)(msg.author?.equals(this.remoteId), void 0, {
366
370
  F: __dxlog_file,
367
- L: 249,
371
+ L: 256,
368
372
  S: this,
369
373
  A: [
370
374
  "msg.author?.equals(this.remoteId)",
371
375
  ""
372
376
  ]
373
377
  });
374
- (0, import_invariant.invariant)((_b = msg.recipient) == null ? void 0 : _b.equals(this.ownId), void 0, {
378
+ (0, import_invariant.invariant)(msg.recipient?.equals(this.ownId), void 0, {
375
379
  F: __dxlog_file,
376
- L: 250,
380
+ L: 257,
377
381
  S: this,
378
382
  A: [
379
383
  "msg.recipient?.equals(this.ownId)",
380
384
  ""
381
385
  ]
382
386
  });
383
- const signals = msg.data.signalBatch ? (_c = msg.data.signalBatch.signals) != null ? _c : [] : [
387
+ const signals = msg.data.signalBatch ? msg.data.signalBatch.signals ?? [] : [
384
388
  msg.data.signal
385
389
  ];
386
390
  for (const signal of signals) {
@@ -394,7 +398,7 @@ var Connection = class {
394
398
  msg: msg.data
395
399
  }, {
396
400
  F: __dxlog_file,
397
- L: 259,
401
+ L: 266,
398
402
  S: this,
399
403
  C: (f, a) => f(...a)
400
404
  });
@@ -402,7 +406,7 @@ var Connection = class {
402
406
  } else {
403
407
  (0, import_invariant.invariant)(this._transport, "Connection not ready to accept signals.", {
404
408
  F: __dxlog_file,
405
- L: 262,
409
+ L: 269,
406
410
  S: this,
407
411
  A: [
408
412
  "this._transport",
@@ -415,7 +419,7 @@ var Connection = class {
415
419
  msg: msg.data
416
420
  }, {
417
421
  F: __dxlog_file,
418
- L: 263,
422
+ L: 270,
419
423
  S: this,
420
424
  C: (f, a) => f(...a)
421
425
  });
@@ -430,13 +434,13 @@ var Connection = class {
430
434
  peerId: this.ownId
431
435
  }, {
432
436
  F: __dxlog_file,
433
- L: 270,
437
+ L: 277,
434
438
  S: this,
435
439
  C: (f, a) => f(...a)
436
440
  });
437
441
  (0, import_invariant.invariant)(state !== this._state, "Already in this state.", {
438
442
  F: __dxlog_file,
439
- L: 271,
443
+ L: 278,
440
444
  S: this,
441
445
  A: [
442
446
  "state !== this._state",
@@ -480,7 +484,6 @@ var SwarmMessenger = class {
480
484
  this._topic = topic;
481
485
  }
482
486
  async receiveMessage({ author, recipient, payload }) {
483
- var _a, _b, _c, _d;
484
487
  if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
485
488
  return;
486
489
  }
@@ -498,21 +501,21 @@ var SwarmMessenger = class {
498
501
  S: this,
499
502
  C: (f, a) => f(...a)
500
503
  });
501
- if ((_a = message.data) == null ? void 0 : _a.offer) {
504
+ if (message.data?.offer) {
502
505
  await this._handleOffer({
503
506
  author,
504
507
  recipient,
505
508
  message
506
509
  });
507
- } else if ((_b = message.data) == null ? void 0 : _b.answer) {
510
+ } else if (message.data?.answer) {
508
511
  await this._resolveAnswers(message);
509
- } else if ((_c = message.data) == null ? void 0 : _c.signal) {
512
+ } else if (message.data?.signal) {
510
513
  await this._handleSignal({
511
514
  author,
512
515
  recipient,
513
516
  message
514
517
  });
515
- } else if ((_d = message.data) == null ? void 0 : _d.signalBatch) {
518
+ } else if (message.data?.signalBatch) {
516
519
  await this._handleSignal({
517
520
  author,
518
521
  recipient,
@@ -530,8 +533,7 @@ var SwarmMessenger = class {
530
533
  }
531
534
  }
532
535
  async signal(message) {
533
- var _a, _b;
534
- (0, import_invariant2.invariant)(((_a = message.data) == null ? void 0 : _a.signal) || ((_b = message.data) == null ? void 0 : _b.signalBatch), "Invalid message", {
536
+ (0, import_invariant2.invariant)(message.data?.signal || message.data?.signalBatch, "Invalid message", {
535
537
  F: __dxlog_file2,
536
538
  L: 85,
537
539
  S: this,
@@ -563,11 +565,10 @@ var SwarmMessenger = class {
563
565
  });
564
566
  }
565
567
  async _sendReliableMessage({ author, recipient, message }) {
566
- var _a;
567
568
  const networkMessage = {
568
569
  ...message,
569
570
  // Setting unique message_id if it not specified yet.
570
- messageId: (_a = message.messageId) != null ? _a : import_keys2.PublicKey.random()
571
+ messageId: message.messageId ?? import_keys2.PublicKey.random()
571
572
  };
572
573
  (0, import_log2.log)("sending", {
573
574
  from: author,
@@ -589,8 +590,7 @@ var SwarmMessenger = class {
589
590
  });
590
591
  }
591
592
  async _resolveAnswers(message) {
592
- var _a, _b, _c;
593
- (0, import_invariant2.invariant)((_b = (_a = message.data) == null ? void 0 : _a.answer) == null ? void 0 : _b.offerMessageId, "No offerMessageId", {
593
+ (0, import_invariant2.invariant)(message.data?.answer?.offerMessageId, "No offerMessageId", {
594
594
  F: __dxlog_file2,
595
595
  L: 135,
596
596
  S: this,
@@ -602,7 +602,7 @@ var SwarmMessenger = class {
602
602
  const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
603
603
  if (offerRecord) {
604
604
  this._offerRecords.delete(message.data.answer.offerMessageId);
605
- (0, import_invariant2.invariant)((_c = message.data) == null ? void 0 : _c.answer, "No answer", {
605
+ (0, import_invariant2.invariant)(message.data?.answer, "No answer", {
606
606
  F: __dxlog_file2,
607
607
  L: 139,
608
608
  S: this,
@@ -717,6 +717,7 @@ function _ts_decorate2(decorators, target, key, desc) {
717
717
  var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
718
718
  var CONNECTION_COUNTS_STABLE_AFTER = 5e3;
719
719
  var Peer = class {
720
+ // TODO(burdon): Convert to map?
720
721
  constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _connectionLimiter, _callbacks) {
721
722
  this.id = id;
722
723
  this.topic = topic;
@@ -736,7 +737,6 @@ var Peer = class {
736
737
  * Respond to remote offer.
737
738
  */
738
739
  async onOffer(message) {
739
- var _a;
740
740
  const remoteId = message.author;
741
741
  if (this.connection || this.initiating) {
742
742
  if (remoteId.toHex() < this.localPeerId.toHex()) {
@@ -744,15 +744,15 @@ var Peer = class {
744
744
  localPeerId: this.id,
745
745
  topic: this.topic,
746
746
  remotePeerId: this.localPeerId,
747
- sessionId: (_a = this.connection) == null ? void 0 : _a.sessionId
747
+ sessionId: this.connection?.sessionId
748
748
  }, {
749
749
  F: __dxlog_file3,
750
- L: 106,
750
+ L: 108,
751
751
  S: this,
752
752
  C: (f, a) => f(...a)
753
753
  });
754
754
  if (this.connection) {
755
- await this.closeConnection();
755
+ await this.closeConnection(new Error("Connection displaced by remote initiator."));
756
756
  }
757
757
  } else {
758
758
  return {
@@ -764,7 +764,7 @@ var Peer = class {
764
764
  if (!this.connection) {
765
765
  (0, import_invariant3.invariant)(message.sessionId, void 0, {
766
766
  F: __dxlog_file3,
767
- L: 126,
767
+ L: 128,
768
768
  S: this,
769
769
  A: [
770
770
  "message.sessionId",
@@ -784,12 +784,12 @@ var Peer = class {
784
784
  err
785
785
  }, {
786
786
  F: __dxlog_file3,
787
- L: 134,
787
+ L: 136,
788
788
  S: this,
789
789
  C: (f, a) => f(...a)
790
790
  });
791
791
  }
792
- await this.closeConnection();
792
+ await this.closeConnection(err);
793
793
  }
794
794
  return {
795
795
  accept: true
@@ -806,7 +806,7 @@ var Peer = class {
806
806
  async initiateConnection() {
807
807
  (0, import_invariant3.invariant)(!this.initiating, "Initiation in progress.", {
808
808
  F: __dxlog_file3,
809
- L: 151,
809
+ L: 153,
810
810
  S: this,
811
811
  A: [
812
812
  "!this.initiating",
@@ -815,7 +815,7 @@ var Peer = class {
815
815
  });
816
816
  (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
817
817
  F: __dxlog_file3,
818
- L: 152,
818
+ L: 154,
819
819
  S: this,
820
820
  A: [
821
821
  "!this.connection",
@@ -830,7 +830,7 @@ var Peer = class {
830
830
  sessionId
831
831
  }, {
832
832
  F: __dxlog_file3,
833
- L: 154,
833
+ L: 156,
834
834
  S: this,
835
835
  C: (f, a) => f(...a)
836
836
  });
@@ -854,14 +854,14 @@ var Peer = class {
854
854
  remoteId: this.id
855
855
  }, {
856
856
  F: __dxlog_file3,
857
- L: 169,
857
+ L: 171,
858
858
  S: this,
859
859
  C: (f, a) => f(...a)
860
860
  });
861
861
  if (connection.state !== ConnectionState.INITIAL) {
862
862
  (0, import_log3.log)("ignoring response", void 0, {
863
863
  F: __dxlog_file3,
864
- L: 171,
864
+ L: 173,
865
865
  S: this,
866
866
  C: (f, a) => f(...a)
867
867
  });
@@ -881,11 +881,11 @@ var Peer = class {
881
881
  remoteId: this.id
882
882
  }, {
883
883
  F: __dxlog_file3,
884
- L: 182,
884
+ L: 184,
885
885
  S: this,
886
886
  C: (f, a) => f(...a)
887
887
  });
888
- await this.closeConnection();
888
+ await this.closeConnection(err);
889
889
  throw err;
890
890
  } finally {
891
891
  this.initiating = false;
@@ -896,7 +896,6 @@ var Peer = class {
896
896
  * Either we're initiating a connection or creating one in response to an offer from the other peer.
897
897
  */
898
898
  _createConnection(initiator, sessionId) {
899
- var _a;
900
899
  (0, import_log3.log)("creating connection", {
901
900
  topic: this.topic,
902
901
  peerId: this.localPeerId,
@@ -905,13 +904,13 @@ var Peer = class {
905
904
  sessionId
906
905
  }, {
907
906
  F: __dxlog_file3,
908
- L: 196,
907
+ L: 198,
909
908
  S: this,
910
909
  C: (f, a) => f(...a)
911
910
  });
912
911
  (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
913
912
  F: __dxlog_file3,
914
- L: 203,
913
+ L: 205,
915
914
  S: this,
916
915
  A: [
917
916
  "!this.connection",
@@ -935,7 +934,7 @@ var Peer = class {
935
934
  this._transportFactory
936
935
  );
937
936
  this._callbacks.onInitiated(connection);
938
- void ((_a = this._connectionCtx) == null ? void 0 : _a.dispose());
937
+ void this._connectionCtx?.dispose();
939
938
  this._connectionCtx = this._ctx.derive();
940
939
  connection.stateChanged.on((state) => {
941
940
  switch (state) {
@@ -952,7 +951,7 @@ var Peer = class {
952
951
  initiator
953
952
  }, {
954
953
  F: __dxlog_file3,
955
- L: 229,
954
+ L: 231,
956
955
  S: this,
957
956
  C: (f, a) => f(...a)
958
957
  });
@@ -966,13 +965,13 @@ var Peer = class {
966
965
  initiator
967
966
  }, {
968
967
  F: __dxlog_file3,
969
- L: 240,
968
+ L: 242,
970
969
  S: this,
971
970
  C: (f, a) => f(...a)
972
971
  });
973
972
  (0, import_invariant3.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
974
973
  F: __dxlog_file3,
975
- L: 241,
974
+ L: 243,
976
975
  S: this,
977
976
  A: [
978
977
  "this.connection === connection",
@@ -987,7 +986,7 @@ var Peer = class {
987
986
  initiator
988
987
  }, {
989
988
  F: __dxlog_file3,
990
- L: 243,
989
+ L: 245,
991
990
  S: this,
992
991
  C: (f, a) => f(...a)
993
992
  });
@@ -1017,7 +1016,7 @@ var Peer = class {
1017
1016
  err
1018
1017
  }, {
1019
1018
  F: __dxlog_file3,
1020
- L: 277,
1019
+ L: 279,
1021
1020
  S: this,
1022
1021
  C: (f, a) => f(...a)
1023
1022
  });
@@ -1030,16 +1029,16 @@ var Peer = class {
1030
1029
  err
1031
1030
  }, {
1032
1031
  F: __dxlog_file3,
1033
- L: 278,
1032
+ L: 280,
1034
1033
  S: this,
1035
1034
  C: (f, a) => f(...a)
1036
1035
  });
1037
- void this.closeConnection();
1036
+ void this.closeConnection(err);
1038
1037
  });
1039
1038
  this.connection = connection;
1040
1039
  return connection;
1041
1040
  }
1042
- async closeConnection() {
1041
+ async closeConnection(err) {
1043
1042
  if (!this.connection) {
1044
1043
  return;
1045
1044
  }
@@ -1049,17 +1048,17 @@ var Peer = class {
1049
1048
  sessionId: connection.sessionId
1050
1049
  }, {
1051
1050
  F: __dxlog_file3,
1052
- L: 301,
1051
+ L: 303,
1053
1052
  S: this,
1054
1053
  C: (f, a) => f(...a)
1055
1054
  });
1056
- await connection.close();
1055
+ await connection.close(err);
1057
1056
  (0, import_log3.log)("closed", {
1058
1057
  peerId: this.id,
1059
1058
  sessionId: connection.sessionId
1060
1059
  }, {
1061
1060
  F: __dxlog_file3,
1062
- L: 307,
1061
+ L: 309,
1063
1062
  S: this,
1064
1063
  C: (f, a) => f(...a)
1065
1064
  });
@@ -1070,7 +1069,7 @@ var Peer = class {
1070
1069
  message
1071
1070
  }, {
1072
1071
  F: __dxlog_file3,
1073
- L: 312,
1072
+ L: 314,
1074
1073
  S: this,
1075
1074
  C: (f, a) => f(...a)
1076
1075
  });
@@ -1079,18 +1078,17 @@ var Peer = class {
1079
1078
  await this.connection.signal(message);
1080
1079
  }
1081
1080
  async destroy() {
1082
- var _a;
1083
1081
  await this._ctx.dispose();
1084
1082
  (0, import_log3.log)("Destroying peer", {
1085
1083
  peerId: this.id,
1086
1084
  topic: this.topic
1087
1085
  }, {
1088
1086
  F: __dxlog_file3,
1089
- L: 322,
1087
+ L: 324,
1090
1088
  S: this,
1091
1089
  C: (f, a) => f(...a)
1092
1090
  });
1093
- await ((_a = this == null ? void 0 : this.connection) == null ? void 0 : _a.close());
1091
+ await this?.connection?.close();
1094
1092
  }
1095
1093
  };
1096
1094
  _ts_decorate2([
@@ -1135,9 +1133,9 @@ var Swarm = class {
1135
1133
  this._transportFactory = _transportFactory;
1136
1134
  this._label = _label;
1137
1135
  this._connectionLimiter = _connectionLimiter;
1138
- this._peers = new import_util2.ComplexMap(import_keys4.PublicKey.hash);
1139
1136
  this._ctx = new import_context4.Context();
1140
1137
  this._listeningHandle = void 0;
1138
+ this._peers = new import_util2.ComplexMap(import_keys4.PublicKey.hash);
1141
1139
  this._instanceId = import_keys4.PublicKey.random().toHex();
1142
1140
  this.connectionAdded = new import_async3.Event();
1143
1141
  this.disconnected = new import_async3.Event();
@@ -1220,14 +1218,13 @@ var Swarm = class {
1220
1218
  });
1221
1219
  }
1222
1220
  async destroy() {
1223
- var _a;
1224
1221
  (0, import_log4.log)("destroying...", void 0, {
1225
1222
  F: __dxlog_file4,
1226
1223
  L: 140,
1227
1224
  S: this,
1228
1225
  C: (f, a) => f(...a)
1229
1226
  });
1230
- await ((_a = this._listeningHandle) == null ? void 0 : _a.unsubscribe());
1227
+ await this._listeningHandle?.unsubscribe();
1231
1228
  this._listeningHandle = void 0;
1232
1229
  await this._ctx.dispose();
1233
1230
  await this._topology.destroy();
@@ -1267,7 +1264,6 @@ var Swarm = class {
1267
1264
  this._topology.update();
1268
1265
  }
1269
1266
  onSwarmEvent(swarmEvent) {
1270
- var _a;
1271
1267
  (0, import_log4.log)("swarm event", {
1272
1268
  swarmEvent
1273
1269
  }, {
@@ -1303,7 +1299,7 @@ var Swarm = class {
1303
1299
  const peer = this._peers.get(import_keys4.PublicKey.from(swarmEvent.peerLeft.peer));
1304
1300
  if (peer) {
1305
1301
  peer.advertizing = false;
1306
- if (((_a = peer.connection) == null ? void 0 : _a.state) !== ConnectionState.CONNECTED) {
1302
+ if (peer.connection?.state !== ConnectionState.CONNECTED) {
1307
1303
  void this._destroyPeer(peer.id).catch((err) => import_log4.log.catch(err, void 0, {
1308
1304
  F: __dxlog_file4,
1309
1305
  L: 188,
@@ -1316,7 +1312,6 @@ var Swarm = class {
1316
1312
  this._topology.update();
1317
1313
  }
1318
1314
  async onOffer(message) {
1319
- var _a, _b;
1320
1315
  (0, import_log4.log)("offer", {
1321
1316
  message
1322
1317
  }, {
@@ -1345,7 +1340,7 @@ var Swarm = class {
1345
1340
  ""
1346
1341
  ]
1347
1342
  });
1348
- if (!((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId))) {
1343
+ if (!message.recipient?.equals(this._ownPeerId)) {
1349
1344
  (0, import_log4.log)("rejecting offer with incorrect peerId", {
1350
1345
  message
1351
1346
  }, {
@@ -1358,7 +1353,7 @@ var Swarm = class {
1358
1353
  accept: false
1359
1354
  };
1360
1355
  }
1361
- if (!((_b = message.topic) == null ? void 0 : _b.equals(this._topic))) {
1356
+ if (!message.topic?.equals(this._topic)) {
1362
1357
  (0, import_log4.log)("rejecting offer with incorrect topic", {
1363
1358
  message
1364
1359
  }, {
@@ -1377,7 +1372,6 @@ var Swarm = class {
1377
1372
  return answer;
1378
1373
  }
1379
1374
  async onSignal(message) {
1380
- var _a, _b;
1381
1375
  (0, import_log4.log)("signal", {
1382
1376
  message
1383
1377
  }, {
@@ -1395,7 +1389,7 @@ var Swarm = class {
1395
1389
  });
1396
1390
  return;
1397
1391
  }
1398
- (0, import_invariant4.invariant)((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1392
+ (0, import_invariant4.invariant)(message.recipient?.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1399
1393
  F: __dxlog_file4,
1400
1394
  L: 227,
1401
1395
  S: this,
@@ -1404,7 +1398,7 @@ var Swarm = class {
1404
1398
  "`Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`"
1405
1399
  ]
1406
1400
  });
1407
- (0, import_invariant4.invariant)((_b = message.topic) == null ? void 0 : _b.equals(this._topic), void 0, {
1401
+ (0, import_invariant4.invariant)(message.topic?.equals(this._topic), void 0, {
1408
1402
  F: __dxlog_file4,
1409
1403
  L: 231,
1410
1404
  S: this,
@@ -1616,8 +1610,7 @@ var SwarmMapper = class {
1616
1610
  }));
1617
1611
  }));
1618
1612
  this._subscriptions.add(_swarm.disconnected.on((peerId) => {
1619
- var _a;
1620
- (_a = this._connectionSubscriptions.get(peerId)) == null ? void 0 : _a();
1613
+ this._connectionSubscriptions.get(peerId)?.();
1621
1614
  this._connectionSubscriptions.delete(peerId);
1622
1615
  this._update();
1623
1616
  }));
@@ -1763,8 +1756,7 @@ var ConnectionLog = class {
1763
1756
  this.update = new import_async6.Event();
1764
1757
  }
1765
1758
  getSwarmInfo(swarmId) {
1766
- var _a;
1767
- return (_a = this._swarms.get(swarmId)) != null ? _a : (0, import_debug3.raise)(new Error(`Swarm not found: ${swarmId}`));
1759
+ return this._swarms.get(swarmId) ?? (0, import_debug3.raise)(new Error(`Swarm not found: ${swarmId}`));
1768
1760
  }
1769
1761
  get swarms() {
1770
1762
  return Array.from(this._swarms.values());
@@ -1780,9 +1772,9 @@ var ConnectionLog = class {
1780
1772
  this._swarms.set(import_keys7.PublicKey.from(swarm._instanceId), info);
1781
1773
  this.update.emit();
1782
1774
  swarm.connectionAdded.on((connection) => {
1783
- var _a, _b;
1784
1775
  const connectionInfo = {
1785
1776
  state: ConnectionState.INITIAL,
1777
+ closeReason: connection.closeReason,
1786
1778
  remotePeerId: connection.remoteId,
1787
1779
  sessionId: connection.sessionId,
1788
1780
  transport: connection.transport && Object.getPrototypeOf(connection.transport).constructor.name,
@@ -1793,13 +1785,14 @@ var ConnectionLog = class {
1793
1785
  this.update.emit();
1794
1786
  connection.stateChanged.on((state) => {
1795
1787
  connectionInfo.state = state;
1788
+ connectionInfo.closeReason = connection.closeReason;
1796
1789
  connectionInfo.events.push({
1797
1790
  type: EventType.CONNECTION_STATE_CHANGED,
1798
1791
  newState: state
1799
1792
  });
1800
1793
  this.update.emit();
1801
1794
  });
1802
- (_b = (_a = connection.protocol) == null ? void 0 : _a.stats) == null ? void 0 : _b.on((stats) => {
1795
+ connection.protocol?.stats?.on((stats) => {
1803
1796
  connectionInfo.streams = stats.channels;
1804
1797
  this.update.emit();
1805
1798
  });
@@ -1834,10 +1827,7 @@ var NetworkManager = class {
1834
1827
  this._instanceId = import_keys8.PublicKey.random().toHex();
1835
1828
  this._transportFactory = transportFactory;
1836
1829
  this._signalManager = signalManager;
1837
- this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => {
1838
- var _a;
1839
- return (_a = this._swarms.get(topic)) == null ? void 0 : _a.onSwarmEvent(event);
1840
- });
1830
+ this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => this._swarms.get(topic)?.onSwarmEvent(event));
1841
1831
  this._messenger = new import_messaging.Messenger({
1842
1832
  signalManager: this._signalManager
1843
1833
  });
@@ -1905,7 +1895,6 @@ var NetworkManager = class {
1905
1895
  * Join the swarm.
1906
1896
  */
1907
1897
  async joinSwarm({ topic, peerId, topology, protocolProvider: protocol, label }) {
1908
- var _a;
1909
1898
  (0, import_invariant6.invariant)(import_keys8.PublicKey.isPublicKey(topic), void 0, {
1910
1899
  F: __dxlog_file7,
1911
1900
  L: 157,
@@ -1979,7 +1968,7 @@ var NetworkManager = class {
1979
1968
  C: (f, a) => f(...a)
1980
1969
  }));
1981
1970
  this.topicsUpdated.emit();
1982
- (_a = this._connectionLog) == null ? void 0 : _a.joinedSwarm(swarm);
1971
+ this._connectionLog?.joinedSwarm(swarm);
1983
1972
  (0, import_log7.log)("joined", {
1984
1973
  topic: import_keys8.PublicKey.from(topic),
1985
1974
  count: this._swarms.size
@@ -1997,7 +1986,6 @@ var NetworkManager = class {
1997
1986
  * Close the connection.
1998
1987
  */
1999
1988
  async leaveSwarm(topic) {
2000
- var _a;
2001
1989
  if (!this._swarms.has(topic)) {
2002
1990
  return;
2003
1991
  }
@@ -2017,7 +2005,7 @@ var NetworkManager = class {
2017
2005
  const map = this._mappers.get(topic);
2018
2006
  map.destroy();
2019
2007
  this._mappers.delete(topic);
2020
- (_a = this._connectionLog) == null ? void 0 : _a.leftSwarm(swarm);
2008
+ this._connectionLog?.leftSwarm(swarm);
2021
2009
  await swarm.destroy();
2022
2010
  this._swarms.delete(topic);
2023
2011
  await this.topicsUpdated.emit();
@@ -2338,9 +2326,12 @@ var createStreamDelay = (delay) => {
2338
2326
  var MemoryTransportFactory = {
2339
2327
  createTransport: (params) => new MemoryTransport(params)
2340
2328
  };
2341
- var _MemoryTransport = class {
2329
+ var MemoryTransport = class _MemoryTransport {
2330
+ static {
2331
+ // TODO(burdon): Remove static properties (inject context into constructor).
2332
+ this._connections = new import_util7.ComplexMap(import_keys9.PublicKey.hash);
2333
+ }
2342
2334
  constructor(options) {
2343
- var _a;
2344
2335
  this.options = options;
2345
2336
  this.closed = new import_async8.Event();
2346
2337
  this.connected = new import_async8.Event();
@@ -2386,7 +2377,7 @@ var _MemoryTransport = class {
2386
2377
  });
2387
2378
  } else {
2388
2379
  this._remote.wait({
2389
- timeout: (_a = this.options.timeout) != null ? _a : 1e3
2380
+ timeout: this.options.timeout ?? 1e3
2390
2381
  }).then((remoteId) => {
2391
2382
  if (this._destroyed) {
2392
2383
  return;
@@ -2473,7 +2464,7 @@ var _MemoryTransport = class {
2473
2464
  S: this,
2474
2465
  C: (f, a) => f(...a)
2475
2466
  });
2476
- if (!(payload == null ? void 0 : payload.transportId)) {
2467
+ if (!payload?.transportId) {
2477
2468
  return;
2478
2469
  }
2479
2470
  const transportId = payload.transportId;
@@ -2483,9 +2474,6 @@ var _MemoryTransport = class {
2483
2474
  }
2484
2475
  }
2485
2476
  };
2486
- var MemoryTransport = _MemoryTransport;
2487
- // TODO(burdon): Remove static properties (inject context into constructor).
2488
- MemoryTransport._connections = new import_util7.ComplexMap(import_keys9.PublicKey.hash);
2489
2477
  _ts_decorate4([
2490
2478
  import_log10.logInfo
2491
2479
  ], MemoryTransport.prototype, "_instanceId", void 0);
@@ -2506,43 +2494,47 @@ var import_protocols5 = require("@dxos/protocols");
2506
2494
  var wrtc = null;
2507
2495
  try {
2508
2496
  wrtc = require("@koush/wrtc");
2509
- } catch (e) {
2497
+ } catch {
2510
2498
  }
2511
2499
 
2512
2500
  // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
2513
2501
  var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
2514
2502
  var WebRTCTransport = class {
2515
2503
  constructor(params) {
2516
- var _a;
2517
2504
  this.params = params;
2505
+ this._instanceId = import_keys10.PublicKey.random().toHex();
2518
2506
  this._closed = false;
2519
2507
  this.closed = new import_async9.Event();
2520
2508
  this.connected = new import_async9.Event();
2521
2509
  this.errors = new import_debug5.ErrorStream();
2522
- this._instanceId = import_keys10.PublicKey.random().toHex();
2523
2510
  import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.begin({
2524
2511
  id: this._instanceId
2525
2512
  }), {
2526
2513
  F: __dxlog_file12,
2527
- L: 39,
2514
+ L: 44,
2528
2515
  S: this,
2529
2516
  C: (f, a) => f(...a)
2530
2517
  });
2531
2518
  (0, import_log11.log)("created connection", params, {
2532
2519
  F: __dxlog_file12,
2533
- L: 40,
2520
+ L: 45,
2534
2521
  S: this,
2535
2522
  C: (f, a) => f(...a)
2536
2523
  });
2537
2524
  this._peer = new import_simple_peer.default({
2538
2525
  initiator: this.params.initiator,
2539
- wrtc: import_simple_peer.default.WEBRTC_SUPPORT ? void 0 : (_a = wrtc) != null ? _a : (0, import_debug5.raise)(new Error("wrtc not available")),
2540
- config: this.params.webrtcConfig
2526
+ config: this.params.webrtcConfig,
2527
+ // https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/createDataChannel
2528
+ channelConfig: {
2529
+ protocol: "dxos.mesh"
2530
+ },
2531
+ // Custom WebRTC implementation (for Node); otherwise get-browser-rtc.
2532
+ wrtc: import_simple_peer.default.WEBRTC_SUPPORT ? void 0 : wrtc ?? (0, import_debug5.raise)(new Error("wrtc not available"))
2541
2533
  });
2542
2534
  this._peer.on("signal", async (data) => {
2543
2535
  (0, import_log11.log)("signal", data, {
2544
2536
  F: __dxlog_file12,
2545
- L: 48,
2537
+ L: 62,
2546
2538
  S: this,
2547
2539
  C: (f, a) => f(...a)
2548
2540
  });
@@ -2555,7 +2547,7 @@ var WebRTCTransport = class {
2555
2547
  this._peer.on("connect", () => {
2556
2548
  (0, import_log11.log)("connected", void 0, {
2557
2549
  F: __dxlog_file12,
2558
- L: 53,
2550
+ L: 67,
2559
2551
  S: this,
2560
2552
  C: (f, a) => f(...a)
2561
2553
  });
@@ -2565,7 +2557,7 @@ var WebRTCTransport = class {
2565
2557
  this._peer.on("close", async () => {
2566
2558
  (0, import_log11.log)("closed", void 0, {
2567
2559
  F: __dxlog_file12,
2568
- L: 59,
2560
+ L: 73,
2569
2561
  S: this,
2570
2562
  C: (f, a) => f(...a)
2571
2563
  });
@@ -2573,23 +2565,22 @@ var WebRTCTransport = class {
2573
2565
  this.closed.emit();
2574
2566
  });
2575
2567
  this._peer.on("error", async (err) => {
2576
- var _a2;
2577
2568
  this.errors.raise(err);
2578
2569
  try {
2579
- if (typeof ((_a2 = this._peer) == null ? void 0 : _a2._pc.getStats) === "function") {
2570
+ if (typeof this._peer?._pc.getStats === "function") {
2580
2571
  this._peer._pc.getStats().then((stats) => {
2581
2572
  import_log11.log.warn("report after webrtc error", {
2582
2573
  config: this.params.webrtcConfig,
2583
2574
  stats
2584
2575
  }, {
2585
2576
  F: __dxlog_file12,
2586
- L: 71,
2577
+ L: 85,
2587
2578
  S: this,
2588
2579
  C: (f, a) => f(...a)
2589
2580
  });
2590
2581
  });
2591
2582
  }
2592
- } catch (e) {
2583
+ } catch {
2593
2584
  }
2594
2585
  await this.destroy();
2595
2586
  });
@@ -2597,7 +2588,7 @@ var WebRTCTransport = class {
2597
2588
  id: this._instanceId
2598
2589
  }), {
2599
2590
  F: __dxlog_file12,
2600
- L: 81,
2591
+ L: 95,
2601
2592
  S: this,
2602
2593
  C: (f, a) => f(...a)
2603
2594
  });
@@ -2605,7 +2596,7 @@ var WebRTCTransport = class {
2605
2596
  async destroy() {
2606
2597
  (0, import_log11.log)("closing...", void 0, {
2607
2598
  F: __dxlog_file12,
2608
- L: 85,
2599
+ L: 99,
2609
2600
  S: this,
2610
2601
  C: (f, a) => f(...a)
2611
2602
  });
@@ -2615,7 +2606,7 @@ var WebRTCTransport = class {
2615
2606
  this.closed.emit();
2616
2607
  (0, import_log11.log)("closed", void 0, {
2617
2608
  F: __dxlog_file12,
2618
- L: 90,
2609
+ L: 104,
2619
2610
  S: this,
2620
2611
  C: (f, a) => f(...a)
2621
2612
  });
@@ -2626,7 +2617,7 @@ var WebRTCTransport = class {
2626
2617
  }
2627
2618
  (0, import_invariant11.invariant)(signal.payload.data, "Signal message must contain signal data.", {
2628
2619
  F: __dxlog_file12,
2629
- L: 98,
2620
+ L: 112,
2630
2621
  S: this,
2631
2622
  A: [
2632
2623
  "signal.payload.data",
@@ -2636,8 +2627,7 @@ var WebRTCTransport = class {
2636
2627
  this._peer.signal(signal.payload.data);
2637
2628
  }
2638
2629
  async _disconnectStreams() {
2639
- var _a, _b, _c, _d;
2640
- (_d = (_c = (_b = (_a = this.params.stream).unpipe) == null ? void 0 : _b.call(_a, this._peer)) == null ? void 0 : _c.unpipe) == null ? void 0 : _d.call(_c, this.params.stream);
2630
+ this.params.stream.unpipe?.(this._peer)?.unpipe?.(this.params.stream);
2641
2631
  }
2642
2632
  };
2643
2633
  var createWebRTCTransportFactory = (webrtcConfig) => ({
@@ -2765,9 +2755,8 @@ var WebRTCTransportService = class {
2765
2755
  }
2766
2756
  }
2767
2757
  async close({ proxyId }) {
2768
- var _a, _b;
2769
- await ((_a = this.transports.get(proxyId)) == null ? void 0 : _a.transport.destroy());
2770
- await ((_b = this.transports.get(proxyId)) == null ? void 0 : _b.stream.end());
2758
+ await this.transports.get(proxyId)?.transport.destroy();
2759
+ await this.transports.get(proxyId)?.stream.end();
2771
2760
  this.transports.delete(proxyId);
2772
2761
  (0, import_log12.log)("Closed.", void 0, {
2773
2762
  F: __dxlog_file13,
@@ -2796,9 +2785,9 @@ var WebRTCTransportProxy = class {
2796
2785
  this._proxyId = import_keys12.PublicKey.random();
2797
2786
  this._ctx = new import_context6.Context();
2798
2787
  this.closed = new import_async10.Event();
2799
- this._closed = false;
2800
2788
  this.connected = new import_async10.Event();
2801
2789
  this.errors = new import_debug6.ErrorStream();
2790
+ this._closed = false;
2802
2791
  this._serviceStream = this._params.bridgeService.open({
2803
2792
  proxyId: this._proxyId,
2804
2793
  initiator: this._params.initiator
@@ -2874,7 +2863,7 @@ var WebRTCTransportProxy = class {
2874
2863
  if (this._closed) {
2875
2864
  return;
2876
2865
  }
2877
- this._serviceStream.close();
2866
+ await this._serviceStream.close();
2878
2867
  try {
2879
2868
  await this._params.bridgeService.close({
2880
2869
  proxyId: this._proxyId
@@ -2895,7 +2884,7 @@ var WebRTCTransportProxy = class {
2895
2884
  */
2896
2885
  // TODO(burdon): Option on close method.
2897
2886
  forceClose() {
2898
- this._serviceStream.close();
2887
+ void this._serviceStream.close();
2899
2888
  this.closed.emit();
2900
2889
  this._closed = true;
2901
2890
  }