@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
@@ -146,7 +146,7 @@ var Connection = class {
146
146
  initiator: this.initiator
147
147
  }, {
148
148
  F: __dxlog_file,
149
- L: 95,
149
+ L: 96,
150
150
  S: this,
151
151
  C: (f, a) => f(...a)
152
152
  });
@@ -166,7 +166,7 @@ var Connection = class {
166
166
  async openConnection() {
167
167
  (0, import_invariant.invariant)(this._state === ConnectionState.INITIAL, "Invalid state.", {
168
168
  F: __dxlog_file,
169
- L: 120,
169
+ L: 121,
170
170
  S: this,
171
171
  A: [
172
172
  "this._state === ConnectionState.INITIAL",
@@ -177,7 +177,7 @@ var Connection = class {
177
177
  id: this._instanceId
178
178
  }), {
179
179
  F: __dxlog_file,
180
- L: 121,
180
+ L: 122,
181
181
  S: this,
182
182
  C: (f, a) => f(...a)
183
183
  });
@@ -189,7 +189,7 @@ var Connection = class {
189
189
  initiator: this.initiator
190
190
  }, {
191
191
  F: __dxlog_file,
192
- L: 122,
192
+ L: 123,
193
193
  S: this,
194
194
  C: (f, a) => f(...a)
195
195
  });
@@ -200,7 +200,7 @@ var Connection = class {
200
200
  this._protocol.stream.on("close", () => {
201
201
  (0, import_log.log)("protocol stream closed", void 0, {
202
202
  F: __dxlog_file,
203
- L: 139,
203
+ L: 140,
204
204
  S: this,
205
205
  C: (f, a) => f(...a)
206
206
  });
@@ -208,7 +208,7 @@ var Connection = class {
208
208
  });
209
209
  (0, import_invariant.invariant)(!this._transport, void 0, {
210
210
  F: __dxlog_file,
211
- L: 143,
211
+ L: 144,
212
212
  S: this,
213
213
  A: [
214
214
  "!this._transport",
@@ -228,6 +228,9 @@ var Connection = class {
228
228
  this.close().catch((err) => this.errors.raise(err));
229
229
  });
230
230
  this._transport.errors.handle((err) => {
231
+ if (!this.closeReason) {
232
+ this.closeReason = err?.message;
233
+ }
231
234
  if (this._state !== ConnectionState.CLOSED && this._state !== ConnectionState.CLOSING) {
232
235
  this.errors.raise(err);
233
236
  }
@@ -240,13 +243,15 @@ var Connection = class {
240
243
  id: this._instanceId
241
244
  }), {
242
245
  F: __dxlog_file,
243
- L: 172,
246
+ L: 176,
244
247
  S: this,
245
248
  C: (f, a) => f(...a)
246
249
  });
247
250
  }
248
- async close() {
249
- var _a2;
251
+ async close(err) {
252
+ if (!this.closeReason) {
253
+ this.closeReason = err?.message;
254
+ }
250
255
  if (this._state === ConnectionState.CLOSED) {
251
256
  return;
252
257
  }
@@ -256,26 +261,26 @@ var Connection = class {
256
261
  peerId: this.ownId
257
262
  }, {
258
263
  F: __dxlog_file,
259
- L: 184,
264
+ L: 191,
260
265
  S: this,
261
266
  C: (f, a) => f(...a)
262
267
  });
263
268
  try {
264
269
  await this._protocol.destroy();
265
- } catch (err) {
266
- import_log.log.catch(err, void 0, {
270
+ } catch (err2) {
271
+ import_log.log.catch(err2, void 0, {
267
272
  F: __dxlog_file,
268
- L: 190,
273
+ L: 197,
269
274
  S: this,
270
275
  C: (f, a) => f(...a)
271
276
  });
272
277
  }
273
278
  try {
274
- await ((_a2 = this._transport) == null ? void 0 : _a2.destroy());
275
- } catch (err) {
276
- import_log.log.catch(err, void 0, {
279
+ await this._transport?.destroy();
280
+ } catch (err2) {
281
+ import_log.log.catch(err2, void 0, {
277
282
  F: __dxlog_file,
278
- L: 197,
283
+ L: 204,
279
284
  S: this,
280
285
  C: (f, a) => f(...a)
281
286
  });
@@ -284,7 +289,7 @@ var Connection = class {
284
289
  peerId: this.ownId
285
290
  }, {
286
291
  F: __dxlog_file,
287
- L: 200,
292
+ L: 207,
288
293
  S: this,
289
294
  C: (f, a) => f(...a)
290
295
  });
@@ -324,7 +329,7 @@ var Connection = class {
324
329
  err
325
330
  }, {
326
331
  F: __dxlog_file,
327
- L: 234,
332
+ L: 241,
328
333
  S: this,
329
334
  C: (f, a) => f(...a)
330
335
  });
@@ -335,10 +340,9 @@ var Connection = class {
335
340
  * Receive a signal from the remote peer.
336
341
  */
337
342
  async signal(msg) {
338
- var _a2, _b, _c;
339
343
  (0, import_invariant.invariant)(msg.sessionId, void 0, {
340
344
  F: __dxlog_file,
341
- L: 243,
345
+ L: 250,
342
346
  S: this,
343
347
  A: [
344
348
  "msg.sessionId",
@@ -348,7 +352,7 @@ var Connection = class {
348
352
  if (!msg.sessionId.equals(this.sessionId)) {
349
353
  (0, import_log.log)("dropping signal for incorrect session id", void 0, {
350
354
  F: __dxlog_file,
351
- L: 245,
355
+ L: 252,
352
356
  S: this,
353
357
  C: (f, a) => f(...a)
354
358
  });
@@ -356,32 +360,32 @@ var Connection = class {
356
360
  }
357
361
  (0, import_invariant.invariant)(msg.data.signal || msg.data.signalBatch, void 0, {
358
362
  F: __dxlog_file,
359
- L: 248,
363
+ L: 255,
360
364
  S: this,
361
365
  A: [
362
366
  "msg.data.signal || msg.data.signalBatch",
363
367
  ""
364
368
  ]
365
369
  });
366
- (0, import_invariant.invariant)((_a2 = msg.author) == null ? void 0 : _a2.equals(this.remoteId), void 0, {
370
+ (0, import_invariant.invariant)(msg.author?.equals(this.remoteId), void 0, {
367
371
  F: __dxlog_file,
368
- L: 249,
372
+ L: 256,
369
373
  S: this,
370
374
  A: [
371
375
  "msg.author?.equals(this.remoteId)",
372
376
  ""
373
377
  ]
374
378
  });
375
- (0, import_invariant.invariant)((_b = msg.recipient) == null ? void 0 : _b.equals(this.ownId), void 0, {
379
+ (0, import_invariant.invariant)(msg.recipient?.equals(this.ownId), void 0, {
376
380
  F: __dxlog_file,
377
- L: 250,
381
+ L: 257,
378
382
  S: this,
379
383
  A: [
380
384
  "msg.recipient?.equals(this.ownId)",
381
385
  ""
382
386
  ]
383
387
  });
384
- const signals = msg.data.signalBatch ? (_c = msg.data.signalBatch.signals) != null ? _c : [] : [
388
+ const signals = msg.data.signalBatch ? msg.data.signalBatch.signals ?? [] : [
385
389
  msg.data.signal
386
390
  ];
387
391
  for (const signal of signals) {
@@ -395,7 +399,7 @@ var Connection = class {
395
399
  msg: msg.data
396
400
  }, {
397
401
  F: __dxlog_file,
398
- L: 259,
402
+ L: 266,
399
403
  S: this,
400
404
  C: (f, a) => f(...a)
401
405
  });
@@ -403,7 +407,7 @@ var Connection = class {
403
407
  } else {
404
408
  (0, import_invariant.invariant)(this._transport, "Connection not ready to accept signals.", {
405
409
  F: __dxlog_file,
406
- L: 262,
410
+ L: 269,
407
411
  S: this,
408
412
  A: [
409
413
  "this._transport",
@@ -416,7 +420,7 @@ var Connection = class {
416
420
  msg: msg.data
417
421
  }, {
418
422
  F: __dxlog_file,
419
- L: 263,
423
+ L: 270,
420
424
  S: this,
421
425
  C: (f, a) => f(...a)
422
426
  });
@@ -431,13 +435,13 @@ var Connection = class {
431
435
  peerId: this.ownId
432
436
  }, {
433
437
  F: __dxlog_file,
434
- L: 270,
438
+ L: 277,
435
439
  S: this,
436
440
  C: (f, a) => f(...a)
437
441
  });
438
442
  (0, import_invariant.invariant)(state !== this._state, "Already in this state.", {
439
443
  F: __dxlog_file,
440
- L: 271,
444
+ L: 278,
441
445
  S: this,
442
446
  A: [
443
447
  "state !== this._state",
@@ -481,7 +485,6 @@ var SwarmMessenger = class {
481
485
  this._topic = topic;
482
486
  }
483
487
  async receiveMessage({ author, recipient, payload }) {
484
- var _a2, _b, _c, _d;
485
488
  if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
486
489
  return;
487
490
  }
@@ -499,21 +502,21 @@ var SwarmMessenger = class {
499
502
  S: this,
500
503
  C: (f, a) => f(...a)
501
504
  });
502
- if ((_a2 = message.data) == null ? void 0 : _a2.offer) {
505
+ if (message.data?.offer) {
503
506
  await this._handleOffer({
504
507
  author,
505
508
  recipient,
506
509
  message
507
510
  });
508
- } else if ((_b = message.data) == null ? void 0 : _b.answer) {
511
+ } else if (message.data?.answer) {
509
512
  await this._resolveAnswers(message);
510
- } else if ((_c = message.data) == null ? void 0 : _c.signal) {
513
+ } else if (message.data?.signal) {
511
514
  await this._handleSignal({
512
515
  author,
513
516
  recipient,
514
517
  message
515
518
  });
516
- } else if ((_d = message.data) == null ? void 0 : _d.signalBatch) {
519
+ } else if (message.data?.signalBatch) {
517
520
  await this._handleSignal({
518
521
  author,
519
522
  recipient,
@@ -531,8 +534,7 @@ var SwarmMessenger = class {
531
534
  }
532
535
  }
533
536
  async signal(message) {
534
- var _a2, _b;
535
- (0, import_invariant2.invariant)(((_a2 = message.data) == null ? void 0 : _a2.signal) || ((_b = message.data) == null ? void 0 : _b.signalBatch), "Invalid message", {
537
+ (0, import_invariant2.invariant)(message.data?.signal || message.data?.signalBatch, "Invalid message", {
536
538
  F: __dxlog_file2,
537
539
  L: 85,
538
540
  S: this,
@@ -564,11 +566,10 @@ var SwarmMessenger = class {
564
566
  });
565
567
  }
566
568
  async _sendReliableMessage({ author, recipient, message }) {
567
- var _a2;
568
569
  const networkMessage = {
569
570
  ...message,
570
571
  // Setting unique message_id if it not specified yet.
571
- messageId: (_a2 = message.messageId) != null ? _a2 : import_keys2.PublicKey.random()
572
+ messageId: message.messageId ?? import_keys2.PublicKey.random()
572
573
  };
573
574
  (0, import_log2.log)("sending", {
574
575
  from: author,
@@ -590,8 +591,7 @@ var SwarmMessenger = class {
590
591
  });
591
592
  }
592
593
  async _resolveAnswers(message) {
593
- var _a2, _b, _c;
594
- (0, import_invariant2.invariant)((_b = (_a2 = message.data) == null ? void 0 : _a2.answer) == null ? void 0 : _b.offerMessageId, "No offerMessageId", {
594
+ (0, import_invariant2.invariant)(message.data?.answer?.offerMessageId, "No offerMessageId", {
595
595
  F: __dxlog_file2,
596
596
  L: 135,
597
597
  S: this,
@@ -603,7 +603,7 @@ var SwarmMessenger = class {
603
603
  const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
604
604
  if (offerRecord) {
605
605
  this._offerRecords.delete(message.data.answer.offerMessageId);
606
- (0, import_invariant2.invariant)((_c = message.data) == null ? void 0 : _c.answer, "No answer", {
606
+ (0, import_invariant2.invariant)(message.data?.answer, "No answer", {
607
607
  F: __dxlog_file2,
608
608
  L: 139,
609
609
  S: this,
@@ -718,6 +718,7 @@ function _ts_decorate2(decorators, target, key, desc) {
718
718
  var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
719
719
  var CONNECTION_COUNTS_STABLE_AFTER = 5e3;
720
720
  var Peer = class {
721
+ // TODO(burdon): Convert to map?
721
722
  constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _connectionLimiter, _callbacks) {
722
723
  this.id = id;
723
724
  this.topic = topic;
@@ -737,7 +738,6 @@ var Peer = class {
737
738
  * Respond to remote offer.
738
739
  */
739
740
  async onOffer(message) {
740
- var _a2;
741
741
  const remoteId = message.author;
742
742
  if (this.connection || this.initiating) {
743
743
  if (remoteId.toHex() < this.localPeerId.toHex()) {
@@ -745,15 +745,15 @@ var Peer = class {
745
745
  localPeerId: this.id,
746
746
  topic: this.topic,
747
747
  remotePeerId: this.localPeerId,
748
- sessionId: (_a2 = this.connection) == null ? void 0 : _a2.sessionId
748
+ sessionId: this.connection?.sessionId
749
749
  }, {
750
750
  F: __dxlog_file3,
751
- L: 106,
751
+ L: 108,
752
752
  S: this,
753
753
  C: (f, a) => f(...a)
754
754
  });
755
755
  if (this.connection) {
756
- await this.closeConnection();
756
+ await this.closeConnection(new Error("Connection displaced by remote initiator."));
757
757
  }
758
758
  } else {
759
759
  return {
@@ -765,7 +765,7 @@ var Peer = class {
765
765
  if (!this.connection) {
766
766
  (0, import_invariant3.invariant)(message.sessionId, void 0, {
767
767
  F: __dxlog_file3,
768
- L: 126,
768
+ L: 128,
769
769
  S: this,
770
770
  A: [
771
771
  "message.sessionId",
@@ -785,12 +785,12 @@ var Peer = class {
785
785
  err
786
786
  }, {
787
787
  F: __dxlog_file3,
788
- L: 134,
788
+ L: 136,
789
789
  S: this,
790
790
  C: (f, a) => f(...a)
791
791
  });
792
792
  }
793
- await this.closeConnection();
793
+ await this.closeConnection(err);
794
794
  }
795
795
  return {
796
796
  accept: true
@@ -807,7 +807,7 @@ var Peer = class {
807
807
  async initiateConnection() {
808
808
  (0, import_invariant3.invariant)(!this.initiating, "Initiation in progress.", {
809
809
  F: __dxlog_file3,
810
- L: 151,
810
+ L: 153,
811
811
  S: this,
812
812
  A: [
813
813
  "!this.initiating",
@@ -816,7 +816,7 @@ var Peer = class {
816
816
  });
817
817
  (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
818
818
  F: __dxlog_file3,
819
- L: 152,
819
+ L: 154,
820
820
  S: this,
821
821
  A: [
822
822
  "!this.connection",
@@ -831,7 +831,7 @@ var Peer = class {
831
831
  sessionId
832
832
  }, {
833
833
  F: __dxlog_file3,
834
- L: 154,
834
+ L: 156,
835
835
  S: this,
836
836
  C: (f, a) => f(...a)
837
837
  });
@@ -855,14 +855,14 @@ var Peer = class {
855
855
  remoteId: this.id
856
856
  }, {
857
857
  F: __dxlog_file3,
858
- L: 169,
858
+ L: 171,
859
859
  S: this,
860
860
  C: (f, a) => f(...a)
861
861
  });
862
862
  if (connection.state !== ConnectionState.INITIAL) {
863
863
  (0, import_log3.log)("ignoring response", void 0, {
864
864
  F: __dxlog_file3,
865
- L: 171,
865
+ L: 173,
866
866
  S: this,
867
867
  C: (f, a) => f(...a)
868
868
  });
@@ -882,11 +882,11 @@ var Peer = class {
882
882
  remoteId: this.id
883
883
  }, {
884
884
  F: __dxlog_file3,
885
- L: 182,
885
+ L: 184,
886
886
  S: this,
887
887
  C: (f, a) => f(...a)
888
888
  });
889
- await this.closeConnection();
889
+ await this.closeConnection(err);
890
890
  throw err;
891
891
  } finally {
892
892
  this.initiating = false;
@@ -897,7 +897,6 @@ var Peer = class {
897
897
  * Either we're initiating a connection or creating one in response to an offer from the other peer.
898
898
  */
899
899
  _createConnection(initiator, sessionId) {
900
- var _a2;
901
900
  (0, import_log3.log)("creating connection", {
902
901
  topic: this.topic,
903
902
  peerId: this.localPeerId,
@@ -906,13 +905,13 @@ var Peer = class {
906
905
  sessionId
907
906
  }, {
908
907
  F: __dxlog_file3,
909
- L: 196,
908
+ L: 198,
910
909
  S: this,
911
910
  C: (f, a) => f(...a)
912
911
  });
913
912
  (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
914
913
  F: __dxlog_file3,
915
- L: 203,
914
+ L: 205,
916
915
  S: this,
917
916
  A: [
918
917
  "!this.connection",
@@ -936,7 +935,7 @@ var Peer = class {
936
935
  this._transportFactory
937
936
  );
938
937
  this._callbacks.onInitiated(connection);
939
- void ((_a2 = this._connectionCtx) == null ? void 0 : _a2.dispose());
938
+ void this._connectionCtx?.dispose();
940
939
  this._connectionCtx = this._ctx.derive();
941
940
  connection.stateChanged.on((state) => {
942
941
  switch (state) {
@@ -953,7 +952,7 @@ var Peer = class {
953
952
  initiator
954
953
  }, {
955
954
  F: __dxlog_file3,
956
- L: 229,
955
+ L: 231,
957
956
  S: this,
958
957
  C: (f, a) => f(...a)
959
958
  });
@@ -967,13 +966,13 @@ var Peer = class {
967
966
  initiator
968
967
  }, {
969
968
  F: __dxlog_file3,
970
- L: 240,
969
+ L: 242,
971
970
  S: this,
972
971
  C: (f, a) => f(...a)
973
972
  });
974
973
  (0, import_invariant3.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
975
974
  F: __dxlog_file3,
976
- L: 241,
975
+ L: 243,
977
976
  S: this,
978
977
  A: [
979
978
  "this.connection === connection",
@@ -988,7 +987,7 @@ var Peer = class {
988
987
  initiator
989
988
  }, {
990
989
  F: __dxlog_file3,
991
- L: 243,
990
+ L: 245,
992
991
  S: this,
993
992
  C: (f, a) => f(...a)
994
993
  });
@@ -1018,7 +1017,7 @@ var Peer = class {
1018
1017
  err
1019
1018
  }, {
1020
1019
  F: __dxlog_file3,
1021
- L: 277,
1020
+ L: 279,
1022
1021
  S: this,
1023
1022
  C: (f, a) => f(...a)
1024
1023
  });
@@ -1031,16 +1030,16 @@ var Peer = class {
1031
1030
  err
1032
1031
  }, {
1033
1032
  F: __dxlog_file3,
1034
- L: 278,
1033
+ L: 280,
1035
1034
  S: this,
1036
1035
  C: (f, a) => f(...a)
1037
1036
  });
1038
- void this.closeConnection();
1037
+ void this.closeConnection(err);
1039
1038
  });
1040
1039
  this.connection = connection;
1041
1040
  return connection;
1042
1041
  }
1043
- async closeConnection() {
1042
+ async closeConnection(err) {
1044
1043
  if (!this.connection) {
1045
1044
  return;
1046
1045
  }
@@ -1050,17 +1049,17 @@ var Peer = class {
1050
1049
  sessionId: connection.sessionId
1051
1050
  }, {
1052
1051
  F: __dxlog_file3,
1053
- L: 301,
1052
+ L: 303,
1054
1053
  S: this,
1055
1054
  C: (f, a) => f(...a)
1056
1055
  });
1057
- await connection.close();
1056
+ await connection.close(err);
1058
1057
  (0, import_log3.log)("closed", {
1059
1058
  peerId: this.id,
1060
1059
  sessionId: connection.sessionId
1061
1060
  }, {
1062
1061
  F: __dxlog_file3,
1063
- L: 307,
1062
+ L: 309,
1064
1063
  S: this,
1065
1064
  C: (f, a) => f(...a)
1066
1065
  });
@@ -1071,7 +1070,7 @@ var Peer = class {
1071
1070
  message
1072
1071
  }, {
1073
1072
  F: __dxlog_file3,
1074
- L: 312,
1073
+ L: 314,
1075
1074
  S: this,
1076
1075
  C: (f, a) => f(...a)
1077
1076
  });
@@ -1080,18 +1079,17 @@ var Peer = class {
1080
1079
  await this.connection.signal(message);
1081
1080
  }
1082
1081
  async destroy() {
1083
- var _a2;
1084
1082
  await this._ctx.dispose();
1085
1083
  (0, import_log3.log)("Destroying peer", {
1086
1084
  peerId: this.id,
1087
1085
  topic: this.topic
1088
1086
  }, {
1089
1087
  F: __dxlog_file3,
1090
- L: 322,
1088
+ L: 324,
1091
1089
  S: this,
1092
1090
  C: (f, a) => f(...a)
1093
1091
  });
1094
- await ((_a2 = this == null ? void 0 : this.connection) == null ? void 0 : _a2.close());
1092
+ await this?.connection?.close();
1095
1093
  }
1096
1094
  };
1097
1095
  _ts_decorate2([
@@ -1136,9 +1134,9 @@ var Swarm = class {
1136
1134
  this._transportFactory = _transportFactory;
1137
1135
  this._label = _label;
1138
1136
  this._connectionLimiter = _connectionLimiter;
1139
- this._peers = new import_util2.ComplexMap(import_keys4.PublicKey.hash);
1140
1137
  this._ctx = new import_context4.Context();
1141
1138
  this._listeningHandle = void 0;
1139
+ this._peers = new import_util2.ComplexMap(import_keys4.PublicKey.hash);
1142
1140
  this._instanceId = import_keys4.PublicKey.random().toHex();
1143
1141
  this.connectionAdded = new import_async3.Event();
1144
1142
  this.disconnected = new import_async3.Event();
@@ -1221,14 +1219,13 @@ var Swarm = class {
1221
1219
  });
1222
1220
  }
1223
1221
  async destroy() {
1224
- var _a2;
1225
1222
  (0, import_log4.log)("destroying...", void 0, {
1226
1223
  F: __dxlog_file4,
1227
1224
  L: 140,
1228
1225
  S: this,
1229
1226
  C: (f, a) => f(...a)
1230
1227
  });
1231
- await ((_a2 = this._listeningHandle) == null ? void 0 : _a2.unsubscribe());
1228
+ await this._listeningHandle?.unsubscribe();
1232
1229
  this._listeningHandle = void 0;
1233
1230
  await this._ctx.dispose();
1234
1231
  await this._topology.destroy();
@@ -1268,7 +1265,6 @@ var Swarm = class {
1268
1265
  this._topology.update();
1269
1266
  }
1270
1267
  onSwarmEvent(swarmEvent) {
1271
- var _a2;
1272
1268
  (0, import_log4.log)("swarm event", {
1273
1269
  swarmEvent
1274
1270
  }, {
@@ -1304,7 +1300,7 @@ var Swarm = class {
1304
1300
  const peer = this._peers.get(import_keys4.PublicKey.from(swarmEvent.peerLeft.peer));
1305
1301
  if (peer) {
1306
1302
  peer.advertizing = false;
1307
- if (((_a2 = peer.connection) == null ? void 0 : _a2.state) !== ConnectionState.CONNECTED) {
1303
+ if (peer.connection?.state !== ConnectionState.CONNECTED) {
1308
1304
  void this._destroyPeer(peer.id).catch((err) => import_log4.log.catch(err, void 0, {
1309
1305
  F: __dxlog_file4,
1310
1306
  L: 188,
@@ -1317,7 +1313,6 @@ var Swarm = class {
1317
1313
  this._topology.update();
1318
1314
  }
1319
1315
  async onOffer(message) {
1320
- var _a2, _b;
1321
1316
  (0, import_log4.log)("offer", {
1322
1317
  message
1323
1318
  }, {
@@ -1346,7 +1341,7 @@ var Swarm = class {
1346
1341
  ""
1347
1342
  ]
1348
1343
  });
1349
- if (!((_a2 = message.recipient) == null ? void 0 : _a2.equals(this._ownPeerId))) {
1344
+ if (!message.recipient?.equals(this._ownPeerId)) {
1350
1345
  (0, import_log4.log)("rejecting offer with incorrect peerId", {
1351
1346
  message
1352
1347
  }, {
@@ -1359,7 +1354,7 @@ var Swarm = class {
1359
1354
  accept: false
1360
1355
  };
1361
1356
  }
1362
- if (!((_b = message.topic) == null ? void 0 : _b.equals(this._topic))) {
1357
+ if (!message.topic?.equals(this._topic)) {
1363
1358
  (0, import_log4.log)("rejecting offer with incorrect topic", {
1364
1359
  message
1365
1360
  }, {
@@ -1378,7 +1373,6 @@ var Swarm = class {
1378
1373
  return answer;
1379
1374
  }
1380
1375
  async onSignal(message) {
1381
- var _a2, _b;
1382
1376
  (0, import_log4.log)("signal", {
1383
1377
  message
1384
1378
  }, {
@@ -1396,7 +1390,7 @@ var Swarm = class {
1396
1390
  });
1397
1391
  return;
1398
1392
  }
1399
- (0, import_invariant4.invariant)((_a2 = message.recipient) == null ? void 0 : _a2.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1393
+ (0, import_invariant4.invariant)(message.recipient?.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1400
1394
  F: __dxlog_file4,
1401
1395
  L: 227,
1402
1396
  S: this,
@@ -1405,7 +1399,7 @@ var Swarm = class {
1405
1399
  "`Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`"
1406
1400
  ]
1407
1401
  });
1408
- (0, import_invariant4.invariant)((_b = message.topic) == null ? void 0 : _b.equals(this._topic), void 0, {
1402
+ (0, import_invariant4.invariant)(message.topic?.equals(this._topic), void 0, {
1409
1403
  F: __dxlog_file4,
1410
1404
  L: 231,
1411
1405
  S: this,
@@ -1617,8 +1611,7 @@ var SwarmMapper = class {
1617
1611
  }));
1618
1612
  }));
1619
1613
  this._subscriptions.add(_swarm.disconnected.on((peerId) => {
1620
- var _a2;
1621
- (_a2 = this._connectionSubscriptions.get(peerId)) == null ? void 0 : _a2();
1614
+ this._connectionSubscriptions.get(peerId)?.();
1622
1615
  this._connectionSubscriptions.delete(peerId);
1623
1616
  this._update();
1624
1617
  }));
@@ -1764,8 +1757,7 @@ var ConnectionLog = class {
1764
1757
  this.update = new import_async6.Event();
1765
1758
  }
1766
1759
  getSwarmInfo(swarmId) {
1767
- var _a2;
1768
- return (_a2 = this._swarms.get(swarmId)) != null ? _a2 : (0, import_debug3.raise)(new Error(`Swarm not found: ${swarmId}`));
1760
+ return this._swarms.get(swarmId) ?? (0, import_debug3.raise)(new Error(`Swarm not found: ${swarmId}`));
1769
1761
  }
1770
1762
  get swarms() {
1771
1763
  return Array.from(this._swarms.values());
@@ -1781,9 +1773,9 @@ var ConnectionLog = class {
1781
1773
  this._swarms.set(import_keys7.PublicKey.from(swarm._instanceId), info);
1782
1774
  this.update.emit();
1783
1775
  swarm.connectionAdded.on((connection) => {
1784
- var _a2, _b;
1785
1776
  const connectionInfo = {
1786
1777
  state: ConnectionState.INITIAL,
1778
+ closeReason: connection.closeReason,
1787
1779
  remotePeerId: connection.remoteId,
1788
1780
  sessionId: connection.sessionId,
1789
1781
  transport: connection.transport && Object.getPrototypeOf(connection.transport).constructor.name,
@@ -1794,13 +1786,14 @@ var ConnectionLog = class {
1794
1786
  this.update.emit();
1795
1787
  connection.stateChanged.on((state) => {
1796
1788
  connectionInfo.state = state;
1789
+ connectionInfo.closeReason = connection.closeReason;
1797
1790
  connectionInfo.events.push({
1798
1791
  type: EventType.CONNECTION_STATE_CHANGED,
1799
1792
  newState: state
1800
1793
  });
1801
1794
  this.update.emit();
1802
1795
  });
1803
- (_b = (_a2 = connection.protocol) == null ? void 0 : _a2.stats) == null ? void 0 : _b.on((stats) => {
1796
+ connection.protocol?.stats?.on((stats) => {
1804
1797
  connectionInfo.streams = stats.channels;
1805
1798
  this.update.emit();
1806
1799
  });
@@ -1827,10 +1820,7 @@ var NetworkManager = class {
1827
1820
  this._instanceId = import_keys8.PublicKey.random().toHex();
1828
1821
  this._transportFactory = transportFactory;
1829
1822
  this._signalManager = signalManager;
1830
- this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => {
1831
- var _a2;
1832
- return (_a2 = this._swarms.get(topic)) == null ? void 0 : _a2.onSwarmEvent(event);
1833
- });
1823
+ this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => this._swarms.get(topic)?.onSwarmEvent(event));
1834
1824
  this._messenger = new import_messaging.Messenger({
1835
1825
  signalManager: this._signalManager
1836
1826
  });
@@ -1898,7 +1888,6 @@ var NetworkManager = class {
1898
1888
  * Join the swarm.
1899
1889
  */
1900
1890
  async joinSwarm({ topic, peerId, topology, protocolProvider: protocol, label }) {
1901
- var _a2;
1902
1891
  (0, import_invariant6.invariant)(import_keys8.PublicKey.isPublicKey(topic), void 0, {
1903
1892
  F: __dxlog_file7,
1904
1893
  L: 157,
@@ -1972,7 +1961,7 @@ var NetworkManager = class {
1972
1961
  C: (f, a) => f(...a)
1973
1962
  }));
1974
1963
  this.topicsUpdated.emit();
1975
- (_a2 = this._connectionLog) == null ? void 0 : _a2.joinedSwarm(swarm);
1964
+ this._connectionLog?.joinedSwarm(swarm);
1976
1965
  (0, import_log7.log)("joined", {
1977
1966
  topic: import_keys8.PublicKey.from(topic),
1978
1967
  count: this._swarms.size
@@ -1990,7 +1979,6 @@ var NetworkManager = class {
1990
1979
  * Close the connection.
1991
1980
  */
1992
1981
  async leaveSwarm(topic) {
1993
- var _a2;
1994
1982
  if (!this._swarms.has(topic)) {
1995
1983
  return;
1996
1984
  }
@@ -2010,7 +1998,7 @@ var NetworkManager = class {
2010
1998
  const map = this._mappers.get(topic);
2011
1999
  map.destroy();
2012
2000
  this._mappers.delete(topic);
2013
- (_a2 = this._connectionLog) == null ? void 0 : _a2.leftSwarm(swarm);
2001
+ this._connectionLog?.leftSwarm(swarm);
2014
2002
  await swarm.destroy();
2015
2003
  this._swarms.delete(topic);
2016
2004
  await this.topicsUpdated.emit();
@@ -2133,9 +2121,12 @@ var createStreamDelay = (delay) => {
2133
2121
  var MemoryTransportFactory = {
2134
2122
  createTransport: (params) => new MemoryTransport(params)
2135
2123
  };
2136
- var _MemoryTransport = class {
2124
+ var MemoryTransport = class _MemoryTransport {
2125
+ static {
2126
+ // TODO(burdon): Remove static properties (inject context into constructor).
2127
+ this._connections = new import_util7.ComplexMap(import_keys9.PublicKey.hash);
2128
+ }
2137
2129
  constructor(options) {
2138
- var _a2;
2139
2130
  this.options = options;
2140
2131
  this.closed = new import_async8.Event();
2141
2132
  this.connected = new import_async8.Event();
@@ -2181,7 +2172,7 @@ var _MemoryTransport = class {
2181
2172
  });
2182
2173
  } else {
2183
2174
  this._remote.wait({
2184
- timeout: (_a2 = this.options.timeout) != null ? _a2 : 1e3
2175
+ timeout: this.options.timeout ?? 1e3
2185
2176
  }).then((remoteId) => {
2186
2177
  if (this._destroyed) {
2187
2178
  return;
@@ -2268,7 +2259,7 @@ var _MemoryTransport = class {
2268
2259
  S: this,
2269
2260
  C: (f, a) => f(...a)
2270
2261
  });
2271
- if (!(payload == null ? void 0 : payload.transportId)) {
2262
+ if (!payload?.transportId) {
2272
2263
  return;
2273
2264
  }
2274
2265
  const transportId = payload.transportId;
@@ -2278,9 +2269,6 @@ var _MemoryTransport = class {
2278
2269
  }
2279
2270
  }
2280
2271
  };
2281
- var MemoryTransport = _MemoryTransport;
2282
- // TODO(burdon): Remove static properties (inject context into constructor).
2283
- MemoryTransport._connections = new import_util7.ComplexMap(import_keys9.PublicKey.hash);
2284
2272
  _ts_decorate4([
2285
2273
  import_log10.logInfo
2286
2274
  ], MemoryTransport.prototype, "_instanceId", void 0);
@@ -2301,43 +2289,47 @@ var import_protocols5 = require("@dxos/protocols");
2301
2289
  var wrtc = null;
2302
2290
  try {
2303
2291
  wrtc = require("@koush/wrtc");
2304
- } catch (e) {
2292
+ } catch {
2305
2293
  }
2306
2294
 
2307
2295
  // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
2308
2296
  var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
2309
2297
  var WebRTCTransport = class {
2310
2298
  constructor(params) {
2311
- var _a2;
2312
2299
  this.params = params;
2300
+ this._instanceId = import_keys10.PublicKey.random().toHex();
2313
2301
  this._closed = false;
2314
2302
  this.closed = new import_async9.Event();
2315
2303
  this.connected = new import_async9.Event();
2316
2304
  this.errors = new import_debug5.ErrorStream();
2317
- this._instanceId = import_keys10.PublicKey.random().toHex();
2318
2305
  import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.begin({
2319
2306
  id: this._instanceId
2320
2307
  }), {
2321
2308
  F: __dxlog_file10,
2322
- L: 39,
2309
+ L: 44,
2323
2310
  S: this,
2324
2311
  C: (f, a) => f(...a)
2325
2312
  });
2326
2313
  (0, import_log11.log)("created connection", params, {
2327
2314
  F: __dxlog_file10,
2328
- L: 40,
2315
+ L: 45,
2329
2316
  S: this,
2330
2317
  C: (f, a) => f(...a)
2331
2318
  });
2332
2319
  this._peer = new import_simple_peer.default({
2333
2320
  initiator: this.params.initiator,
2334
- wrtc: import_simple_peer.default.WEBRTC_SUPPORT ? void 0 : (_a2 = wrtc) != null ? _a2 : (0, import_debug5.raise)(new Error("wrtc not available")),
2335
- config: this.params.webrtcConfig
2321
+ config: this.params.webrtcConfig,
2322
+ // https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/createDataChannel
2323
+ channelConfig: {
2324
+ protocol: "dxos.mesh"
2325
+ },
2326
+ // Custom WebRTC implementation (for Node); otherwise get-browser-rtc.
2327
+ wrtc: import_simple_peer.default.WEBRTC_SUPPORT ? void 0 : wrtc ?? (0, import_debug5.raise)(new Error("wrtc not available"))
2336
2328
  });
2337
2329
  this._peer.on("signal", async (data) => {
2338
2330
  (0, import_log11.log)("signal", data, {
2339
2331
  F: __dxlog_file10,
2340
- L: 48,
2332
+ L: 62,
2341
2333
  S: this,
2342
2334
  C: (f, a) => f(...a)
2343
2335
  });
@@ -2350,7 +2342,7 @@ var WebRTCTransport = class {
2350
2342
  this._peer.on("connect", () => {
2351
2343
  (0, import_log11.log)("connected", void 0, {
2352
2344
  F: __dxlog_file10,
2353
- L: 53,
2345
+ L: 67,
2354
2346
  S: this,
2355
2347
  C: (f, a) => f(...a)
2356
2348
  });
@@ -2360,7 +2352,7 @@ var WebRTCTransport = class {
2360
2352
  this._peer.on("close", async () => {
2361
2353
  (0, import_log11.log)("closed", void 0, {
2362
2354
  F: __dxlog_file10,
2363
- L: 59,
2355
+ L: 73,
2364
2356
  S: this,
2365
2357
  C: (f, a) => f(...a)
2366
2358
  });
@@ -2368,23 +2360,22 @@ var WebRTCTransport = class {
2368
2360
  this.closed.emit();
2369
2361
  });
2370
2362
  this._peer.on("error", async (err) => {
2371
- var _a3;
2372
2363
  this.errors.raise(err);
2373
2364
  try {
2374
- if (typeof ((_a3 = this._peer) == null ? void 0 : _a3._pc.getStats) === "function") {
2365
+ if (typeof this._peer?._pc.getStats === "function") {
2375
2366
  this._peer._pc.getStats().then((stats) => {
2376
2367
  import_log11.log.warn("report after webrtc error", {
2377
2368
  config: this.params.webrtcConfig,
2378
2369
  stats
2379
2370
  }, {
2380
2371
  F: __dxlog_file10,
2381
- L: 71,
2372
+ L: 85,
2382
2373
  S: this,
2383
2374
  C: (f, a) => f(...a)
2384
2375
  });
2385
2376
  });
2386
2377
  }
2387
- } catch (e) {
2378
+ } catch {
2388
2379
  }
2389
2380
  await this.destroy();
2390
2381
  });
@@ -2392,7 +2383,7 @@ var WebRTCTransport = class {
2392
2383
  id: this._instanceId
2393
2384
  }), {
2394
2385
  F: __dxlog_file10,
2395
- L: 81,
2386
+ L: 95,
2396
2387
  S: this,
2397
2388
  C: (f, a) => f(...a)
2398
2389
  });
@@ -2400,7 +2391,7 @@ var WebRTCTransport = class {
2400
2391
  async destroy() {
2401
2392
  (0, import_log11.log)("closing...", void 0, {
2402
2393
  F: __dxlog_file10,
2403
- L: 85,
2394
+ L: 99,
2404
2395
  S: this,
2405
2396
  C: (f, a) => f(...a)
2406
2397
  });
@@ -2410,7 +2401,7 @@ var WebRTCTransport = class {
2410
2401
  this.closed.emit();
2411
2402
  (0, import_log11.log)("closed", void 0, {
2412
2403
  F: __dxlog_file10,
2413
- L: 90,
2404
+ L: 104,
2414
2405
  S: this,
2415
2406
  C: (f, a) => f(...a)
2416
2407
  });
@@ -2421,7 +2412,7 @@ var WebRTCTransport = class {
2421
2412
  }
2422
2413
  (0, import_invariant11.invariant)(signal.payload.data, "Signal message must contain signal data.", {
2423
2414
  F: __dxlog_file10,
2424
- L: 98,
2415
+ L: 112,
2425
2416
  S: this,
2426
2417
  A: [
2427
2418
  "signal.payload.data",
@@ -2431,8 +2422,7 @@ var WebRTCTransport = class {
2431
2422
  this._peer.signal(signal.payload.data);
2432
2423
  }
2433
2424
  async _disconnectStreams() {
2434
- var _a2, _b, _c, _d;
2435
- (_d = (_c = (_b = (_a2 = this.params.stream).unpipe) == null ? void 0 : _b.call(_a2, this._peer)) == null ? void 0 : _c.unpipe) == null ? void 0 : _d.call(_c, this.params.stream);
2425
+ this.params.stream.unpipe?.(this._peer)?.unpipe?.(this.params.stream);
2436
2426
  }
2437
2427
  };
2438
2428
 
@@ -2554,9 +2544,8 @@ var WebRTCTransportService = class {
2554
2544
  }
2555
2545
  }
2556
2546
  async close({ proxyId }) {
2557
- var _a2, _b;
2558
- await ((_a2 = this.transports.get(proxyId)) == null ? void 0 : _a2.transport.destroy());
2559
- await ((_b = this.transports.get(proxyId)) == null ? void 0 : _b.stream.end());
2547
+ await this.transports.get(proxyId)?.transport.destroy();
2548
+ await this.transports.get(proxyId)?.stream.end();
2560
2549
  this.transports.delete(proxyId);
2561
2550
  (0, import_log12.log)("Closed.", void 0, {
2562
2551
  F: __dxlog_file11,
@@ -2585,9 +2574,9 @@ var WebRTCTransportProxy = class {
2585
2574
  this._proxyId = import_keys12.PublicKey.random();
2586
2575
  this._ctx = new import_context6.Context();
2587
2576
  this.closed = new import_async10.Event();
2588
- this._closed = false;
2589
2577
  this.connected = new import_async10.Event();
2590
2578
  this.errors = new import_debug6.ErrorStream();
2579
+ this._closed = false;
2591
2580
  this._serviceStream = this._params.bridgeService.open({
2592
2581
  proxyId: this._proxyId,
2593
2582
  initiator: this._params.initiator
@@ -2663,7 +2652,7 @@ var WebRTCTransportProxy = class {
2663
2652
  if (this._closed) {
2664
2653
  return;
2665
2654
  }
2666
- this._serviceStream.close();
2655
+ await this._serviceStream.close();
2667
2656
  try {
2668
2657
  await this._params.bridgeService.close({
2669
2658
  proxyId: this._proxyId
@@ -2684,7 +2673,7 @@ var WebRTCTransportProxy = class {
2684
2673
  */
2685
2674
  // TODO(burdon): Option on close method.
2686
2675
  forceClose() {
2687
- this._serviceStream.close();
2676
+ void this._serviceStream.close();
2688
2677
  this.closed.emit();
2689
2678
  this._closed = true;
2690
2679
  }
@@ -2752,18 +2741,20 @@ var createTeleportProtocolFactory = (onConnection) => {
2752
2741
  // packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts
2753
2742
  var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts";
2754
2743
  var TestWireProtocol = class {
2755
- constructor(peerId) {
2744
+ constructor(peerId, _extensionFactory = () => []) {
2756
2745
  this.peerId = peerId;
2746
+ this._extensionFactory = _extensionFactory;
2757
2747
  this.connections = new import_util10.ComplexMap(import_keys13.PublicKey.hash);
2758
2748
  this.streamConnections = new import_util10.ComplexMap(import_keys13.PublicKey.hash);
2759
2749
  this.connected = new import_async11.Event();
2760
2750
  this.disconnected = new import_async11.Event();
2751
+ this.otherConnections = new import_util10.ComplexMap(({ remotePeerId, extension }) => remotePeerId.toHex() + extension);
2761
2752
  this.factory = createTeleportProtocolFactory(async (teleport) => {
2762
2753
  (0, import_log14.log)("create", {
2763
2754
  remotePeerId: teleport.remotePeerId
2764
2755
  }, {
2765
2756
  F: __dxlog_file13,
2766
- L: 23,
2757
+ L: 38,
2767
2758
  S: this,
2768
2759
  C: (f, a) => f(...a)
2769
2760
  });
@@ -2783,6 +2774,13 @@ var TestWireProtocol = class {
2783
2774
  });
2784
2775
  this.streamConnections.set(teleport.remotePeerId, streamExtension);
2785
2776
  teleport.addExtension("test-stream", streamExtension);
2777
+ for (const { name, extension: extension2 } of this._extensionFactory()) {
2778
+ this.otherConnections.set({
2779
+ remotePeerId: teleport.remotePeerId,
2780
+ extension: name
2781
+ }, extension2);
2782
+ teleport.addExtension(name, extension2);
2783
+ }
2786
2784
  });
2787
2785
  }
2788
2786
  async waitForConnection(peerId) {
@@ -2793,7 +2791,7 @@ var TestWireProtocol = class {
2793
2791
  peerId
2794
2792
  }, {
2795
2793
  F: __dxlog_file13,
2796
- L: 47,
2794
+ L: 67,
2797
2795
  S: this,
2798
2796
  C: (f, a) => f(...a)
2799
2797
  });
@@ -2825,8 +2823,7 @@ var TestWireProtocol = class {
2825
2823
  };
2826
2824
 
2827
2825
  // packages/core/mesh/network-manager/src/testing/test-builder.ts
2828
- var _a;
2829
- var port = (_a = process.env.SIGNAL_PORT) != null ? _a : 4e3;
2826
+ var port = process.env.SIGNAL_PORT ?? 4e3;
2830
2827
  var TEST_SIGNAL_HOSTS = [
2831
2828
  {
2832
2829
  server: `ws://localhost:${port}/.well-known/dx/signal`
@@ -2897,17 +2894,15 @@ var TestPeer = class {
2897
2894
  });
2898
2895
  }
2899
2896
  async open() {
2900
- var _a2, _b;
2901
2897
  await this._networkManager.open();
2902
- await ((_a2 = this._proxy) == null ? void 0 : _a2.open());
2903
- await ((_b = this._service) == null ? void 0 : _b.open());
2898
+ await this._proxy?.open();
2899
+ await this._service?.open();
2904
2900
  }
2905
2901
  async close() {
2906
- var _a2, _b;
2907
2902
  await Promise.all(Array.from(this._swarms.values()).map((swarm) => swarm.leave()));
2908
2903
  this._swarms.clear();
2909
- await ((_a2 = this._proxy) == null ? void 0 : _a2.close());
2910
- await ((_b = this._service) == null ? void 0 : _b.close());
2904
+ await this._proxy?.close();
2905
+ await this._service?.close();
2911
2906
  await this._networkManager.close();
2912
2907
  }
2913
2908
  getSwarm(topic) {
@@ -2917,8 +2912,8 @@ var TestPeer = class {
2917
2912
  }
2918
2913
  return swarm;
2919
2914
  }
2920
- createSwarm(topic) {
2921
- const swarm = new TestSwarmConnection(this, topic);
2915
+ createSwarm(topic, extensionFactory = () => []) {
2916
+ const swarm = new TestSwarmConnection(this, topic, extensionFactory);
2922
2917
  this._swarms.set(topic, swarm);
2923
2918
  return swarm;
2924
2919
  }
@@ -2930,10 +2925,11 @@ var TestPeer = class {
2930
2925
  }
2931
2926
  };
2932
2927
  var TestSwarmConnection = class {
2933
- constructor(peer, topic) {
2928
+ constructor(peer, topic, extensionFactory) {
2934
2929
  this.peer = peer;
2935
2930
  this.topic = topic;
2936
- this.protocol = new TestWireProtocol(this.peer.peerId);
2931
+ this.extensionFactory = extensionFactory;
2932
+ this.protocol = new TestWireProtocol(this.peer.peerId, this.extensionFactory);
2937
2933
  }
2938
2934
  // TODO(burdon): Need to create new plugin instance per swarm?
2939
2935
  // If so, then perhaps joinSwarm should return swarm object with access to plugins.