@dxos/network-manager 0.1.58-main.75cbd70 → 0.1.58-main.7858df5

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 (53) hide show
  1. package/dist/lib/browser/{chunk-VP6DWC3F.mjs → chunk-HKHUCTRB.mjs} +205 -114
  2. package/dist/lib/browser/chunk-HKHUCTRB.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 +146 -13
  6. package/dist/lib/browser/testing/index.mjs.map +4 -4
  7. package/dist/lib/node/index.cjs +493 -402
  8. package/dist/lib/node/index.cjs.map +4 -4
  9. package/dist/lib/node/meta.json +1 -1
  10. package/dist/lib/node/testing/index.cjs +1085 -860
  11. package/dist/lib/node/testing/index.cjs.map +4 -4
  12. package/dist/types/src/swarm/connection-limiter.d.ts +1 -1
  13. package/dist/types/src/swarm/connection-limiter.d.ts.map +1 -1
  14. package/dist/types/src/swarm/connection.d.ts +3 -1
  15. package/dist/types/src/swarm/connection.d.ts.map +1 -1
  16. package/dist/types/src/swarm/peer.d.ts +3 -3
  17. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  18. package/dist/types/src/swarm/swarm-mapper.d.ts.map +1 -1
  19. package/dist/types/src/swarm/swarm.d.ts +2 -2
  20. package/dist/types/src/swarm/swarm.d.ts.map +1 -1
  21. package/dist/types/src/testing/test-builder.d.ts +1 -1
  22. package/dist/types/src/testing/test-builder.d.ts.map +1 -1
  23. package/dist/types/src/tests/basic-test-suite.d.ts.map +1 -1
  24. package/dist/types/src/tests/tcp-transport.test.d.ts +2 -0
  25. package/dist/types/src/tests/tcp-transport.test.d.ts.map +1 -0
  26. package/dist/types/src/topology/star-topology.d.ts.map +1 -1
  27. package/dist/types/src/transport/simplepeer-transport-proxy.d.ts.map +1 -1
  28. package/dist/types/src/transport/simplepeer-transport-service.d.ts.map +1 -1
  29. package/dist/types/src/transport/simplepeer-transport.d.ts.map +1 -1
  30. package/dist/types/src/transport/tcp-transport.d.ts +23 -0
  31. package/dist/types/src/transport/tcp-transport.d.ts.map +1 -0
  32. package/dist/types/src/transport/transport.d.ts +2 -1
  33. package/dist/types/src/transport/transport.d.ts.map +1 -1
  34. package/package.json +17 -18
  35. package/src/swarm/connection-limiter.ts +2 -2
  36. package/src/swarm/connection.test.ts +1 -1
  37. package/src/swarm/connection.ts +53 -27
  38. package/src/swarm/peer.ts +6 -3
  39. package/src/swarm/swarm-mapper.ts +4 -7
  40. package/src/swarm/swarm.test.ts +3 -3
  41. package/src/swarm/swarm.ts +3 -3
  42. package/src/testing/test-builder.ts +6 -2
  43. package/src/tests/basic-test-suite.ts +1 -1
  44. package/src/tests/memory-transport.test.ts +2 -2
  45. package/src/tests/tcp-transport.test.ts +67 -0
  46. package/src/tests/webrtc-transport.test.ts +1 -1
  47. package/src/topology/star-topology.ts +1 -4
  48. package/src/transport/simplepeer-transport-proxy.ts +48 -21
  49. package/src/transport/simplepeer-transport-service.ts +1 -4
  50. package/src/transport/simplepeer-transport.ts +38 -4
  51. package/src/transport/tcp-transport.ts +107 -0
  52. package/src/transport/transport.ts +1 -0
  53. package/dist/lib/browser/chunk-VP6DWC3F.mjs.map +0 -7
@@ -513,37 +513,148 @@ module.exports = __toCommonJS(testing_exports);
513
513
 
514
514
  // packages/core/mesh/network-manager/src/testing/test-builder.ts
515
515
  var import_keys14 = require("@dxos/keys");
516
- var import_log16 = require("@dxos/log");
516
+ var import_log17 = require("@dxos/log");
517
517
  var import_messaging2 = require("@dxos/messaging");
518
- var import_protocols6 = require("@dxos/protocols");
518
+ var import_protocols9 = require("@dxos/protocols");
519
519
  var import_services2 = require("@dxos/protocols/proto/dxos/client/services");
520
520
  var import_rpc = require("@dxos/rpc");
521
521
  var import_util11 = require("@dxos/util");
522
522
 
523
+ // packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts
524
+ var import_async = require("@dxos/async");
525
+ var import_keys = require("@dxos/keys");
526
+ var import_log = require("@dxos/log");
527
+ var import_teleport2 = require("@dxos/teleport");
528
+ var import_util = require("@dxos/util");
529
+
530
+ // packages/core/mesh/network-manager/src/wire-protocol.ts
531
+ var import_teleport = require("@dxos/teleport");
532
+ var createTeleportProtocolFactory = (onConnection) => {
533
+ return (params) => {
534
+ const teleport = new import_teleport.Teleport(params);
535
+ return {
536
+ stream: teleport.stream,
537
+ open: async () => {
538
+ await teleport.open();
539
+ await onConnection(teleport);
540
+ },
541
+ close: async () => {
542
+ await teleport.close();
543
+ },
544
+ abort: async () => {
545
+ await teleport.abort();
546
+ }
547
+ };
548
+ };
549
+ };
550
+
551
+ // packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts
552
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts";
553
+ var TestWireProtocol = class {
554
+ constructor(peerId, _extensionFactory = () => []) {
555
+ this.peerId = peerId;
556
+ this._extensionFactory = _extensionFactory;
557
+ this.connections = new import_util.ComplexMap(import_keys.PublicKey.hash);
558
+ this.streamConnections = new import_util.ComplexMap(import_keys.PublicKey.hash);
559
+ this.connected = new import_async.Event();
560
+ this.disconnected = new import_async.Event();
561
+ this.otherConnections = new import_util.ComplexMap(({ remotePeerId, extension }) => remotePeerId.toHex() + extension);
562
+ this.factory = createTeleportProtocolFactory(async (teleport) => {
563
+ (0, import_log.log)("create", {
564
+ remotePeerId: teleport.remotePeerId
565
+ }, {
566
+ F: __dxlog_file,
567
+ L: 38,
568
+ S: this,
569
+ C: (f, a) => f(...a)
570
+ });
571
+ const extension = new import_teleport2.TestExtension({
572
+ onClose: async () => {
573
+ this.connections.delete(teleport.remotePeerId);
574
+ this.disconnected.emit(teleport.remotePeerId);
575
+ }
576
+ });
577
+ this.connections.set(teleport.remotePeerId, extension);
578
+ teleport.addExtension("test", extension);
579
+ this.connected.emit(teleport.remotePeerId);
580
+ const streamExtension = new import_teleport2.TestExtensionWithStreams({
581
+ onClose: async () => {
582
+ this.streamConnections.delete(teleport.remotePeerId);
583
+ }
584
+ });
585
+ this.streamConnections.set(teleport.remotePeerId, streamExtension);
586
+ teleport.addExtension("test-stream", streamExtension);
587
+ for (const { name, extension: extension2 } of this._extensionFactory()) {
588
+ this.otherConnections.set({
589
+ remotePeerId: teleport.remotePeerId,
590
+ extension: name
591
+ }, extension2);
592
+ teleport.addExtension(name, extension2);
593
+ }
594
+ });
595
+ }
596
+ async waitForConnection(peerId) {
597
+ if (this.connections.has(peerId)) {
598
+ return this.connections.get(peerId);
599
+ }
600
+ (0, import_log.log)("waitForConnection", {
601
+ peerId
602
+ }, {
603
+ F: __dxlog_file,
604
+ L: 67,
605
+ S: this,
606
+ C: (f, a) => f(...a)
607
+ });
608
+ await (0, import_async.asyncTimeout)(
609
+ this.connected.waitFor((connectedId) => connectedId.equals(peerId)),
610
+ // TODO(nf): Make this configurable.
611
+ 1e4
612
+ );
613
+ return this.connections.get(peerId);
614
+ }
615
+ async testConnection(peerId, message) {
616
+ const connection = await this.waitForConnection(peerId);
617
+ await connection.test(message);
618
+ }
619
+ async openStream(peerId, streamTag, streamLoadInterval, streamLoadChunkSize) {
620
+ if (!this.streamConnections.has(peerId)) {
621
+ throw new Error("Connection does not exist.");
622
+ }
623
+ const connection = this.streamConnections.get(peerId);
624
+ return connection.addNewStream(streamLoadInterval, streamLoadChunkSize, streamTag);
625
+ }
626
+ async closeStream(peerId, streamTag) {
627
+ if (!this.streamConnections.has(peerId)) {
628
+ throw new Error("Connection does not exist.");
629
+ }
630
+ const connection = this.streamConnections.get(peerId);
631
+ return connection.closeStream(streamTag);
632
+ }
633
+ };
634
+
523
635
  // packages/core/mesh/network-manager/src/network-manager.ts
524
- var import_async7 = require("@dxos/async");
636
+ var import_async8 = require("@dxos/async");
525
637
  var import_invariant6 = require("@dxos/invariant");
526
- var import_keys8 = require("@dxos/keys");
527
- var import_log7 = require("@dxos/log");
638
+ var import_keys9 = require("@dxos/keys");
639
+ var import_log8 = require("@dxos/log");
528
640
  var import_messaging = require("@dxos/messaging");
529
- var import_protocols4 = require("@dxos/protocols");
641
+ var import_protocols6 = require("@dxos/protocols");
530
642
  var import_services = require("@dxos/protocols/proto/dxos/client/services");
531
- var import_util6 = require("@dxos/util");
643
+ var import_util7 = require("@dxos/util");
532
644
 
533
645
  // packages/core/mesh/network-manager/src/connection-log.ts
534
- var import_async6 = require("@dxos/async");
646
+ var import_async7 = require("@dxos/async");
535
647
  var import_debug3 = require("@dxos/debug");
536
- var import_keys7 = require("@dxos/keys");
537
- var import_util5 = require("@dxos/util");
648
+ var import_keys8 = require("@dxos/keys");
649
+ var import_util6 = require("@dxos/util");
538
650
 
539
651
  // packages/core/mesh/network-manager/src/swarm/connection.ts
540
- var import_async = require("@dxos/async");
652
+ var import_async2 = require("@dxos/async");
541
653
  var import_context = require("@dxos/context");
542
654
  var import_debug = require("@dxos/debug");
543
- var import_errors = require("@dxos/errors");
544
655
  var import_invariant = require("@dxos/invariant");
545
- var import_keys = require("@dxos/keys");
546
- var import_log = require("@dxos/log");
656
+ var import_keys2 = require("@dxos/keys");
657
+ var import_log2 = require("@dxos/log");
547
658
  var import_protocols = require("@dxos/protocols");
548
659
  function _ts_decorate(decorators, target, key, desc) {
549
660
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -555,7 +666,7 @@ function _ts_decorate(decorators, target, key, desc) {
555
666
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
556
667
  return c > 3 && r && Object.defineProperty(target, key, r), r;
557
668
  }
558
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection.ts";
669
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection.ts";
559
670
  var STARTING_SIGNALLING_DELAY = 10;
560
671
  var MAX_SIGNALLING_DELAY = 300;
561
672
  var ConnectionState;
@@ -597,6 +708,8 @@ var ConnectionState;
597
708
  */
598
709
  "CLOSED"
599
710
  ] = "CLOSED";
711
+ ConnectionState6["ABORTING"] = "ABORTING";
712
+ ConnectionState6["ABORTED"] = "ABORTED";
600
713
  })(ConnectionState || (ConnectionState = {}));
601
714
  var Connection = class {
602
715
  constructor(topic, ownId, remoteId, sessionId, initiator, _signalMessaging, _protocol, _transportFactory, _callbacks) {
@@ -613,22 +726,22 @@ var Connection = class {
613
726
  this._state = ConnectionState.CREATED;
614
727
  this._incomingSignalBuffer = [];
615
728
  this._outgoingSignalBuffer = [];
616
- this.stateChanged = new import_async.Event();
729
+ this.stateChanged = new import_async2.Event();
617
730
  this.errors = new import_debug.ErrorStream();
618
- this._instanceId = import_keys.PublicKey.random().toHex();
619
- this._signalSendTask = new import_async.DeferredTask(this._ctx, async () => {
731
+ this._instanceId = import_keys2.PublicKey.random().toHex();
732
+ this._signalSendTask = new import_async2.DeferredTask(this._ctx, async () => {
620
733
  await this._flushSignalBuffer();
621
734
  });
622
735
  this._signallingDelay = STARTING_SIGNALLING_DELAY;
623
- import_log.log.trace("dxos.mesh.connection.construct", {
736
+ import_log2.log.trace("dxos.mesh.connection.construct", {
624
737
  sessionId: this.sessionId,
625
738
  topic: this.topic,
626
739
  localPeerId: this.ownId,
627
740
  remotePeerId: this.remoteId,
628
741
  initiator: this.initiator
629
742
  }, {
630
- F: __dxlog_file,
631
- L: 114,
743
+ F: __dxlog_file2,
744
+ L: 123,
632
745
  S: this,
633
746
  C: (f, a) => f(...a)
634
747
  });
@@ -647,31 +760,31 @@ var Connection = class {
647
760
  */
648
761
  async openConnection() {
649
762
  (0, import_invariant.invariant)(this._state === ConnectionState.INITIAL, "Invalid state.", {
650
- F: __dxlog_file,
651
- L: 139,
763
+ F: __dxlog_file2,
764
+ L: 148,
652
765
  S: this,
653
766
  A: [
654
767
  "this._state === ConnectionState.INITIAL",
655
768
  "'Invalid state.'"
656
769
  ]
657
770
  });
658
- import_log.log.trace("dxos.mesh.connection.open-connection", import_protocols.trace.begin({
771
+ import_log2.log.trace("dxos.mesh.connection.open-connection", import_protocols.trace.begin({
659
772
  id: this._instanceId
660
773
  }), {
661
- F: __dxlog_file,
662
- L: 140,
774
+ F: __dxlog_file2,
775
+ L: 149,
663
776
  S: this,
664
777
  C: (f, a) => f(...a)
665
778
  });
666
- import_log.log.trace("dxos.mesh.connection.open", {
779
+ import_log2.log.trace("dxos.mesh.connection.open", {
667
780
  sessionId: this.sessionId,
668
781
  topic: this.topic,
669
782
  localPeerId: this.ownId,
670
783
  remotePeerId: this.remoteId,
671
784
  initiator: this.initiator
672
785
  }, {
673
- F: __dxlog_file,
674
- L: 141,
786
+ F: __dxlog_file2,
787
+ L: 150,
675
788
  S: this,
676
789
  C: (f, a) => f(...a)
677
790
  });
@@ -680,17 +793,17 @@ var Connection = class {
680
793
  this.errors.raise(err);
681
794
  });
682
795
  this._protocol.stream.on("close", () => {
683
- (0, import_log.log)("protocol stream closed", void 0, {
684
- F: __dxlog_file,
685
- L: 158,
796
+ (0, import_log2.log)("protocol stream closed", void 0, {
797
+ F: __dxlog_file2,
798
+ L: 167,
686
799
  S: this,
687
800
  C: (f, a) => f(...a)
688
801
  });
689
- this.close().catch((err) => this.errors.raise(err));
802
+ this.close(new import_protocols.ProtocolError("protocol stream closed")).catch((err) => this.errors.raise(err));
690
803
  });
691
804
  (0, import_invariant.invariant)(!this._transport, void 0, {
692
- F: __dxlog_file,
693
- L: 162,
805
+ F: __dxlog_file2,
806
+ L: 171,
694
807
  S: this,
695
808
  A: [
696
809
  "!this._transport",
@@ -708,34 +821,51 @@ var Connection = class {
708
821
  });
709
822
  this._transport.closed.once(() => {
710
823
  this._transport = void 0;
711
- (0, import_log.log)("abort triggered by transport close", void 0, {
712
- F: __dxlog_file,
713
- L: 176,
824
+ (0, import_log2.log)("abort triggered by transport close", void 0, {
825
+ F: __dxlog_file2,
826
+ L: 185,
714
827
  S: this,
715
828
  C: (f, a) => f(...a)
716
829
  });
717
830
  this.abort().catch((err) => this.errors.raise(err));
718
831
  });
719
832
  this._transport.errors.handle((err) => {
720
- (0, import_log.log)("transport error:", {
833
+ (0, import_log2.log)("transport error:", {
721
834
  err
722
835
  }, {
723
- F: __dxlog_file,
724
- L: 181,
836
+ F: __dxlog_file2,
837
+ L: 190,
725
838
  S: this,
726
839
  C: (f, a) => f(...a)
727
840
  });
728
841
  if (!this.closeReason) {
729
842
  this.closeReason = err?.message;
730
843
  }
731
- if (err?.message.includes("OperationError")) {
732
- (0, import_log.log)("aborting due to transport OperationError", void 0, {
733
- F: __dxlog_file,
734
- L: 187,
844
+ if (err instanceof import_protocols.ConnectionResetError) {
845
+ (0, import_log2.log)("aborting due to transport ConnectionResetError", void 0, {
846
+ F: __dxlog_file2,
847
+ L: 197,
848
+ S: this,
849
+ C: (f, a) => f(...a)
850
+ });
851
+ this.abort().catch((err2) => this.errors.raise(err2));
852
+ } else if (err instanceof import_protocols.ConnectivityError) {
853
+ (0, import_log2.log)("aborting due to transport ConnectivityError", void 0, {
854
+ F: __dxlog_file2,
855
+ L: 200,
735
856
  S: this,
736
857
  C: (f, a) => f(...a)
737
858
  });
738
859
  this.abort().catch((err2) => this.errors.raise(err2));
860
+ } else if (err instanceof import_protocols.UnknownProtocolError) {
861
+ (0, import_log2.log)("unsure what to do for UnknownProtocolError", {
862
+ err
863
+ }, {
864
+ F: __dxlog_file2,
865
+ L: 203,
866
+ S: this,
867
+ C: (f, a) => f(...a)
868
+ });
739
869
  }
740
870
  if (this._state !== ConnectionState.CLOSED && this._state !== ConnectionState.CLOSING) {
741
871
  this.errors.raise(err);
@@ -745,52 +875,52 @@ var Connection = class {
745
875
  void this._transport.signal(signal);
746
876
  }
747
877
  this._incomingSignalBuffer = [];
748
- import_log.log.trace("dxos.mesh.connection.open-connection", import_protocols.trace.end({
878
+ import_log2.log.trace("dxos.mesh.connection.open-connection", import_protocols.trace.end({
749
879
  id: this._instanceId
750
880
  }), {
751
- F: __dxlog_file,
752
- L: 203,
881
+ F: __dxlog_file2,
882
+ L: 218,
753
883
  S: this,
754
884
  C: (f, a) => f(...a)
755
885
  });
756
886
  }
757
887
  async abort(err) {
758
- (0, import_log.log)("aborting...", {
888
+ (0, import_log2.log)("aborting...", {
759
889
  err
760
890
  }, {
761
- F: __dxlog_file,
762
- L: 209,
891
+ F: __dxlog_file2,
892
+ L: 224,
763
893
  S: this,
764
894
  C: (f, a) => f(...a)
765
895
  });
766
- if (!this.closeReason) {
767
- this.closeReason = err?.message;
768
- }
769
- if (this._state === ConnectionState.CLOSED) {
770
- (0, import_log.log)("abort ignored: already closed", this.closeReason, {
771
- F: __dxlog_file,
772
- L: 214,
896
+ if (this._state === ConnectionState.CLOSED || this._state === ConnectionState.ABORTED) {
897
+ (0, import_log2.log)(`abort ignored: already ${this._state}`, this.closeReason, {
898
+ F: __dxlog_file2,
899
+ L: 226,
773
900
  S: this,
774
901
  C: (f, a) => f(...a)
775
902
  });
776
903
  return;
777
904
  }
778
- this._changeState(ConnectionState.CLOSING);
905
+ this._changeState(ConnectionState.ABORTING);
906
+ if (!this.closeReason) {
907
+ this.closeReason = err?.message;
908
+ }
779
909
  await this._ctx.dispose();
780
910
  try {
781
- (0, import_log.log)("aborting protocol... ", {
911
+ (0, import_log2.log)("aborting protocol... ", {
782
912
  proto: this._protocol
783
913
  }, {
784
- F: __dxlog_file,
785
- L: 224,
914
+ F: __dxlog_file2,
915
+ L: 239,
786
916
  S: this,
787
917
  C: (f, a) => f(...a)
788
918
  });
789
919
  await this._protocol.abort();
790
920
  } catch (err2) {
791
- import_log.log.catch(err2, void 0, {
792
- F: __dxlog_file,
793
- L: 227,
921
+ import_log2.log.catch(err2, void 0, {
922
+ F: __dxlog_file2,
923
+ L: 242,
794
924
  S: this,
795
925
  C: (f, a) => f(...a)
796
926
  });
@@ -798,58 +928,70 @@ var Connection = class {
798
928
  try {
799
929
  await this._transport?.destroy();
800
930
  } catch (err2) {
801
- import_log.log.catch(err2, void 0, {
802
- F: __dxlog_file,
803
- L: 234,
931
+ import_log2.log.catch(err2, void 0, {
932
+ F: __dxlog_file2,
933
+ L: 249,
804
934
  S: this,
805
935
  C: (f, a) => f(...a)
806
936
  });
807
937
  }
808
- this._changeState(ConnectionState.CLOSED);
809
- this._callbacks?.onClosed?.(err);
938
+ try {
939
+ this._callbacks?.onClosed?.(err);
940
+ } catch (err2) {
941
+ import_log2.log.catch(err2, void 0, {
942
+ F: __dxlog_file2,
943
+ L: 255,
944
+ S: this,
945
+ C: (f, a) => f(...a)
946
+ });
947
+ }
948
+ this._changeState(ConnectionState.ABORTED);
810
949
  }
811
950
  async close(err) {
812
951
  if (!this.closeReason) {
813
952
  this.closeReason = err?.message;
814
953
  }
815
- if (this._state === ConnectionState.CLOSED) {
954
+ if (this._state === ConnectionState.CLOSED || this._state === ConnectionState.ABORTING || this._state === ConnectionState.ABORTED) {
816
955
  return;
817
956
  }
957
+ const lastState = this._state;
818
958
  this._changeState(ConnectionState.CLOSING);
819
959
  await this._ctx.dispose();
820
- (0, import_log.log)("closing...", {
960
+ (0, import_log2.log)("closing...", {
821
961
  peerId: this.ownId
822
962
  }, {
823
- F: __dxlog_file,
824
- L: 253,
963
+ F: __dxlog_file2,
964
+ L: 277,
825
965
  S: this,
826
966
  C: (f, a) => f(...a)
827
967
  });
828
- try {
829
- await this._protocol.close();
830
- } catch (err2) {
831
- import_log.log.catch(err2, void 0, {
832
- F: __dxlog_file,
833
- L: 259,
834
- S: this,
835
- C: (f, a) => f(...a)
836
- });
837
- }
838
- try {
839
- await this._transport?.destroy();
840
- } catch (err2) {
841
- import_log.log.catch(err2, void 0, {
842
- F: __dxlog_file,
843
- L: 266,
844
- S: this,
845
- C: (f, a) => f(...a)
846
- });
968
+ if (lastState === ConnectionState.CONNECTED) {
969
+ try {
970
+ await this._protocol.close();
971
+ } catch (err2) {
972
+ import_log2.log.catch(err2, void 0, {
973
+ F: __dxlog_file2,
974
+ L: 284,
975
+ S: this,
976
+ C: (f, a) => f(...a)
977
+ });
978
+ }
979
+ try {
980
+ await this._transport?.destroy();
981
+ } catch (err2) {
982
+ import_log2.log.catch(err2, void 0, {
983
+ F: __dxlog_file2,
984
+ L: 291,
985
+ S: this,
986
+ C: (f, a) => f(...a)
987
+ });
988
+ }
847
989
  }
848
- (0, import_log.log)("closed", {
990
+ (0, import_log2.log)("closed", {
849
991
  peerId: this.ownId
850
992
  }, {
851
- F: __dxlog_file,
852
- L: 269,
993
+ F: __dxlog_file2,
994
+ L: 295,
853
995
  S: this,
854
996
  C: (f, a) => f(...a)
855
997
  });
@@ -866,7 +1008,7 @@ var Connection = class {
866
1008
  }
867
1009
  try {
868
1010
  if (process.env.NODE_ENV !== "test") {
869
- await (0, import_context.cancelWithContext)(this._ctx, (0, import_async.sleep)(this._signallingDelay));
1011
+ await (0, import_context.cancelWithContext)(this._ctx, (0, import_async2.sleep)(this._signallingDelay));
870
1012
  this._signallingDelay = Math.min(this._signallingDelay * 2, MAX_SIGNALLING_DELAY);
871
1013
  }
872
1014
  const signals = [
@@ -885,14 +1027,14 @@ var Connection = class {
885
1027
  }
886
1028
  });
887
1029
  } catch (err) {
888
- if (err instanceof import_errors.CancelledError) {
1030
+ if (err instanceof import_protocols.CancelledError) {
889
1031
  return;
890
1032
  }
891
- import_log.log.warn("signal failed", {
1033
+ import_log2.log.warn("signal failed", {
892
1034
  err
893
1035
  }, {
894
- F: __dxlog_file,
895
- L: 306,
1036
+ F: __dxlog_file2,
1037
+ L: 332,
896
1038
  S: this,
897
1039
  C: (f, a) => f(...a)
898
1040
  });
@@ -904,8 +1046,8 @@ var Connection = class {
904
1046
  */
905
1047
  async signal(msg) {
906
1048
  (0, import_invariant.invariant)(msg.sessionId, void 0, {
907
- F: __dxlog_file,
908
- L: 315,
1049
+ F: __dxlog_file2,
1050
+ L: 341,
909
1051
  S: this,
910
1052
  A: [
911
1053
  "msg.sessionId",
@@ -913,17 +1055,17 @@ var Connection = class {
913
1055
  ]
914
1056
  });
915
1057
  if (!msg.sessionId.equals(this.sessionId)) {
916
- (0, import_log.log)("dropping signal for incorrect session id", void 0, {
917
- F: __dxlog_file,
918
- L: 317,
1058
+ (0, import_log2.log)("dropping signal for incorrect session id", void 0, {
1059
+ F: __dxlog_file2,
1060
+ L: 343,
919
1061
  S: this,
920
1062
  C: (f, a) => f(...a)
921
1063
  });
922
1064
  return;
923
1065
  }
924
1066
  (0, import_invariant.invariant)(msg.data.signal || msg.data.signalBatch, void 0, {
925
- F: __dxlog_file,
926
- L: 320,
1067
+ F: __dxlog_file2,
1068
+ L: 346,
927
1069
  S: this,
928
1070
  A: [
929
1071
  "msg.data.signal || msg.data.signalBatch",
@@ -931,8 +1073,8 @@ var Connection = class {
931
1073
  ]
932
1074
  });
933
1075
  (0, import_invariant.invariant)(msg.author?.equals(this.remoteId), void 0, {
934
- F: __dxlog_file,
935
- L: 321,
1076
+ F: __dxlog_file2,
1077
+ L: 347,
936
1078
  S: this,
937
1079
  A: [
938
1080
  "msg.author?.equals(this.remoteId)",
@@ -940,8 +1082,8 @@ var Connection = class {
940
1082
  ]
941
1083
  });
942
1084
  (0, import_invariant.invariant)(msg.recipient?.equals(this.ownId), void 0, {
943
- F: __dxlog_file,
944
- L: 322,
1085
+ F: __dxlog_file2,
1086
+ L: 348,
945
1087
  S: this,
946
1088
  A: [
947
1089
  "msg.recipient?.equals(this.ownId)",
@@ -959,34 +1101,34 @@ var Connection = class {
959
1101
  ConnectionState.CREATED,
960
1102
  ConnectionState.INITIAL
961
1103
  ].includes(this.state)) {
962
- (0, import_log.log)("buffered signal", {
1104
+ (0, import_log2.log)("buffered signal", {
963
1105
  peerId: this.ownId,
964
1106
  remoteId: this.remoteId,
965
1107
  msg: msg.data
966
1108
  }, {
967
- F: __dxlog_file,
968
- L: 331,
1109
+ F: __dxlog_file2,
1110
+ L: 357,
969
1111
  S: this,
970
1112
  C: (f, a) => f(...a)
971
1113
  });
972
1114
  this._incomingSignalBuffer.push(signal);
973
1115
  } else {
974
1116
  (0, import_invariant.invariant)(this._transport, "Connection not ready to accept signals.", {
975
- F: __dxlog_file,
976
- L: 334,
1117
+ F: __dxlog_file2,
1118
+ L: 360,
977
1119
  S: this,
978
1120
  A: [
979
1121
  "this._transport",
980
1122
  "'Connection not ready to accept signals.'"
981
1123
  ]
982
1124
  });
983
- (0, import_log.log)("received signal", {
1125
+ (0, import_log2.log)("received signal", {
984
1126
  peerId: this.ownId,
985
1127
  remoteId: this.remoteId,
986
1128
  msg: msg.data
987
1129
  }, {
988
- F: __dxlog_file,
989
- L: 335,
1130
+ F: __dxlog_file2,
1131
+ L: 361,
990
1132
  S: this,
991
1133
  C: (f, a) => f(...a)
992
1134
  });
@@ -998,19 +1140,19 @@ var Connection = class {
998
1140
  this._changeState(ConnectionState.INITIAL);
999
1141
  }
1000
1142
  _changeState(state) {
1001
- (0, import_log.log)("stateChanged", {
1143
+ (0, import_log2.log)("stateChanged", {
1002
1144
  from: this._state,
1003
- too: state,
1145
+ to: state,
1004
1146
  peerId: this.ownId
1005
1147
  }, {
1006
- F: __dxlog_file,
1007
- L: 346,
1148
+ F: __dxlog_file2,
1149
+ L: 372,
1008
1150
  S: this,
1009
1151
  C: (f, a) => f(...a)
1010
1152
  });
1011
1153
  (0, import_invariant.invariant)(state !== this._state, "Already in this state.", {
1012
- F: __dxlog_file,
1013
- L: 347,
1154
+ F: __dxlog_file2,
1155
+ L: 373,
1014
1156
  S: this,
1015
1157
  A: [
1016
1158
  "state !== this._state",
@@ -1022,261 +1164,29 @@ var Connection = class {
1022
1164
  }
1023
1165
  };
1024
1166
  _ts_decorate([
1025
- import_async.synchronized
1167
+ import_async2.synchronized
1026
1168
  ], Connection.prototype, "abort", null);
1027
1169
  _ts_decorate([
1028
- import_async.synchronized
1170
+ import_async2.synchronized
1029
1171
  ], Connection.prototype, "close", null);
1030
1172
 
1031
1173
  // packages/core/mesh/network-manager/src/swarm/swarm.ts
1032
- var import_async3 = require("@dxos/async");
1174
+ var import_async4 = require("@dxos/async");
1033
1175
  var import_context4 = require("@dxos/context");
1034
1176
  var import_debug2 = require("@dxos/debug");
1035
1177
  var import_invariant4 = require("@dxos/invariant");
1036
- var import_keys4 = require("@dxos/keys");
1037
- var import_log4 = require("@dxos/log");
1038
- var import_protocols3 = require("@dxos/protocols");
1039
- var import_util2 = require("@dxos/util");
1178
+ var import_keys5 = require("@dxos/keys");
1179
+ var import_log5 = require("@dxos/log");
1180
+ var import_protocols4 = require("@dxos/protocols");
1181
+ var import_util3 = require("@dxos/util");
1040
1182
 
1041
- // packages/core/mesh/network-manager/src/signal/swarm-messenger.ts
1183
+ // packages/core/mesh/network-manager/src/swarm/peer.ts
1184
+ var import_async3 = require("@dxos/async");
1042
1185
  var import_context2 = require("@dxos/context");
1043
1186
  var import_invariant2 = require("@dxos/invariant");
1044
- var import_keys2 = require("@dxos/keys");
1045
- var import_log2 = require("@dxos/log");
1187
+ var import_keys3 = require("@dxos/keys");
1188
+ var import_log3 = require("@dxos/log");
1046
1189
  var import_protocols2 = require("@dxos/protocols");
1047
- var import_util = require("@dxos/util");
1048
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/signal/swarm-messenger.ts";
1049
- var SwarmMessage = import_protocols2.schema.getCodecForType("dxos.mesh.swarm.SwarmMessage");
1050
- var SwarmMessenger = class {
1051
- constructor({ sendMessage, onSignal, onOffer, topic }) {
1052
- this._ctx = new import_context2.Context();
1053
- this._offerRecords = new import_util.ComplexMap((key) => key.toHex());
1054
- this._sendMessage = sendMessage;
1055
- this._onSignal = onSignal;
1056
- this._onOffer = onOffer;
1057
- this._topic = topic;
1058
- }
1059
- async receiveMessage({ author, recipient, payload }) {
1060
- if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
1061
- return;
1062
- }
1063
- const message = SwarmMessage.decode(payload.value);
1064
- if (!this._topic.equals(message.topic)) {
1065
- return;
1066
- }
1067
- (0, import_log2.log)("received", {
1068
- from: author,
1069
- to: recipient,
1070
- msg: message
1071
- }, {
1072
- F: __dxlog_file2,
1073
- L: 69,
1074
- S: this,
1075
- C: (f, a) => f(...a)
1076
- });
1077
- if (message.data?.offer) {
1078
- await this._handleOffer({
1079
- author,
1080
- recipient,
1081
- message
1082
- });
1083
- } else if (message.data?.answer) {
1084
- await this._resolveAnswers(message);
1085
- } else if (message.data?.signal) {
1086
- await this._handleSignal({
1087
- author,
1088
- recipient,
1089
- message
1090
- });
1091
- } else if (message.data?.signalBatch) {
1092
- await this._handleSignal({
1093
- author,
1094
- recipient,
1095
- message
1096
- });
1097
- } else {
1098
- import_log2.log.warn("unknown message", {
1099
- message
1100
- }, {
1101
- F: __dxlog_file2,
1102
- L: 80,
1103
- S: this,
1104
- C: (f, a) => f(...a)
1105
- });
1106
- }
1107
- }
1108
- async signal(message) {
1109
- (0, import_invariant2.invariant)(message.data?.signal || message.data?.signalBatch, "Invalid message", {
1110
- F: __dxlog_file2,
1111
- L: 85,
1112
- S: this,
1113
- A: [
1114
- "message.data?.signal || message.data?.signalBatch",
1115
- "'Invalid message'"
1116
- ]
1117
- });
1118
- await this._sendReliableMessage({
1119
- author: message.author,
1120
- recipient: message.recipient,
1121
- message
1122
- });
1123
- }
1124
- async offer(message) {
1125
- const networkMessage = {
1126
- ...message,
1127
- messageId: import_keys2.PublicKey.random()
1128
- };
1129
- return new Promise((resolve, reject) => {
1130
- this._offerRecords.set(networkMessage.messageId, {
1131
- resolve
1132
- });
1133
- this._sendReliableMessage({
1134
- author: message.author,
1135
- recipient: message.recipient,
1136
- message: networkMessage
1137
- }).catch((err) => reject(err));
1138
- });
1139
- }
1140
- async _sendReliableMessage({ author, recipient, message }) {
1141
- const networkMessage = {
1142
- ...message,
1143
- // Setting unique message_id if it not specified yet.
1144
- messageId: message.messageId ?? import_keys2.PublicKey.random()
1145
- };
1146
- (0, import_log2.log)("sending", {
1147
- from: author,
1148
- to: recipient,
1149
- msg: networkMessage
1150
- }, {
1151
- F: __dxlog_file2,
1152
- L: 123,
1153
- S: this,
1154
- C: (f, a) => f(...a)
1155
- });
1156
- await this._sendMessage({
1157
- author,
1158
- recipient,
1159
- payload: {
1160
- type_url: "dxos.mesh.swarm.SwarmMessage",
1161
- value: SwarmMessage.encode(networkMessage)
1162
- }
1163
- });
1164
- }
1165
- async _resolveAnswers(message) {
1166
- (0, import_invariant2.invariant)(message.data?.answer?.offerMessageId, "No offerMessageId", {
1167
- F: __dxlog_file2,
1168
- L: 135,
1169
- S: this,
1170
- A: [
1171
- "message.data?.answer?.offerMessageId",
1172
- "'No offerMessageId'"
1173
- ]
1174
- });
1175
- const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
1176
- if (offerRecord) {
1177
- this._offerRecords.delete(message.data.answer.offerMessageId);
1178
- (0, import_invariant2.invariant)(message.data?.answer, "No answer", {
1179
- F: __dxlog_file2,
1180
- L: 139,
1181
- S: this,
1182
- A: [
1183
- "message.data?.answer",
1184
- "'No answer'"
1185
- ]
1186
- });
1187
- (0, import_log2.log)("resolving", {
1188
- answer: message.data.answer
1189
- }, {
1190
- F: __dxlog_file2,
1191
- L: 140,
1192
- S: this,
1193
- C: (f, a) => f(...a)
1194
- });
1195
- offerRecord.resolve(message.data.answer);
1196
- }
1197
- }
1198
- async _handleOffer({ author, recipient, message }) {
1199
- (0, import_invariant2.invariant)(message.data.offer, "No offer", {
1200
- F: __dxlog_file2,
1201
- L: 154,
1202
- S: this,
1203
- A: [
1204
- "message.data.offer",
1205
- "'No offer'"
1206
- ]
1207
- });
1208
- const offerMessage = {
1209
- author,
1210
- recipient,
1211
- ...message,
1212
- data: {
1213
- offer: message.data.offer
1214
- }
1215
- };
1216
- const answer = await this._onOffer(offerMessage);
1217
- answer.offerMessageId = message.messageId;
1218
- try {
1219
- await this._sendReliableMessage({
1220
- author: recipient,
1221
- recipient: author,
1222
- message: {
1223
- topic: message.topic,
1224
- sessionId: message.sessionId,
1225
- data: {
1226
- answer
1227
- }
1228
- }
1229
- });
1230
- } catch (err) {
1231
- import_log2.log.warn("error sending answer", {
1232
- err
1233
- }, {
1234
- F: __dxlog_file2,
1235
- L: 174,
1236
- S: this,
1237
- C: (f, a) => f(...a)
1238
- });
1239
- }
1240
- }
1241
- async _handleSignal({ author, recipient, message }) {
1242
- (0, import_invariant2.invariant)(message.messageId, void 0, {
1243
- F: __dxlog_file2,
1244
- L: 187,
1245
- S: this,
1246
- A: [
1247
- "message.messageId",
1248
- ""
1249
- ]
1250
- });
1251
- (0, import_invariant2.invariant)(message.data.signal || message.data.signalBatch, "Invalid message", {
1252
- F: __dxlog_file2,
1253
- L: 188,
1254
- S: this,
1255
- A: [
1256
- "message.data.signal || message.data.signalBatch",
1257
- "'Invalid message'"
1258
- ]
1259
- });
1260
- const signalMessage = {
1261
- author,
1262
- recipient,
1263
- ...message,
1264
- data: {
1265
- signal: message.data.signal,
1266
- signalBatch: message.data.signalBatch
1267
- }
1268
- };
1269
- await this._onSignal(signalMessage);
1270
- }
1271
- };
1272
-
1273
- // packages/core/mesh/network-manager/src/swarm/peer.ts
1274
- var import_async2 = require("@dxos/async");
1275
- var import_context3 = require("@dxos/context");
1276
- var import_errors2 = require("@dxos/errors");
1277
- var import_invariant3 = require("@dxos/invariant");
1278
- var import_keys3 = require("@dxos/keys");
1279
- var import_log3 = require("@dxos/log");
1280
1190
  function _ts_decorate2(decorators, target, key, desc) {
1281
1191
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1282
1192
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -1288,7 +1198,7 @@ function _ts_decorate2(decorators, target, key, desc) {
1288
1198
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1289
1199
  }
1290
1200
  var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
1291
- var ConnectionDisplacedError = class extends import_errors2.SystemError {
1201
+ var ConnectionDisplacedError = class extends import_protocols2.SystemError {
1292
1202
  constructor() {
1293
1203
  super("Connection displaced by remote initiator.");
1294
1204
  }
@@ -1307,10 +1217,10 @@ var Peer = class {
1307
1217
  this._callbacks = _callbacks;
1308
1218
  this._availableAfter = 0;
1309
1219
  this.availableToConnect = true;
1310
- this._ctx = new import_context3.Context();
1220
+ this._ctx = new import_context2.Context();
1311
1221
  this.advertizing = false;
1312
1222
  this.initiating = false;
1313
- this.connectionDisplaced = new import_async2.Event();
1223
+ this.connectionDisplaced = new import_async3.Event();
1314
1224
  }
1315
1225
  /**
1316
1226
  * Respond to remote offer.
@@ -1341,7 +1251,7 @@ var Peer = class {
1341
1251
  }
1342
1252
  if (await this._callbacks.onOffer(remoteId)) {
1343
1253
  if (!this.connection) {
1344
- (0, import_invariant3.invariant)(message.sessionId, void 0, {
1254
+ (0, import_invariant2.invariant)(message.sessionId, void 0, {
1345
1255
  F: __dxlog_file3,
1346
1256
  L: 137,
1347
1257
  S: this,
@@ -1356,7 +1266,7 @@ var Peer = class {
1356
1266
  connection.initiate();
1357
1267
  await connection.openConnection();
1358
1268
  } catch (err) {
1359
- if (!(err instanceof import_errors2.CancelledError)) {
1269
+ if (!(err instanceof import_protocols2.CancelledError)) {
1360
1270
  import_log3.log.warn("connection error", {
1361
1271
  topic: this.topic,
1362
1272
  peerId: this.localPeerId,
@@ -1384,7 +1294,7 @@ var Peer = class {
1384
1294
  * Initiate a connection to the remote peer.
1385
1295
  */
1386
1296
  async initiateConnection() {
1387
- (0, import_invariant3.invariant)(!this.initiating, "Initiation in progress.", {
1297
+ (0, import_invariant2.invariant)(!this.initiating, "Initiation in progress.", {
1388
1298
  F: __dxlog_file3,
1389
1299
  L: 164,
1390
1300
  S: this,
@@ -1393,7 +1303,7 @@ var Peer = class {
1393
1303
  "'Initiation in progress.'"
1394
1304
  ]
1395
1305
  });
1396
- (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
1306
+ (0, import_invariant2.invariant)(!this.connection, "Already connected.", {
1397
1307
  F: __dxlog_file3,
1398
1308
  L: 165,
1399
1309
  S: this,
@@ -1466,6 +1376,9 @@ var Peer = class {
1466
1376
  S: this,
1467
1377
  C: (f, a) => f(...a)
1468
1378
  });
1379
+ if (err instanceof import_protocols2.TimeoutError) {
1380
+ await connection.abort(err);
1381
+ }
1469
1382
  await this.closeConnection(err);
1470
1383
  throw err;
1471
1384
  } finally {
@@ -1485,13 +1398,13 @@ var Peer = class {
1485
1398
  sessionId
1486
1399
  }, {
1487
1400
  F: __dxlog_file3,
1488
- L: 210,
1401
+ L: 213,
1489
1402
  S: this,
1490
1403
  C: (f, a) => f(...a)
1491
1404
  });
1492
- (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
1405
+ (0, import_invariant2.invariant)(!this.connection, "Already connected.", {
1493
1406
  F: __dxlog_file3,
1494
- L: 217,
1407
+ L: 220,
1495
1408
  S: this,
1496
1409
  A: [
1497
1410
  "!this.connection",
@@ -1527,7 +1440,7 @@ var Peer = class {
1527
1440
  initiator
1528
1441
  }, {
1529
1442
  F: __dxlog_file3,
1530
- L: 236,
1443
+ L: 239,
1531
1444
  S: this,
1532
1445
  C: (f, a) => f(...a)
1533
1446
  });
@@ -1540,14 +1453,14 @@ var Peer = class {
1540
1453
  initiator
1541
1454
  }, {
1542
1455
  F: __dxlog_file3,
1543
- L: 245,
1456
+ L: 248,
1544
1457
  S: this,
1545
1458
  C: (f, a) => f(...a)
1546
1459
  });
1547
1460
  this._connectionLimiter.doneConnecting(sessionId);
1548
- (0, import_invariant3.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
1461
+ (0, import_invariant2.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
1549
1462
  F: __dxlog_file3,
1550
- L: 250,
1463
+ L: 253,
1551
1464
  S: this,
1552
1465
  A: [
1553
1466
  "this.connection === connection",
@@ -1562,7 +1475,7 @@ var Peer = class {
1562
1475
  initiator
1563
1476
  }, {
1564
1477
  F: __dxlog_file3,
1565
- L: 252,
1478
+ L: 255,
1566
1479
  S: this,
1567
1480
  C: (f, a) => f(...a)
1568
1481
  });
@@ -1576,7 +1489,7 @@ var Peer = class {
1576
1489
  this._availableAfter = increaseInterval(this._availableAfter);
1577
1490
  }
1578
1491
  this._callbacks.onDisconnected();
1579
- (0, import_async2.scheduleTask)(this._connectionCtx, () => {
1492
+ (0, import_async3.scheduleTask)(this._connectionCtx, () => {
1580
1493
  this.availableToConnect = true;
1581
1494
  this._callbacks.onPeerAvailable();
1582
1495
  }, this._availableAfter);
@@ -1597,95 +1510,327 @@ var Peer = class {
1597
1510
  err
1598
1511
  }, {
1599
1512
  F: __dxlog_file3,
1600
- L: 292,
1513
+ L: 295,
1514
+ S: this,
1515
+ C: (f, a) => f(...a)
1516
+ });
1517
+ import_log3.log.trace("dxos.mesh.connection.error", {
1518
+ topic: this.topic,
1519
+ localPeerId: this.localPeerId,
1520
+ remotePeerId: this.id,
1521
+ sessionId,
1522
+ initiator,
1523
+ err
1524
+ }, {
1525
+ F: __dxlog_file3,
1526
+ L: 296,
1527
+ S: this,
1528
+ C: (f, a) => f(...a)
1529
+ });
1530
+ void this.closeConnection(err);
1531
+ });
1532
+ this.connection = connection;
1533
+ return connection;
1534
+ }
1535
+ async closeConnection(err) {
1536
+ if (!this.connection) {
1537
+ return;
1538
+ }
1539
+ const connection = this.connection;
1540
+ (0, import_log3.log)("closing...", {
1541
+ peerId: this.id,
1542
+ sessionId: connection.sessionId
1543
+ }, {
1544
+ F: __dxlog_file3,
1545
+ L: 321,
1546
+ S: this,
1547
+ C: (f, a) => f(...a)
1548
+ });
1549
+ await connection.close(err);
1550
+ (0, import_log3.log)("closed", {
1551
+ peerId: this.id,
1552
+ sessionId: connection.sessionId
1553
+ }, {
1554
+ F: __dxlog_file3,
1555
+ L: 327,
1556
+ S: this,
1557
+ C: (f, a) => f(...a)
1558
+ });
1559
+ }
1560
+ async onSignal(message) {
1561
+ if (!this.connection) {
1562
+ (0, import_log3.log)("dropping signal message for non-existent connection", {
1563
+ message
1564
+ }, {
1565
+ F: __dxlog_file3,
1566
+ L: 332,
1567
+ S: this,
1568
+ C: (f, a) => f(...a)
1569
+ });
1570
+ return;
1571
+ }
1572
+ await this.connection.signal(message);
1573
+ }
1574
+ async destroy() {
1575
+ await this._ctx.dispose();
1576
+ (0, import_log3.log)("Destroying peer", {
1577
+ peerId: this.id,
1578
+ topic: this.topic
1579
+ }, {
1580
+ F: __dxlog_file3,
1581
+ L: 342,
1582
+ S: this,
1583
+ C: (f, a) => f(...a)
1584
+ });
1585
+ await this?.connection?.close();
1586
+ }
1587
+ };
1588
+ _ts_decorate2([
1589
+ import_async3.synchronized
1590
+ ], Peer.prototype, "destroy", null);
1591
+ var increaseInterval = (interval) => {
1592
+ if (interval === 0) {
1593
+ return 50;
1594
+ } else if (interval < 500) {
1595
+ return 500;
1596
+ } else if (interval < 1e3) {
1597
+ return 1e3;
1598
+ } else if (interval < 5e3) {
1599
+ return 5e3;
1600
+ }
1601
+ return 1e4;
1602
+ };
1603
+
1604
+ // packages/core/mesh/network-manager/src/signal/swarm-messenger.ts
1605
+ var import_context3 = require("@dxos/context");
1606
+ var import_invariant3 = require("@dxos/invariant");
1607
+ var import_keys4 = require("@dxos/keys");
1608
+ var import_log4 = require("@dxos/log");
1609
+ var import_protocols3 = require("@dxos/protocols");
1610
+ var import_util2 = require("@dxos/util");
1611
+ var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/signal/swarm-messenger.ts";
1612
+ var SwarmMessage = import_protocols3.schema.getCodecForType("dxos.mesh.swarm.SwarmMessage");
1613
+ var SwarmMessenger = class {
1614
+ constructor({ sendMessage, onSignal, onOffer, topic }) {
1615
+ this._ctx = new import_context3.Context();
1616
+ this._offerRecords = new import_util2.ComplexMap((key) => key.toHex());
1617
+ this._sendMessage = sendMessage;
1618
+ this._onSignal = onSignal;
1619
+ this._onOffer = onOffer;
1620
+ this._topic = topic;
1621
+ }
1622
+ async receiveMessage({ author, recipient, payload }) {
1623
+ if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
1624
+ return;
1625
+ }
1626
+ const message = SwarmMessage.decode(payload.value);
1627
+ if (!this._topic.equals(message.topic)) {
1628
+ return;
1629
+ }
1630
+ (0, import_log4.log)("received", {
1631
+ from: author,
1632
+ to: recipient,
1633
+ msg: message
1634
+ }, {
1635
+ F: __dxlog_file4,
1636
+ L: 69,
1637
+ S: this,
1638
+ C: (f, a) => f(...a)
1639
+ });
1640
+ if (message.data?.offer) {
1641
+ await this._handleOffer({
1642
+ author,
1643
+ recipient,
1644
+ message
1645
+ });
1646
+ } else if (message.data?.answer) {
1647
+ await this._resolveAnswers(message);
1648
+ } else if (message.data?.signal) {
1649
+ await this._handleSignal({
1650
+ author,
1651
+ recipient,
1652
+ message
1653
+ });
1654
+ } else if (message.data?.signalBatch) {
1655
+ await this._handleSignal({
1656
+ author,
1657
+ recipient,
1658
+ message
1659
+ });
1660
+ } else {
1661
+ import_log4.log.warn("unknown message", {
1662
+ message
1663
+ }, {
1664
+ F: __dxlog_file4,
1665
+ L: 80,
1601
1666
  S: this,
1602
1667
  C: (f, a) => f(...a)
1603
1668
  });
1604
- import_log3.log.trace("dxos.mesh.connection.error", {
1605
- topic: this.topic,
1606
- localPeerId: this.localPeerId,
1607
- remotePeerId: this.id,
1608
- sessionId,
1609
- initiator,
1610
- err
1669
+ }
1670
+ }
1671
+ async signal(message) {
1672
+ (0, import_invariant3.invariant)(message.data?.signal || message.data?.signalBatch, "Invalid message", {
1673
+ F: __dxlog_file4,
1674
+ L: 85,
1675
+ S: this,
1676
+ A: [
1677
+ "message.data?.signal || message.data?.signalBatch",
1678
+ "'Invalid message'"
1679
+ ]
1680
+ });
1681
+ await this._sendReliableMessage({
1682
+ author: message.author,
1683
+ recipient: message.recipient,
1684
+ message
1685
+ });
1686
+ }
1687
+ async offer(message) {
1688
+ const networkMessage = {
1689
+ ...message,
1690
+ messageId: import_keys4.PublicKey.random()
1691
+ };
1692
+ return new Promise((resolve, reject) => {
1693
+ this._offerRecords.set(networkMessage.messageId, {
1694
+ resolve
1695
+ });
1696
+ this._sendReliableMessage({
1697
+ author: message.author,
1698
+ recipient: message.recipient,
1699
+ message: networkMessage
1700
+ }).catch((err) => reject(err));
1701
+ });
1702
+ }
1703
+ async _sendReliableMessage({ author, recipient, message }) {
1704
+ const networkMessage = {
1705
+ ...message,
1706
+ // Setting unique message_id if it not specified yet.
1707
+ messageId: message.messageId ?? import_keys4.PublicKey.random()
1708
+ };
1709
+ (0, import_log4.log)("sending", {
1710
+ from: author,
1711
+ to: recipient,
1712
+ msg: networkMessage
1713
+ }, {
1714
+ F: __dxlog_file4,
1715
+ L: 123,
1716
+ S: this,
1717
+ C: (f, a) => f(...a)
1718
+ });
1719
+ await this._sendMessage({
1720
+ author,
1721
+ recipient,
1722
+ payload: {
1723
+ type_url: "dxos.mesh.swarm.SwarmMessage",
1724
+ value: SwarmMessage.encode(networkMessage)
1725
+ }
1726
+ });
1727
+ }
1728
+ async _resolveAnswers(message) {
1729
+ (0, import_invariant3.invariant)(message.data?.answer?.offerMessageId, "No offerMessageId", {
1730
+ F: __dxlog_file4,
1731
+ L: 135,
1732
+ S: this,
1733
+ A: [
1734
+ "message.data?.answer?.offerMessageId",
1735
+ "'No offerMessageId'"
1736
+ ]
1737
+ });
1738
+ const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
1739
+ if (offerRecord) {
1740
+ this._offerRecords.delete(message.data.answer.offerMessageId);
1741
+ (0, import_invariant3.invariant)(message.data?.answer, "No answer", {
1742
+ F: __dxlog_file4,
1743
+ L: 139,
1744
+ S: this,
1745
+ A: [
1746
+ "message.data?.answer",
1747
+ "'No answer'"
1748
+ ]
1749
+ });
1750
+ (0, import_log4.log)("resolving", {
1751
+ answer: message.data.answer
1611
1752
  }, {
1612
- F: __dxlog_file3,
1613
- L: 293,
1753
+ F: __dxlog_file4,
1754
+ L: 140,
1614
1755
  S: this,
1615
1756
  C: (f, a) => f(...a)
1616
1757
  });
1617
- void this.closeConnection(err);
1618
- });
1619
- this.connection = connection;
1620
- return connection;
1621
- }
1622
- async closeConnection(err) {
1623
- if (!this.connection) {
1624
- return;
1758
+ offerRecord.resolve(message.data.answer);
1625
1759
  }
1626
- const connection = this.connection;
1627
- (0, import_log3.log)("closing...", {
1628
- peerId: this.id,
1629
- sessionId: connection.sessionId
1630
- }, {
1631
- F: __dxlog_file3,
1632
- L: 318,
1633
- S: this,
1634
- C: (f, a) => f(...a)
1635
- });
1636
- await connection.close(err);
1637
- (0, import_log3.log)("closed", {
1638
- peerId: this.id,
1639
- sessionId: connection.sessionId
1640
- }, {
1641
- F: __dxlog_file3,
1642
- L: 324,
1760
+ }
1761
+ async _handleOffer({ author, recipient, message }) {
1762
+ (0, import_invariant3.invariant)(message.data.offer, "No offer", {
1763
+ F: __dxlog_file4,
1764
+ L: 154,
1643
1765
  S: this,
1644
- C: (f, a) => f(...a)
1766
+ A: [
1767
+ "message.data.offer",
1768
+ "'No offer'"
1769
+ ]
1645
1770
  });
1646
- }
1647
- async onSignal(message) {
1648
- if (!this.connection) {
1649
- (0, import_log3.log)("dropping signal message for non-existent connection", {
1650
- message
1771
+ const offerMessage = {
1772
+ author,
1773
+ recipient,
1774
+ ...message,
1775
+ data: {
1776
+ offer: message.data.offer
1777
+ }
1778
+ };
1779
+ const answer = await this._onOffer(offerMessage);
1780
+ answer.offerMessageId = message.messageId;
1781
+ try {
1782
+ await this._sendReliableMessage({
1783
+ author: recipient,
1784
+ recipient: author,
1785
+ message: {
1786
+ topic: message.topic,
1787
+ sessionId: message.sessionId,
1788
+ data: {
1789
+ answer
1790
+ }
1791
+ }
1792
+ });
1793
+ } catch (err) {
1794
+ import_log4.log.warn("error sending answer", {
1795
+ err
1651
1796
  }, {
1652
- F: __dxlog_file3,
1653
- L: 329,
1797
+ F: __dxlog_file4,
1798
+ L: 174,
1654
1799
  S: this,
1655
1800
  C: (f, a) => f(...a)
1656
1801
  });
1657
- return;
1658
1802
  }
1659
- await this.connection.signal(message);
1660
1803
  }
1661
- async destroy() {
1662
- await this._ctx.dispose();
1663
- (0, import_log3.log)("Destroying peer", {
1664
- peerId: this.id,
1665
- topic: this.topic
1666
- }, {
1667
- F: __dxlog_file3,
1668
- L: 339,
1804
+ async _handleSignal({ author, recipient, message }) {
1805
+ (0, import_invariant3.invariant)(message.messageId, void 0, {
1806
+ F: __dxlog_file4,
1807
+ L: 187,
1669
1808
  S: this,
1670
- C: (f, a) => f(...a)
1809
+ A: [
1810
+ "message.messageId",
1811
+ ""
1812
+ ]
1671
1813
  });
1672
- await this?.connection?.close();
1673
- }
1674
- };
1675
- _ts_decorate2([
1676
- import_async2.synchronized
1677
- ], Peer.prototype, "destroy", null);
1678
- var increaseInterval = (interval) => {
1679
- if (interval === 0) {
1680
- return 50;
1681
- } else if (interval < 500) {
1682
- return 500;
1683
- } else if (interval < 1e3) {
1684
- return 1e3;
1685
- } else if (interval < 5e3) {
1686
- return 5e3;
1814
+ (0, import_invariant3.invariant)(message.data.signal || message.data.signalBatch, "Invalid message", {
1815
+ F: __dxlog_file4,
1816
+ L: 188,
1817
+ S: this,
1818
+ A: [
1819
+ "message.data.signal || message.data.signalBatch",
1820
+ "'Invalid message'"
1821
+ ]
1822
+ });
1823
+ const signalMessage = {
1824
+ author,
1825
+ recipient,
1826
+ ...message,
1827
+ data: {
1828
+ signal: message.data.signal,
1829
+ signalBatch: message.data.signalBatch
1830
+ }
1831
+ };
1832
+ await this._onSignal(signalMessage);
1687
1833
  }
1688
- return 1e4;
1689
1834
  };
1690
1835
 
1691
1836
  // packages/core/mesh/network-manager/src/swarm/swarm.ts
@@ -1699,7 +1844,7 @@ function _ts_decorate3(decorators, target, key, desc) {
1699
1844
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1700
1845
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1701
1846
  }
1702
- var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts";
1847
+ var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts";
1703
1848
  var INITIATION_DELAY = 100;
1704
1849
  var getClassName = (obj) => Object.getPrototypeOf(obj).constructor.name;
1705
1850
  var Swarm = class {
@@ -1717,28 +1862,28 @@ var Swarm = class {
1717
1862
  this._initiationDelay = _initiationDelay;
1718
1863
  this._ctx = new import_context4.Context();
1719
1864
  this._listeningHandle = void 0;
1720
- this._peers = new import_util2.ComplexMap(import_keys4.PublicKey.hash);
1721
- this._instanceId = import_keys4.PublicKey.random().toHex();
1722
- this.connectionAdded = new import_async3.Event();
1723
- this.disconnected = new import_async3.Event();
1724
- this.connected = new import_async3.Event();
1865
+ this._peers = new import_util3.ComplexMap(import_keys5.PublicKey.hash);
1866
+ this._instanceId = import_keys5.PublicKey.random().toHex();
1867
+ this.connectionAdded = new import_async4.Event();
1868
+ this.disconnected = new import_async4.Event();
1869
+ this.connected = new import_async4.Event();
1725
1870
  this.errors = new import_debug2.ErrorStream();
1726
- import_log4.log.trace("dxos.mesh.swarm.constructor", import_protocols3.trace.begin({
1871
+ import_log5.log.trace("dxos.mesh.swarm.constructor", import_protocols4.trace.begin({
1727
1872
  id: this._instanceId,
1728
1873
  data: {
1729
1874
  topic: this._topic.toHex(),
1730
1875
  peerId: this._ownPeerId.toHex()
1731
1876
  }
1732
1877
  }), {
1733
- F: __dxlog_file4,
1878
+ F: __dxlog_file5,
1734
1879
  L: 88,
1735
1880
  S: this,
1736
1881
  C: (f, a) => f(...a)
1737
1882
  });
1738
- (0, import_log4.log)("creating swarm", {
1883
+ (0, import_log5.log)("creating swarm", {
1739
1884
  peerId: _ownPeerId
1740
1885
  }, {
1741
- F: __dxlog_file4,
1886
+ F: __dxlog_file5,
1742
1887
  L: 92,
1743
1888
  S: this,
1744
1889
  C: (f, a) => f(...a)
@@ -1750,17 +1895,17 @@ var Swarm = class {
1750
1895
  onOffer: async (msg) => await this.onOffer(msg),
1751
1896
  topic: this._topic
1752
1897
  });
1753
- import_log4.log.trace("dxos.mesh.swarm.constructor", import_protocols3.trace.end({
1898
+ import_log5.log.trace("dxos.mesh.swarm.constructor", import_protocols4.trace.end({
1754
1899
  id: this._instanceId
1755
1900
  }), {
1756
- F: __dxlog_file4,
1901
+ F: __dxlog_file5,
1757
1902
  L: 101,
1758
1903
  S: this,
1759
1904
  C: (f, a) => f(...a)
1760
1905
  });
1761
1906
  }
1762
1907
  get connections() {
1763
- return Array.from(this._peers.values()).map((peer) => peer.connection).filter(import_util2.isNotNullOrUndefined);
1908
+ return Array.from(this._peers.values()).map((peer) => peer.connection).filter(import_util3.isNotNullOrUndefined);
1764
1909
  }
1765
1910
  get ownPeerId() {
1766
1911
  return this._ownPeerId;
@@ -1776,7 +1921,7 @@ var Swarm = class {
1776
1921
  }
1777
1922
  async open() {
1778
1923
  (0, import_invariant4.invariant)(!this._listeningHandle, void 0, {
1779
- F: __dxlog_file4,
1924
+ F: __dxlog_file5,
1780
1925
  L: 128,
1781
1926
  S: this,
1782
1927
  A: [
@@ -1788,10 +1933,10 @@ var Swarm = class {
1788
1933
  peerId: this._ownPeerId,
1789
1934
  payloadType: "dxos.mesh.swarm.SwarmMessage",
1790
1935
  onMessage: async (message) => {
1791
- await this._swarmMessenger.receiveMessage(message).catch((err) => import_log4.log.warn("Error while receiving message", {
1936
+ await this._swarmMessenger.receiveMessage(message).catch((err) => import_log5.log.warn("Error while receiving message", {
1792
1937
  err
1793
1938
  }, {
1794
- F: __dxlog_file4,
1939
+ F: __dxlog_file5,
1795
1940
  L: 135,
1796
1941
  S: this,
1797
1942
  C: (f, a) => f(...a)
@@ -1800,8 +1945,8 @@ var Swarm = class {
1800
1945
  });
1801
1946
  }
1802
1947
  async destroy() {
1803
- (0, import_log4.log)("destroying...", void 0, {
1804
- F: __dxlog_file4,
1948
+ (0, import_log5.log)("destroying...", void 0, {
1949
+ F: __dxlog_file5,
1805
1950
  L: 141,
1806
1951
  S: this,
1807
1952
  C: (f, a) => f(...a)
@@ -1811,8 +1956,8 @@ var Swarm = class {
1811
1956
  await this._ctx.dispose();
1812
1957
  await this._topology.destroy();
1813
1958
  await Promise.all(Array.from(this._peers.keys()).map((key) => this._destroyPeer(key)));
1814
- (0, import_log4.log)("destroyed", void 0, {
1815
- F: __dxlog_file4,
1959
+ (0, import_log5.log)("destroyed", void 0, {
1960
+ F: __dxlog_file5,
1816
1961
  L: 148,
1817
1962
  S: this,
1818
1963
  C: (f, a) => f(...a)
@@ -1820,7 +1965,7 @@ var Swarm = class {
1820
1965
  }
1821
1966
  async setTopology(topology) {
1822
1967
  (0, import_invariant4.invariant)(!this._ctx.disposed, "Swarm is offline", {
1823
- F: __dxlog_file4,
1968
+ F: __dxlog_file5,
1824
1969
  L: 152,
1825
1970
  S: this,
1826
1971
  A: [
@@ -1831,11 +1976,11 @@ var Swarm = class {
1831
1976
  if (topology === this._topology) {
1832
1977
  return;
1833
1978
  }
1834
- (0, import_log4.log)("setting topology", {
1979
+ (0, import_log5.log)("setting topology", {
1835
1980
  previous: getClassName(this._topology),
1836
1981
  topology: getClassName(topology)
1837
1982
  }, {
1838
- F: __dxlog_file4,
1983
+ F: __dxlog_file5,
1839
1984
  L: 156,
1840
1985
  S: this,
1841
1986
  C: (f, a) => f(...a)
@@ -1846,17 +1991,17 @@ var Swarm = class {
1846
1991
  this._topology.update();
1847
1992
  }
1848
1993
  onSwarmEvent(swarmEvent) {
1849
- (0, import_log4.log)("swarm event", {
1994
+ (0, import_log5.log)("swarm event", {
1850
1995
  swarmEvent
1851
1996
  }, {
1852
- F: __dxlog_file4,
1997
+ F: __dxlog_file5,
1853
1998
  L: 169,
1854
1999
  S: this,
1855
2000
  C: (f, a) => f(...a)
1856
2001
  });
1857
2002
  if (this._ctx.disposed) {
1858
- (0, import_log4.log)("swarm event ignored for disposed swarm", void 0, {
1859
- F: __dxlog_file4,
2003
+ (0, import_log5.log)("swarm event ignored for disposed swarm", void 0, {
2004
+ F: __dxlog_file5,
1860
2005
  L: 172,
1861
2006
  S: this,
1862
2007
  C: (f, a) => f(...a)
@@ -1864,11 +2009,11 @@ var Swarm = class {
1864
2009
  return;
1865
2010
  }
1866
2011
  if (swarmEvent.peerAvailable) {
1867
- const peerId = import_keys4.PublicKey.from(swarmEvent.peerAvailable.peer);
1868
- (0, import_log4.log)("new peer", {
2012
+ const peerId = import_keys5.PublicKey.from(swarmEvent.peerAvailable.peer);
2013
+ (0, import_log5.log)("new peer", {
1869
2014
  peerId
1870
2015
  }, {
1871
- F: __dxlog_file4,
2016
+ F: __dxlog_file5,
1872
2017
  L: 178,
1873
2018
  S: this,
1874
2019
  C: (f, a) => f(...a)
@@ -1878,12 +2023,12 @@ var Swarm = class {
1878
2023
  peer.advertizing = true;
1879
2024
  }
1880
2025
  } else if (swarmEvent.peerLeft) {
1881
- const peer = this._peers.get(import_keys4.PublicKey.from(swarmEvent.peerLeft.peer));
2026
+ const peer = this._peers.get(import_keys5.PublicKey.from(swarmEvent.peerLeft.peer));
1882
2027
  if (peer) {
1883
2028
  peer.advertizing = false;
1884
2029
  if (peer.connection?.state !== ConnectionState.CONNECTED) {
1885
- void this._destroyPeer(peer.id).catch((err) => import_log4.log.catch(err, void 0, {
1886
- F: __dxlog_file4,
2030
+ void this._destroyPeer(peer.id).catch((err) => import_log5.log.catch(err, void 0, {
2031
+ F: __dxlog_file5,
1887
2032
  L: 189,
1888
2033
  S: this,
1889
2034
  C: (f, a) => f(...a)
@@ -1894,17 +2039,17 @@ var Swarm = class {
1894
2039
  this._topology.update();
1895
2040
  }
1896
2041
  async onOffer(message) {
1897
- (0, import_log4.log)("offer", {
2042
+ (0, import_log5.log)("offer", {
1898
2043
  message
1899
2044
  }, {
1900
- F: __dxlog_file4,
2045
+ F: __dxlog_file5,
1901
2046
  L: 199,
1902
2047
  S: this,
1903
2048
  C: (f, a) => f(...a)
1904
2049
  });
1905
2050
  if (this._ctx.disposed) {
1906
- (0, import_log4.log)("ignored for disposed swarm", void 0, {
1907
- F: __dxlog_file4,
2051
+ (0, import_log5.log)("ignored for disposed swarm", void 0, {
2052
+ F: __dxlog_file5,
1908
2053
  L: 201,
1909
2054
  S: this,
1910
2055
  C: (f, a) => f(...a)
@@ -1914,7 +2059,7 @@ var Swarm = class {
1914
2059
  };
1915
2060
  }
1916
2061
  (0, import_invariant4.invariant)(message.author, void 0, {
1917
- F: __dxlog_file4,
2062
+ F: __dxlog_file5,
1918
2063
  L: 206,
1919
2064
  S: this,
1920
2065
  A: [
@@ -1923,10 +2068,10 @@ var Swarm = class {
1923
2068
  ]
1924
2069
  });
1925
2070
  if (!message.recipient?.equals(this._ownPeerId)) {
1926
- (0, import_log4.log)("rejecting offer with incorrect peerId", {
2071
+ (0, import_log5.log)("rejecting offer with incorrect peerId", {
1927
2072
  message
1928
2073
  }, {
1929
- F: __dxlog_file4,
2074
+ F: __dxlog_file5,
1930
2075
  L: 208,
1931
2076
  S: this,
1932
2077
  C: (f, a) => f(...a)
@@ -1936,10 +2081,10 @@ var Swarm = class {
1936
2081
  };
1937
2082
  }
1938
2083
  if (!message.topic?.equals(this._topic)) {
1939
- (0, import_log4.log)("rejecting offer with incorrect topic", {
2084
+ (0, import_log5.log)("rejecting offer with incorrect topic", {
1940
2085
  message
1941
2086
  }, {
1942
- F: __dxlog_file4,
2087
+ F: __dxlog_file5,
1943
2088
  L: 212,
1944
2089
  S: this,
1945
2090
  C: (f, a) => f(...a)
@@ -1954,17 +2099,17 @@ var Swarm = class {
1954
2099
  return answer;
1955
2100
  }
1956
2101
  async onSignal(message) {
1957
- (0, import_log4.log)("signal", {
2102
+ (0, import_log5.log)("signal", {
1958
2103
  message
1959
2104
  }, {
1960
- F: __dxlog_file4,
2105
+ F: __dxlog_file5,
1961
2106
  L: 223,
1962
2107
  S: this,
1963
2108
  C: (f, a) => f(...a)
1964
2109
  });
1965
2110
  if (this._ctx.disposed) {
1966
- import_log4.log.info("ignored for offline swarm", void 0, {
1967
- F: __dxlog_file4,
2111
+ import_log5.log.info("ignored for offline swarm", void 0, {
2112
+ F: __dxlog_file5,
1968
2113
  L: 225,
1969
2114
  S: this,
1970
2115
  C: (f, a) => f(...a)
@@ -1972,7 +2117,7 @@ var Swarm = class {
1972
2117
  return;
1973
2118
  }
1974
2119
  (0, import_invariant4.invariant)(message.recipient?.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1975
- F: __dxlog_file4,
2120
+ F: __dxlog_file5,
1976
2121
  L: 228,
1977
2122
  S: this,
1978
2123
  A: [
@@ -1981,7 +2126,7 @@ var Swarm = class {
1981
2126
  ]
1982
2127
  });
1983
2128
  (0, import_invariant4.invariant)(message.topic?.equals(this._topic), void 0, {
1984
- F: __dxlog_file4,
2129
+ F: __dxlog_file5,
1985
2130
  L: 232,
1986
2131
  S: this,
1987
2132
  A: [
@@ -1990,7 +2135,7 @@ var Swarm = class {
1990
2135
  ]
1991
2136
  });
1992
2137
  (0, import_invariant4.invariant)(message.author, void 0, {
1993
- F: __dxlog_file4,
2138
+ F: __dxlog_file5,
1994
2139
  L: 233,
1995
2140
  S: this,
1996
2141
  A: [
@@ -2050,7 +2195,7 @@ var Swarm = class {
2050
2195
  }
2051
2196
  async _destroyPeer(peerId) {
2052
2197
  (0, import_invariant4.invariant)(this._peers.has(peerId), void 0, {
2053
- F: __dxlog_file4,
2198
+ F: __dxlog_file5,
2054
2199
  L: 303,
2055
2200
  S: this,
2056
2201
  A: [
@@ -2072,12 +2217,12 @@ var Swarm = class {
2072
2217
  if (this._ctx.disposed) {
2073
2218
  return;
2074
2219
  }
2075
- (0, import_async3.scheduleTask)(this._ctx, async () => {
2220
+ (0, import_async4.scheduleTask)(this._ctx, async () => {
2076
2221
  try {
2077
2222
  await this._initiateConnection(peer);
2078
2223
  } catch (err) {
2079
- (0, import_log4.log)("initiation error", err, {
2080
- F: __dxlog_file4,
2224
+ (0, import_log5.log)("initiation error", err, {
2225
+ F: __dxlog_file5,
2081
2226
  L: 329,
2082
2227
  S: this,
2083
2228
  C: (f, a) => f(...a)
@@ -2089,7 +2234,7 @@ var Swarm = class {
2089
2234
  if (this._ctx.disposed) {
2090
2235
  return;
2091
2236
  }
2092
- (0, import_async3.scheduleTask)(this._ctx, async () => {
2237
+ (0, import_async4.scheduleTask)(this._ctx, async () => {
2093
2238
  await this._closeConnection(peer);
2094
2239
  this._topology.update();
2095
2240
  });
@@ -2102,15 +2247,15 @@ var Swarm = class {
2102
2247
  async _initiateConnection(remoteId) {
2103
2248
  const ctx = this._ctx;
2104
2249
  if (remoteId.toHex() < this._ownPeerId.toHex()) {
2105
- (0, import_log4.log)("initiation delay", {
2250
+ (0, import_log5.log)("initiation delay", {
2106
2251
  remoteId
2107
2252
  }, {
2108
- F: __dxlog_file4,
2253
+ F: __dxlog_file5,
2109
2254
  L: 356,
2110
2255
  S: this,
2111
2256
  C: (f, a) => f(...a)
2112
2257
  });
2113
- await (0, import_async3.sleep)(this._initiationDelay);
2258
+ await (0, import_async4.sleep)(this._initiationDelay);
2114
2259
  }
2115
2260
  if (ctx.disposed) {
2116
2261
  return;
@@ -2119,20 +2264,20 @@ var Swarm = class {
2119
2264
  if (peer.connection) {
2120
2265
  return;
2121
2266
  }
2122
- (0, import_log4.log)("initiating connection...", {
2267
+ (0, import_log5.log)("initiating connection...", {
2123
2268
  remoteId
2124
2269
  }, {
2125
- F: __dxlog_file4,
2270
+ F: __dxlog_file5,
2126
2271
  L: 370,
2127
2272
  S: this,
2128
2273
  C: (f, a) => f(...a)
2129
2274
  });
2130
2275
  await peer.initiateConnection();
2131
2276
  this._topology.update();
2132
- (0, import_log4.log)("initiated", {
2277
+ (0, import_log5.log)("initiated", {
2133
2278
  remoteId
2134
2279
  }, {
2135
- F: __dxlog_file4,
2280
+ F: __dxlog_file5,
2136
2281
  L: 373,
2137
2282
  S: this,
2138
2283
  C: (f, a) => f(...a)
@@ -2147,44 +2292,43 @@ var Swarm = class {
2147
2292
  }
2148
2293
  };
2149
2294
  _ts_decorate3([
2150
- import_log4.logInfo
2295
+ import_log5.logInfo
2151
2296
  ], Swarm.prototype, "_instanceId", void 0);
2152
2297
  _ts_decorate3([
2153
- import_log4.logInfo
2298
+ import_log5.logInfo
2154
2299
  ], Swarm.prototype, "ownPeerId", null);
2155
2300
  _ts_decorate3([
2156
- import_log4.logInfo
2301
+ import_log5.logInfo
2157
2302
  ], Swarm.prototype, "topic", null);
2158
2303
  _ts_decorate3([
2159
- import_async3.synchronized
2304
+ import_async4.synchronized
2160
2305
  ], Swarm.prototype, "onSwarmEvent", null);
2161
2306
  _ts_decorate3([
2162
- import_async3.synchronized
2307
+ import_async4.synchronized
2163
2308
  ], Swarm.prototype, "onOffer", null);
2164
2309
  _ts_decorate3([
2165
- import_async3.synchronized
2310
+ import_async4.synchronized
2166
2311
  ], Swarm.prototype, "goOffline", null);
2167
2312
  _ts_decorate3([
2168
- import_async3.synchronized
2313
+ import_async4.synchronized
2169
2314
  ], Swarm.prototype, "goOnline", null);
2170
2315
 
2171
2316
  // packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts
2172
- var import_async4 = require("@dxos/async");
2173
- var import_keys5 = require("@dxos/keys");
2174
- var import_log5 = require("@dxos/log");
2175
- var import_util3 = require("@dxos/util");
2176
- var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts";
2317
+ var import_async5 = require("@dxos/async");
2318
+ var import_keys6 = require("@dxos/keys");
2319
+ var import_log6 = require("@dxos/log");
2320
+ var import_util4 = require("@dxos/util");
2321
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts";
2177
2322
  var SwarmMapper = class {
2178
2323
  get peers() {
2179
2324
  return Array.from(this._peers.values());
2180
2325
  }
2181
- // prettier-ignore
2182
2326
  constructor(_swarm) {
2183
2327
  this._swarm = _swarm;
2184
- this._subscriptions = new import_async4.EventSubscriptions();
2185
- this._connectionSubscriptions = new import_util3.ComplexMap(import_keys5.PublicKey.hash);
2186
- this._peers = new import_util3.ComplexMap(import_keys5.PublicKey.hash);
2187
- this.mapUpdated = new import_async4.Event();
2328
+ this._subscriptions = new import_async5.EventSubscriptions();
2329
+ this._connectionSubscriptions = new import_util4.ComplexMap(import_keys6.PublicKey.hash);
2330
+ this._peers = new import_util4.ComplexMap(import_keys6.PublicKey.hash);
2331
+ this.mapUpdated = new import_async5.Event();
2188
2332
  this._subscriptions.add(_swarm.connectionAdded.on((connection) => {
2189
2333
  this._update();
2190
2334
  this._connectionSubscriptions.set(connection.remoteId, connection.stateChanged.on(() => {
@@ -2199,9 +2343,9 @@ var SwarmMapper = class {
2199
2343
  this._update();
2200
2344
  }
2201
2345
  _update() {
2202
- (0, import_log5.log)("updating swarm", void 0, {
2203
- F: __dxlog_file5,
2204
- L: 75,
2346
+ (0, import_log6.log)("updating swarm", void 0, {
2347
+ F: __dxlog_file6,
2348
+ L: 72,
2205
2349
  S: this,
2206
2350
  C: (f, a) => f(...a)
2207
2351
  });
@@ -2220,12 +2364,12 @@ var SwarmMapper = class {
2220
2364
  ]
2221
2365
  });
2222
2366
  }
2223
- (0, import_log5.log)("graph changed", {
2367
+ (0, import_log6.log)("graph changed", {
2224
2368
  directConnections: this._swarm.connections.length,
2225
2369
  totalPeersInSwarm: this._peers.size
2226
2370
  }, {
2227
- F: __dxlog_file5,
2228
- L: 116,
2371
+ F: __dxlog_file6,
2372
+ L: 113,
2229
2373
  S: this,
2230
2374
  C: (f, a) => f(...a)
2231
2375
  });
@@ -2239,23 +2383,23 @@ var SwarmMapper = class {
2239
2383
  };
2240
2384
 
2241
2385
  // packages/core/mesh/network-manager/src/swarm/connection-limiter.ts
2242
- var import_async5 = require("@dxos/async");
2386
+ var import_async6 = require("@dxos/async");
2243
2387
  var import_context5 = require("@dxos/context");
2244
- var import_errors3 = require("@dxos/errors");
2245
2388
  var import_invariant5 = require("@dxos/invariant");
2246
- var import_keys6 = require("@dxos/keys");
2247
- var import_log6 = require("@dxos/log");
2248
- var import_util4 = require("@dxos/util");
2249
- var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
2250
- var MAX_CONCURRENT_INITIATING_CONNECTIONS = 10;
2389
+ var import_keys7 = require("@dxos/keys");
2390
+ var import_log7 = require("@dxos/log");
2391
+ var import_protocols5 = require("@dxos/protocols");
2392
+ var import_util5 = require("@dxos/util");
2393
+ var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
2394
+ var MAX_CONCURRENT_INITIATING_CONNECTIONS = 50;
2251
2395
  var ConnectionLimiter = class {
2252
2396
  constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
2253
2397
  this._ctx = new import_context5.Context();
2254
2398
  /**
2255
2399
  * Queue of promises to resolve when initiating connections amount is below the limit.
2256
2400
  */
2257
- this._waitingPromises = new import_util4.ComplexMap(import_keys6.PublicKey.hash);
2258
- this.resolveWaitingPromises = new import_async5.DeferredTask(this._ctx, async () => {
2401
+ this._waitingPromises = new import_util5.ComplexMap(import_keys7.PublicKey.hash);
2402
+ this.resolveWaitingPromises = new import_async6.DeferredTask(this._ctx, async () => {
2259
2403
  Array.from(this._waitingPromises.values()).slice(0, this._maxConcurrentInitConnections).forEach(({ resolve }) => {
2260
2404
  resolve();
2261
2405
  });
@@ -2267,7 +2411,7 @@ var ConnectionLimiter = class {
2267
2411
  */
2268
2412
  async connecting(sessionId) {
2269
2413
  (0, import_invariant5.invariant)(!this._waitingPromises.has(sessionId), "Peer is already waiting for connection", {
2270
- F: __dxlog_file6,
2414
+ F: __dxlog_file7,
2271
2415
  L: 48,
2272
2416
  S: this,
2273
2417
  A: [
@@ -2275,10 +2419,10 @@ var ConnectionLimiter = class {
2275
2419
  "'Peer is already waiting for connection'"
2276
2420
  ]
2277
2421
  });
2278
- (0, import_log6.log)("waiting", {
2422
+ (0, import_log7.log)("waiting", {
2279
2423
  sessionId
2280
2424
  }, {
2281
- F: __dxlog_file6,
2425
+ F: __dxlog_file7,
2282
2426
  L: 49,
2283
2427
  S: this,
2284
2428
  C: (f, a) => f(...a)
@@ -2290,10 +2434,10 @@ var ConnectionLimiter = class {
2290
2434
  });
2291
2435
  this.resolveWaitingPromises.schedule();
2292
2436
  });
2293
- (0, import_log6.log)("allow", {
2437
+ (0, import_log7.log)("allow", {
2294
2438
  sessionId
2295
2439
  }, {
2296
- F: __dxlog_file6,
2440
+ F: __dxlog_file7,
2297
2441
  L: 57,
2298
2442
  S: this,
2299
2443
  C: (f, a) => f(...a)
@@ -2303,10 +2447,10 @@ var ConnectionLimiter = class {
2303
2447
  * Rejects promise returned by `connecting` method.
2304
2448
  */
2305
2449
  doneConnecting(sessionId) {
2306
- (0, import_log6.log)("done", {
2450
+ (0, import_log7.log)("done", {
2307
2451
  sessionId
2308
2452
  }, {
2309
- F: __dxlog_file6,
2453
+ F: __dxlog_file7,
2310
2454
  L: 64,
2311
2455
  S: this,
2312
2456
  C: (f, a) => f(...a)
@@ -2314,7 +2458,7 @@ var ConnectionLimiter = class {
2314
2458
  if (!this._waitingPromises.has(sessionId)) {
2315
2459
  return;
2316
2460
  }
2317
- this._waitingPromises.get(sessionId).reject(new import_errors3.CancelledError());
2461
+ this._waitingPromises.get(sessionId).reject(new import_protocols5.CancelledError());
2318
2462
  this._waitingPromises.delete(sessionId);
2319
2463
  this.resolveWaitingPromises.schedule();
2320
2464
  }
@@ -2334,8 +2478,8 @@ var ConnectionLog = class {
2334
2478
  /**
2335
2479
  * SwarmId => info
2336
2480
  */
2337
- this._swarms = new import_util5.ComplexMap(import_keys7.PublicKey.hash);
2338
- this.update = new import_async6.Event();
2481
+ this._swarms = new import_util6.ComplexMap(import_keys8.PublicKey.hash);
2482
+ this.update = new import_async7.Event();
2339
2483
  }
2340
2484
  getSwarmInfo(swarmId) {
2341
2485
  return this._swarms.get(swarmId) ?? (0, import_debug3.raise)(new Error(`Swarm not found: ${swarmId}`));
@@ -2345,13 +2489,13 @@ var ConnectionLog = class {
2345
2489
  }
2346
2490
  joinedSwarm(swarm) {
2347
2491
  const info = {
2348
- id: import_keys7.PublicKey.from(swarm._instanceId),
2492
+ id: import_keys8.PublicKey.from(swarm._instanceId),
2349
2493
  topic: swarm.topic,
2350
2494
  isActive: true,
2351
2495
  label: swarm.label,
2352
2496
  connections: []
2353
2497
  };
2354
- this._swarms.set(import_keys7.PublicKey.from(swarm._instanceId), info);
2498
+ this._swarms.set(import_keys8.PublicKey.from(swarm._instanceId), info);
2355
2499
  this.update.emit();
2356
2500
  swarm.connectionAdded.on((connection) => {
2357
2501
  const connectionInfo = {
@@ -2381,24 +2525,24 @@ var ConnectionLog = class {
2381
2525
  });
2382
2526
  }
2383
2527
  leftSwarm(swarm) {
2384
- this.getSwarmInfo(import_keys7.PublicKey.from(swarm._instanceId)).isActive = false;
2528
+ this.getSwarmInfo(import_keys8.PublicKey.from(swarm._instanceId)).isActive = false;
2385
2529
  this.update.emit();
2386
2530
  }
2387
2531
  };
2388
2532
 
2389
2533
  // packages/core/mesh/network-manager/src/network-manager.ts
2390
- var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts";
2534
+ var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts";
2391
2535
  var NetworkManager = class {
2392
2536
  constructor({ transportFactory, signalManager, log: log1 }) {
2393
2537
  /**
2394
2538
  * @internal
2395
2539
  */
2396
- this._swarms = new import_util6.ComplexMap(import_keys8.PublicKey.hash);
2397
- this._mappers = new import_util6.ComplexMap(import_keys8.PublicKey.hash);
2540
+ this._swarms = new import_util7.ComplexMap(import_keys9.PublicKey.hash);
2541
+ this._mappers = new import_util7.ComplexMap(import_keys9.PublicKey.hash);
2398
2542
  this._connectionState = import_services.ConnectionState.ONLINE;
2399
- this.connectionStateChanged = new import_async7.Event();
2400
- this.topicsUpdated = new import_async7.Event();
2401
- this._instanceId = import_keys8.PublicKey.random().toHex();
2543
+ this.connectionStateChanged = new import_async8.Event();
2544
+ this.topicsUpdated = new import_async8.Event();
2545
+ this._instanceId = import_keys9.PublicKey.random().toHex();
2402
2546
  this._transportFactory = transportFactory;
2403
2547
  this._signalManager = signalManager;
2404
2548
  this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => this._swarms.get(topic)?.onSwarmEvent(event));
@@ -2432,20 +2576,20 @@ var NetworkManager = class {
2432
2576
  return this._swarms.get(topic);
2433
2577
  }
2434
2578
  async open() {
2435
- import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols4.trace.begin({
2579
+ import_log8.log.trace("dxos.mesh.network-manager.open", import_protocols6.trace.begin({
2436
2580
  id: this._instanceId
2437
2581
  }), {
2438
- F: __dxlog_file7,
2582
+ F: __dxlog_file8,
2439
2583
  L: 130,
2440
2584
  S: this,
2441
2585
  C: (f, a) => f(...a)
2442
2586
  });
2443
2587
  await this._messenger.open();
2444
2588
  await this._signalManager.open();
2445
- import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols4.trace.end({
2589
+ import_log8.log.trace("dxos.mesh.network-manager.open", import_protocols6.trace.end({
2446
2590
  id: this._instanceId
2447
2591
  }), {
2448
- F: __dxlog_file7,
2592
+ F: __dxlog_file8,
2449
2593
  L: 133,
2450
2594
  S: this,
2451
2595
  C: (f, a) => f(...a)
@@ -2454,8 +2598,8 @@ var NetworkManager = class {
2454
2598
  async close() {
2455
2599
  for (const topic of this._swarms.keys()) {
2456
2600
  await this.leaveSwarm(topic).catch((err) => {
2457
- (0, import_log7.log)(err, void 0, {
2458
- F: __dxlog_file7,
2601
+ (0, import_log8.log)(err, void 0, {
2602
+ F: __dxlog_file8,
2459
2603
  L: 139,
2460
2604
  S: this,
2461
2605
  C: (f, a) => f(...a)
@@ -2469,8 +2613,8 @@ var NetworkManager = class {
2469
2613
  * Join the swarm.
2470
2614
  */
2471
2615
  async joinSwarm({ topic, peerId, topology, protocolProvider: protocol, label }) {
2472
- (0, import_invariant6.invariant)(import_keys8.PublicKey.isPublicKey(topic), void 0, {
2473
- F: __dxlog_file7,
2616
+ (0, import_invariant6.invariant)(import_keys9.PublicKey.isPublicKey(topic), void 0, {
2617
+ F: __dxlog_file8,
2474
2618
  L: 157,
2475
2619
  S: this,
2476
2620
  A: [
@@ -2478,8 +2622,8 @@ var NetworkManager = class {
2478
2622
  ""
2479
2623
  ]
2480
2624
  });
2481
- (0, import_invariant6.invariant)(import_keys8.PublicKey.isPublicKey(peerId), void 0, {
2482
- F: __dxlog_file7,
2625
+ (0, import_invariant6.invariant)(import_keys9.PublicKey.isPublicKey(peerId), void 0, {
2626
+ F: __dxlog_file8,
2483
2627
  L: 158,
2484
2628
  S: this,
2485
2629
  A: [
@@ -2488,7 +2632,7 @@ var NetworkManager = class {
2488
2632
  ]
2489
2633
  });
2490
2634
  (0, import_invariant6.invariant)(topology, void 0, {
2491
- F: __dxlog_file7,
2635
+ F: __dxlog_file8,
2492
2636
  L: 159,
2493
2637
  S: this,
2494
2638
  A: [
@@ -2497,7 +2641,7 @@ var NetworkManager = class {
2497
2641
  ]
2498
2642
  });
2499
2643
  (0, import_invariant6.invariant)(typeof protocol === "function", void 0, {
2500
- F: __dxlog_file7,
2644
+ F: __dxlog_file8,
2501
2645
  L: 160,
2502
2646
  S: this,
2503
2647
  A: [
@@ -2506,24 +2650,24 @@ var NetworkManager = class {
2506
2650
  ]
2507
2651
  });
2508
2652
  if (this._swarms.has(topic)) {
2509
- throw new Error(`Already connected to swarm: ${import_keys8.PublicKey.from(topic)}`);
2653
+ throw new Error(`Already connected to swarm: ${import_keys9.PublicKey.from(topic)}`);
2510
2654
  }
2511
- (0, import_log7.log)("joining", {
2512
- topic: import_keys8.PublicKey.from(topic),
2655
+ (0, import_log8.log)("joining", {
2656
+ topic: import_keys9.PublicKey.from(topic),
2513
2657
  peerId,
2514
2658
  topology: topology.toString()
2515
2659
  }, {
2516
- F: __dxlog_file7,
2660
+ F: __dxlog_file8,
2517
2661
  L: 165,
2518
2662
  S: this,
2519
2663
  C: (f, a) => f(...a)
2520
2664
  });
2521
2665
  const swarm = new Swarm(topic, peerId, topology, protocol, this._messenger, this._transportFactory, label, this._connectionLimiter);
2522
2666
  swarm.errors.handle((error) => {
2523
- (0, import_log7.log)("swarm error", {
2667
+ (0, import_log8.log)("swarm error", {
2524
2668
  error
2525
2669
  }, {
2526
- F: __dxlog_file7,
2670
+ F: __dxlog_file8,
2527
2671
  L: 178,
2528
2672
  S: this,
2529
2673
  C: (f, a) => f(...a)
@@ -2535,19 +2679,19 @@ var NetworkManager = class {
2535
2679
  this._signalConnection.join({
2536
2680
  topic,
2537
2681
  peerId
2538
- }).catch((error) => import_log7.log.catch(error, void 0, {
2539
- F: __dxlog_file7,
2682
+ }).catch((error) => import_log8.log.catch(error, void 0, {
2683
+ F: __dxlog_file8,
2540
2684
  L: 187,
2541
2685
  S: this,
2542
2686
  C: (f, a) => f(...a)
2543
2687
  }));
2544
2688
  this.topicsUpdated.emit();
2545
2689
  this._connectionLog?.joinedSwarm(swarm);
2546
- (0, import_log7.log)("joined", {
2547
- topic: import_keys8.PublicKey.from(topic),
2690
+ (0, import_log8.log)("joined", {
2691
+ topic: import_keys9.PublicKey.from(topic),
2548
2692
  count: this._swarms.size
2549
2693
  }, {
2550
- F: __dxlog_file7,
2694
+ F: __dxlog_file8,
2551
2695
  L: 191,
2552
2696
  S: this,
2553
2697
  C: (f, a) => f(...a)
@@ -2563,10 +2707,10 @@ var NetworkManager = class {
2563
2707
  if (!this._swarms.has(topic)) {
2564
2708
  return;
2565
2709
  }
2566
- (0, import_log7.log)("leaving", {
2567
- topic: import_keys8.PublicKey.from(topic)
2710
+ (0, import_log8.log)("leaving", {
2711
+ topic: import_keys9.PublicKey.from(topic)
2568
2712
  }, {
2569
- F: __dxlog_file7,
2713
+ F: __dxlog_file8,
2570
2714
  L: 207,
2571
2715
  S: this,
2572
2716
  C: (f, a) => f(...a)
@@ -2583,11 +2727,11 @@ var NetworkManager = class {
2583
2727
  await swarm.destroy();
2584
2728
  this._swarms.delete(topic);
2585
2729
  await this.topicsUpdated.emit();
2586
- (0, import_log7.log)("left", {
2587
- topic: import_keys8.PublicKey.from(topic),
2730
+ (0, import_log8.log)("left", {
2731
+ topic: import_keys9.PublicKey.from(topic),
2588
2732
  count: this._swarms.size
2589
2733
  }, {
2590
- F: __dxlog_file7,
2734
+ F: __dxlog_file8,
2591
2735
  L: 221,
2592
2736
  S: this,
2593
2737
  C: (f, a) => f(...a)
@@ -2623,14 +2767,14 @@ var NetworkManager = class {
2623
2767
 
2624
2768
  // packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts
2625
2769
  var import_invariant7 = require("@dxos/invariant");
2626
- var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
2770
+ var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
2627
2771
  var FullyConnectedTopology = class {
2628
2772
  toString() {
2629
2773
  return "FullyConnectedTopology";
2630
2774
  }
2631
2775
  init(controller) {
2632
2776
  (0, import_invariant7.invariant)(!this._controller, "Already initialized", {
2633
- F: __dxlog_file8,
2777
+ F: __dxlog_file9,
2634
2778
  L: 18,
2635
2779
  S: this,
2636
2780
  A: [
@@ -2642,7 +2786,7 @@ var FullyConnectedTopology = class {
2642
2786
  }
2643
2787
  update() {
2644
2788
  (0, import_invariant7.invariant)(this._controller, "Not initialized", {
2645
- F: __dxlog_file8,
2789
+ F: __dxlog_file9,
2646
2790
  L: 23,
2647
2791
  S: this,
2648
2792
  A: [
@@ -2665,20 +2809,20 @@ var FullyConnectedTopology = class {
2665
2809
  // packages/core/mesh/network-manager/src/topology/mmst-topology.ts
2666
2810
  var import_xor_distance = __toESM(require("xor-distance"));
2667
2811
  var import_invariant8 = require("@dxos/invariant");
2668
- var import_log8 = require("@dxos/log");
2812
+ var import_log9 = require("@dxos/log");
2669
2813
 
2670
2814
  // packages/core/mesh/network-manager/src/topology/star-topology.ts
2671
2815
  var import_invariant9 = require("@dxos/invariant");
2672
- var import_log9 = require("@dxos/log");
2816
+ var import_log10 = require("@dxos/log");
2673
2817
 
2674
2818
  // packages/core/mesh/network-manager/src/transport/memory-transport.ts
2675
2819
  var import_node_stream = require("node:stream");
2676
- var import_async8 = require("@dxos/async");
2820
+ var import_async9 = require("@dxos/async");
2677
2821
  var import_debug4 = require("@dxos/debug");
2678
2822
  var import_invariant10 = require("@dxos/invariant");
2679
- var import_keys9 = require("@dxos/keys");
2680
- var import_log10 = require("@dxos/log");
2681
- var import_util7 = require("@dxos/util");
2823
+ var import_keys10 = require("@dxos/keys");
2824
+ var import_log11 = require("@dxos/log");
2825
+ var import_util8 = require("@dxos/util");
2682
2826
  function _ts_decorate4(decorators, target, key, desc) {
2683
2827
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2684
2828
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -2689,7 +2833,7 @@ function _ts_decorate4(decorators, target, key, desc) {
2689
2833
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2690
2834
  return c > 3 && r && Object.defineProperty(target, key, r), r;
2691
2835
  }
2692
- var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
2836
+ var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
2693
2837
  var MEMORY_TRANSPORT_DELAY = 1;
2694
2838
  var createStreamDelay = (delay) => {
2695
2839
  return new import_node_stream.Transform({
@@ -2705,26 +2849,26 @@ var MemoryTransportFactory = {
2705
2849
  var MemoryTransport = class _MemoryTransport {
2706
2850
  static {
2707
2851
  // TODO(burdon): Remove static properties (inject context into constructor).
2708
- this._connections = new import_util7.ComplexMap(import_keys9.PublicKey.hash);
2852
+ this._connections = new import_util8.ComplexMap(import_keys10.PublicKey.hash);
2709
2853
  }
2710
2854
  constructor(options) {
2711
2855
  this.options = options;
2712
- this.closed = new import_async8.Event();
2713
- this.connected = new import_async8.Event();
2856
+ this.closed = new import_async9.Event();
2857
+ this.connected = new import_async9.Event();
2714
2858
  this.errors = new import_debug4.ErrorStream();
2715
- this._instanceId = import_keys9.PublicKey.random();
2716
- this._remote = new import_async8.Trigger();
2859
+ this._instanceId = import_keys10.PublicKey.random();
2860
+ this._remote = new import_async9.Trigger();
2717
2861
  this._outgoingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
2718
2862
  this._incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
2719
2863
  this._destroyed = false;
2720
- (0, import_log10.log)("creating", void 0, {
2721
- F: __dxlog_file9,
2864
+ (0, import_log11.log)("creating", void 0, {
2865
+ F: __dxlog_file10,
2722
2866
  L: 64,
2723
2867
  S: this,
2724
2868
  C: (f, a) => f(...a)
2725
2869
  });
2726
2870
  (0, import_invariant10.invariant)(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection", {
2727
- F: __dxlog_file9,
2871
+ F: __dxlog_file10,
2728
2872
  L: 66,
2729
2873
  S: this,
2730
2874
  A: [
@@ -2735,8 +2879,8 @@ var MemoryTransport = class _MemoryTransport {
2735
2879
  _MemoryTransport._connections.set(this._instanceId, this);
2736
2880
  if (this.options.initiator) {
2737
2881
  setTimeout(async () => {
2738
- (0, import_log10.log)("sending signal", void 0, {
2739
- F: __dxlog_file9,
2882
+ (0, import_log11.log)("sending signal", void 0, {
2883
+ F: __dxlog_file10,
2740
2884
  L: 73,
2741
2885
  S: this,
2742
2886
  C: (f, a) => f(...a)
@@ -2766,7 +2910,7 @@ var MemoryTransport = class _MemoryTransport {
2766
2910
  return;
2767
2911
  }
2768
2912
  (0, import_invariant10.invariant)(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`, {
2769
- F: __dxlog_file9,
2913
+ F: __dxlog_file10,
2770
2914
  L: 99,
2771
2915
  S: this,
2772
2916
  A: [
@@ -2776,8 +2920,8 @@ var MemoryTransport = class _MemoryTransport {
2776
2920
  });
2777
2921
  this._remoteConnection._remoteConnection = this;
2778
2922
  this._remoteConnection._remoteInstanceId = this._instanceId;
2779
- (0, import_log10.log)("connected", void 0, {
2780
- F: __dxlog_file9,
2923
+ (0, import_log11.log)("connected", void 0, {
2924
+ F: __dxlog_file10,
2781
2925
  L: 103,
2782
2926
  S: this,
2783
2927
  C: (f, a) => f(...a)
@@ -2794,8 +2938,8 @@ var MemoryTransport = class _MemoryTransport {
2794
2938
  }
2795
2939
  }
2796
2940
  async destroy() {
2797
- (0, import_log10.log)("closing", void 0, {
2798
- F: __dxlog_file9,
2941
+ (0, import_log11.log)("closing", void 0, {
2942
+ F: __dxlog_file10,
2799
2943
  L: 124,
2800
2944
  S: this,
2801
2945
  C: (f, a) => f(...a)
@@ -2803,8 +2947,8 @@ var MemoryTransport = class _MemoryTransport {
2803
2947
  this._destroyed = true;
2804
2948
  _MemoryTransport._connections.delete(this._instanceId);
2805
2949
  if (this._remoteConnection) {
2806
- (0, import_log10.log)("closing", void 0, {
2807
- F: __dxlog_file9,
2950
+ (0, import_log11.log)("closing", void 0, {
2951
+ F: __dxlog_file10,
2808
2952
  L: 129,
2809
2953
  S: this,
2810
2954
  C: (f, a) => f(...a)
@@ -2816,26 +2960,26 @@ var MemoryTransport = class _MemoryTransport {
2816
2960
  this._remoteConnection.closed.emit();
2817
2961
  this._remoteConnection._remoteConnection = void 0;
2818
2962
  this._remoteConnection = void 0;
2819
- (0, import_log10.log)("closed", void 0, {
2820
- F: __dxlog_file9,
2963
+ (0, import_log11.log)("closed", void 0, {
2964
+ F: __dxlog_file10,
2821
2965
  L: 147,
2822
2966
  S: this,
2823
2967
  C: (f, a) => f(...a)
2824
2968
  });
2825
2969
  }
2826
2970
  this.closed.emit();
2827
- (0, import_log10.log)("closed", void 0, {
2828
- F: __dxlog_file9,
2971
+ (0, import_log11.log)("closed", void 0, {
2972
+ F: __dxlog_file10,
2829
2973
  L: 151,
2830
2974
  S: this,
2831
2975
  C: (f, a) => f(...a)
2832
2976
  });
2833
2977
  }
2834
2978
  signal({ payload }) {
2835
- (0, import_log10.log)("received signal", {
2979
+ (0, import_log11.log)("received signal", {
2836
2980
  payload
2837
2981
  }, {
2838
- F: __dxlog_file9,
2982
+ F: __dxlog_file10,
2839
2983
  L: 155,
2840
2984
  S: this,
2841
2985
  C: (f, a) => f(...a)
@@ -2845,16 +2989,16 @@ var MemoryTransport = class _MemoryTransport {
2845
2989
  }
2846
2990
  const transportId = payload.transportId;
2847
2991
  if (transportId) {
2848
- const remoteId = import_keys9.PublicKey.fromHex(transportId);
2992
+ const remoteId = import_keys10.PublicKey.fromHex(transportId);
2849
2993
  this._remote.wake(remoteId);
2850
2994
  }
2851
2995
  }
2852
2996
  };
2853
2997
  _ts_decorate4([
2854
- import_log10.logInfo
2998
+ import_log11.logInfo
2855
2999
  ], MemoryTransport.prototype, "_instanceId", void 0);
2856
3000
  _ts_decorate4([
2857
- import_log10.logInfo
3001
+ import_log11.logInfo
2858
3002
  ], MemoryTransport.prototype, "_remoteInstanceId", void 0);
2859
3003
 
2860
3004
  // packages/core/mesh/network-manager/src/transport/transport.ts
@@ -2864,16 +3008,17 @@ var TransportKind;
2864
3008
  TransportKind2["LIBDATACHANNEL"] = "LIBDATACHANNEL";
2865
3009
  TransportKind2["SIMPLE_PEER_PROXY"] = "SIMPLE_PEER_PROXY";
2866
3010
  TransportKind2["MEMORY"] = "MEMORY";
3011
+ TransportKind2["TCP"] = "TCP";
2867
3012
  })(TransportKind || (TransportKind = {}));
2868
3013
 
2869
3014
  // packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts
2870
3015
  var import_simple_peer = __toESM(require("simple-peer"));
2871
3016
  var import_tiny_invariant = __toESM(require("tiny-invariant"));
2872
- var import_async9 = require("@dxos/async");
3017
+ var import_async10 = require("@dxos/async");
2873
3018
  var import_debug5 = require("@dxos/debug");
2874
- var import_keys10 = require("@dxos/keys");
2875
- var import_log11 = require("@dxos/log");
2876
- var import_protocols5 = require("@dxos/protocols");
3019
+ var import_keys11 = require("@dxos/keys");
3020
+ var import_log12 = require("@dxos/log");
3021
+ var import_protocols7 = require("@dxos/protocols");
2877
3022
 
2878
3023
  // packages/core/mesh/network-manager/src/transport/webrtc.ts
2879
3024
  var wrtc = null;
@@ -2883,26 +3028,26 @@ try {
2883
3028
  }
2884
3029
 
2885
3030
  // packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts
2886
- var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts";
3031
+ var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts";
2887
3032
  var SimplePeerTransport = class {
2888
3033
  constructor(params) {
2889
3034
  this.params = params;
2890
3035
  this._closed = false;
2891
- this.closed = new import_async9.Event();
2892
- this.connected = new import_async9.Event();
3036
+ this.closed = new import_async10.Event();
3037
+ this.connected = new import_async10.Event();
2893
3038
  this.errors = new import_debug5.ErrorStream();
2894
- this._instanceId = import_keys10.PublicKey.random().toHex();
2895
- import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.begin({
3039
+ this._instanceId = import_keys11.PublicKey.random().toHex();
3040
+ import_log12.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols7.trace.begin({
2896
3041
  id: this._instanceId
2897
3042
  }), {
2898
- F: __dxlog_file10,
2899
- L: 39,
3043
+ F: __dxlog_file11,
3044
+ L: 40,
2900
3045
  S: this,
2901
3046
  C: (f, a) => f(...a)
2902
3047
  });
2903
- (0, import_log11.log)("created connection", params, {
2904
- F: __dxlog_file10,
2905
- L: 40,
3048
+ (0, import_log12.log)("created connection", params, {
3049
+ F: __dxlog_file11,
3050
+ L: 41,
2906
3051
  S: this,
2907
3052
  C: (f, a) => f(...a)
2908
3053
  });
@@ -2913,9 +3058,9 @@ var SimplePeerTransport = class {
2913
3058
  config: this.params.webrtcConfig
2914
3059
  });
2915
3060
  this._peer.on("signal", async (data) => {
2916
- (0, import_log11.log)("signal", data, {
2917
- F: __dxlog_file10,
2918
- L: 49,
3061
+ (0, import_log12.log)("signal", data, {
3062
+ F: __dxlog_file11,
3063
+ L: 50,
2919
3064
  S: this,
2920
3065
  C: (f, a) => f(...a)
2921
3066
  });
@@ -2926,9 +3071,9 @@ var SimplePeerTransport = class {
2926
3071
  });
2927
3072
  });
2928
3073
  this._peer.on("connect", () => {
2929
- (0, import_log11.log)("connected", void 0, {
2930
- F: __dxlog_file10,
2931
- L: 54,
3074
+ (0, import_log12.log)("connected", void 0, {
3075
+ F: __dxlog_file11,
3076
+ L: 55,
2932
3077
  S: this,
2933
3078
  C: (f, a) => f(...a)
2934
3079
  });
@@ -2936,9 +3081,9 @@ var SimplePeerTransport = class {
2936
3081
  this.connected.emit();
2937
3082
  });
2938
3083
  this._peer.on("close", async () => {
2939
- (0, import_log11.log)("closed", void 0, {
2940
- F: __dxlog_file10,
2941
- L: 60,
3084
+ (0, import_log12.log)("closed", void 0, {
3085
+ F: __dxlog_file11,
3086
+ L: 61,
2942
3087
  S: this,
2943
3088
  C: (f, a) => f(...a)
2944
3089
  });
@@ -2946,38 +3091,82 @@ var SimplePeerTransport = class {
2946
3091
  this.closed.emit();
2947
3092
  });
2948
3093
  this._peer.on("error", async (err) => {
2949
- this.errors.raise(err);
3094
+ if (typeof RTCError !== "undefined" && err instanceof RTCError) {
3095
+ if (err.errorDetail === "sctp-failure") {
3096
+ this.errors.raise(new import_protocols7.ConnectionResetError("sctp-failure from RTCError", err));
3097
+ } else {
3098
+ this.errors.raise(new import_protocols7.UnknownProtocolError("unknown RTCError", err));
3099
+ }
3100
+ } else if ("code" in err) {
3101
+ import_log12.log.info("simple-peer error", err, {
3102
+ F: __dxlog_file11,
3103
+ L: 77,
3104
+ S: this,
3105
+ C: (f, a) => f(...a)
3106
+ });
3107
+ switch (err.code) {
3108
+ case "ERR_WEBRTC_SUPPORT":
3109
+ this.errors.raise(new import_protocols7.ProtocolError("WebRTC not supported", err));
3110
+ case "ERR_ICE_CONNECTION_FAILURE":
3111
+ case "ERR_DATA_CHANNEL":
3112
+ case "ERR_CONNECTION_FAILURE":
3113
+ case "ERR_SIGNALING":
3114
+ this.errors.raise(new import_protocols7.ConnectivityError("unknown communication failure", err));
3115
+ case "ERR_CREATE_OFFER":
3116
+ case "ERR_CREATE_ANSWER":
3117
+ case "ERR_SET_LOCAL_DESCRIPTION":
3118
+ case "ERR_SET_REMOTE_DESCRIPTION":
3119
+ case "ERR_ADD_ICE_CANDIDATE":
3120
+ this.errors.raise(new import_protocols7.UnknownProtocolError("unknown simple-peer library failure", err));
3121
+ default:
3122
+ this.errors.raise(new Error("unknown simple-peer error"));
3123
+ }
3124
+ } else {
3125
+ import_log12.log.info("unknown peer connection error", err, {
3126
+ F: __dxlog_file11,
3127
+ L: 96,
3128
+ S: this,
3129
+ C: (f, a) => f(...a)
3130
+ });
3131
+ this.errors.raise(err);
3132
+ }
2950
3133
  try {
2951
- if (typeof this._peer?._pc.getStats === "function") {
3134
+ if (typeof this._peer?._pc?.getStats === "function") {
2952
3135
  this._peer._pc.getStats().then((stats) => {
2953
- import_log11.log.warn("report after webrtc error", {
3136
+ import_log12.log.warn("report after webrtc error", {
2954
3137
  config: this.params.webrtcConfig,
2955
3138
  stats
2956
3139
  }, {
2957
- F: __dxlog_file10,
2958
- L: 72,
3140
+ F: __dxlog_file11,
3141
+ L: 104,
2959
3142
  S: this,
2960
3143
  C: (f, a) => f(...a)
2961
3144
  });
2962
3145
  });
2963
3146
  }
2964
- } catch {
3147
+ } catch (err2) {
3148
+ import_log12.log.catch(err2, void 0, {
3149
+ F: __dxlog_file11,
3150
+ L: 111,
3151
+ S: this,
3152
+ C: (f, a) => f(...a)
3153
+ });
2965
3154
  }
2966
3155
  await this.destroy();
2967
3156
  });
2968
- import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.end({
3157
+ import_log12.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols7.trace.end({
2969
3158
  id: this._instanceId
2970
3159
  }), {
2971
- F: __dxlog_file10,
2972
- L: 82,
3160
+ F: __dxlog_file11,
3161
+ L: 116,
2973
3162
  S: this,
2974
3163
  C: (f, a) => f(...a)
2975
3164
  });
2976
3165
  }
2977
3166
  async destroy() {
2978
- (0, import_log11.log)("closing...", void 0, {
2979
- F: __dxlog_file10,
2980
- L: 86,
3167
+ (0, import_log12.log)("closing...", void 0, {
3168
+ F: __dxlog_file11,
3169
+ L: 120,
2981
3170
  S: this,
2982
3171
  C: (f, a) => f(...a)
2983
3172
  });
@@ -2985,9 +3174,9 @@ var SimplePeerTransport = class {
2985
3174
  await this._disconnectStreams();
2986
3175
  this._peer.destroy();
2987
3176
  this.closed.emit();
2988
- (0, import_log11.log)("closed", void 0, {
2989
- F: __dxlog_file10,
2990
- L: 91,
3177
+ (0, import_log12.log)("closed", void 0, {
3178
+ F: __dxlog_file11,
3179
+ L: 125,
2991
3180
  S: this,
2992
3181
  C: (f, a) => f(...a)
2993
3182
  });
@@ -3014,16 +3203,15 @@ var createSimplePeerTransportFactory = (webrtcConfig) => ({
3014
3203
  var import_node_stream2 = require("node:stream");
3015
3204
  var import_codec_protobuf = require("@dxos/codec-protobuf");
3016
3205
  var import_invariant11 = require("@dxos/invariant");
3017
- var import_keys11 = require("@dxos/keys");
3018
- var import_log12 = require("@dxos/log");
3206
+ var import_keys12 = require("@dxos/keys");
3207
+ var import_log13 = require("@dxos/log");
3019
3208
  var import_bridge = require("@dxos/protocols/proto/dxos/mesh/bridge");
3020
- var import_util8 = require("@dxos/util");
3021
- var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-service.ts";
3209
+ var import_util9 = require("@dxos/util");
3210
+ var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-service.ts";
3022
3211
  var SimplePeerTransportService = class {
3023
- // prettier-ignore
3024
3212
  constructor(_webrtcConfig) {
3025
3213
  this._webrtcConfig = _webrtcConfig;
3026
- this.transports = new import_util8.ComplexMap(import_keys11.PublicKey.hash);
3214
+ this.transports = new import_util9.ComplexMap(import_keys12.PublicKey.hash);
3027
3215
  }
3028
3216
  open(request) {
3029
3217
  const rpcStream = new import_codec_protobuf.Stream(({ ready, next, close }) => {
@@ -3099,8 +3287,8 @@ var SimplePeerTransportService = class {
3099
3287
  }
3100
3288
  async sendSignal({ proxyId, signal }) {
3101
3289
  (0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
3102
- F: __dxlog_file11,
3103
- L: 113,
3290
+ F: __dxlog_file12,
3291
+ L: 110,
3104
3292
  S: this,
3105
3293
  A: [
3106
3294
  "this.transports.has(proxyId)",
@@ -3111,8 +3299,8 @@ var SimplePeerTransportService = class {
3111
3299
  }
3112
3300
  async sendData({ proxyId, payload }) {
3113
3301
  (0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
3114
- F: __dxlog_file11,
3115
- L: 118,
3302
+ F: __dxlog_file12,
3303
+ L: 115,
3116
3304
  S: this,
3117
3305
  A: [
3118
3306
  "this.transports.has(proxyId)",
@@ -3131,9 +3319,9 @@ var SimplePeerTransportService = class {
3131
3319
  await this.transports.get(proxyId)?.transport.destroy();
3132
3320
  await this.transports.get(proxyId)?.stream.end();
3133
3321
  this.transports.delete(proxyId);
3134
- (0, import_log12.log)("Closed.", void 0, {
3135
- F: __dxlog_file11,
3136
- L: 132,
3322
+ (0, import_log13.log)("Closed.", void 0, {
3323
+ F: __dxlog_file12,
3324
+ L: 129,
3137
3325
  S: this,
3138
3326
  C: (f, a) => f(...a)
3139
3327
  });
@@ -3142,23 +3330,23 @@ var SimplePeerTransportService = class {
3142
3330
 
3143
3331
  // packages/core/mesh/network-manager/src/transport/simplepeer-transport-proxy.ts
3144
3332
  var import_node_stream3 = require("node:stream");
3145
- var import_async10 = require("@dxos/async");
3333
+ var import_async11 = require("@dxos/async");
3146
3334
  var import_context6 = require("@dxos/context");
3147
3335
  var import_debug6 = require("@dxos/debug");
3148
3336
  var import_invariant12 = require("@dxos/invariant");
3149
- var import_keys12 = require("@dxos/keys");
3150
- var import_log13 = require("@dxos/log");
3337
+ var import_keys13 = require("@dxos/keys");
3338
+ var import_log14 = require("@dxos/log");
3339
+ var import_protocols8 = require("@dxos/protocols");
3151
3340
  var import_bridge2 = require("@dxos/protocols/proto/dxos/mesh/bridge");
3152
- var import_util9 = require("@dxos/util");
3153
- var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-proxy.ts";
3341
+ var import_util10 = require("@dxos/util");
3342
+ var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-proxy.ts";
3154
3343
  var SimplePeerTransportProxy = class {
3155
- // prettier-ignore
3156
3344
  constructor(_params) {
3157
3345
  this._params = _params;
3158
- this._proxyId = import_keys12.PublicKey.random();
3346
+ this._proxyId = import_keys13.PublicKey.random();
3159
3347
  this._ctx = new import_context6.Context();
3160
- this.closed = new import_async10.Event();
3161
- this.connected = new import_async10.Event();
3348
+ this.closed = new import_async11.Event();
3349
+ this.connected = new import_async11.Event();
3162
3350
  this.errors = new import_debug6.ErrorStream();
3163
3351
  this._closed = false;
3164
3352
  this._serviceStream = this._params.bridgeService.open({
@@ -3167,9 +3355,9 @@ var SimplePeerTransportProxy = class {
3167
3355
  });
3168
3356
  this._serviceStream.waitUntilReady().then(() => {
3169
3357
  this._serviceStream.subscribe(async (event) => {
3170
- (0, import_log13.log)("SimplePeerTransportProxy: event", event, {
3171
- F: __dxlog_file12,
3172
- L: 51,
3358
+ (0, import_log14.log)("SimplePeerTransportProxy: event", event, {
3359
+ F: __dxlog_file13,
3360
+ L: 55,
3173
3361
  S: this,
3174
3362
  C: (f, a) => f(...a)
3175
3363
  });
@@ -3187,25 +3375,25 @@ var SimplePeerTransportProxy = class {
3187
3375
  proxyId: this._proxyId,
3188
3376
  payload: chunk
3189
3377
  }).then(() => callback(), (err) => {
3190
- import_log13.log.catch(err, void 0, {
3191
- F: __dxlog_file12,
3192
- L: 69,
3378
+ import_log14.log.catch(err, void 0, {
3379
+ F: __dxlog_file13,
3380
+ L: 76,
3193
3381
  S: this,
3194
3382
  C: (f, a) => f(...a)
3195
3383
  });
3196
3384
  });
3197
3385
  }
3198
3386
  }));
3199
- }, (error) => import_log13.log.catch(error, void 0, {
3200
- F: __dxlog_file12,
3201
- L: 75,
3387
+ }, (error) => import_log14.log.catch(error, void 0, {
3388
+ F: __dxlog_file13,
3389
+ L: 83,
3202
3390
  S: this,
3203
3391
  C: (f, a) => f(...a)
3204
3392
  }));
3205
3393
  }
3206
3394
  async _handleConnection(connectionEvent) {
3207
3395
  if (connectionEvent.error) {
3208
- this.errors.raise(new Error(connectionEvent.error));
3396
+ this.errors.raise(decodeError(connectionEvent.error));
3209
3397
  }
3210
3398
  switch (connectionEvent.state) {
3211
3399
  case import_bridge2.ConnectionState.CONNECTED: {
@@ -3219,7 +3407,7 @@ var SimplePeerTransportProxy = class {
3219
3407
  }
3220
3408
  }
3221
3409
  _handleData(dataEvent) {
3222
- this._params.stream.write((0, import_util9.arrayToBuffer)(dataEvent.payload));
3410
+ this._params.stream.write((0, import_util10.arrayToBuffer)(dataEvent.payload));
3223
3411
  }
3224
3412
  async _handleSignal(signalEvent) {
3225
3413
  await this._params.sendSignal(signalEvent.payload);
@@ -3228,7 +3416,7 @@ var SimplePeerTransportProxy = class {
3228
3416
  this._params.bridgeService.sendSignal({
3229
3417
  proxyId: this._proxyId,
3230
3418
  signal
3231
- }).catch((err) => this.errors.raise(err));
3419
+ }).catch((err) => this.errors.raise(decodeError(err)));
3232
3420
  }
3233
3421
  // TODO(burdon): Move open from constructor.
3234
3422
  async destroy() {
@@ -3242,9 +3430,9 @@ var SimplePeerTransportProxy = class {
3242
3430
  proxyId: this._proxyId
3243
3431
  });
3244
3432
  } catch (err) {
3245
- import_log13.log.catch(err, void 0, {
3246
- F: __dxlog_file12,
3247
- L: 126,
3433
+ import_log14.log.catch(err, void 0, {
3434
+ F: __dxlog_file13,
3435
+ L: 134,
3248
3436
  S: this,
3249
3437
  C: (f, a) => f(...a)
3250
3438
  });
@@ -3279,8 +3467,8 @@ var SimplePeerTransportProxyFactory = class {
3279
3467
  }
3280
3468
  createTransport(options) {
3281
3469
  (0, import_invariant12.invariant)(this._bridgeService, "SimplePeerTransportProxyFactory is not ready to open connections", {
3282
- F: __dxlog_file12,
3283
- L: 163,
3470
+ F: __dxlog_file13,
3471
+ L: 171,
3284
3472
  S: this,
3285
3473
  A: [
3286
3474
  "this._bridgeService",
@@ -3296,15 +3484,31 @@ var SimplePeerTransportProxyFactory = class {
3296
3484
  return transport;
3297
3485
  }
3298
3486
  };
3487
+ var decodeError = (err) => {
3488
+ const message = typeof err === "string" ? err : err.message;
3489
+ if (message.includes("CONNECTION_RESET")) {
3490
+ return new import_protocols8.ConnectionResetError(message);
3491
+ } else if (message.includes("TIMEOUT")) {
3492
+ return new import_protocols8.TimeoutError(message);
3493
+ } else if (message.includes("PROTOCOL_ERROR")) {
3494
+ return new import_protocols8.ProtocolError(message);
3495
+ } else if (message.includes("CONNECTIVITY_ERROR")) {
3496
+ return new import_protocols8.ConnectivityError(message);
3497
+ } else if (message.includes("UNKNOWN_PROTOCOL_ERROR")) {
3498
+ return new import_protocols8.UnknownProtocolError(message);
3499
+ } else {
3500
+ return typeof err === "string" ? new Error(err) : err;
3501
+ }
3502
+ };
3299
3503
 
3300
3504
  // packages/core/mesh/network-manager/src/transport/index.ts
3301
3505
  init_datachannel();
3302
3506
 
3303
3507
  // packages/core/mesh/network-manager/src/transport/libdatachannel-transport.ts
3304
3508
  var import_stream = require("stream");
3305
- var import_async11 = require("@dxos/async");
3509
+ var import_async12 = require("@dxos/async");
3306
3510
  var import_debug7 = require("@dxos/debug");
3307
- var import_log14 = require("@dxos/log");
3511
+ var import_log15 = require("@dxos/log");
3308
3512
  function _ts_decorate5(decorators, target, key, desc) {
3309
3513
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3310
3514
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -3315,7 +3519,7 @@ function _ts_decorate5(decorators, target, key, desc) {
3315
3519
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
3316
3520
  return c > 3 && r && Object.defineProperty(target, key, r), r;
3317
3521
  }
3318
- var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/libdatachannel-transport.ts";
3522
+ var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/libdatachannel-transport.ts";
3319
3523
  var DATACHANNEL_LABEL = "dxos.mesh.transport";
3320
3524
  var MAX_BUFFERED_AMOUNT = 64 * 1024;
3321
3525
  var MAX_MESSAGE_SIZE = 64 * 1024;
@@ -3323,11 +3527,11 @@ var LibDataChannelTransport = class {
3323
3527
  constructor(params) {
3324
3528
  this.params = params;
3325
3529
  this._closed = false;
3326
- this.closed = new import_async11.Event();
3530
+ this.closed = new import_async12.Event();
3327
3531
  this._connected = false;
3328
- this.connected = new import_async11.Event();
3532
+ this.connected = new import_async12.Event();
3329
3533
  this.errors = new import_debug7.ErrorStream();
3330
- this._readyForCandidates = new import_async11.Trigger();
3534
+ this._readyForCandidates = new import_async12.Trigger();
3331
3535
  this._writeCallback = null;
3332
3536
  this._peer = (async () => {
3333
3537
  const { RTCPeerConnection: PeerConnection2 } = await Promise.resolve().then(() => (init_datachannel(), datachannel_exports));
@@ -3336,32 +3540,32 @@ var LibDataChannelTransport = class {
3336
3540
  }
3337
3541
  const peer = new PeerConnection2(params.webrtcConfig);
3338
3542
  peer.onicecandidateerror = (event) => {
3339
- import_log14.log.error("peer.onicecandidateerror", {
3543
+ import_log15.log.error("peer.onicecandidateerror", {
3340
3544
  event
3341
3545
  }, {
3342
- F: __dxlog_file13,
3546
+ F: __dxlog_file14,
3343
3547
  L: 53,
3344
3548
  S: this,
3345
3549
  C: (f, a) => f(...a)
3346
3550
  });
3347
3551
  };
3348
3552
  peer.onconnectionstatechange = (event) => {
3349
- import_log14.log.debug("peer.onconnectionstatechange", {
3553
+ import_log15.log.debug("peer.onconnectionstatechange", {
3350
3554
  event,
3351
3555
  peerConnectionState: peer.connectionState,
3352
3556
  transportConnectionState: this._connected
3353
3557
  }, {
3354
- F: __dxlog_file13,
3558
+ F: __dxlog_file14,
3355
3559
  L: 57,
3356
3560
  S: this,
3357
3561
  C: (f, a) => f(...a)
3358
3562
  });
3359
3563
  };
3360
3564
  peer.onicecandidate = async (event) => {
3361
- import_log14.log.debug("peer.onicecandidate", {
3565
+ import_log15.log.debug("peer.onicecandidate", {
3362
3566
  event
3363
3567
  }, {
3364
- F: __dxlog_file13,
3568
+ F: __dxlog_file14,
3365
3569
  L: 66,
3366
3570
  S: this,
3367
3571
  C: (f, a) => f(...a)
@@ -3382,10 +3586,10 @@ var LibDataChannelTransport = class {
3382
3586
  }
3383
3587
  });
3384
3588
  } catch (err) {
3385
- import_log14.log.warn("signaling errror", {
3589
+ import_log15.log.warn("signaling errror", {
3386
3590
  err
3387
3591
  }, {
3388
- F: __dxlog_file13,
3592
+ F: __dxlog_file14,
3389
3593
  L: 83,
3390
3594
  S: this,
3391
3595
  C: (f, a) => f(...a)
@@ -3396,20 +3600,20 @@ var LibDataChannelTransport = class {
3396
3600
  if (params.initiator) {
3397
3601
  peer.createOffer().then(async (offer) => {
3398
3602
  if (peer.connectionState !== "connecting") {
3399
- import_log14.log.error("i am initiator but peer not in state connecting", {
3603
+ import_log15.log.error("i am initiator but peer not in state connecting", {
3400
3604
  peer
3401
3605
  }, {
3402
- F: __dxlog_file13,
3606
+ F: __dxlog_file14,
3403
3607
  L: 92,
3404
3608
  S: this,
3405
3609
  C: (f, a) => f(...a)
3406
3610
  });
3407
3611
  this.errors.raise(new Error("invalid state: peer is initiator, but other peer not in state connecting"));
3408
3612
  }
3409
- import_log14.log.debug(`im the initiator, creating offer, peer is in state ${peer.connectionState}`, {
3613
+ import_log15.log.debug(`im the initiator, creating offer, peer is in state ${peer.connectionState}`, {
3410
3614
  offer
3411
3615
  }, {
3412
- F: __dxlog_file13,
3616
+ F: __dxlog_file14,
3413
3617
  L: 95,
3414
3618
  S: this,
3415
3619
  C: (f, a) => f(...a)
@@ -3428,8 +3632,8 @@ var LibDataChannelTransport = class {
3428
3632
  this.errors.raise(err);
3429
3633
  });
3430
3634
  this.handleChannel(peer.createDataChannel(DATACHANNEL_LABEL));
3431
- import_log14.log.debug("created data channel", void 0, {
3432
- F: __dxlog_file13,
3635
+ import_log15.log.debug("created data channel", void 0, {
3636
+ F: __dxlog_file14,
3433
3637
  L: 104,
3434
3638
  S: this,
3435
3639
  C: (f, a) => f(...a)
@@ -3439,10 +3643,10 @@ var LibDataChannelTransport = class {
3439
3643
  };
3440
3644
  } else {
3441
3645
  peer.ondatachannel = (event) => {
3442
- import_log14.log.debug("peer.ondatachannel (non-initiator)", {
3646
+ import_log15.log.debug("peer.ondatachannel (non-initiator)", {
3443
3647
  event
3444
3648
  }, {
3445
- F: __dxlog_file13,
3649
+ F: __dxlog_file14,
3446
3650
  L: 110,
3447
3651
  S: this,
3448
3652
  C: (f, a) => f(...a)
@@ -3459,8 +3663,8 @@ var LibDataChannelTransport = class {
3459
3663
  handleChannel(dataChannel) {
3460
3664
  this._channel = dataChannel;
3461
3665
  this._channel.onopen = () => {
3462
- import_log14.log.debug("dataChannel.onopen", void 0, {
3463
- F: __dxlog_file13,
3666
+ import_log15.log.debug("dataChannel.onopen", void 0, {
3667
+ F: __dxlog_file14,
3464
3668
  L: 126,
3465
3669
  S: this,
3466
3670
  C: (f, a) => f(...a)
@@ -3475,8 +3679,8 @@ var LibDataChannelTransport = class {
3475
3679
  dataChannel.send(chunk);
3476
3680
  if (this._channel.bufferedAmount > MAX_BUFFERED_AMOUNT) {
3477
3681
  if (this._writeCallback !== null) {
3478
- import_log14.log.error("consumer trying to write before we're ready for more data", void 0, {
3479
- F: __dxlog_file13,
3682
+ import_log15.log.error("consumer trying to write before we're ready for more data", void 0, {
3683
+ F: __dxlog_file14,
3480
3684
  L: 139,
3481
3685
  S: this,
3482
3686
  C: (f, a) => f(...a)
@@ -3498,10 +3702,10 @@ var LibDataChannelTransport = class {
3498
3702
  await this._close();
3499
3703
  };
3500
3704
  this._channel.onclose = async (err) => {
3501
- import_log14.log.info("channel onclose", {
3705
+ import_log15.log.info("channel onclose", {
3502
3706
  err
3503
3707
  }, {
3504
- F: __dxlog_file13,
3708
+ F: __dxlog_file14,
3505
3709
  L: 159,
3506
3710
  S: this,
3507
3711
  C: (f, a) => f(...a)
@@ -3535,10 +3739,10 @@ var LibDataChannelTransport = class {
3535
3739
  switch (data.type) {
3536
3740
  case "offer": {
3537
3741
  if ((await this._peer).connectionState !== "new") {
3538
- import_log14.log.error("received offer but peer not in state new", {
3742
+ import_log15.log.error("received offer but peer not in state new", {
3539
3743
  peer
3540
3744
  }, {
3541
- F: __dxlog_file13,
3745
+ F: __dxlog_file14,
3542
3746
  L: 194,
3543
3747
  S: this,
3544
3748
  C: (f, a) => f(...a)
@@ -3563,10 +3767,10 @@ var LibDataChannelTransport = class {
3563
3767
  });
3564
3768
  this._readyForCandidates.wake();
3565
3769
  } catch (err) {
3566
- import_log14.log.error("can't handle offer from signalling server", {
3770
+ import_log15.log.error("can't handle offer from signalling server", {
3567
3771
  err
3568
3772
  }, {
3569
- F: __dxlog_file13,
3773
+ F: __dxlog_file14,
3570
3774
  L: 205,
3571
3775
  S: this,
3572
3776
  C: (f, a) => f(...a)
@@ -3583,10 +3787,10 @@ var LibDataChannelTransport = class {
3583
3787
  });
3584
3788
  this._readyForCandidates.wake();
3585
3789
  } catch (err) {
3586
- import_log14.log.error("can't handle answer from signalling server", {
3790
+ import_log15.log.error("can't handle answer from signalling server", {
3587
3791
  err
3588
3792
  }, {
3589
- F: __dxlog_file13,
3793
+ F: __dxlog_file14,
3590
3794
  L: 216,
3591
3795
  S: this,
3592
3796
  C: (f, a) => f(...a)
@@ -3601,11 +3805,11 @@ var LibDataChannelTransport = class {
3601
3805
  });
3602
3806
  break;
3603
3807
  default:
3604
- import_log14.log.error("unhandled signal type", {
3808
+ import_log15.log.error("unhandled signal type", {
3605
3809
  type: data.type,
3606
3810
  signal
3607
3811
  }, {
3608
- F: __dxlog_file13,
3812
+ F: __dxlog_file14,
3609
3813
  L: 227,
3610
3814
  S: this,
3611
3815
  C: (f, a) => f(...a)
@@ -3613,8 +3817,8 @@ var LibDataChannelTransport = class {
3613
3817
  this.errors.raise(new Error(`unhandled signal type ${data.type}`));
3614
3818
  }
3615
3819
  }).catch((err) => {
3616
- import_log14.log.catch(err, void 0, {
3617
- F: __dxlog_file13,
3820
+ import_log15.log.catch(err, void 0, {
3821
+ F: __dxlog_file14,
3618
3822
  L: 232,
3619
3823
  S: this,
3620
3824
  C: (f, a) => f(...a)
@@ -3630,7 +3834,7 @@ var LibDataChannelTransport = class {
3630
3834
  }
3631
3835
  };
3632
3836
  _ts_decorate5([
3633
- import_async11.synchronized
3837
+ import_async12.synchronized
3634
3838
  ], LibDataChannelTransport.prototype, "_close", null);
3635
3839
  var createLibDataChannelTransportFactory = (webrtcConfig) => ({
3636
3840
  createTransport: (params) => new LibDataChannelTransport({
@@ -3639,120 +3843,138 @@ var createLibDataChannelTransportFactory = (webrtcConfig) => ({
3639
3843
  })
3640
3844
  });
3641
3845
 
3642
- // packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts
3643
- var import_async12 = require("@dxos/async");
3644
- var import_keys13 = require("@dxos/keys");
3645
- var import_log15 = require("@dxos/log");
3646
- var import_teleport2 = require("@dxos/teleport");
3647
- var import_util10 = require("@dxos/util");
3648
-
3649
- // packages/core/mesh/network-manager/src/wire-protocol.ts
3650
- var import_teleport = require("@dxos/teleport");
3651
- var createTeleportProtocolFactory = (onConnection) => {
3652
- return (params) => {
3653
- const teleport = new import_teleport.Teleport(params);
3654
- return {
3655
- stream: teleport.stream,
3656
- open: async () => {
3657
- await teleport.open();
3658
- await onConnection(teleport);
3659
- },
3660
- close: async () => {
3661
- await teleport.close();
3662
- },
3663
- abort: async () => {
3664
- await teleport.abort();
3665
- }
3666
- };
3667
- };
3846
+ // packages/core/mesh/network-manager/src/transport/tcp-transport.ts
3847
+ var import_node_net = require("node:net");
3848
+ var import_async13 = require("@dxos/async");
3849
+ var import_debug8 = require("@dxos/debug");
3850
+ var import_log16 = require("@dxos/log");
3851
+ var __dxlog_file15 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/tcp-transport.ts";
3852
+ var TcpTransportFactory = {
3853
+ createTransport: (params) => new TcpTransport(params)
3668
3854
  };
3669
-
3670
- // packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts
3671
- var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts";
3672
- var TestWireProtocol = class {
3673
- constructor(peerId, _extensionFactory = () => []) {
3674
- this.peerId = peerId;
3675
- this._extensionFactory = _extensionFactory;
3676
- this.connections = new import_util10.ComplexMap(import_keys13.PublicKey.hash);
3677
- this.streamConnections = new import_util10.ComplexMap(import_keys13.PublicKey.hash);
3678
- this.connected = new import_async12.Event();
3679
- this.disconnected = new import_async12.Event();
3680
- this.otherConnections = new import_util10.ComplexMap(({ remotePeerId, extension }) => remotePeerId.toHex() + extension);
3681
- this.factory = createTeleportProtocolFactory(async (teleport) => {
3682
- (0, import_log15.log)("create", {
3683
- remotePeerId: teleport.remotePeerId
3684
- }, {
3685
- F: __dxlog_file14,
3686
- L: 38,
3687
- S: this,
3688
- C: (f, a) => f(...a)
3689
- });
3690
- const extension = new import_teleport2.TestExtension({
3691
- onClose: async () => {
3692
- this.connections.delete(teleport.remotePeerId);
3693
- this.disconnected.emit(teleport.remotePeerId);
3694
- }
3695
- });
3696
- this.connections.set(teleport.remotePeerId, extension);
3697
- teleport.addExtension("test", extension);
3698
- this.connected.emit(teleport.remotePeerId);
3699
- const streamExtension = new import_teleport2.TestExtensionWithStreams({
3700
- onClose: async () => {
3701
- this.streamConnections.delete(teleport.remotePeerId);
3702
- }
3855
+ var TcpTransport = class {
3856
+ constructor(options) {
3857
+ this.options = options;
3858
+ this._server = void 0;
3859
+ this._socket = void 0;
3860
+ this.closed = new import_async13.Event();
3861
+ this.connected = new import_async13.Event();
3862
+ this.errors = new import_debug8.ErrorStream();
3863
+ this._destroyed = false;
3864
+ this._connected = false;
3865
+ (0, import_log16.log)("creating", void 0, {
3866
+ F: __dxlog_file15,
3867
+ L: 34,
3868
+ S: this,
3869
+ C: (f, a) => f(...a)
3870
+ });
3871
+ if (this.options.initiator) {
3872
+ setTimeout(async () => {
3873
+ const { Server } = await import("node:net");
3874
+ this._server = new Server((socket) => {
3875
+ (0, import_log16.log)("new connection", void 0, {
3876
+ F: __dxlog_file15,
3877
+ L: 42,
3878
+ S: this,
3879
+ C: (f, a) => f(...a)
3880
+ });
3881
+ if (this._connected) {
3882
+ socket.destroy();
3883
+ }
3884
+ this._handleSocket(socket);
3885
+ });
3886
+ this._server.on("listening", () => {
3887
+ const { port: port2 } = this._server.address();
3888
+ (0, import_log16.log)("listening", {
3889
+ port: port2
3890
+ }, {
3891
+ F: __dxlog_file15,
3892
+ L: 51,
3893
+ S: this,
3894
+ C: (f, a) => f(...a)
3895
+ });
3896
+ void this.options.sendSignal({
3897
+ payload: {
3898
+ port: port2
3899
+ }
3900
+ }).catch((err) => {
3901
+ if (!this._destroyed) {
3902
+ this.errors.raise(err);
3903
+ }
3904
+ });
3905
+ });
3906
+ this._server.on("error", (err) => {
3907
+ this.errors.raise(err);
3908
+ });
3909
+ this._server.listen(0);
3703
3910
  });
3704
- this.streamConnections.set(teleport.remotePeerId, streamExtension);
3705
- teleport.addExtension("test-stream", streamExtension);
3706
- for (const { name, extension: extension2 } of this._extensionFactory()) {
3707
- this.otherConnections.set({
3708
- remotePeerId: teleport.remotePeerId,
3709
- extension: name
3710
- }, extension2);
3711
- teleport.addExtension(name, extension2);
3712
- }
3911
+ }
3912
+ }
3913
+ async destroy() {
3914
+ (0, import_log16.log)("closing", void 0, {
3915
+ F: __dxlog_file15,
3916
+ L: 72,
3917
+ S: this,
3918
+ C: (f, a) => f(...a)
3713
3919
  });
3920
+ this._socket?.destroy();
3921
+ this._server?.close();
3922
+ this._destroyed = true;
3714
3923
  }
3715
- async waitForConnection(peerId) {
3716
- if (this.connections.has(peerId)) {
3717
- return this.connections.get(peerId);
3718
- }
3719
- (0, import_log15.log)("waitForConnection", {
3720
- peerId
3924
+ signal({ payload }) {
3925
+ (0, import_log16.log)("received signal", {
3926
+ payload
3721
3927
  }, {
3722
- F: __dxlog_file14,
3723
- L: 67,
3928
+ F: __dxlog_file15,
3929
+ L: 80,
3724
3930
  S: this,
3725
3931
  C: (f, a) => f(...a)
3726
3932
  });
3727
- await (0, import_async12.asyncTimeout)(
3728
- this.connected.waitFor((connectedId) => connectedId.equals(peerId)),
3729
- // TODO(nf): Make this configurable.
3730
- 1e4
3731
- );
3732
- return this.connections.get(peerId);
3733
- }
3734
- async testConnection(peerId, message) {
3735
- const connection = await this.waitForConnection(peerId);
3736
- await connection.test(message);
3737
- }
3738
- async openStream(peerId, streamTag, streamLoadInterval, streamLoadChunkSize) {
3739
- if (!this.streamConnections.has(peerId)) {
3740
- throw new Error("Connection does not exist.");
3933
+ if (this.options.initiator || this._connected) {
3934
+ return;
3741
3935
  }
3742
- const connection = this.streamConnections.get(peerId);
3743
- return connection.addNewStream(streamLoadInterval, streamLoadChunkSize, streamTag);
3936
+ const socket = new import_node_net.Socket();
3937
+ this._handleSocket(socket);
3938
+ socket.connect({
3939
+ port: payload.port,
3940
+ host: "localhost"
3941
+ });
3744
3942
  }
3745
- async closeStream(peerId, streamTag) {
3746
- if (!this.streamConnections.has(peerId)) {
3747
- throw new Error("Connection does not exist.");
3748
- }
3749
- const connection = this.streamConnections.get(peerId);
3750
- return connection.closeStream(streamTag);
3943
+ _handleSocket(socket) {
3944
+ (0, import_log16.log)("handling socket", {
3945
+ remotePort: socket.remotePort,
3946
+ localPort: socket.localPort
3947
+ }, {
3948
+ F: __dxlog_file15,
3949
+ L: 91,
3950
+ S: this,
3951
+ C: (f, a) => f(...a)
3952
+ });
3953
+ this._socket = socket;
3954
+ this.connected.emit();
3955
+ this.options.stream.pipe(this._socket).pipe(this.options.stream);
3956
+ this._socket.on("connect", () => {
3957
+ (0, import_log16.log)("connected to", {
3958
+ port: this._socket?.remotePort
3959
+ }, {
3960
+ F: __dxlog_file15,
3961
+ L: 97,
3962
+ S: this,
3963
+ C: (f, a) => f(...a)
3964
+ });
3965
+ this._connected = true;
3966
+ });
3967
+ this._socket.on("error", (err) => {
3968
+ this.errors.raise(err);
3969
+ });
3970
+ this._socket.on("close", () => {
3971
+ this.closed.emit();
3972
+ });
3751
3973
  }
3752
3974
  };
3753
3975
 
3754
3976
  // packages/core/mesh/network-manager/src/testing/test-builder.ts
3755
- var __dxlog_file15 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/testing/test-builder.ts";
3977
+ var __dxlog_file16 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/testing/test-builder.ts";
3756
3978
  var port = process.env.SIGNAL_PORT ?? 4e3;
3757
3979
  var TEST_SIGNAL_HOSTS = [
3758
3980
  {
@@ -3794,15 +4016,18 @@ var TestPeer = class {
3794
4016
  createNetworkManager(transport) {
3795
4017
  let transportFactory;
3796
4018
  if (this.testBuilder.options.signalHosts) {
3797
- import_log16.log.info(`using ${transport} transport with signal server.`, void 0, {
3798
- F: __dxlog_file15,
3799
- L: 104,
4019
+ import_log17.log.info(`using ${transport} transport with signal server.`, void 0, {
4020
+ F: __dxlog_file16,
4021
+ L: 105,
3800
4022
  S: this,
3801
4023
  C: (f, a) => f(...a)
3802
4024
  });
3803
4025
  switch (transport) {
3804
4026
  case TransportKind.MEMORY:
3805
4027
  throw new Error("Memory transport not supported with signal server.");
4028
+ case TransportKind.TCP:
4029
+ transportFactory = TcpTransportFactory;
4030
+ break;
3806
4031
  case TransportKind.SIMPLE_PEER:
3807
4032
  transportFactory = createSimplePeerTransportFactory();
3808
4033
  break;
@@ -3815,7 +4040,7 @@ var TestPeer = class {
3815
4040
  this._proxy = (0, import_rpc.createProtoRpcPeer)({
3816
4041
  port: proxyPort,
3817
4042
  requested: {
3818
- BridgeService: import_protocols6.schema.getService("dxos.mesh.bridge.BridgeService")
4043
+ BridgeService: import_protocols9.schema.getService("dxos.mesh.bridge.BridgeService")
3819
4044
  },
3820
4045
  noHandshake: true,
3821
4046
  encodingOptions: {
@@ -3825,7 +4050,7 @@ var TestPeer = class {
3825
4050
  this._service = (0, import_rpc.createProtoRpcPeer)({
3826
4051
  port: servicePort,
3827
4052
  exposed: {
3828
- BridgeService: import_protocols6.schema.getService("dxos.mesh.bridge.BridgeService")
4053
+ BridgeService: import_protocols9.schema.getService("dxos.mesh.bridge.BridgeService")
3829
4054
  },
3830
4055
  handlers: {
3831
4056
  BridgeService: new SimplePeerTransportService()
@@ -3842,17 +4067,17 @@ var TestPeer = class {
3842
4067
  throw new Error(`Unsupported transport: ${transport}`);
3843
4068
  }
3844
4069
  } else {
3845
- if (transport !== TransportKind.MEMORY) {
3846
- import_log16.log.warn(`specified transport ${transport} but no signalling configured, using memory transport instead`, void 0, {
3847
- F: __dxlog_file15,
3848
- L: 150,
4070
+ if (transport !== TransportKind.MEMORY && transport !== TransportKind.TCP) {
4071
+ import_log17.log.warn(`specified transport ${transport} but no signalling configured, using memory transport instead`, void 0, {
4072
+ F: __dxlog_file16,
4073
+ L: 154,
3849
4074
  S: this,
3850
4075
  C: (f, a) => f(...a)
3851
4076
  });
3852
4077
  }
3853
- import_log16.log.info(`using ${transport} transport without signal server.`, void 0, {
3854
- F: __dxlog_file15,
3855
- L: 152,
4078
+ import_log17.log.info(`using ${transport} transport without signal server.`, void 0, {
4079
+ F: __dxlog_file16,
4080
+ L: 156,
3856
4081
  S: this,
3857
4082
  C: (f, a) => f(...a)
3858
4083
  });