@dxos/teleport 0.1.56-main.e47dfd1 → 0.1.56-main.e79d64a

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.
@@ -40,15 +40,15 @@ module.exports = __toCommonJS(testing_exports);
40
40
 
41
41
  // packages/core/mesh/teleport/src/testing/test-builder.ts
42
42
  var import_node_stream3 = require("node:stream");
43
- var import_tiny_invariant4 = __toESM(require("tiny-invariant"));
43
+ var import_invariant4 = require("@dxos/invariant");
44
44
  var import_keys2 = require("@dxos/keys");
45
45
  var import_log4 = require("@dxos/log");
46
46
 
47
47
  // packages/core/mesh/teleport/src/teleport.ts
48
- var import_tiny_invariant3 = __toESM(require("tiny-invariant"));
49
48
  var import_async3 = require("@dxos/async");
50
49
  var import_context2 = require("@dxos/context");
51
50
  var import_debug2 = require("@dxos/debug");
51
+ var import_invariant3 = require("@dxos/invariant");
52
52
  var import_keys = require("@dxos/keys");
53
53
  var import_log3 = require("@dxos/log");
54
54
  var import_protocols2 = require("@dxos/protocols");
@@ -57,10 +57,15 @@ var import_util = require("@dxos/util");
57
57
 
58
58
  // packages/core/mesh/teleport/src/muxing/framer.ts
59
59
  var import_node_stream = require("node:stream");
60
- var import_tiny_invariant = __toESM(require("tiny-invariant"));
60
+ var import_invariant = require("@dxos/invariant");
61
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/muxing/framer.ts";
61
62
  var FRAME_LENGTH_SIZE = 2;
62
63
  var Framer = class {
63
64
  constructor() {
65
+ // private readonly _tagBuffer = Buffer.alloc(4)
66
+ this._messageCb = void 0;
67
+ this._subscribeCb = void 0;
68
+ this._buffer = void 0;
64
69
  this._sendCallbacks = [];
65
70
  this._bytesSent = 0;
66
71
  this._bytesReceived = 0;
@@ -70,7 +75,15 @@ var Framer = class {
70
75
  this._processResponseQueue();
71
76
  },
72
77
  write: (chunk, encoding, callback) => {
73
- (0, import_tiny_invariant.default)(!this._subscribeCb, "Internal Framer bug. Concurrent writes detected.");
78
+ (0, import_invariant.invariant)(!this._subscribeCb, "Internal Framer bug. Concurrent writes detected.", {
79
+ F: __dxlog_file,
80
+ L: 34,
81
+ S: this,
82
+ A: [
83
+ "!this._subscribeCb",
84
+ "'Internal Framer bug. Concurrent writes detected.'"
85
+ ]
86
+ });
74
87
  this._bytesReceived += chunk.length;
75
88
  if (this._buffer && this._buffer.length > 0) {
76
89
  this._buffer = Buffer.concat([
@@ -106,10 +119,17 @@ var Framer = class {
106
119
  });
107
120
  },
108
121
  subscribe: (callback) => {
109
- var _a;
110
- (0, import_tiny_invariant.default)(!this._messageCb, "Rpc port already has a message listener.");
122
+ (0, import_invariant.invariant)(!this._messageCb, "Rpc port already has a message listener.", {
123
+ F: __dxlog_file,
124
+ L: 73,
125
+ S: this,
126
+ A: [
127
+ "!this._messageCb",
128
+ "'Rpc port already has a message listener.'"
129
+ ]
130
+ });
111
131
  this._messageCb = callback;
112
- (_a = this._subscribeCb) == null ? void 0 : _a.call(this);
132
+ this._subscribeCb?.();
113
133
  return () => {
114
134
  this._messageCb = void 0;
115
135
  };
@@ -177,10 +197,10 @@ var encodeFrame = (payload) => {
177
197
 
178
198
  // packages/core/mesh/teleport/src/muxing/muxer.ts
179
199
  var import_node_stream2 = require("node:stream");
180
- var import_tiny_invariant2 = __toESM(require("tiny-invariant"));
181
200
  var import_async2 = require("@dxos/async");
182
201
  var import_context = require("@dxos/context");
183
202
  var import_debug = require("@dxos/debug");
203
+ var import_invariant2 = require("@dxos/invariant");
184
204
  var import_log2 = require("@dxos/log");
185
205
  var import_protocols = require("@dxos/protocols");
186
206
 
@@ -188,7 +208,7 @@ var import_protocols = require("@dxos/protocols");
188
208
  var varint = __toESM(require("varint"));
189
209
  var import_async = require("@dxos/async");
190
210
  var import_log = require("@dxos/log");
191
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/muxing/balancer.ts";
211
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/muxing/balancer.ts";
192
212
  var MAX_CHUNK_SIZE = 8192;
193
213
  var Balancer = class {
194
214
  constructor(_sysChannelId) {
@@ -238,7 +258,7 @@ var Balancer = class {
238
258
  });
239
259
  if (noCalls) {
240
260
  this._sendChunks().catch((err) => import_log.log.catch(err, void 0, {
241
- F: __dxlog_file,
261
+ F: __dxlog_file2,
242
262
  L: 96,
243
263
  S: this,
244
264
  C: (f, a) => f(...a)
@@ -298,16 +318,15 @@ var Balancer = class {
298
318
  return chunk;
299
319
  }
300
320
  async _sendChunks() {
301
- var _a, _b;
302
321
  if (this._sendBuffers.size === 0) {
303
322
  return;
304
323
  }
305
324
  const chunk = this._getNextChunk();
306
325
  try {
307
326
  await this._framer.port.send(chunk.msg);
308
- (_a = chunk.trigger) == null ? void 0 : _a.wake();
327
+ chunk.trigger?.wake();
309
328
  } catch (err) {
310
- (_b = chunk.trigger) == null ? void 0 : _b.throw(err);
329
+ chunk.trigger?.throw(err);
311
330
  }
312
331
  await this._sendChunks();
313
332
  }
@@ -340,7 +359,7 @@ var decodeChunk = (data, withLength) => {
340
359
  };
341
360
 
342
361
  // packages/core/mesh/teleport/src/muxing/muxer.ts
343
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/muxing/muxer.ts";
362
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/muxing/muxer.ts";
344
363
  var Command = import_protocols.schema.getCodecForType("dxos.mesh.muxer.Command");
345
364
  var STATS_INTERVAL = 1e3;
346
365
  var MAX_SAFE_FRAME_SIZE = 1e6;
@@ -354,6 +373,8 @@ var Muxer = class {
354
373
  this._nextId = 1;
355
374
  this._destroyed = false;
356
375
  this._destroying = false;
376
+ this._lastStats = void 0;
377
+ this._lastChannelStats = /* @__PURE__ */ new Map();
357
378
  this.close = new import_async2.Event();
358
379
  this.statsUpdated = new import_async2.Event();
359
380
  this.stream = this._balancer.stream;
@@ -373,7 +394,15 @@ var Muxer = class {
373
394
  tag,
374
395
  contentType: opts.contentType
375
396
  });
376
- (0, import_tiny_invariant2.default)(!channel.push, `Channel already open: ${tag}`);
397
+ (0, import_invariant2.invariant)(!channel.push, `Channel already open: ${tag}`, {
398
+ F: __dxlog_file3,
399
+ L: 134,
400
+ S: this,
401
+ A: [
402
+ "!channel.push",
403
+ "`Channel already open: ${tag}`"
404
+ ]
405
+ });
377
406
  const stream = new import_node_stream2.Duplex({
378
407
  write: (data, encoding, callback) => {
379
408
  this._sendData(channel, data).then(() => callback()).catch(callback);
@@ -413,7 +442,15 @@ var Muxer = class {
413
442
  tag,
414
443
  contentType: opts.contentType
415
444
  });
416
- (0, import_tiny_invariant2.default)(!channel.push, `Channel already open: ${tag}`);
445
+ (0, import_invariant2.invariant)(!channel.push, `Channel already open: ${tag}`, {
446
+ F: __dxlog_file3,
447
+ L: 186,
448
+ S: this,
449
+ A: [
450
+ "!channel.push",
451
+ "`Channel already open: ${tag}`"
452
+ ]
453
+ });
417
454
  let inboundBuffer = [];
418
455
  let callback;
419
456
  channel.push = (data) => {
@@ -429,7 +466,15 @@ var Muxer = class {
429
466
  await this._sendData(channel, data);
430
467
  },
431
468
  subscribe: (cb) => {
432
- (0, import_tiny_invariant2.default)(!callback, "Only one subscriber is allowed");
469
+ (0, import_invariant2.invariant)(!callback, "Only one subscriber is allowed", {
470
+ F: __dxlog_file3,
471
+ L: 208,
472
+ S: this,
473
+ A: [
474
+ "!callback",
475
+ "'Only one subscriber is allowed'"
476
+ ]
477
+ });
433
478
  callback = cb;
434
479
  for (const data of inboundBuffer) {
435
480
  cb(data);
@@ -461,7 +506,7 @@ var Muxer = class {
461
506
  this._destroying = true;
462
507
  this._sendCommand({
463
508
  destroy: {
464
- error: err == null ? void 0 : err.message
509
+ error: err?.message
465
510
  }
466
511
  }, SYSTEM_CHANNEL_ID).then(() => {
467
512
  this._dispose();
@@ -471,35 +516,36 @@ var Muxer = class {
471
516
  void this._ctx.dispose();
472
517
  }
473
518
  _dispose(err) {
474
- var _a;
475
519
  if (this._destroyed) {
476
520
  return;
477
521
  }
478
522
  this._destroyed = true;
479
523
  this._balancer.destroy();
480
524
  for (const channel of this._channelsByTag.values()) {
481
- (_a = channel.destroy) == null ? void 0 : _a.call(channel, err);
525
+ channel.destroy?.(err);
482
526
  }
483
527
  this.close.emit(err);
484
528
  this._channelsByLocalId.clear();
485
529
  this._channelsByTag.clear();
486
530
  }
487
531
  async _handleCommand(cmd) {
488
- var _a;
489
532
  (0, import_log2.log)("Received command", {
490
533
  cmd
491
534
  }, {
492
- F: __dxlog_file2,
493
- L: 240,
535
+ F: __dxlog_file3,
536
+ L: 283,
494
537
  S: this,
495
538
  C: (f, a) => f(...a)
496
539
  });
497
540
  if (this._destroyed || this._destroying) {
541
+ if (cmd.destroy) {
542
+ return;
543
+ }
498
544
  import_log2.log.warn("Received command after destroy", {
499
545
  cmd
500
546
  }, {
501
- F: __dxlog_file2,
502
- L: 243,
547
+ F: __dxlog_file3,
548
+ L: 290,
503
549
  S: this,
504
550
  C: (f, a) => f(...a)
505
551
  });
@@ -521,13 +567,13 @@ var Muxer = class {
521
567
  }
522
568
  channel.buffer = [];
523
569
  } else if (cmd.data) {
524
- const stream = (_a = this._channelsByLocalId.get(cmd.data.channelId)) != null ? _a : (0, import_debug.failUndefined)();
570
+ const stream = this._channelsByLocalId.get(cmd.data.channelId) ?? (0, import_debug.failUndefined)();
525
571
  if (!stream.push) {
526
572
  import_log2.log.warn("Received data for channel before it was opened", {
527
573
  tag: stream.tag
528
574
  }, {
529
- F: __dxlog_file2,
530
- L: 270,
575
+ F: __dxlog_file3,
576
+ L: 317,
531
577
  S: this,
532
578
  C: (f, a) => f(...a)
533
579
  });
@@ -575,8 +621,8 @@ var Muxer = class {
575
621
  size: data.length,
576
622
  threshold: MAX_SAFE_FRAME_SIZE
577
623
  }, {
578
- F: __dxlog_file2,
579
- L: 314,
624
+ F: __dxlog_file3,
625
+ L: 362,
580
626
  S: this,
581
627
  C: (f, a) => f(...a)
582
628
  });
@@ -602,21 +648,40 @@ var Muxer = class {
602
648
  }
603
649
  async _emitStats() {
604
650
  if (this._destroyed || this._destroying) {
651
+ this._lastStats = void 0;
652
+ this._lastChannelStats.clear();
605
653
  return;
606
654
  }
607
655
  const bytesSent = this._balancer.bytesSent;
608
656
  const bytesReceived = this._balancer.bytesReceived;
609
- this.statsUpdated.emit({
610
- channels: Array.from(this._channelsByTag.values()).map((channel) => ({
611
- id: channel.id,
612
- tag: channel.tag,
613
- contentType: channel.contentType,
614
- bytesSent: channel.stats.bytesSent,
615
- bytesReceived: channel.stats.bytesReceived
616
- })),
657
+ const now = Date.now();
658
+ const interval = this._lastStats ? (now - this._lastStats.timestamp) / 1e3 : 0;
659
+ const calculateThroughput = (current, last) => last ? {
660
+ bytesSentRate: interval ? (current.bytesSent - last.bytesSent) / interval : void 0,
661
+ bytesReceivedRate: interval ? (current.bytesReceived - last.bytesReceived) / interval : void 0
662
+ } : {};
663
+ this._lastStats = {
664
+ timestamp: now,
665
+ channels: Array.from(this._channelsByTag.values()).map((channel) => {
666
+ const stats = {
667
+ id: channel.id,
668
+ tag: channel.tag,
669
+ contentType: channel.contentType,
670
+ bytesSent: channel.stats.bytesSent,
671
+ bytesReceived: channel.stats.bytesReceived,
672
+ ...calculateThroughput(channel.stats, this._lastChannelStats.get(channel.id))
673
+ };
674
+ this._lastChannelStats.set(channel.id, stats);
675
+ return stats;
676
+ }),
617
677
  bytesSent,
618
- bytesReceived
619
- });
678
+ bytesReceived,
679
+ ...calculateThroughput({
680
+ bytesSent,
681
+ bytesReceived
682
+ }, this._lastStats)
683
+ };
684
+ this.statsUpdated.emit(this._lastStats);
620
685
  }
621
686
  };
622
687
 
@@ -631,14 +696,14 @@ function _ts_decorate(decorators, target, key, desc) {
631
696
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
632
697
  return c > 3 && r && Object.defineProperty(target, key, r), r;
633
698
  }
634
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/teleport.ts";
699
+ var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/teleport.ts";
635
700
  var Teleport = class {
636
701
  constructor({ initiator, localPeerId, remotePeerId }) {
637
702
  this._ctx = new import_context2.Context({
638
703
  onError: (err) => {
639
704
  void this.destroy(err).catch(() => {
640
705
  import_log3.log.error("Error during destroy", err, {
641
- F: __dxlog_file3,
706
+ F: __dxlog_file4,
642
707
  L: 34,
643
708
  S: this,
644
709
  C: (f, a) => f(...a)
@@ -652,7 +717,7 @@ var Teleport = class {
652
717
  heartbeatTimeout: 1e4,
653
718
  onTimeout: () => {
654
719
  this.destroy(new Error("Connection timed out")).catch((err) => import_log3.log.catch(err, void 0, {
655
- F: __dxlog_file3,
720
+ F: __dxlog_file4,
656
721
  L: 45,
657
722
  S: this,
658
723
  C: (f, a) => f(...a)
@@ -662,9 +727,33 @@ var Teleport = class {
662
727
  this._extensions = /* @__PURE__ */ new Map();
663
728
  this._remoteExtensions = /* @__PURE__ */ new Set();
664
729
  this._open = false;
665
- (0, import_tiny_invariant3.default)(typeof initiator === "boolean");
666
- (0, import_tiny_invariant3.default)(import_keys.PublicKey.isPublicKey(localPeerId));
667
- (0, import_tiny_invariant3.default)(import_keys.PublicKey.isPublicKey(remotePeerId));
730
+ (0, import_invariant3.invariant)(typeof initiator === "boolean", void 0, {
731
+ F: __dxlog_file4,
732
+ L: 55,
733
+ S: this,
734
+ A: [
735
+ "typeof initiator === 'boolean'",
736
+ ""
737
+ ]
738
+ });
739
+ (0, import_invariant3.invariant)(import_keys.PublicKey.isPublicKey(localPeerId), void 0, {
740
+ F: __dxlog_file4,
741
+ L: 56,
742
+ S: this,
743
+ A: [
744
+ "PublicKey.isPublicKey(localPeerId)",
745
+ ""
746
+ ]
747
+ });
748
+ (0, import_invariant3.invariant)(import_keys.PublicKey.isPublicKey(remotePeerId), void 0, {
749
+ F: __dxlog_file4,
750
+ L: 57,
751
+ S: this,
752
+ A: [
753
+ "PublicKey.isPublicKey(remotePeerId)",
754
+ ""
755
+ ]
756
+ });
668
757
  this.initiator = initiator;
669
758
  this.localPeerId = localPeerId;
670
759
  this.remotePeerId = remotePeerId;
@@ -672,12 +761,20 @@ var Teleport = class {
672
761
  (0, import_log3.log)("remote extension", {
673
762
  name
674
763
  }, {
675
- F: __dxlog_file3,
764
+ F: __dxlog_file4,
676
765
  L: 63,
677
766
  S: this,
678
767
  C: (f, a) => f(...a)
679
768
  });
680
- (0, import_tiny_invariant3.default)(!this._remoteExtensions.has(name), "Remote extension already exists");
769
+ (0, import_invariant3.invariant)(!this._remoteExtensions.has(name), "Remote extension already exists", {
770
+ F: __dxlog_file4,
771
+ L: 64,
772
+ S: this,
773
+ A: [
774
+ "!this._remoteExtensions.has(name)",
775
+ "'Remote extension already exists'"
776
+ ]
777
+ });
681
778
  this._remoteExtensions.add(name);
682
779
  if (this._extensions.has(name)) {
683
780
  try {
@@ -700,11 +797,13 @@ var Teleport = class {
700
797
  localPeerId,
701
798
  remotePeerId,
702
799
  bytesSent: stats.bytesSent,
800
+ bytesSentRate: stats.bytesSentRate,
703
801
  bytesReceived: stats.bytesReceived,
802
+ bytesReceivedRate: stats.bytesReceivedRate,
704
803
  channels: stats.channels
705
804
  }, {
706
- F: __dxlog_file3,
707
- L: 88,
805
+ F: __dxlog_file4,
806
+ L: 89,
708
807
  S: this,
709
808
  C: (f, a) => f(...a)
710
809
  });
@@ -737,8 +836,8 @@ var Teleport = class {
737
836
  await extension.onClose(err);
738
837
  } catch (err2) {
739
838
  import_log3.log.catch(err2, void 0, {
740
- F: __dxlog_file3,
741
- L: 132,
839
+ F: __dxlog_file4,
840
+ L: 137,
742
841
  S: this,
743
842
  C: (f, a) => f(...a)
744
843
  });
@@ -753,8 +852,8 @@ var Teleport = class {
753
852
  (0, import_log3.log)("addExtension", {
754
853
  name
755
854
  }, {
756
- F: __dxlog_file3,
757
- L: 144,
855
+ F: __dxlog_file4,
856
+ L: 149,
758
857
  S: this,
759
858
  C: (f, a) => f(...a)
760
859
  });
@@ -776,31 +875,62 @@ var Teleport = class {
776
875
  }
777
876
  }
778
877
  _setExtension(extensionName, extension) {
779
- (0, import_tiny_invariant3.default)(!extensionName.includes("/"), "Invalid extension name");
780
- (0, import_tiny_invariant3.default)(!this._extensions.has(extensionName), "Extension already exists");
878
+ (0, import_invariant3.invariant)(!extensionName.includes("/"), "Invalid extension name", {
879
+ F: __dxlog_file4,
880
+ L: 173,
881
+ S: this,
882
+ A: [
883
+ "!extensionName.includes('/')",
884
+ "'Invalid extension name'"
885
+ ]
886
+ });
887
+ (0, import_invariant3.invariant)(!this._extensions.has(extensionName), "Extension already exists", {
888
+ F: __dxlog_file4,
889
+ L: 174,
890
+ S: this,
891
+ A: [
892
+ "!this._extensions.has(extensionName)",
893
+ "'Extension already exists'"
894
+ ]
895
+ });
781
896
  this._extensions.set(extensionName, extension);
782
897
  }
783
898
  async _openExtension(extensionName) {
784
- var _a;
785
899
  (0, import_log3.log)("open extension", {
786
900
  extensionName
787
901
  }, {
788
- F: __dxlog_file3,
789
- L: 174,
902
+ F: __dxlog_file4,
903
+ L: 179,
790
904
  S: this,
791
905
  C: (f, a) => f(...a)
792
906
  });
793
- const extension = (_a = this._extensions.get(extensionName)) != null ? _a : (0, import_debug2.failUndefined)();
907
+ const extension = this._extensions.get(extensionName) ?? (0, import_debug2.failUndefined)();
794
908
  const context = {
795
909
  initiator: this.initiator,
796
910
  localPeerId: this.localPeerId,
797
911
  remotePeerId: this.remotePeerId,
798
912
  createPort: async (channelName, opts) => {
799
- (0, import_tiny_invariant3.default)(!channelName.includes("/"), "Invalid channel name");
913
+ (0, import_invariant3.invariant)(!channelName.includes("/"), "Invalid channel name", {
914
+ F: __dxlog_file4,
915
+ L: 187,
916
+ S: this,
917
+ A: [
918
+ "!channelName.includes('/')",
919
+ "'Invalid channel name'"
920
+ ]
921
+ });
800
922
  return this._muxer.createPort(`${extensionName}/${channelName}`, opts);
801
923
  },
802
924
  createStream: async (channelName, opts) => {
803
- (0, import_tiny_invariant3.default)(!channelName.includes("/"), "Invalid channel name");
925
+ (0, import_invariant3.invariant)(!channelName.includes("/"), "Invalid channel name", {
926
+ F: __dxlog_file4,
927
+ L: 191,
928
+ S: this,
929
+ A: [
930
+ "!channelName.includes('/')",
931
+ "'Invalid channel name'"
932
+ ]
933
+ });
804
934
  return this._muxer.createStream(`${extensionName}/${channelName}`, opts);
805
935
  },
806
936
  close: (err) => {
@@ -813,8 +943,8 @@ var Teleport = class {
813
943
  (0, import_log3.log)("extension opened", {
814
944
  extensionName
815
945
  }, {
816
- F: __dxlog_file3,
817
- L: 197,
946
+ F: __dxlog_file4,
947
+ L: 202,
818
948
  S: this,
819
949
  C: (f, a) => f(...a)
820
950
  });
@@ -876,7 +1006,7 @@ var ControlExtension = class {
876
1006
  };
877
1007
 
878
1008
  // packages/core/mesh/teleport/src/testing/test-builder.ts
879
- var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/testing/test-builder.ts";
1009
+ var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/testing/test-builder.ts";
880
1010
  var TestBuilder = class {
881
1011
  constructor() {
882
1012
  this._peers = /* @__PURE__ */ new Set();
@@ -895,9 +1025,33 @@ var TestBuilder = class {
895
1025
  await Promise.all(Array.from(this._peers).map((agent) => agent.destroy()));
896
1026
  }
897
1027
  async connect(peer1, peer2) {
898
- (0, import_tiny_invariant4.default)(peer1 !== peer2);
899
- (0, import_tiny_invariant4.default)(this._peers.has(peer1));
900
- (0, import_tiny_invariant4.default)(this._peers.has(peer1));
1028
+ (0, import_invariant4.invariant)(peer1 !== peer2, void 0, {
1029
+ F: __dxlog_file5,
1030
+ L: 37,
1031
+ S: this,
1032
+ A: [
1033
+ "peer1 !== peer2",
1034
+ ""
1035
+ ]
1036
+ });
1037
+ (0, import_invariant4.invariant)(this._peers.has(peer1), void 0, {
1038
+ F: __dxlog_file5,
1039
+ L: 38,
1040
+ S: this,
1041
+ A: [
1042
+ "this._peers.has(peer1)",
1043
+ ""
1044
+ ]
1045
+ });
1046
+ (0, import_invariant4.invariant)(this._peers.has(peer1), void 0, {
1047
+ F: __dxlog_file5,
1048
+ L: 39,
1049
+ S: this,
1050
+ A: [
1051
+ "this._peers.has(peer1)",
1052
+ ""
1053
+ ]
1054
+ });
901
1055
  const connection1 = peer1.createConnection({
902
1056
  initiator: true,
903
1057
  remotePeerId: peer2.peerId
@@ -917,13 +1071,53 @@ var TestBuilder = class {
917
1071
  ];
918
1072
  }
919
1073
  async disconnect(peer1, peer2) {
920
- (0, import_tiny_invariant4.default)(peer1 !== peer2);
921
- (0, import_tiny_invariant4.default)(this._peers.has(peer1));
922
- (0, import_tiny_invariant4.default)(this._peers.has(peer1));
1074
+ (0, import_invariant4.invariant)(peer1 !== peer2, void 0, {
1075
+ F: __dxlog_file5,
1076
+ L: 51,
1077
+ S: this,
1078
+ A: [
1079
+ "peer1 !== peer2",
1080
+ ""
1081
+ ]
1082
+ });
1083
+ (0, import_invariant4.invariant)(this._peers.has(peer1), void 0, {
1084
+ F: __dxlog_file5,
1085
+ L: 52,
1086
+ S: this,
1087
+ A: [
1088
+ "this._peers.has(peer1)",
1089
+ ""
1090
+ ]
1091
+ });
1092
+ (0, import_invariant4.invariant)(this._peers.has(peer1), void 0, {
1093
+ F: __dxlog_file5,
1094
+ L: 53,
1095
+ S: this,
1096
+ A: [
1097
+ "this._peers.has(peer1)",
1098
+ ""
1099
+ ]
1100
+ });
923
1101
  const connection1 = Array.from(peer1.connections).find((connection) => connection.remotePeerId.equals(peer2.peerId));
924
1102
  const connection2 = Array.from(peer2.connections).find((connection) => connection.remotePeerId.equals(peer1.peerId));
925
- (0, import_tiny_invariant4.default)(connection1);
926
- (0, import_tiny_invariant4.default)(connection2);
1103
+ (0, import_invariant4.invariant)(connection1, void 0, {
1104
+ F: __dxlog_file5,
1105
+ L: 62,
1106
+ S: this,
1107
+ A: [
1108
+ "connection1",
1109
+ ""
1110
+ ]
1111
+ });
1112
+ (0, import_invariant4.invariant)(connection2, void 0, {
1113
+ F: __dxlog_file5,
1114
+ L: 63,
1115
+ S: this,
1116
+ A: [
1117
+ "connection2",
1118
+ ""
1119
+ ]
1120
+ });
927
1121
  await Promise.all([
928
1122
  peer1.closeConnection(connection1),
929
1123
  peer2.closeConnection(connection2)
@@ -945,12 +1139,28 @@ var TestPeer = class {
945
1139
  return connection;
946
1140
  }
947
1141
  async openConnection(connection) {
948
- (0, import_tiny_invariant4.default)(this.connections.has(connection));
1142
+ (0, import_invariant4.invariant)(this.connections.has(connection), void 0, {
1143
+ F: __dxlog_file5,
1144
+ L: 84,
1145
+ S: this,
1146
+ A: [
1147
+ "this.connections.has(connection)",
1148
+ ""
1149
+ ]
1150
+ });
949
1151
  await connection.teleport.open();
950
1152
  await this.onOpen(connection);
951
1153
  }
952
1154
  async closeConnection(connection) {
953
- (0, import_tiny_invariant4.default)(this.connections.has(connection));
1155
+ (0, import_invariant4.invariant)(this.connections.has(connection), void 0, {
1156
+ F: __dxlog_file5,
1157
+ L: 90,
1158
+ S: this,
1159
+ A: [
1160
+ "this.connections.has(connection)",
1161
+ ""
1162
+ ]
1163
+ });
954
1164
  await this.onClose(connection);
955
1165
  await connection.teleport.close();
956
1166
  this.connections.delete(connection);
@@ -965,7 +1175,7 @@ var pipeStreams = (stream1, stream2) => {
965
1175
  (0, import_node_stream3.pipeline)(stream1, stream2, (err) => {
966
1176
  if (err && err.code !== "ERR_STREAM_PREMATURE_CLOSE") {
967
1177
  import_log4.log.catch(err, void 0, {
968
- F: __dxlog_file4,
1178
+ F: __dxlog_file5,
969
1179
  L: 106,
970
1180
  S: void 0,
971
1181
  C: (f, a) => f(...a)
@@ -975,7 +1185,7 @@ var pipeStreams = (stream1, stream2) => {
975
1185
  (0, import_node_stream3.pipeline)(stream2, stream1, (err) => {
976
1186
  if (err && err.code !== "ERR_STREAM_PREMATURE_CLOSE") {
977
1187
  import_log4.log.catch(err, void 0, {
978
- F: __dxlog_file4,
1188
+ F: __dxlog_file5,
979
1189
  L: 111,
980
1190
  S: void 0,
981
1191
  C: (f, a) => f(...a)
@@ -997,12 +1207,12 @@ var TestConnection = class {
997
1207
  };
998
1208
 
999
1209
  // packages/core/mesh/teleport/src/testing/test-extension.ts
1000
- var import_tiny_invariant5 = __toESM(require("tiny-invariant"));
1001
1210
  var import_async4 = require("@dxos/async");
1211
+ var import_invariant5 = require("@dxos/invariant");
1002
1212
  var import_log5 = require("@dxos/log");
1003
1213
  var import_protocols3 = require("@dxos/protocols");
1004
1214
  var import_rpc2 = require("@dxos/rpc");
1005
- var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/testing/test-extension.ts";
1215
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/testing/test-extension.ts";
1006
1216
  var TestExtension = class {
1007
1217
  constructor(callbacks = {}) {
1008
1218
  this.callbacks = callbacks;
@@ -1010,17 +1220,15 @@ var TestExtension = class {
1010
1220
  this.closed = new import_async4.Trigger();
1011
1221
  }
1012
1222
  get remotePeerId() {
1013
- var _a;
1014
- return (_a = this.extensionContext) == null ? void 0 : _a.remotePeerId;
1223
+ return this.extensionContext?.remotePeerId;
1015
1224
  }
1016
1225
  async onOpen(context) {
1017
- var _a, _b;
1018
1226
  (0, import_log5.log)("onOpen", {
1019
1227
  localPeerId: context.localPeerId,
1020
1228
  remotePeerId: context.remotePeerId
1021
1229
  }, {
1022
- F: __dxlog_file5,
1023
- L: 33,
1230
+ F: __dxlog_file6,
1231
+ L: 32,
1024
1232
  S: this,
1025
1233
  C: (f, a) => f(...a)
1026
1234
  });
@@ -1049,22 +1257,21 @@ var TestExtension = class {
1049
1257
  timeout: 2e3
1050
1258
  });
1051
1259
  await this._rpc.open();
1052
- await ((_b = (_a = this.callbacks).onOpen) == null ? void 0 : _b.call(_a));
1260
+ await this.callbacks.onOpen?.();
1053
1261
  this.open.wake();
1054
1262
  }
1055
1263
  async onClose(err) {
1056
- var _a, _b, _c;
1057
1264
  (0, import_log5.log)("onClose", {
1058
1265
  err
1059
1266
  }, {
1060
- F: __dxlog_file5,
1061
- L: 67,
1267
+ F: __dxlog_file6,
1268
+ L: 66,
1062
1269
  S: this,
1063
1270
  C: (f, a) => f(...a)
1064
1271
  });
1065
- await ((_b = (_a = this.callbacks).onClose) == null ? void 0 : _b.call(_a));
1272
+ await this.callbacks.onClose?.();
1066
1273
  this.closed.wake();
1067
- await ((_c = this._rpc) == null ? void 0 : _c.close());
1274
+ await this._rpc?.close();
1068
1275
  }
1069
1276
  async test(message = "test") {
1070
1277
  await this.open.wait({
@@ -1073,25 +1280,32 @@ var TestExtension = class {
1073
1280
  const res = await (0, import_async4.asyncTimeout)(this._rpc.rpc.TestService.testCall({
1074
1281
  data: message
1075
1282
  }), 1500);
1076
- (0, import_tiny_invariant5.default)(res.data === message);
1283
+ (0, import_invariant5.invariant)(res.data === message, void 0, {
1284
+ F: __dxlog_file6,
1285
+ L: 75,
1286
+ S: this,
1287
+ A: [
1288
+ "res.data === message",
1289
+ ""
1290
+ ]
1291
+ });
1077
1292
  }
1078
1293
  /**
1079
1294
  * Force-close the connection.
1080
1295
  */
1081
1296
  async closeConnection(err) {
1082
- var _a;
1083
- (_a = this.extensionContext) == null ? void 0 : _a.close(err);
1297
+ this.extensionContext?.close(err);
1084
1298
  }
1085
1299
  };
1086
1300
 
1087
1301
  // packages/core/mesh/teleport/src/testing/test-extension-with-streams.ts
1088
- var import_node_assert = __toESM(require("node:assert"));
1089
1302
  var import_node_crypto = require("node:crypto");
1090
1303
  var import_async5 = require("@dxos/async");
1304
+ var import_invariant6 = require("@dxos/invariant");
1091
1305
  var import_log6 = require("@dxos/log");
1092
1306
  var import_protocols4 = require("@dxos/protocols");
1093
1307
  var import_rpc3 = require("@dxos/rpc");
1094
- var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/testing/test-extension-with-streams.ts";
1308
+ var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/testing/test-extension-with-streams.ts";
1095
1309
  var TestExtensionWithStreams = class {
1096
1310
  constructor(callbacks = {}) {
1097
1311
  this.callbacks = callbacks;
@@ -1100,11 +1314,18 @@ var TestExtensionWithStreams = class {
1100
1314
  this._streams = /* @__PURE__ */ new Map();
1101
1315
  }
1102
1316
  get remotePeerId() {
1103
- var _a;
1104
- return (_a = this.extensionContext) == null ? void 0 : _a.remotePeerId;
1317
+ return this.extensionContext?.remotePeerId;
1105
1318
  }
1106
1319
  async _openStream(streamTag, interval = 5, chunkSize = 2048) {
1107
- (0, import_node_assert.default)(!this._streams.has(streamTag), `Stream already exists: ${streamTag}`);
1320
+ (0, import_invariant6.invariant)(!this._streams.has(streamTag), `Stream already exists: ${streamTag}`, {
1321
+ F: __dxlog_file7,
1322
+ L: 37,
1323
+ S: this,
1324
+ A: [
1325
+ "!this._streams.has(streamTag)",
1326
+ "`Stream already exists: ${streamTag}`"
1327
+ ]
1328
+ });
1108
1329
  const networkStream = await this.extensionContext.createStream(streamTag, {
1109
1330
  contentType: "application/x-test-stream"
1110
1331
  });
@@ -1144,7 +1365,6 @@ var TestExtensionWithStreams = class {
1144
1365
  networkStream.removeAllListeners();
1145
1366
  });
1146
1367
  streamEntry.reportingTimer = setInterval(() => {
1147
- var _a, _b;
1148
1368
  const { bytesSent, bytesReceived, sendErrors, receiveErrors } = streamEntry;
1149
1369
  import_log6.log.trace("dxos.test.stream-stats", {
1150
1370
  streamTag,
@@ -1152,10 +1372,10 @@ var TestExtensionWithStreams = class {
1152
1372
  bytesReceived,
1153
1373
  sendErrors,
1154
1374
  receiveErrors,
1155
- from: (_a = this.extensionContext) == null ? void 0 : _a.localPeerId,
1156
- to: (_b = this.extensionContext) == null ? void 0 : _b.remotePeerId
1375
+ from: this.extensionContext?.localPeerId,
1376
+ to: this.extensionContext?.remotePeerId
1157
1377
  }, {
1158
- F: __dxlog_file6,
1378
+ F: __dxlog_file7,
1159
1379
  L: 91,
1160
1380
  S: this,
1161
1381
  C: (f, a) => f(...a)
@@ -1163,7 +1383,15 @@ var TestExtensionWithStreams = class {
1163
1383
  }, 100);
1164
1384
  }
1165
1385
  _closeStream(streamTag) {
1166
- (0, import_node_assert.default)(this._streams.has(streamTag), `Stream does not exist: ${streamTag}`);
1386
+ (0, import_invariant6.invariant)(this._streams.has(streamTag), `Stream does not exist: ${streamTag}`, {
1387
+ F: __dxlog_file7,
1388
+ L: 104,
1389
+ S: this,
1390
+ A: [
1391
+ "this._streams.has(streamTag)",
1392
+ "`Stream does not exist: ${streamTag}`"
1393
+ ]
1394
+ });
1167
1395
  const stream = this._streams.get(streamTag);
1168
1396
  clearTimeout(stream.timer);
1169
1397
  clearTimeout(stream.reportingTimer);
@@ -1175,16 +1403,15 @@ var TestExtensionWithStreams = class {
1175
1403
  bytesReceived,
1176
1404
  sendErrors,
1177
1405
  receiveErrors,
1178
- runningTime: Date.now() - (startTimestamp != null ? startTimestamp : 0)
1406
+ runningTime: Date.now() - (startTimestamp ?? 0)
1179
1407
  };
1180
1408
  }
1181
1409
  async onOpen(context) {
1182
- var _a, _b;
1183
1410
  (0, import_log6.log)("onOpen", {
1184
1411
  localPeerId: context.localPeerId,
1185
1412
  remotePeerId: context.remotePeerId
1186
1413
  }, {
1187
- F: __dxlog_file6,
1414
+ F: __dxlog_file7,
1188
1415
  L: 126,
1189
1416
  S: this,
1190
1417
  C: (f, a) => f(...a)
@@ -1226,26 +1453,25 @@ var TestExtensionWithStreams = class {
1226
1453
  timeout: 2e3
1227
1454
  });
1228
1455
  await this._rpc.open();
1229
- await ((_b = (_a = this.callbacks).onOpen) == null ? void 0 : _b.call(_a));
1456
+ await this.callbacks.onOpen?.();
1230
1457
  this.open.wake();
1231
1458
  }
1232
1459
  async onClose(err) {
1233
- var _a, _b, _c;
1234
1460
  (0, import_log6.log)("onClose", {
1235
1461
  err
1236
1462
  }, {
1237
- F: __dxlog_file6,
1463
+ F: __dxlog_file7,
1238
1464
  L: 177,
1239
1465
  S: this,
1240
1466
  C: (f, a) => f(...a)
1241
1467
  });
1242
- await ((_b = (_a = this.callbacks).onClose) == null ? void 0 : _b.call(_a));
1468
+ await this.callbacks.onClose?.();
1243
1469
  this.closed.wake();
1244
1470
  for (const [streamTag, stream] of Object.entries(this._streams)) {
1245
1471
  (0, import_log6.log)("closing stream", {
1246
1472
  streamTag
1247
1473
  }, {
1248
- F: __dxlog_file6,
1474
+ F: __dxlog_file7,
1249
1475
  L: 181,
1250
1476
  S: this,
1251
1477
  C: (f, a) => f(...a)
@@ -1253,7 +1479,7 @@ var TestExtensionWithStreams = class {
1253
1479
  clearTimeout(stream.interval);
1254
1480
  stream.networkStream.destroy();
1255
1481
  }
1256
- await ((_c = this._rpc) == null ? void 0 : _c.close());
1482
+ await this._rpc?.close();
1257
1483
  }
1258
1484
  async addNewStream(streamLoadInterval, streamLoadChunkSize, streamTag) {
1259
1485
  await this.open.wait({
@@ -1267,7 +1493,15 @@ var TestExtensionWithStreams = class {
1267
1493
  streamLoadInterval,
1268
1494
  streamLoadChunkSize
1269
1495
  });
1270
- (0, import_node_assert.default)(data === streamTag);
1496
+ (0, import_invariant6.invariant)(data === streamTag, void 0, {
1497
+ F: __dxlog_file7,
1498
+ L: 198,
1499
+ S: this,
1500
+ A: [
1501
+ "data === streamTag",
1502
+ ""
1503
+ ]
1504
+ });
1271
1505
  await this._openStream(streamTag, streamLoadInterval, streamLoadChunkSize);
1272
1506
  return streamTag;
1273
1507
  }
@@ -1278,7 +1512,15 @@ var TestExtensionWithStreams = class {
1278
1512
  const { data, bytesSent, bytesReceived, sendErrors, receiveErrors, runningTime } = await this._rpc.rpc.TestServiceWithStreams.closeTestStream({
1279
1513
  data: streamTag
1280
1514
  });
1281
- (0, import_node_assert.default)(data === streamTag);
1515
+ (0, import_invariant6.invariant)(data === streamTag, void 0, {
1516
+ F: __dxlog_file7,
1517
+ L: 211,
1518
+ S: this,
1519
+ A: [
1520
+ "data === streamTag",
1521
+ ""
1522
+ ]
1523
+ });
1282
1524
  const local = this._closeStream(streamTag);
1283
1525
  return {
1284
1526
  streamTag,
@@ -1298,8 +1540,7 @@ var TestExtensionWithStreams = class {
1298
1540
  * Force-close the connection.
1299
1541
  */
1300
1542
  async closeConnection(err) {
1301
- var _a;
1302
- (_a = this.extensionContext) == null ? void 0 : _a.close(err);
1543
+ this.extensionContext?.close(err);
1303
1544
  }
1304
1545
  };
1305
1546
  // Annotate the CommonJS export names for ESM import in node: