@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
@@ -4,7 +4,7 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
4
4
  }) : x)(function(x) {
5
5
  if (typeof require !== "undefined")
6
6
  return require.apply(this, arguments);
7
- throw new Error('Dynamic require of "' + x + '" is not supported');
7
+ throw Error('Dynamic require of "' + x + '" is not supported');
8
8
  });
9
9
 
10
10
  // packages/core/mesh/network-manager/src/swarm/connection.ts
@@ -92,7 +92,7 @@ var Connection = class {
92
92
  initiator: this.initiator
93
93
  }, {
94
94
  F: __dxlog_file,
95
- L: 95,
95
+ L: 96,
96
96
  S: this,
97
97
  C: (f, a) => f(...a)
98
98
  });
@@ -112,7 +112,7 @@ var Connection = class {
112
112
  async openConnection() {
113
113
  invariant(this._state === ConnectionState.INITIAL, "Invalid state.", {
114
114
  F: __dxlog_file,
115
- L: 120,
115
+ L: 121,
116
116
  S: this,
117
117
  A: [
118
118
  "this._state === ConnectionState.INITIAL",
@@ -123,7 +123,7 @@ var Connection = class {
123
123
  id: this._instanceId
124
124
  }), {
125
125
  F: __dxlog_file,
126
- L: 121,
126
+ L: 122,
127
127
  S: this,
128
128
  C: (f, a) => f(...a)
129
129
  });
@@ -135,7 +135,7 @@ var Connection = class {
135
135
  initiator: this.initiator
136
136
  }, {
137
137
  F: __dxlog_file,
138
- L: 122,
138
+ L: 123,
139
139
  S: this,
140
140
  C: (f, a) => f(...a)
141
141
  });
@@ -146,7 +146,7 @@ var Connection = class {
146
146
  this._protocol.stream.on("close", () => {
147
147
  log("protocol stream closed", void 0, {
148
148
  F: __dxlog_file,
149
- L: 139,
149
+ L: 140,
150
150
  S: this,
151
151
  C: (f, a) => f(...a)
152
152
  });
@@ -154,7 +154,7 @@ var Connection = class {
154
154
  });
155
155
  invariant(!this._transport, void 0, {
156
156
  F: __dxlog_file,
157
- L: 143,
157
+ L: 144,
158
158
  S: this,
159
159
  A: [
160
160
  "!this._transport",
@@ -174,6 +174,9 @@ var Connection = class {
174
174
  this.close().catch((err) => this.errors.raise(err));
175
175
  });
176
176
  this._transport.errors.handle((err) => {
177
+ if (!this.closeReason) {
178
+ this.closeReason = err?.message;
179
+ }
177
180
  if (this._state !== ConnectionState.CLOSED && this._state !== ConnectionState.CLOSING) {
178
181
  this.errors.raise(err);
179
182
  }
@@ -186,13 +189,15 @@ var Connection = class {
186
189
  id: this._instanceId
187
190
  }), {
188
191
  F: __dxlog_file,
189
- L: 172,
192
+ L: 176,
190
193
  S: this,
191
194
  C: (f, a) => f(...a)
192
195
  });
193
196
  }
194
- async close() {
195
- var _a;
197
+ async close(err) {
198
+ if (!this.closeReason) {
199
+ this.closeReason = err?.message;
200
+ }
196
201
  if (this._state === ConnectionState.CLOSED) {
197
202
  return;
198
203
  }
@@ -202,26 +207,26 @@ var Connection = class {
202
207
  peerId: this.ownId
203
208
  }, {
204
209
  F: __dxlog_file,
205
- L: 184,
210
+ L: 191,
206
211
  S: this,
207
212
  C: (f, a) => f(...a)
208
213
  });
209
214
  try {
210
215
  await this._protocol.destroy();
211
- } catch (err) {
212
- log.catch(err, void 0, {
216
+ } catch (err2) {
217
+ log.catch(err2, void 0, {
213
218
  F: __dxlog_file,
214
- L: 190,
219
+ L: 197,
215
220
  S: this,
216
221
  C: (f, a) => f(...a)
217
222
  });
218
223
  }
219
224
  try {
220
- await ((_a = this._transport) == null ? void 0 : _a.destroy());
221
- } catch (err) {
222
- log.catch(err, void 0, {
225
+ await this._transport?.destroy();
226
+ } catch (err2) {
227
+ log.catch(err2, void 0, {
223
228
  F: __dxlog_file,
224
- L: 197,
229
+ L: 204,
225
230
  S: this,
226
231
  C: (f, a) => f(...a)
227
232
  });
@@ -230,7 +235,7 @@ var Connection = class {
230
235
  peerId: this.ownId
231
236
  }, {
232
237
  F: __dxlog_file,
233
- L: 200,
238
+ L: 207,
234
239
  S: this,
235
240
  C: (f, a) => f(...a)
236
241
  });
@@ -270,7 +275,7 @@ var Connection = class {
270
275
  err
271
276
  }, {
272
277
  F: __dxlog_file,
273
- L: 234,
278
+ L: 241,
274
279
  S: this,
275
280
  C: (f, a) => f(...a)
276
281
  });
@@ -281,10 +286,9 @@ var Connection = class {
281
286
  * Receive a signal from the remote peer.
282
287
  */
283
288
  async signal(msg) {
284
- var _a, _b, _c;
285
289
  invariant(msg.sessionId, void 0, {
286
290
  F: __dxlog_file,
287
- L: 243,
291
+ L: 250,
288
292
  S: this,
289
293
  A: [
290
294
  "msg.sessionId",
@@ -294,7 +298,7 @@ var Connection = class {
294
298
  if (!msg.sessionId.equals(this.sessionId)) {
295
299
  log("dropping signal for incorrect session id", void 0, {
296
300
  F: __dxlog_file,
297
- L: 245,
301
+ L: 252,
298
302
  S: this,
299
303
  C: (f, a) => f(...a)
300
304
  });
@@ -302,32 +306,32 @@ var Connection = class {
302
306
  }
303
307
  invariant(msg.data.signal || msg.data.signalBatch, void 0, {
304
308
  F: __dxlog_file,
305
- L: 248,
309
+ L: 255,
306
310
  S: this,
307
311
  A: [
308
312
  "msg.data.signal || msg.data.signalBatch",
309
313
  ""
310
314
  ]
311
315
  });
312
- invariant((_a = msg.author) == null ? void 0 : _a.equals(this.remoteId), void 0, {
316
+ invariant(msg.author?.equals(this.remoteId), void 0, {
313
317
  F: __dxlog_file,
314
- L: 249,
318
+ L: 256,
315
319
  S: this,
316
320
  A: [
317
321
  "msg.author?.equals(this.remoteId)",
318
322
  ""
319
323
  ]
320
324
  });
321
- invariant((_b = msg.recipient) == null ? void 0 : _b.equals(this.ownId), void 0, {
325
+ invariant(msg.recipient?.equals(this.ownId), void 0, {
322
326
  F: __dxlog_file,
323
- L: 250,
327
+ L: 257,
324
328
  S: this,
325
329
  A: [
326
330
  "msg.recipient?.equals(this.ownId)",
327
331
  ""
328
332
  ]
329
333
  });
330
- const signals = msg.data.signalBatch ? (_c = msg.data.signalBatch.signals) != null ? _c : [] : [
334
+ const signals = msg.data.signalBatch ? msg.data.signalBatch.signals ?? [] : [
331
335
  msg.data.signal
332
336
  ];
333
337
  for (const signal of signals) {
@@ -341,7 +345,7 @@ var Connection = class {
341
345
  msg: msg.data
342
346
  }, {
343
347
  F: __dxlog_file,
344
- L: 259,
348
+ L: 266,
345
349
  S: this,
346
350
  C: (f, a) => f(...a)
347
351
  });
@@ -349,7 +353,7 @@ var Connection = class {
349
353
  } else {
350
354
  invariant(this._transport, "Connection not ready to accept signals.", {
351
355
  F: __dxlog_file,
352
- L: 262,
356
+ L: 269,
353
357
  S: this,
354
358
  A: [
355
359
  "this._transport",
@@ -362,7 +366,7 @@ var Connection = class {
362
366
  msg: msg.data
363
367
  }, {
364
368
  F: __dxlog_file,
365
- L: 263,
369
+ L: 270,
366
370
  S: this,
367
371
  C: (f, a) => f(...a)
368
372
  });
@@ -377,13 +381,13 @@ var Connection = class {
377
381
  peerId: this.ownId
378
382
  }, {
379
383
  F: __dxlog_file,
380
- L: 270,
384
+ L: 277,
381
385
  S: this,
382
386
  C: (f, a) => f(...a)
383
387
  });
384
388
  invariant(state !== this._state, "Already in this state.", {
385
389
  F: __dxlog_file,
386
- L: 271,
390
+ L: 278,
387
391
  S: this,
388
392
  A: [
389
393
  "state !== this._state",
@@ -417,7 +421,6 @@ var SwarmMessenger = class {
417
421
  this._topic = topic;
418
422
  }
419
423
  async receiveMessage({ author, recipient, payload }) {
420
- var _a, _b, _c, _d;
421
424
  if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
422
425
  return;
423
426
  }
@@ -435,21 +438,21 @@ var SwarmMessenger = class {
435
438
  S: this,
436
439
  C: (f, a) => f(...a)
437
440
  });
438
- if ((_a = message.data) == null ? void 0 : _a.offer) {
441
+ if (message.data?.offer) {
439
442
  await this._handleOffer({
440
443
  author,
441
444
  recipient,
442
445
  message
443
446
  });
444
- } else if ((_b = message.data) == null ? void 0 : _b.answer) {
447
+ } else if (message.data?.answer) {
445
448
  await this._resolveAnswers(message);
446
- } else if ((_c = message.data) == null ? void 0 : _c.signal) {
449
+ } else if (message.data?.signal) {
447
450
  await this._handleSignal({
448
451
  author,
449
452
  recipient,
450
453
  message
451
454
  });
452
- } else if ((_d = message.data) == null ? void 0 : _d.signalBatch) {
455
+ } else if (message.data?.signalBatch) {
453
456
  await this._handleSignal({
454
457
  author,
455
458
  recipient,
@@ -467,8 +470,7 @@ var SwarmMessenger = class {
467
470
  }
468
471
  }
469
472
  async signal(message) {
470
- var _a, _b;
471
- invariant2(((_a = message.data) == null ? void 0 : _a.signal) || ((_b = message.data) == null ? void 0 : _b.signalBatch), "Invalid message", {
473
+ invariant2(message.data?.signal || message.data?.signalBatch, "Invalid message", {
472
474
  F: __dxlog_file2,
473
475
  L: 85,
474
476
  S: this,
@@ -500,11 +502,10 @@ var SwarmMessenger = class {
500
502
  });
501
503
  }
502
504
  async _sendReliableMessage({ author, recipient, message }) {
503
- var _a;
504
505
  const networkMessage = {
505
506
  ...message,
506
507
  // Setting unique message_id if it not specified yet.
507
- messageId: (_a = message.messageId) != null ? _a : PublicKey2.random()
508
+ messageId: message.messageId ?? PublicKey2.random()
508
509
  };
509
510
  log2("sending", {
510
511
  from: author,
@@ -526,8 +527,7 @@ var SwarmMessenger = class {
526
527
  });
527
528
  }
528
529
  async _resolveAnswers(message) {
529
- var _a, _b, _c;
530
- invariant2((_b = (_a = message.data) == null ? void 0 : _a.answer) == null ? void 0 : _b.offerMessageId, "No offerMessageId", {
530
+ invariant2(message.data?.answer?.offerMessageId, "No offerMessageId", {
531
531
  F: __dxlog_file2,
532
532
  L: 135,
533
533
  S: this,
@@ -539,7 +539,7 @@ var SwarmMessenger = class {
539
539
  const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
540
540
  if (offerRecord) {
541
541
  this._offerRecords.delete(message.data.answer.offerMessageId);
542
- invariant2((_c = message.data) == null ? void 0 : _c.answer, "No answer", {
542
+ invariant2(message.data?.answer, "No answer", {
543
543
  F: __dxlog_file2,
544
544
  L: 139,
545
545
  S: this,
@@ -664,6 +664,7 @@ function _ts_decorate2(decorators, target, key, desc) {
664
664
  var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
665
665
  var CONNECTION_COUNTS_STABLE_AFTER = 5e3;
666
666
  var Peer = class {
667
+ // TODO(burdon): Convert to map?
667
668
  constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _connectionLimiter, _callbacks) {
668
669
  this.id = id;
669
670
  this.topic = topic;
@@ -683,7 +684,6 @@ var Peer = class {
683
684
  * Respond to remote offer.
684
685
  */
685
686
  async onOffer(message) {
686
- var _a;
687
687
  const remoteId = message.author;
688
688
  if (this.connection || this.initiating) {
689
689
  if (remoteId.toHex() < this.localPeerId.toHex()) {
@@ -691,15 +691,15 @@ var Peer = class {
691
691
  localPeerId: this.id,
692
692
  topic: this.topic,
693
693
  remotePeerId: this.localPeerId,
694
- sessionId: (_a = this.connection) == null ? void 0 : _a.sessionId
694
+ sessionId: this.connection?.sessionId
695
695
  }, {
696
696
  F: __dxlog_file3,
697
- L: 106,
697
+ L: 108,
698
698
  S: this,
699
699
  C: (f, a) => f(...a)
700
700
  });
701
701
  if (this.connection) {
702
- await this.closeConnection();
702
+ await this.closeConnection(new Error("Connection displaced by remote initiator."));
703
703
  }
704
704
  } else {
705
705
  return {
@@ -711,7 +711,7 @@ var Peer = class {
711
711
  if (!this.connection) {
712
712
  invariant3(message.sessionId, void 0, {
713
713
  F: __dxlog_file3,
714
- L: 126,
714
+ L: 128,
715
715
  S: this,
716
716
  A: [
717
717
  "message.sessionId",
@@ -731,12 +731,12 @@ var Peer = class {
731
731
  err
732
732
  }, {
733
733
  F: __dxlog_file3,
734
- L: 134,
734
+ L: 136,
735
735
  S: this,
736
736
  C: (f, a) => f(...a)
737
737
  });
738
738
  }
739
- await this.closeConnection();
739
+ await this.closeConnection(err);
740
740
  }
741
741
  return {
742
742
  accept: true
@@ -753,7 +753,7 @@ var Peer = class {
753
753
  async initiateConnection() {
754
754
  invariant3(!this.initiating, "Initiation in progress.", {
755
755
  F: __dxlog_file3,
756
- L: 151,
756
+ L: 153,
757
757
  S: this,
758
758
  A: [
759
759
  "!this.initiating",
@@ -762,7 +762,7 @@ var Peer = class {
762
762
  });
763
763
  invariant3(!this.connection, "Already connected.", {
764
764
  F: __dxlog_file3,
765
- L: 152,
765
+ L: 154,
766
766
  S: this,
767
767
  A: [
768
768
  "!this.connection",
@@ -777,7 +777,7 @@ var Peer = class {
777
777
  sessionId
778
778
  }, {
779
779
  F: __dxlog_file3,
780
- L: 154,
780
+ L: 156,
781
781
  S: this,
782
782
  C: (f, a) => f(...a)
783
783
  });
@@ -801,14 +801,14 @@ var Peer = class {
801
801
  remoteId: this.id
802
802
  }, {
803
803
  F: __dxlog_file3,
804
- L: 169,
804
+ L: 171,
805
805
  S: this,
806
806
  C: (f, a) => f(...a)
807
807
  });
808
808
  if (connection.state !== ConnectionState.INITIAL) {
809
809
  log3("ignoring response", void 0, {
810
810
  F: __dxlog_file3,
811
- L: 171,
811
+ L: 173,
812
812
  S: this,
813
813
  C: (f, a) => f(...a)
814
814
  });
@@ -828,11 +828,11 @@ var Peer = class {
828
828
  remoteId: this.id
829
829
  }, {
830
830
  F: __dxlog_file3,
831
- L: 182,
831
+ L: 184,
832
832
  S: this,
833
833
  C: (f, a) => f(...a)
834
834
  });
835
- await this.closeConnection();
835
+ await this.closeConnection(err);
836
836
  throw err;
837
837
  } finally {
838
838
  this.initiating = false;
@@ -843,7 +843,6 @@ var Peer = class {
843
843
  * Either we're initiating a connection or creating one in response to an offer from the other peer.
844
844
  */
845
845
  _createConnection(initiator, sessionId) {
846
- var _a;
847
846
  log3("creating connection", {
848
847
  topic: this.topic,
849
848
  peerId: this.localPeerId,
@@ -852,13 +851,13 @@ var Peer = class {
852
851
  sessionId
853
852
  }, {
854
853
  F: __dxlog_file3,
855
- L: 196,
854
+ L: 198,
856
855
  S: this,
857
856
  C: (f, a) => f(...a)
858
857
  });
859
858
  invariant3(!this.connection, "Already connected.", {
860
859
  F: __dxlog_file3,
861
- L: 203,
860
+ L: 205,
862
861
  S: this,
863
862
  A: [
864
863
  "!this.connection",
@@ -882,7 +881,7 @@ var Peer = class {
882
881
  this._transportFactory
883
882
  );
884
883
  this._callbacks.onInitiated(connection);
885
- void ((_a = this._connectionCtx) == null ? void 0 : _a.dispose());
884
+ void this._connectionCtx?.dispose();
886
885
  this._connectionCtx = this._ctx.derive();
887
886
  connection.stateChanged.on((state) => {
888
887
  switch (state) {
@@ -899,7 +898,7 @@ var Peer = class {
899
898
  initiator
900
899
  }, {
901
900
  F: __dxlog_file3,
902
- L: 229,
901
+ L: 231,
903
902
  S: this,
904
903
  C: (f, a) => f(...a)
905
904
  });
@@ -913,13 +912,13 @@ var Peer = class {
913
912
  initiator
914
913
  }, {
915
914
  F: __dxlog_file3,
916
- L: 240,
915
+ L: 242,
917
916
  S: this,
918
917
  C: (f, a) => f(...a)
919
918
  });
920
919
  invariant3(this.connection === connection, "Connection mismatch (race condition).", {
921
920
  F: __dxlog_file3,
922
- L: 241,
921
+ L: 243,
923
922
  S: this,
924
923
  A: [
925
924
  "this.connection === connection",
@@ -934,7 +933,7 @@ var Peer = class {
934
933
  initiator
935
934
  }, {
936
935
  F: __dxlog_file3,
937
- L: 243,
936
+ L: 245,
938
937
  S: this,
939
938
  C: (f, a) => f(...a)
940
939
  });
@@ -964,7 +963,7 @@ var Peer = class {
964
963
  err
965
964
  }, {
966
965
  F: __dxlog_file3,
967
- L: 277,
966
+ L: 279,
968
967
  S: this,
969
968
  C: (f, a) => f(...a)
970
969
  });
@@ -977,16 +976,16 @@ var Peer = class {
977
976
  err
978
977
  }, {
979
978
  F: __dxlog_file3,
980
- L: 278,
979
+ L: 280,
981
980
  S: this,
982
981
  C: (f, a) => f(...a)
983
982
  });
984
- void this.closeConnection();
983
+ void this.closeConnection(err);
985
984
  });
986
985
  this.connection = connection;
987
986
  return connection;
988
987
  }
989
- async closeConnection() {
988
+ async closeConnection(err) {
990
989
  if (!this.connection) {
991
990
  return;
992
991
  }
@@ -996,17 +995,17 @@ var Peer = class {
996
995
  sessionId: connection.sessionId
997
996
  }, {
998
997
  F: __dxlog_file3,
999
- L: 301,
998
+ L: 303,
1000
999
  S: this,
1001
1000
  C: (f, a) => f(...a)
1002
1001
  });
1003
- await connection.close();
1002
+ await connection.close(err);
1004
1003
  log3("closed", {
1005
1004
  peerId: this.id,
1006
1005
  sessionId: connection.sessionId
1007
1006
  }, {
1008
1007
  F: __dxlog_file3,
1009
- L: 307,
1008
+ L: 309,
1010
1009
  S: this,
1011
1010
  C: (f, a) => f(...a)
1012
1011
  });
@@ -1017,7 +1016,7 @@ var Peer = class {
1017
1016
  message
1018
1017
  }, {
1019
1018
  F: __dxlog_file3,
1020
- L: 312,
1019
+ L: 314,
1021
1020
  S: this,
1022
1021
  C: (f, a) => f(...a)
1023
1022
  });
@@ -1026,18 +1025,17 @@ var Peer = class {
1026
1025
  await this.connection.signal(message);
1027
1026
  }
1028
1027
  async destroy() {
1029
- var _a;
1030
1028
  await this._ctx.dispose();
1031
1029
  log3("Destroying peer", {
1032
1030
  peerId: this.id,
1033
1031
  topic: this.topic
1034
1032
  }, {
1035
1033
  F: __dxlog_file3,
1036
- L: 322,
1034
+ L: 324,
1037
1035
  S: this,
1038
1036
  C: (f, a) => f(...a)
1039
1037
  });
1040
- await ((_a = this == null ? void 0 : this.connection) == null ? void 0 : _a.close());
1038
+ await this?.connection?.close();
1041
1039
  }
1042
1040
  };
1043
1041
  _ts_decorate2([
@@ -1082,9 +1080,9 @@ var Swarm = class {
1082
1080
  this._transportFactory = _transportFactory;
1083
1081
  this._label = _label;
1084
1082
  this._connectionLimiter = _connectionLimiter;
1085
- this._peers = new ComplexMap2(PublicKey4.hash);
1086
1083
  this._ctx = new Context4();
1087
1084
  this._listeningHandle = void 0;
1085
+ this._peers = new ComplexMap2(PublicKey4.hash);
1088
1086
  this._instanceId = PublicKey4.random().toHex();
1089
1087
  this.connectionAdded = new Event2();
1090
1088
  this.disconnected = new Event2();
@@ -1167,14 +1165,13 @@ var Swarm = class {
1167
1165
  });
1168
1166
  }
1169
1167
  async destroy() {
1170
- var _a;
1171
1168
  log4("destroying...", void 0, {
1172
1169
  F: __dxlog_file4,
1173
1170
  L: 140,
1174
1171
  S: this,
1175
1172
  C: (f, a) => f(...a)
1176
1173
  });
1177
- await ((_a = this._listeningHandle) == null ? void 0 : _a.unsubscribe());
1174
+ await this._listeningHandle?.unsubscribe();
1178
1175
  this._listeningHandle = void 0;
1179
1176
  await this._ctx.dispose();
1180
1177
  await this._topology.destroy();
@@ -1214,7 +1211,6 @@ var Swarm = class {
1214
1211
  this._topology.update();
1215
1212
  }
1216
1213
  onSwarmEvent(swarmEvent) {
1217
- var _a;
1218
1214
  log4("swarm event", {
1219
1215
  swarmEvent
1220
1216
  }, {
@@ -1250,7 +1246,7 @@ var Swarm = class {
1250
1246
  const peer = this._peers.get(PublicKey4.from(swarmEvent.peerLeft.peer));
1251
1247
  if (peer) {
1252
1248
  peer.advertizing = false;
1253
- if (((_a = peer.connection) == null ? void 0 : _a.state) !== ConnectionState.CONNECTED) {
1249
+ if (peer.connection?.state !== ConnectionState.CONNECTED) {
1254
1250
  void this._destroyPeer(peer.id).catch((err) => log4.catch(err, void 0, {
1255
1251
  F: __dxlog_file4,
1256
1252
  L: 188,
@@ -1263,7 +1259,6 @@ var Swarm = class {
1263
1259
  this._topology.update();
1264
1260
  }
1265
1261
  async onOffer(message) {
1266
- var _a, _b;
1267
1262
  log4("offer", {
1268
1263
  message
1269
1264
  }, {
@@ -1292,7 +1287,7 @@ var Swarm = class {
1292
1287
  ""
1293
1288
  ]
1294
1289
  });
1295
- if (!((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId))) {
1290
+ if (!message.recipient?.equals(this._ownPeerId)) {
1296
1291
  log4("rejecting offer with incorrect peerId", {
1297
1292
  message
1298
1293
  }, {
@@ -1305,7 +1300,7 @@ var Swarm = class {
1305
1300
  accept: false
1306
1301
  };
1307
1302
  }
1308
- if (!((_b = message.topic) == null ? void 0 : _b.equals(this._topic))) {
1303
+ if (!message.topic?.equals(this._topic)) {
1309
1304
  log4("rejecting offer with incorrect topic", {
1310
1305
  message
1311
1306
  }, {
@@ -1324,7 +1319,6 @@ var Swarm = class {
1324
1319
  return answer;
1325
1320
  }
1326
1321
  async onSignal(message) {
1327
- var _a, _b;
1328
1322
  log4("signal", {
1329
1323
  message
1330
1324
  }, {
@@ -1342,7 +1336,7 @@ var Swarm = class {
1342
1336
  });
1343
1337
  return;
1344
1338
  }
1345
- invariant4((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1339
+ invariant4(message.recipient?.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1346
1340
  F: __dxlog_file4,
1347
1341
  L: 227,
1348
1342
  S: this,
@@ -1351,7 +1345,7 @@ var Swarm = class {
1351
1345
  "`Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`"
1352
1346
  ]
1353
1347
  });
1354
- invariant4((_b = message.topic) == null ? void 0 : _b.equals(this._topic), void 0, {
1348
+ invariant4(message.topic?.equals(this._topic), void 0, {
1355
1349
  F: __dxlog_file4,
1356
1350
  L: 231,
1357
1351
  S: this,
@@ -1563,8 +1557,7 @@ var SwarmMapper = class {
1563
1557
  }));
1564
1558
  }));
1565
1559
  this._subscriptions.add(_swarm.disconnected.on((peerId) => {
1566
- var _a;
1567
- (_a = this._connectionSubscriptions.get(peerId)) == null ? void 0 : _a();
1560
+ this._connectionSubscriptions.get(peerId)?.();
1568
1561
  this._connectionSubscriptions.delete(peerId);
1569
1562
  this._update();
1570
1563
  }));
@@ -1714,8 +1707,7 @@ var ConnectionLog = class {
1714
1707
  this.update = new Event4();
1715
1708
  }
1716
1709
  getSwarmInfo(swarmId) {
1717
- var _a;
1718
- return (_a = this._swarms.get(swarmId)) != null ? _a : raise(new Error(`Swarm not found: ${swarmId}`));
1710
+ return this._swarms.get(swarmId) ?? raise(new Error(`Swarm not found: ${swarmId}`));
1719
1711
  }
1720
1712
  get swarms() {
1721
1713
  return Array.from(this._swarms.values());
@@ -1731,9 +1723,9 @@ var ConnectionLog = class {
1731
1723
  this._swarms.set(PublicKey7.from(swarm._instanceId), info);
1732
1724
  this.update.emit();
1733
1725
  swarm.connectionAdded.on((connection) => {
1734
- var _a, _b;
1735
1726
  const connectionInfo = {
1736
1727
  state: ConnectionState.INITIAL,
1728
+ closeReason: connection.closeReason,
1737
1729
  remotePeerId: connection.remoteId,
1738
1730
  sessionId: connection.sessionId,
1739
1731
  transport: connection.transport && Object.getPrototypeOf(connection.transport).constructor.name,
@@ -1744,13 +1736,14 @@ var ConnectionLog = class {
1744
1736
  this.update.emit();
1745
1737
  connection.stateChanged.on((state) => {
1746
1738
  connectionInfo.state = state;
1739
+ connectionInfo.closeReason = connection.closeReason;
1747
1740
  connectionInfo.events.push({
1748
1741
  type: EventType.CONNECTION_STATE_CHANGED,
1749
1742
  newState: state
1750
1743
  });
1751
1744
  this.update.emit();
1752
1745
  });
1753
- (_b = (_a = connection.protocol) == null ? void 0 : _a.stats) == null ? void 0 : _b.on((stats) => {
1746
+ connection.protocol?.stats?.on((stats) => {
1754
1747
  connectionInfo.streams = stats.channels;
1755
1748
  this.update.emit();
1756
1749
  });
@@ -1785,10 +1778,7 @@ var NetworkManager = class {
1785
1778
  this._instanceId = PublicKey8.random().toHex();
1786
1779
  this._transportFactory = transportFactory;
1787
1780
  this._signalManager = signalManager;
1788
- this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => {
1789
- var _a;
1790
- return (_a = this._swarms.get(topic)) == null ? void 0 : _a.onSwarmEvent(event);
1791
- });
1781
+ this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => this._swarms.get(topic)?.onSwarmEvent(event));
1792
1782
  this._messenger = new Messenger({
1793
1783
  signalManager: this._signalManager
1794
1784
  });
@@ -1856,7 +1846,6 @@ var NetworkManager = class {
1856
1846
  * Join the swarm.
1857
1847
  */
1858
1848
  async joinSwarm({ topic, peerId, topology, protocolProvider: protocol, label }) {
1859
- var _a;
1860
1849
  invariant6(PublicKey8.isPublicKey(topic), void 0, {
1861
1850
  F: __dxlog_file7,
1862
1851
  L: 157,
@@ -1930,7 +1919,7 @@ var NetworkManager = class {
1930
1919
  C: (f, a) => f(...a)
1931
1920
  }));
1932
1921
  this.topicsUpdated.emit();
1933
- (_a = this._connectionLog) == null ? void 0 : _a.joinedSwarm(swarm);
1922
+ this._connectionLog?.joinedSwarm(swarm);
1934
1923
  log7("joined", {
1935
1924
  topic: PublicKey8.from(topic),
1936
1925
  count: this._swarms.size
@@ -1948,7 +1937,6 @@ var NetworkManager = class {
1948
1937
  * Close the connection.
1949
1938
  */
1950
1939
  async leaveSwarm(topic) {
1951
- var _a;
1952
1940
  if (!this._swarms.has(topic)) {
1953
1941
  return;
1954
1942
  }
@@ -1968,7 +1956,7 @@ var NetworkManager = class {
1968
1956
  const map = this._mappers.get(topic);
1969
1957
  map.destroy();
1970
1958
  this._mappers.delete(topic);
1971
- (_a = this._connectionLog) == null ? void 0 : _a.leftSwarm(swarm);
1959
+ this._connectionLog?.leftSwarm(swarm);
1972
1960
  await swarm.destroy();
1973
1961
  this._swarms.delete(topic);
1974
1962
  await this.topicsUpdated.emit();
@@ -2289,9 +2277,12 @@ var createStreamDelay = (delay) => {
2289
2277
  var MemoryTransportFactory = {
2290
2278
  createTransport: (params) => new MemoryTransport(params)
2291
2279
  };
2292
- var _MemoryTransport = class {
2280
+ var MemoryTransport = class _MemoryTransport {
2281
+ static {
2282
+ // TODO(burdon): Remove static properties (inject context into constructor).
2283
+ this._connections = new ComplexMap7(PublicKey9.hash);
2284
+ }
2293
2285
  constructor(options) {
2294
- var _a;
2295
2286
  this.options = options;
2296
2287
  this.closed = new Event6();
2297
2288
  this.connected = new Event6();
@@ -2337,7 +2328,7 @@ var _MemoryTransport = class {
2337
2328
  });
2338
2329
  } else {
2339
2330
  this._remote.wait({
2340
- timeout: (_a = this.options.timeout) != null ? _a : 1e3
2331
+ timeout: this.options.timeout ?? 1e3
2341
2332
  }).then((remoteId) => {
2342
2333
  if (this._destroyed) {
2343
2334
  return;
@@ -2424,7 +2415,7 @@ var _MemoryTransport = class {
2424
2415
  S: this,
2425
2416
  C: (f, a) => f(...a)
2426
2417
  });
2427
- if (!(payload == null ? void 0 : payload.transportId)) {
2418
+ if (!payload?.transportId) {
2428
2419
  return;
2429
2420
  }
2430
2421
  const transportId = payload.transportId;
@@ -2434,9 +2425,6 @@ var _MemoryTransport = class {
2434
2425
  }
2435
2426
  }
2436
2427
  };
2437
- var MemoryTransport = _MemoryTransport;
2438
- // TODO(burdon): Remove static properties (inject context into constructor).
2439
- MemoryTransport._connections = new ComplexMap7(PublicKey9.hash);
2440
2428
  _ts_decorate4([
2441
2429
  logInfo2
2442
2430
  ], MemoryTransport.prototype, "_instanceId", void 0);
@@ -2457,43 +2445,47 @@ import { trace as trace4 } from "@dxos/protocols";
2457
2445
  var wrtc = null;
2458
2446
  try {
2459
2447
  wrtc = __require("@koush/wrtc");
2460
- } catch (e) {
2448
+ } catch {
2461
2449
  }
2462
2450
 
2463
2451
  // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
2464
2452
  var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
2465
2453
  var WebRTCTransport = class {
2466
2454
  constructor(params) {
2467
- var _a;
2468
2455
  this.params = params;
2456
+ this._instanceId = PublicKey10.random().toHex();
2469
2457
  this._closed = false;
2470
2458
  this.closed = new Event7();
2471
2459
  this.connected = new Event7();
2472
2460
  this.errors = new ErrorStream4();
2473
- this._instanceId = PublicKey10.random().toHex();
2474
2461
  log11.trace("dxos.mesh.webrtc-transport.constructor", trace4.begin({
2475
2462
  id: this._instanceId
2476
2463
  }), {
2477
2464
  F: __dxlog_file12,
2478
- L: 39,
2465
+ L: 44,
2479
2466
  S: this,
2480
2467
  C: (f, a) => f(...a)
2481
2468
  });
2482
2469
  log11("created connection", params, {
2483
2470
  F: __dxlog_file12,
2484
- L: 40,
2471
+ L: 45,
2485
2472
  S: this,
2486
2473
  C: (f, a) => f(...a)
2487
2474
  });
2488
2475
  this._peer = new SimplePeerConstructor({
2489
2476
  initiator: this.params.initiator,
2490
- wrtc: SimplePeerConstructor.WEBRTC_SUPPORT ? void 0 : (_a = wrtc) != null ? _a : raise2(new Error("wrtc not available")),
2491
- config: this.params.webrtcConfig
2477
+ config: this.params.webrtcConfig,
2478
+ // https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/createDataChannel
2479
+ channelConfig: {
2480
+ protocol: "dxos.mesh"
2481
+ },
2482
+ // Custom WebRTC implementation (for Node); otherwise get-browser-rtc.
2483
+ wrtc: SimplePeerConstructor.WEBRTC_SUPPORT ? void 0 : wrtc ?? raise2(new Error("wrtc not available"))
2492
2484
  });
2493
2485
  this._peer.on("signal", async (data) => {
2494
2486
  log11("signal", data, {
2495
2487
  F: __dxlog_file12,
2496
- L: 48,
2488
+ L: 62,
2497
2489
  S: this,
2498
2490
  C: (f, a) => f(...a)
2499
2491
  });
@@ -2506,7 +2498,7 @@ var WebRTCTransport = class {
2506
2498
  this._peer.on("connect", () => {
2507
2499
  log11("connected", void 0, {
2508
2500
  F: __dxlog_file12,
2509
- L: 53,
2501
+ L: 67,
2510
2502
  S: this,
2511
2503
  C: (f, a) => f(...a)
2512
2504
  });
@@ -2516,7 +2508,7 @@ var WebRTCTransport = class {
2516
2508
  this._peer.on("close", async () => {
2517
2509
  log11("closed", void 0, {
2518
2510
  F: __dxlog_file12,
2519
- L: 59,
2511
+ L: 73,
2520
2512
  S: this,
2521
2513
  C: (f, a) => f(...a)
2522
2514
  });
@@ -2524,23 +2516,22 @@ var WebRTCTransport = class {
2524
2516
  this.closed.emit();
2525
2517
  });
2526
2518
  this._peer.on("error", async (err) => {
2527
- var _a2;
2528
2519
  this.errors.raise(err);
2529
2520
  try {
2530
- if (typeof ((_a2 = this._peer) == null ? void 0 : _a2._pc.getStats) === "function") {
2521
+ if (typeof this._peer?._pc.getStats === "function") {
2531
2522
  this._peer._pc.getStats().then((stats) => {
2532
2523
  log11.warn("report after webrtc error", {
2533
2524
  config: this.params.webrtcConfig,
2534
2525
  stats
2535
2526
  }, {
2536
2527
  F: __dxlog_file12,
2537
- L: 71,
2528
+ L: 85,
2538
2529
  S: this,
2539
2530
  C: (f, a) => f(...a)
2540
2531
  });
2541
2532
  });
2542
2533
  }
2543
- } catch (e) {
2534
+ } catch {
2544
2535
  }
2545
2536
  await this.destroy();
2546
2537
  });
@@ -2548,7 +2539,7 @@ var WebRTCTransport = class {
2548
2539
  id: this._instanceId
2549
2540
  }), {
2550
2541
  F: __dxlog_file12,
2551
- L: 81,
2542
+ L: 95,
2552
2543
  S: this,
2553
2544
  C: (f, a) => f(...a)
2554
2545
  });
@@ -2556,7 +2547,7 @@ var WebRTCTransport = class {
2556
2547
  async destroy() {
2557
2548
  log11("closing...", void 0, {
2558
2549
  F: __dxlog_file12,
2559
- L: 85,
2550
+ L: 99,
2560
2551
  S: this,
2561
2552
  C: (f, a) => f(...a)
2562
2553
  });
@@ -2566,7 +2557,7 @@ var WebRTCTransport = class {
2566
2557
  this.closed.emit();
2567
2558
  log11("closed", void 0, {
2568
2559
  F: __dxlog_file12,
2569
- L: 90,
2560
+ L: 104,
2570
2561
  S: this,
2571
2562
  C: (f, a) => f(...a)
2572
2563
  });
@@ -2577,7 +2568,7 @@ var WebRTCTransport = class {
2577
2568
  }
2578
2569
  invariant11(signal.payload.data, "Signal message must contain signal data.", {
2579
2570
  F: __dxlog_file12,
2580
- L: 98,
2571
+ L: 112,
2581
2572
  S: this,
2582
2573
  A: [
2583
2574
  "signal.payload.data",
@@ -2587,8 +2578,7 @@ var WebRTCTransport = class {
2587
2578
  this._peer.signal(signal.payload.data);
2588
2579
  }
2589
2580
  async _disconnectStreams() {
2590
- var _a, _b, _c, _d;
2591
- (_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);
2581
+ this.params.stream.unpipe?.(this._peer)?.unpipe?.(this.params.stream);
2592
2582
  }
2593
2583
  };
2594
2584
  var createWebRTCTransportFactory = (webrtcConfig) => ({
@@ -2716,9 +2706,8 @@ var WebRTCTransportService = class {
2716
2706
  }
2717
2707
  }
2718
2708
  async close({ proxyId }) {
2719
- var _a, _b;
2720
- await ((_a = this.transports.get(proxyId)) == null ? void 0 : _a.transport.destroy());
2721
- await ((_b = this.transports.get(proxyId)) == null ? void 0 : _b.stream.end());
2709
+ await this.transports.get(proxyId)?.transport.destroy();
2710
+ await this.transports.get(proxyId)?.stream.end();
2722
2711
  this.transports.delete(proxyId);
2723
2712
  log12("Closed.", void 0, {
2724
2713
  F: __dxlog_file13,
@@ -2747,9 +2736,9 @@ var WebRTCTransportProxy = class {
2747
2736
  this._proxyId = PublicKey12.random();
2748
2737
  this._ctx = new Context6();
2749
2738
  this.closed = new Event8();
2750
- this._closed = false;
2751
2739
  this.connected = new Event8();
2752
2740
  this.errors = new ErrorStream5();
2741
+ this._closed = false;
2753
2742
  this._serviceStream = this._params.bridgeService.open({
2754
2743
  proxyId: this._proxyId,
2755
2744
  initiator: this._params.initiator
@@ -2825,7 +2814,7 @@ var WebRTCTransportProxy = class {
2825
2814
  if (this._closed) {
2826
2815
  return;
2827
2816
  }
2828
- this._serviceStream.close();
2817
+ await this._serviceStream.close();
2829
2818
  try {
2830
2819
  await this._params.bridgeService.close({
2831
2820
  proxyId: this._proxyId
@@ -2846,7 +2835,7 @@ var WebRTCTransportProxy = class {
2846
2835
  */
2847
2836
  // TODO(burdon): Option on close method.
2848
2837
  forceClose() {
2849
- this._serviceStream.close();
2838
+ void this._serviceStream.close();
2850
2839
  this.closed.emit();
2851
2840
  this._closed = true;
2852
2841
  }
@@ -2927,4 +2916,4 @@ export {
2927
2916
  WebRTCTransportProxyFactory,
2928
2917
  createTeleportProtocolFactory
2929
2918
  };
2930
- //# sourceMappingURL=chunk-FC6CWDES.mjs.map
2919
+ //# sourceMappingURL=chunk-YADPADXG.mjs.map