@dxos/teleport 0.1.56-main.c19b7cd → 0.1.56-main.c277f68

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -115,7 +115,6 @@ var Framer = class {
115
115
  });
116
116
  },
117
117
  subscribe: (callback) => {
118
- var _a;
119
118
  (0, import_invariant.invariant)(!this._messageCb, "Rpc port already has a message listener.", {
120
119
  F: __dxlog_file,
121
120
  L: 73,
@@ -126,7 +125,7 @@ var Framer = class {
126
125
  ]
127
126
  });
128
127
  this._messageCb = callback;
129
- (_a = this._subscribeCb) == null ? void 0 : _a.call(this);
128
+ this._subscribeCb?.();
130
129
  return () => {
131
130
  this._messageCb = void 0;
132
131
  };
@@ -315,16 +314,15 @@ var Balancer = class {
315
314
  return chunk;
316
315
  }
317
316
  async _sendChunks() {
318
- var _a, _b;
319
317
  if (this._sendBuffers.size === 0) {
320
318
  return;
321
319
  }
322
320
  const chunk = this._getNextChunk();
323
321
  try {
324
322
  await this._framer.port.send(chunk.msg);
325
- (_a = chunk.trigger) == null ? void 0 : _a.wake();
323
+ chunk.trigger?.wake();
326
324
  } catch (err) {
327
- (_b = chunk.trigger) == null ? void 0 : _b.throw(err);
325
+ chunk.trigger?.throw(err);
328
326
  }
329
327
  await this._sendChunks();
330
328
  }
@@ -374,6 +372,7 @@ var Muxer = class {
374
372
  this.close = new import_async2.Event();
375
373
  this.statsUpdated = new import_async2.Event();
376
374
  this.stream = this._balancer.stream;
375
+ this._lastChannelStats = /* @__PURE__ */ new Map();
377
376
  this._balancer.incomingData.on(async (msg) => {
378
377
  await this._handleCommand(Command.decode(msg));
379
378
  });
@@ -392,7 +391,7 @@ var Muxer = class {
392
391
  });
393
392
  (0, import_invariant2.invariant)(!channel.push, `Channel already open: ${tag}`, {
394
393
  F: __dxlog_file3,
395
- L: 91,
394
+ L: 131,
396
395
  S: this,
397
396
  A: [
398
397
  "!channel.push",
@@ -440,7 +439,7 @@ var Muxer = class {
440
439
  });
441
440
  (0, import_invariant2.invariant)(!channel.push, `Channel already open: ${tag}`, {
442
441
  F: __dxlog_file3,
443
- L: 143,
442
+ L: 183,
444
443
  S: this,
445
444
  A: [
446
445
  "!channel.push",
@@ -464,7 +463,7 @@ var Muxer = class {
464
463
  subscribe: (cb) => {
465
464
  (0, import_invariant2.invariant)(!callback, "Only one subscriber is allowed", {
466
465
  F: __dxlog_file3,
467
- L: 165,
466
+ L: 205,
468
467
  S: this,
469
468
  A: [
470
469
  "!callback",
@@ -502,7 +501,7 @@ var Muxer = class {
502
501
  this._destroying = true;
503
502
  this._sendCommand({
504
503
  destroy: {
505
- error: err == null ? void 0 : err.message
504
+ error: err?.message
506
505
  }
507
506
  }, SYSTEM_CHANNEL_ID).then(() => {
508
507
  this._dispose();
@@ -512,26 +511,24 @@ var Muxer = class {
512
511
  void this._ctx.dispose();
513
512
  }
514
513
  _dispose(err) {
515
- var _a;
516
514
  if (this._destroyed) {
517
515
  return;
518
516
  }
519
517
  this._destroyed = true;
520
518
  this._balancer.destroy();
521
519
  for (const channel of this._channelsByTag.values()) {
522
- (_a = channel.destroy) == null ? void 0 : _a.call(channel, err);
520
+ channel.destroy?.(err);
523
521
  }
524
522
  this.close.emit(err);
525
523
  this._channelsByLocalId.clear();
526
524
  this._channelsByTag.clear();
527
525
  }
528
526
  async _handleCommand(cmd) {
529
- var _a;
530
527
  (0, import_log2.log)("Received command", {
531
528
  cmd
532
529
  }, {
533
530
  F: __dxlog_file3,
534
- L: 240,
531
+ L: 280,
535
532
  S: this,
536
533
  C: (f, a) => f(...a)
537
534
  });
@@ -543,7 +540,7 @@ var Muxer = class {
543
540
  cmd
544
541
  }, {
545
542
  F: __dxlog_file3,
546
- L: 247,
543
+ L: 287,
547
544
  S: this,
548
545
  C: (f, a) => f(...a)
549
546
  });
@@ -565,13 +562,13 @@ var Muxer = class {
565
562
  }
566
563
  channel.buffer = [];
567
564
  } else if (cmd.data) {
568
- const stream = (_a = this._channelsByLocalId.get(cmd.data.channelId)) != null ? _a : (0, import_debug.failUndefined)();
565
+ const stream = this._channelsByLocalId.get(cmd.data.channelId) ?? (0, import_debug.failUndefined)();
569
566
  if (!stream.push) {
570
567
  import_log2.log.warn("Received data for channel before it was opened", {
571
568
  tag: stream.tag
572
569
  }, {
573
570
  F: __dxlog_file3,
574
- L: 274,
571
+ L: 314,
575
572
  S: this,
576
573
  C: (f, a) => f(...a)
577
574
  });
@@ -620,7 +617,7 @@ var Muxer = class {
620
617
  threshold: MAX_SAFE_FRAME_SIZE
621
618
  }, {
622
619
  F: __dxlog_file3,
623
- L: 318,
620
+ L: 359,
624
621
  S: this,
625
622
  C: (f, a) => f(...a)
626
623
  });
@@ -646,21 +643,40 @@ var Muxer = class {
646
643
  }
647
644
  async _emitStats() {
648
645
  if (this._destroyed || this._destroying) {
646
+ this._lastStats = void 0;
647
+ this._lastChannelStats.clear();
649
648
  return;
650
649
  }
651
650
  const bytesSent = this._balancer.bytesSent;
652
651
  const bytesReceived = this._balancer.bytesReceived;
653
- this.statsUpdated.emit({
654
- channels: Array.from(this._channelsByTag.values()).map((channel) => ({
655
- id: channel.id,
656
- tag: channel.tag,
657
- contentType: channel.contentType,
658
- bytesSent: channel.stats.bytesSent,
659
- bytesReceived: channel.stats.bytesReceived
660
- })),
652
+ const now = Date.now();
653
+ const interval = this._lastStats ? (now - this._lastStats.timestamp) / 1e3 : 0;
654
+ const calculateThroughput = (current, last) => last ? {
655
+ bytesSentRate: interval ? (current.bytesSent - last.bytesSent) / interval : void 0,
656
+ bytesReceivedRate: interval ? (current.bytesReceived - last.bytesReceived) / interval : void 0
657
+ } : {};
658
+ this._lastStats = {
659
+ timestamp: now,
660
+ channels: Array.from(this._channelsByTag.values()).map((channel) => {
661
+ const stats = {
662
+ id: channel.id,
663
+ tag: channel.tag,
664
+ contentType: channel.contentType,
665
+ bytesSent: channel.stats.bytesSent,
666
+ bytesReceived: channel.stats.bytesReceived,
667
+ ...calculateThroughput(channel.stats, this._lastChannelStats.get(channel.id))
668
+ };
669
+ this._lastChannelStats.set(channel.id, stats);
670
+ return stats;
671
+ }),
661
672
  bytesSent,
662
- bytesReceived
663
- });
673
+ bytesReceived,
674
+ ...calculateThroughput({
675
+ bytesSent,
676
+ bytesReceived
677
+ }, this._lastStats)
678
+ };
679
+ this.statsUpdated.emit(this._lastStats);
664
680
  }
665
681
  };
666
682
 
@@ -776,11 +792,13 @@ var Teleport = class {
776
792
  localPeerId,
777
793
  remotePeerId,
778
794
  bytesSent: stats.bytesSent,
795
+ bytesSentRate: stats.bytesSentRate,
779
796
  bytesReceived: stats.bytesReceived,
797
+ bytesReceivedRate: stats.bytesReceivedRate,
780
798
  channels: stats.channels
781
799
  }, {
782
800
  F: __dxlog_file4,
783
- L: 88,
801
+ L: 89,
784
802
  S: this,
785
803
  C: (f, a) => f(...a)
786
804
  });
@@ -814,7 +832,7 @@ var Teleport = class {
814
832
  } catch (err2) {
815
833
  import_log3.log.catch(err2, void 0, {
816
834
  F: __dxlog_file4,
817
- L: 132,
835
+ L: 137,
818
836
  S: this,
819
837
  C: (f, a) => f(...a)
820
838
  });
@@ -830,7 +848,7 @@ var Teleport = class {
830
848
  name
831
849
  }, {
832
850
  F: __dxlog_file4,
833
- L: 144,
851
+ L: 149,
834
852
  S: this,
835
853
  C: (f, a) => f(...a)
836
854
  });
@@ -854,7 +872,7 @@ var Teleport = class {
854
872
  _setExtension(extensionName, extension) {
855
873
  (0, import_invariant3.invariant)(!extensionName.includes("/"), "Invalid extension name", {
856
874
  F: __dxlog_file4,
857
- L: 168,
875
+ L: 173,
858
876
  S: this,
859
877
  A: [
860
878
  "!extensionName.includes('/')",
@@ -863,7 +881,7 @@ var Teleport = class {
863
881
  });
864
882
  (0, import_invariant3.invariant)(!this._extensions.has(extensionName), "Extension already exists", {
865
883
  F: __dxlog_file4,
866
- L: 169,
884
+ L: 174,
867
885
  S: this,
868
886
  A: [
869
887
  "!this._extensions.has(extensionName)",
@@ -873,16 +891,15 @@ var Teleport = class {
873
891
  this._extensions.set(extensionName, extension);
874
892
  }
875
893
  async _openExtension(extensionName) {
876
- var _a;
877
894
  (0, import_log3.log)("open extension", {
878
895
  extensionName
879
896
  }, {
880
897
  F: __dxlog_file4,
881
- L: 174,
898
+ L: 179,
882
899
  S: this,
883
900
  C: (f, a) => f(...a)
884
901
  });
885
- const extension = (_a = this._extensions.get(extensionName)) != null ? _a : (0, import_debug2.failUndefined)();
902
+ const extension = this._extensions.get(extensionName) ?? (0, import_debug2.failUndefined)();
886
903
  const context = {
887
904
  initiator: this.initiator,
888
905
  localPeerId: this.localPeerId,
@@ -890,7 +907,7 @@ var Teleport = class {
890
907
  createPort: async (channelName, opts) => {
891
908
  (0, import_invariant3.invariant)(!channelName.includes("/"), "Invalid channel name", {
892
909
  F: __dxlog_file4,
893
- L: 182,
910
+ L: 187,
894
911
  S: this,
895
912
  A: [
896
913
  "!channelName.includes('/')",
@@ -902,7 +919,7 @@ var Teleport = class {
902
919
  createStream: async (channelName, opts) => {
903
920
  (0, import_invariant3.invariant)(!channelName.includes("/"), "Invalid channel name", {
904
921
  F: __dxlog_file4,
905
- L: 186,
922
+ L: 191,
906
923
  S: this,
907
924
  A: [
908
925
  "!channelName.includes('/')",
@@ -922,7 +939,7 @@ var Teleport = class {
922
939
  extensionName
923
940
  }, {
924
941
  F: __dxlog_file4,
925
- L: 197,
942
+ L: 202,
926
943
  S: this,
927
944
  C: (f, a) => f(...a)
928
945
  });
@@ -1198,11 +1215,9 @@ var TestExtension = class {
1198
1215
  this.closed = new import_async4.Trigger();
1199
1216
  }
1200
1217
  get remotePeerId() {
1201
- var _a;
1202
- return (_a = this.extensionContext) == null ? void 0 : _a.remotePeerId;
1218
+ return this.extensionContext?.remotePeerId;
1203
1219
  }
1204
1220
  async onOpen(context) {
1205
- var _a, _b;
1206
1221
  (0, import_log5.log)("onOpen", {
1207
1222
  localPeerId: context.localPeerId,
1208
1223
  remotePeerId: context.remotePeerId
@@ -1237,11 +1252,10 @@ var TestExtension = class {
1237
1252
  timeout: 2e3
1238
1253
  });
1239
1254
  await this._rpc.open();
1240
- await ((_b = (_a = this.callbacks).onOpen) == null ? void 0 : _b.call(_a));
1255
+ await this.callbacks.onOpen?.();
1241
1256
  this.open.wake();
1242
1257
  }
1243
1258
  async onClose(err) {
1244
- var _a, _b, _c;
1245
1259
  (0, import_log5.log)("onClose", {
1246
1260
  err
1247
1261
  }, {
@@ -1250,9 +1264,9 @@ var TestExtension = class {
1250
1264
  S: this,
1251
1265
  C: (f, a) => f(...a)
1252
1266
  });
1253
- await ((_b = (_a = this.callbacks).onClose) == null ? void 0 : _b.call(_a));
1267
+ await this.callbacks.onClose?.();
1254
1268
  this.closed.wake();
1255
- await ((_c = this._rpc) == null ? void 0 : _c.close());
1269
+ await this._rpc?.close();
1256
1270
  }
1257
1271
  async test(message = "test") {
1258
1272
  await this.open.wait({
@@ -1275,8 +1289,7 @@ var TestExtension = class {
1275
1289
  * Force-close the connection.
1276
1290
  */
1277
1291
  async closeConnection(err) {
1278
- var _a;
1279
- (_a = this.extensionContext) == null ? void 0 : _a.close(err);
1292
+ this.extensionContext?.close(err);
1280
1293
  }
1281
1294
  };
1282
1295
 
@@ -1296,8 +1309,7 @@ var TestExtensionWithStreams = class {
1296
1309
  this._streams = /* @__PURE__ */ new Map();
1297
1310
  }
1298
1311
  get remotePeerId() {
1299
- var _a;
1300
- return (_a = this.extensionContext) == null ? void 0 : _a.remotePeerId;
1312
+ return this.extensionContext?.remotePeerId;
1301
1313
  }
1302
1314
  async _openStream(streamTag, interval = 5, chunkSize = 2048) {
1303
1315
  (0, import_invariant6.invariant)(!this._streams.has(streamTag), `Stream already exists: ${streamTag}`, {
@@ -1348,7 +1360,6 @@ var TestExtensionWithStreams = class {
1348
1360
  networkStream.removeAllListeners();
1349
1361
  });
1350
1362
  streamEntry.reportingTimer = setInterval(() => {
1351
- var _a, _b;
1352
1363
  const { bytesSent, bytesReceived, sendErrors, receiveErrors } = streamEntry;
1353
1364
  import_log6.log.trace("dxos.test.stream-stats", {
1354
1365
  streamTag,
@@ -1356,8 +1367,8 @@ var TestExtensionWithStreams = class {
1356
1367
  bytesReceived,
1357
1368
  sendErrors,
1358
1369
  receiveErrors,
1359
- from: (_a = this.extensionContext) == null ? void 0 : _a.localPeerId,
1360
- to: (_b = this.extensionContext) == null ? void 0 : _b.remotePeerId
1370
+ from: this.extensionContext?.localPeerId,
1371
+ to: this.extensionContext?.remotePeerId
1361
1372
  }, {
1362
1373
  F: __dxlog_file7,
1363
1374
  L: 91,
@@ -1387,11 +1398,10 @@ var TestExtensionWithStreams = class {
1387
1398
  bytesReceived,
1388
1399
  sendErrors,
1389
1400
  receiveErrors,
1390
- runningTime: Date.now() - (startTimestamp != null ? startTimestamp : 0)
1401
+ runningTime: Date.now() - (startTimestamp ?? 0)
1391
1402
  };
1392
1403
  }
1393
1404
  async onOpen(context) {
1394
- var _a, _b;
1395
1405
  (0, import_log6.log)("onOpen", {
1396
1406
  localPeerId: context.localPeerId,
1397
1407
  remotePeerId: context.remotePeerId
@@ -1438,11 +1448,10 @@ var TestExtensionWithStreams = class {
1438
1448
  timeout: 2e3
1439
1449
  });
1440
1450
  await this._rpc.open();
1441
- await ((_b = (_a = this.callbacks).onOpen) == null ? void 0 : _b.call(_a));
1451
+ await this.callbacks.onOpen?.();
1442
1452
  this.open.wake();
1443
1453
  }
1444
1454
  async onClose(err) {
1445
- var _a, _b, _c;
1446
1455
  (0, import_log6.log)("onClose", {
1447
1456
  err
1448
1457
  }, {
@@ -1451,7 +1460,7 @@ var TestExtensionWithStreams = class {
1451
1460
  S: this,
1452
1461
  C: (f, a) => f(...a)
1453
1462
  });
1454
- await ((_b = (_a = this.callbacks).onClose) == null ? void 0 : _b.call(_a));
1463
+ await this.callbacks.onClose?.();
1455
1464
  this.closed.wake();
1456
1465
  for (const [streamTag, stream] of Object.entries(this._streams)) {
1457
1466
  (0, import_log6.log)("closing stream", {
@@ -1465,7 +1474,7 @@ var TestExtensionWithStreams = class {
1465
1474
  clearTimeout(stream.interval);
1466
1475
  stream.networkStream.destroy();
1467
1476
  }
1468
- await ((_c = this._rpc) == null ? void 0 : _c.close());
1477
+ await this._rpc?.close();
1469
1478
  }
1470
1479
  async addNewStream(streamLoadInterval, streamLoadChunkSize, streamTag) {
1471
1480
  await this.open.wait({
@@ -1526,8 +1535,7 @@ var TestExtensionWithStreams = class {
1526
1535
  * Force-close the connection.
1527
1536
  */
1528
1537
  async closeConnection(err) {
1529
- var _a;
1530
- (_a = this.extensionContext) == null ? void 0 : _a.close(err);
1538
+ this.extensionContext?.close(err);
1531
1539
  }
1532
1540
  };
1533
1541
  // Annotate the CommonJS export names for ESM import in node: