@dxos/teleport 0.1.55-main.b01c908 → 0.1.55-main.de1d511

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.
@@ -1,5 +1,22 @@
1
1
  import "@dxos/node-std/globals";
2
2
 
3
+ // packages/core/mesh/teleport/src/testing/test-builder.ts
4
+ import { pipeline } from "@dxos/node-std/stream";
5
+ import invariant4 from "tiny-invariant";
6
+ import { PublicKey as PublicKey2 } from "@dxos/keys";
7
+ import { log as log3 } from "@dxos/log";
8
+
9
+ // packages/core/mesh/teleport/src/teleport.ts
10
+ import invariant3 from "tiny-invariant";
11
+ import { asyncTimeout, scheduleTaskInterval as scheduleTaskInterval2, runInContextAsync, synchronized, scheduleTask } from "@dxos/async";
12
+ import { Context as Context2 } from "@dxos/context";
13
+ import { failUndefined as failUndefined2 } from "@dxos/debug";
14
+ import { PublicKey } from "@dxos/keys";
15
+ import { log as log2 } from "@dxos/log";
16
+ import { schema as schema2, RpcClosedError } from "@dxos/protocols";
17
+ import { createProtoRpcPeer } from "@dxos/rpc";
18
+ import { Callback } from "@dxos/util";
19
+
3
20
  // packages/core/mesh/teleport/src/muxing/framer.ts
4
21
  import { Duplex } from "@dxos/node-std/stream";
5
22
  import invariant from "tiny-invariant";
@@ -318,11 +335,11 @@ var Muxer = class {
318
335
  return;
319
336
  }
320
337
  this._destroying = true;
321
- Promise.resolve(this._sendCommand({
338
+ this._sendCommand({
322
339
  destroy: {
323
340
  error: err == null ? void 0 : err.message
324
341
  }
325
- }, SYSTEM_CHANNEL_ID)).then(() => {
342
+ }, SYSTEM_CHANNEL_ID).then(() => {
326
343
  this._dispose();
327
344
  }).catch((err2) => {
328
345
  this._dispose(err2);
@@ -350,7 +367,7 @@ var Muxer = class {
350
367
  cmd
351
368
  }, {
352
369
  F: __dxlog_file,
353
- L: 239,
370
+ L: 237,
354
371
  S: this,
355
372
  C: (f, a) => f(...a)
356
373
  });
@@ -359,7 +376,7 @@ var Muxer = class {
359
376
  cmd
360
377
  }, {
361
378
  F: __dxlog_file,
362
- L: 242,
379
+ L: 240,
363
380
  S: this,
364
381
  C: (f, a) => f(...a)
365
382
  });
@@ -387,7 +404,7 @@ var Muxer = class {
387
404
  tag: stream.tag
388
405
  }, {
389
406
  F: __dxlog_file,
390
- L: 269,
407
+ L: 267,
391
408
  S: this,
392
409
  C: (f, a) => f(...a)
393
410
  });
@@ -463,15 +480,6 @@ var Muxer = class {
463
480
  };
464
481
 
465
482
  // packages/core/mesh/teleport/src/teleport.ts
466
- import invariant3 from "tiny-invariant";
467
- import { asyncTimeout, scheduleTaskInterval as scheduleTaskInterval2, runInContextAsync, synchronized, scheduleTask } from "@dxos/async";
468
- import { Context as Context2 } from "@dxos/context";
469
- import { failUndefined as failUndefined2 } from "@dxos/debug";
470
- import { PublicKey } from "@dxos/keys";
471
- import { log as log2 } from "@dxos/log";
472
- import { schema as schema2, RpcClosedError } from "@dxos/protocols";
473
- import { createProtoRpcPeer } from "@dxos/rpc";
474
- import { Callback } from "@dxos/util";
475
483
  function _ts_decorate(decorators, target, key, desc) {
476
484
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
477
485
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -516,7 +524,6 @@ var Teleport = class {
516
524
  invariant3(typeof initiator === "boolean");
517
525
  invariant3(PublicKey.isPublicKey(localPeerId));
518
526
  invariant3(PublicKey.isPublicKey(remotePeerId));
519
- invariant3(typeof initiator === "boolean");
520
527
  this.initiator = initiator;
521
528
  this.localPeerId = localPeerId;
522
529
  this.remotePeerId = remotePeerId;
@@ -525,7 +532,7 @@ var Teleport = class {
525
532
  name
526
533
  }, {
527
534
  F: __dxlog_file2,
528
- L: 65,
535
+ L: 64,
529
536
  S: this,
530
537
  C: (f, a) => f(...a)
531
538
  });
@@ -576,7 +583,7 @@ var Teleport = class {
576
583
  } catch (err2) {
577
584
  log2.catch(err2, void 0, {
578
585
  F: __dxlog_file2,
579
- L: 125,
586
+ L: 124,
580
587
  S: this,
581
588
  C: (f, a) => f(...a)
582
589
  });
@@ -592,7 +599,7 @@ var Teleport = class {
592
599
  name
593
600
  }, {
594
601
  F: __dxlog_file2,
595
- L: 137,
602
+ L: 136,
596
603
  S: this,
597
604
  C: (f, a) => f(...a)
598
605
  });
@@ -624,7 +631,7 @@ var Teleport = class {
624
631
  extensionName
625
632
  }, {
626
633
  F: __dxlog_file2,
627
- L: 167,
634
+ L: 166,
628
635
  S: this,
629
636
  C: (f, a) => f(...a)
630
637
  });
@@ -652,7 +659,7 @@ var Teleport = class {
652
659
  extensionName
653
660
  }, {
654
661
  F: __dxlog_file2,
655
- L: 190,
662
+ L: 189,
656
663
  S: this,
657
664
  C: (f, a) => f(...a)
658
665
  });
@@ -713,13 +720,134 @@ var ControlExtension = class {
713
720
  }
714
721
  };
715
722
 
723
+ // packages/core/mesh/teleport/src/testing/test-builder.ts
724
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/testing/test-builder.ts";
725
+ var TestBuilder = class {
726
+ constructor() {
727
+ this._peers = /* @__PURE__ */ new Set();
728
+ }
729
+ createPeer(opts) {
730
+ const peer = opts.factory();
731
+ this._peers.add(peer);
732
+ return peer;
733
+ }
734
+ *createPeers(opts) {
735
+ while (true) {
736
+ yield this.createPeer(opts);
737
+ }
738
+ }
739
+ async destroy() {
740
+ await Promise.all(Array.from(this._peers).map((agent) => agent.destroy()));
741
+ }
742
+ async connect(peer1, peer2) {
743
+ invariant4(peer1 !== peer2);
744
+ invariant4(this._peers.has(peer1));
745
+ invariant4(this._peers.has(peer1));
746
+ const connection1 = peer1.createConnection({
747
+ initiator: true,
748
+ remotePeerId: peer2.peerId
749
+ });
750
+ const connection2 = peer2.createConnection({
751
+ initiator: false,
752
+ remotePeerId: peer1.peerId
753
+ });
754
+ pipeStreams(connection1.teleport.stream, connection2.teleport.stream);
755
+ await Promise.all([
756
+ peer1.openConnection(connection1),
757
+ peer2.openConnection(connection2)
758
+ ]);
759
+ return [
760
+ connection1,
761
+ connection2
762
+ ];
763
+ }
764
+ async disconnect(peer1, peer2) {
765
+ invariant4(peer1 !== peer2);
766
+ invariant4(this._peers.has(peer1));
767
+ invariant4(this._peers.has(peer1));
768
+ const connection1 = Array.from(peer1.connections).find((connection) => connection.remotePeerId.equals(peer2.peerId));
769
+ const connection2 = Array.from(peer2.connections).find((connection) => connection.remotePeerId.equals(peer1.peerId));
770
+ invariant4(connection1);
771
+ invariant4(connection2);
772
+ await Promise.all([
773
+ peer1.closeConnection(connection1),
774
+ peer2.closeConnection(connection2)
775
+ ]);
776
+ }
777
+ };
778
+ var TestPeer = class {
779
+ constructor(peerId = PublicKey2.random()) {
780
+ this.peerId = peerId;
781
+ this.connections = /* @__PURE__ */ new Set();
782
+ }
783
+ async onOpen(connection) {
784
+ }
785
+ async onClose(connection) {
786
+ }
787
+ createConnection({ initiator, remotePeerId }) {
788
+ const connection = new TestConnection(this.peerId, remotePeerId, initiator);
789
+ this.connections.add(connection);
790
+ return connection;
791
+ }
792
+ async openConnection(connection) {
793
+ invariant4(this.connections.has(connection));
794
+ await connection.teleport.open();
795
+ await this.onOpen(connection);
796
+ }
797
+ async closeConnection(connection) {
798
+ invariant4(this.connections.has(connection));
799
+ await this.onClose(connection);
800
+ await connection.teleport.close();
801
+ this.connections.delete(connection);
802
+ }
803
+ async destroy() {
804
+ for (const teleport of this.connections) {
805
+ await this.closeConnection(teleport);
806
+ }
807
+ }
808
+ };
809
+ var pipeStreams = (stream1, stream2) => {
810
+ pipeline(stream1, stream2, (err) => {
811
+ if (err && err.code !== "ERR_STREAM_PREMATURE_CLOSE") {
812
+ log3.catch(err, void 0, {
813
+ F: __dxlog_file3,
814
+ L: 106,
815
+ S: void 0,
816
+ C: (f, a) => f(...a)
817
+ });
818
+ }
819
+ });
820
+ pipeline(stream2, stream1, (err) => {
821
+ if (err && err.code !== "ERR_STREAM_PREMATURE_CLOSE") {
822
+ log3.catch(err, void 0, {
823
+ F: __dxlog_file3,
824
+ L: 111,
825
+ S: void 0,
826
+ C: (f, a) => f(...a)
827
+ });
828
+ }
829
+ });
830
+ };
831
+ var TestConnection = class {
832
+ constructor(localPeerId, remotePeerId, initiator) {
833
+ this.localPeerId = localPeerId;
834
+ this.remotePeerId = remotePeerId;
835
+ this.initiator = initiator;
836
+ this.teleport = new Teleport({
837
+ initiator,
838
+ localPeerId,
839
+ remotePeerId
840
+ });
841
+ }
842
+ };
843
+
716
844
  // packages/core/mesh/teleport/src/testing/test-extension.ts
717
- import invariant4 from "tiny-invariant";
845
+ import invariant5 from "tiny-invariant";
718
846
  import { asyncTimeout as asyncTimeout2, Trigger as Trigger2 } from "@dxos/async";
719
- import { log as log3 } from "@dxos/log";
847
+ import { log as log4 } from "@dxos/log";
720
848
  import { schema as schema3 } from "@dxos/protocols";
721
849
  import { createProtoRpcPeer as createProtoRpcPeer2 } from "@dxos/rpc";
722
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/testing/test-extension.ts";
850
+ var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/testing/test-extension.ts";
723
851
  var TestExtension = class {
724
852
  constructor(callbacks = {}) {
725
853
  this.callbacks = callbacks;
@@ -732,11 +860,11 @@ var TestExtension = class {
732
860
  }
733
861
  async onOpen(context) {
734
862
  var _a, _b;
735
- log3("onOpen", {
863
+ log4("onOpen", {
736
864
  localPeerId: context.localPeerId,
737
865
  remotePeerId: context.remotePeerId
738
866
  }, {
739
- F: __dxlog_file3,
867
+ F: __dxlog_file4,
740
868
  L: 33,
741
869
  S: this,
742
870
  C: (f, a) => f(...a)
@@ -771,10 +899,10 @@ var TestExtension = class {
771
899
  }
772
900
  async onClose(err) {
773
901
  var _a, _b, _c;
774
- log3("onClose", {
902
+ log4("onClose", {
775
903
  err
776
904
  }, {
777
- F: __dxlog_file3,
905
+ F: __dxlog_file4,
778
906
  L: 67,
779
907
  S: this,
780
908
  C: (f, a) => f(...a)
@@ -790,7 +918,207 @@ var TestExtension = class {
790
918
  const res = await asyncTimeout2(this._rpc.rpc.TestService.testCall({
791
919
  data: message
792
920
  }), 1500);
793
- invariant4(res.data === message);
921
+ invariant5(res.data === message);
922
+ }
923
+ /**
924
+ * Force-close the connection.
925
+ */
926
+ async closeConnection(err) {
927
+ var _a;
928
+ (_a = this.extensionContext) == null ? void 0 : _a.close(err);
929
+ }
930
+ };
931
+
932
+ // packages/core/mesh/teleport/src/testing/test-extension-with-streams.ts
933
+ import assert from "@dxos/node-std/assert";
934
+ import { randomBytes } from "@dxos/node-std/crypto";
935
+ import { Trigger as Trigger3 } from "@dxos/async";
936
+ import { log as log5 } from "@dxos/log";
937
+ import { schema as schema4 } from "@dxos/protocols";
938
+ import { createProtoRpcPeer as createProtoRpcPeer3 } from "@dxos/rpc";
939
+ var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/testing/test-extension-with-streams.ts";
940
+ var TestExtensionWithStreams = class {
941
+ constructor(callbacks = {}) {
942
+ this.callbacks = callbacks;
943
+ this.open = new Trigger3();
944
+ this.closed = new Trigger3();
945
+ this._streams = /* @__PURE__ */ new Map();
946
+ }
947
+ get remotePeerId() {
948
+ var _a;
949
+ return (_a = this.extensionContext) == null ? void 0 : _a.remotePeerId;
950
+ }
951
+ async _openStream(streamTag, interval = 5, chunkSize = 2048) {
952
+ assert(!this._streams.has(streamTag), `Stream already exists: ${streamTag}`);
953
+ const networkStream = await this.extensionContext.createStream(streamTag, {
954
+ contentType: "application/x-test-stream"
955
+ });
956
+ const streamEntry = {
957
+ networkStream,
958
+ bytesSent: 0,
959
+ bytesReceived: 0,
960
+ sendErrors: 0,
961
+ receiveErrors: 0,
962
+ startTimestamp: Date.now()
963
+ };
964
+ const pushChunk = () => {
965
+ streamEntry.timer = setTimeout(() => {
966
+ const chunk = randomBytes(chunkSize);
967
+ if (!networkStream.write(chunk, "binary", (err) => {
968
+ if (!err) {
969
+ streamEntry.bytesSent += chunk.length;
970
+ } else {
971
+ streamEntry.sendErrors += 1;
972
+ }
973
+ })) {
974
+ networkStream.once("drain", pushChunk);
975
+ } else {
976
+ process.nextTick(pushChunk);
977
+ }
978
+ }, interval);
979
+ };
980
+ pushChunk();
981
+ this._streams.set(streamTag, streamEntry);
982
+ networkStream.on("data", (data) => {
983
+ streamEntry.bytesReceived += data.length;
984
+ });
985
+ networkStream.on("error", (err) => {
986
+ streamEntry.receiveErrors += 1;
987
+ });
988
+ networkStream.on("close", () => {
989
+ networkStream.removeAllListeners();
990
+ });
991
+ }
992
+ _closeStream(streamTag) {
993
+ assert(this._streams.has(streamTag), `Stream does not exist: ${streamTag}`);
994
+ const stream = this._streams.get(streamTag);
995
+ clearTimeout(stream.timer);
996
+ const { bytesSent, bytesReceived, sendErrors, receiveErrors, startTimestamp } = stream;
997
+ stream.networkStream.destroy();
998
+ this._streams.delete(streamTag);
999
+ return {
1000
+ bytesSent,
1001
+ bytesReceived,
1002
+ sendErrors,
1003
+ receiveErrors,
1004
+ runningTime: Date.now() - (startTimestamp != null ? startTimestamp : 0)
1005
+ };
1006
+ }
1007
+ async onOpen(context) {
1008
+ var _a, _b;
1009
+ log5("onOpen", {
1010
+ localPeerId: context.localPeerId,
1011
+ remotePeerId: context.remotePeerId
1012
+ }, {
1013
+ F: __dxlog_file5,
1014
+ L: 111,
1015
+ S: this,
1016
+ C: (f, a) => f(...a)
1017
+ });
1018
+ this.extensionContext = context;
1019
+ this._rpc = createProtoRpcPeer3({
1020
+ port: await context.createPort("rpc", {
1021
+ contentType: 'application/x-protobuf; messageType="dxos.rpc.Message"'
1022
+ }),
1023
+ requested: {
1024
+ TestServiceWithStreams: schema4.getService("example.testing.rpc.TestServiceWithStreams")
1025
+ },
1026
+ exposed: {
1027
+ TestServiceWithStreams: schema4.getService("example.testing.rpc.TestServiceWithStreams")
1028
+ },
1029
+ handlers: {
1030
+ TestServiceWithStreams: {
1031
+ requestTestStream: async (request) => {
1032
+ const { data: streamTag, streamLoadInterval, streamLoadChunkSize } = request;
1033
+ await this._openStream(streamTag, streamLoadInterval, streamLoadChunkSize);
1034
+ return {
1035
+ data: streamTag
1036
+ };
1037
+ },
1038
+ closeTestStream: async (request) => {
1039
+ const streamTag = request.data;
1040
+ const { bytesSent, bytesReceived, sendErrors, receiveErrors, runningTime } = this._closeStream(streamTag);
1041
+ return {
1042
+ data: streamTag,
1043
+ bytesSent,
1044
+ bytesReceived,
1045
+ sendErrors,
1046
+ receiveErrors,
1047
+ runningTime
1048
+ };
1049
+ }
1050
+ }
1051
+ },
1052
+ timeout: 2e3
1053
+ });
1054
+ await this._rpc.open();
1055
+ await ((_b = (_a = this.callbacks).onOpen) == null ? void 0 : _b.call(_a));
1056
+ this.open.wake();
1057
+ }
1058
+ async onClose(err) {
1059
+ var _a, _b, _c;
1060
+ log5("onClose", {
1061
+ err
1062
+ }, {
1063
+ F: __dxlog_file5,
1064
+ L: 162,
1065
+ S: this,
1066
+ C: (f, a) => f(...a)
1067
+ });
1068
+ await ((_b = (_a = this.callbacks).onClose) == null ? void 0 : _b.call(_a));
1069
+ this.closed.wake();
1070
+ for (const [streamTag, stream] of Object.entries(this._streams)) {
1071
+ log5("closing stream", {
1072
+ streamTag
1073
+ }, {
1074
+ F: __dxlog_file5,
1075
+ L: 166,
1076
+ S: this,
1077
+ C: (f, a) => f(...a)
1078
+ });
1079
+ clearTimeout(stream.interval);
1080
+ stream.networkStream.destroy();
1081
+ }
1082
+ await ((_c = this._rpc) == null ? void 0 : _c.close());
1083
+ }
1084
+ async addNewStream(streamLoadInterval, streamLoadChunkSize, streamTag) {
1085
+ await this.open.wait({
1086
+ timeout: 1500
1087
+ });
1088
+ if (!streamTag) {
1089
+ streamTag = `stream-${randomBytes(4).toString("hex")}`;
1090
+ }
1091
+ const { data } = await this._rpc.rpc.TestServiceWithStreams.requestTestStream({
1092
+ data: streamTag,
1093
+ streamLoadInterval,
1094
+ streamLoadChunkSize
1095
+ });
1096
+ assert(data === streamTag);
1097
+ await this._openStream(streamTag, streamLoadInterval, streamLoadChunkSize);
1098
+ return streamTag;
1099
+ }
1100
+ async closeStream(streamTag) {
1101
+ await this.open.wait({
1102
+ timeout: 1500
1103
+ });
1104
+ const { data, bytesSent, bytesReceived, sendErrors, receiveErrors, runningTime } = await this._rpc.rpc.TestServiceWithStreams.closeTestStream({
1105
+ data: streamTag
1106
+ });
1107
+ assert(data === streamTag);
1108
+ const local = this._closeStream(streamTag);
1109
+ return {
1110
+ streamTag,
1111
+ stats: {
1112
+ local,
1113
+ remote: {
1114
+ bytesSent,
1115
+ bytesReceived,
1116
+ sendErrors,
1117
+ receiveErrors,
1118
+ runningTime
1119
+ }
1120
+ }
1121
+ };
794
1122
  }
795
1123
  /**
796
1124
  * Force-close the connection.
@@ -807,6 +1135,10 @@ export {
807
1135
  encodeFrame,
808
1136
  Muxer,
809
1137
  Teleport,
810
- TestExtension
1138
+ TestBuilder,
1139
+ TestPeer,
1140
+ TestConnection,
1141
+ TestExtension,
1142
+ TestExtensionWithStreams
811
1143
  };
812
- //# sourceMappingURL=chunk-7TGKNWZQ.mjs.map
1144
+ //# sourceMappingURL=chunk-HCC6HY7F.mjs.map