@dxos/network-manager 0.1.56-main.c19b7cd → 0.1.56-main.c277f68

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