@dxos/teleport 0.8.4-main.5ad4a44 → 0.8.4-main.66e292d

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.
@@ -132,23 +132,29 @@ import { RpcClosedError } from "@dxos/protocols";
132
132
  import { schema } from "@dxos/protocols/proto";
133
133
  import { createProtoRpcPeer } from "@dxos/rpc";
134
134
  import { Callback } from "@dxos/util";
135
- function _define_property(obj, key, value) {
136
- if (key in obj) {
137
- Object.defineProperty(obj, key, {
138
- value,
139
- enumerable: true,
140
- configurable: true,
141
- writable: true
142
- });
143
- } else {
144
- obj[key] = value;
145
- }
146
- return obj;
147
- }
148
135
  var __dxlog_file = "/__w/dxos/dxos/packages/core/mesh/teleport/src/control-extension.ts";
149
136
  var HEARTBEAT_RTT_WARN_THRESH = 1e4;
150
137
  var DEBUG_PRINT_HEARTBEAT = false;
151
138
  var ControlExtension = class {
139
+ opts;
140
+ localPeerId;
141
+ remotePeerId;
142
+ _ctx = new Context({
143
+ onError: (err) => {
144
+ this._extensionContext.close(err);
145
+ }
146
+ }, {
147
+ F: __dxlog_file,
148
+ L: 31
149
+ });
150
+ onExtensionRegistered = new Callback();
151
+ _extensionContext;
152
+ _rpc;
153
+ constructor(opts, localPeerId, remotePeerId) {
154
+ this.opts = opts;
155
+ this.localPeerId = localPeerId;
156
+ this.remotePeerId = remotePeerId;
157
+ }
152
158
  async registerExtension(name) {
153
159
  await this._rpc.rpc.Control.registerExtension({
154
160
  name
@@ -273,27 +279,6 @@ var ControlExtension = class {
273
279
  await this._ctx.dispose();
274
280
  await this._rpc.abort();
275
281
  }
276
- constructor(opts, localPeerId, remotePeerId) {
277
- _define_property(this, "opts", void 0);
278
- _define_property(this, "localPeerId", void 0);
279
- _define_property(this, "remotePeerId", void 0);
280
- _define_property(this, "_ctx", void 0);
281
- _define_property(this, "onExtensionRegistered", void 0);
282
- _define_property(this, "_extensionContext", void 0);
283
- _define_property(this, "_rpc", void 0);
284
- this.opts = opts;
285
- this.localPeerId = localPeerId;
286
- this.remotePeerId = remotePeerId;
287
- this._ctx = new Context({
288
- onError: (err) => {
289
- this._extensionContext.close(err);
290
- }
291
- }, {
292
- F: __dxlog_file,
293
- L: 31
294
- });
295
- this.onExtensionRegistered = new Callback();
296
- }
297
282
  };
298
283
 
299
284
  // src/muxing/framer.ts
@@ -301,22 +286,85 @@ import { Duplex } from "@dxos/node-std/stream";
301
286
  import { Event } from "@dxos/async";
302
287
  import { invariant } from "@dxos/invariant";
303
288
  import { log as log2 } from "@dxos/log";
304
- function _define_property2(obj, key, value) {
305
- if (key in obj) {
306
- Object.defineProperty(obj, key, {
307
- value,
308
- enumerable: true,
309
- configurable: true,
310
- writable: true
311
- });
312
- } else {
313
- obj[key] = value;
314
- }
315
- return obj;
316
- }
317
289
  var __dxlog_file2 = "/__w/dxos/dxos/packages/core/mesh/teleport/src/muxing/framer.ts";
318
290
  var FRAME_LENGTH_SIZE = 2;
319
291
  var Framer = class {
292
+ // private readonly _tagBuffer = Buffer.alloc(4)
293
+ _messageCb = void 0;
294
+ _subscribeCb = void 0;
295
+ _buffer = void 0;
296
+ _sendCallbacks = [];
297
+ _bytesSent = 0;
298
+ _bytesReceived = 0;
299
+ _writable = true;
300
+ drain = new Event();
301
+ // TODO(egorgripasov): Consider using a Transform stream if it provides better backpressure handling.
302
+ _stream = new Duplex({
303
+ objectMode: false,
304
+ read: () => {
305
+ this._processResponseQueue();
306
+ },
307
+ write: (chunk, encoding, callback) => {
308
+ invariant(!this._subscribeCb, "Internal Framer bug. Concurrent writes detected.", {
309
+ F: __dxlog_file2,
310
+ L: 40,
311
+ S: this,
312
+ A: [
313
+ "!this._subscribeCb",
314
+ "'Internal Framer bug. Concurrent writes detected.'"
315
+ ]
316
+ });
317
+ this._bytesReceived += chunk.length;
318
+ if (this._buffer && this._buffer.length > 0) {
319
+ this._buffer = Buffer.concat([
320
+ this._buffer,
321
+ chunk
322
+ ]);
323
+ } else {
324
+ this._buffer = chunk;
325
+ }
326
+ if (this._messageCb) {
327
+ this._popFrames();
328
+ callback();
329
+ } else {
330
+ this._subscribeCb = () => {
331
+ this._popFrames();
332
+ this._subscribeCb = void 0;
333
+ callback();
334
+ };
335
+ }
336
+ }
337
+ });
338
+ port = {
339
+ send: (message) => {
340
+ return new Promise((resolve) => {
341
+ const frame = encodeFrame(message);
342
+ this._bytesSent += frame.length;
343
+ this._writable = this._stream.push(frame);
344
+ if (!this._writable) {
345
+ this._sendCallbacks.push(resolve);
346
+ } else {
347
+ resolve();
348
+ }
349
+ });
350
+ },
351
+ subscribe: (callback) => {
352
+ invariant(!this._messageCb, "Rpc port already has a message listener.", {
353
+ F: __dxlog_file2,
354
+ L: 79,
355
+ S: this,
356
+ A: [
357
+ "!this._messageCb",
358
+ "'Rpc port already has a message listener.'"
359
+ ]
360
+ });
361
+ this._messageCb = callback;
362
+ this._subscribeCb?.();
363
+ return () => {
364
+ this._messageCb = void 0;
365
+ };
366
+ }
367
+ };
320
368
  get stream() {
321
369
  return this._stream;
322
370
  }
@@ -374,82 +422,6 @@ var Framer = class {
374
422
  }
375
423
  this._stream.destroy();
376
424
  }
377
- constructor() {
378
- _define_property2(this, "_messageCb", void 0);
379
- _define_property2(this, "_subscribeCb", void 0);
380
- _define_property2(this, "_buffer", void 0);
381
- _define_property2(this, "_sendCallbacks", []);
382
- _define_property2(this, "_bytesSent", 0);
383
- _define_property2(this, "_bytesReceived", 0);
384
- _define_property2(this, "_writable", true);
385
- _define_property2(this, "drain", new Event());
386
- _define_property2(this, "_stream", new Duplex({
387
- objectMode: false,
388
- read: () => {
389
- this._processResponseQueue();
390
- },
391
- write: (chunk, encoding, callback) => {
392
- invariant(!this._subscribeCb, "Internal Framer bug. Concurrent writes detected.", {
393
- F: __dxlog_file2,
394
- L: 40,
395
- S: this,
396
- A: [
397
- "!this._subscribeCb",
398
- "'Internal Framer bug. Concurrent writes detected.'"
399
- ]
400
- });
401
- this._bytesReceived += chunk.length;
402
- if (this._buffer && this._buffer.length > 0) {
403
- this._buffer = Buffer.concat([
404
- this._buffer,
405
- chunk
406
- ]);
407
- } else {
408
- this._buffer = chunk;
409
- }
410
- if (this._messageCb) {
411
- this._popFrames();
412
- callback();
413
- } else {
414
- this._subscribeCb = () => {
415
- this._popFrames();
416
- this._subscribeCb = void 0;
417
- callback();
418
- };
419
- }
420
- }
421
- }));
422
- _define_property2(this, "port", {
423
- send: (message) => {
424
- return new Promise((resolve) => {
425
- const frame = encodeFrame(message);
426
- this._bytesSent += frame.length;
427
- this._writable = this._stream.push(frame);
428
- if (!this._writable) {
429
- this._sendCallbacks.push(resolve);
430
- } else {
431
- resolve();
432
- }
433
- });
434
- },
435
- subscribe: (callback) => {
436
- invariant(!this._messageCb, "Rpc port already has a message listener.", {
437
- F: __dxlog_file2,
438
- L: 79,
439
- S: this,
440
- A: [
441
- "!this._messageCb",
442
- "'Rpc port already has a message listener.'"
443
- ]
444
- });
445
- this._messageCb = callback;
446
- this._subscribeCb?.();
447
- return () => {
448
- this._messageCb = void 0;
449
- };
450
- }
451
- });
452
- }
453
425
  };
454
426
  var decodeFrame = (buffer, offset) => {
455
427
  if (buffer.length < offset + FRAME_LENGTH_SIZE) {
@@ -488,22 +460,24 @@ var import_varint = __toESM(require_varint(), 1);
488
460
  import { Event as Event2 } from "@dxos/async";
489
461
  import { invariant as invariant2 } from "@dxos/invariant";
490
462
  import { log as log3 } from "@dxos/log";
491
- function _define_property3(obj, key, value) {
492
- if (key in obj) {
493
- Object.defineProperty(obj, key, {
494
- value,
495
- enumerable: true,
496
- configurable: true,
497
- writable: true
498
- });
499
- } else {
500
- obj[key] = value;
501
- }
502
- return obj;
503
- }
504
463
  var __dxlog_file3 = "/__w/dxos/dxos/packages/core/mesh/teleport/src/muxing/balancer.ts";
505
464
  var MAX_CHUNK_SIZE = 8192;
506
465
  var Balancer = class {
466
+ _sysChannelId;
467
+ _lastCallerIndex = 0;
468
+ _channels = [];
469
+ _framer = new Framer();
470
+ // TODO(egorgripasov): Will cause a memory leak if channels do not appreciate the backpressure.
471
+ _sendBuffers = /* @__PURE__ */ new Map();
472
+ _receiveBuffers = /* @__PURE__ */ new Map();
473
+ _sending = false;
474
+ incomingData = new Event2();
475
+ stream = this._framer.stream;
476
+ constructor(_sysChannelId) {
477
+ this._sysChannelId = _sysChannelId;
478
+ this._channels.push(_sysChannelId);
479
+ this._framer.port.subscribe(this._processIncomingMessage.bind(this));
480
+ }
507
481
  get bytesSent() {
508
482
  return this._framer.bytesSent;
509
483
  }
@@ -664,28 +638,6 @@ var Balancer = class {
664
638
  });
665
639
  this._sending = false;
666
640
  }
667
- constructor(_sysChannelId) {
668
- _define_property3(this, "_sysChannelId", void 0);
669
- _define_property3(this, "_lastCallerIndex", void 0);
670
- _define_property3(this, "_channels", void 0);
671
- _define_property3(this, "_framer", void 0);
672
- _define_property3(this, "_sendBuffers", void 0);
673
- _define_property3(this, "_receiveBuffers", void 0);
674
- _define_property3(this, "_sending", void 0);
675
- _define_property3(this, "incomingData", void 0);
676
- _define_property3(this, "stream", void 0);
677
- this._sysChannelId = _sysChannelId;
678
- this._lastCallerIndex = 0;
679
- this._channels = [];
680
- this._framer = new Framer();
681
- this._sendBuffers = /* @__PURE__ */ new Map();
682
- this._receiveBuffers = /* @__PURE__ */ new Map();
683
- this._sending = false;
684
- this.incomingData = new Event2();
685
- this.stream = this._framer.stream;
686
- this._channels.push(_sysChannelId);
687
- this._framer.port.subscribe(this._processIncomingMessage.bind(this));
688
- }
689
641
  };
690
642
  var encodeChunk = ({ channelId, dataLength, chunk }) => {
691
643
  const channelTagLength = import_varint.default.encodingLength(channelId);
@@ -715,19 +667,6 @@ var decodeChunk = (data, withLength) => {
715
667
  };
716
668
 
717
669
  // src/muxing/muxer.ts
718
- function _define_property4(obj, key, value) {
719
- if (key in obj) {
720
- Object.defineProperty(obj, key, {
721
- value,
722
- enumerable: true,
723
- configurable: true,
724
- writable: true
725
- });
726
- } else {
727
- obj[key] = value;
728
- }
729
- return obj;
730
- }
731
670
  function _ts_decorate(decorators, target, key, desc) {
732
671
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
733
672
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -743,6 +682,28 @@ var MAX_SAFE_FRAME_SIZE = 1e6;
743
682
  var SYSTEM_CHANNEL_ID = 0;
744
683
  var GRACEFUL_CLOSE_TIMEOUT = 3e3;
745
684
  var Muxer = class {
685
+ _balancer = new Balancer(SYSTEM_CHANNEL_ID);
686
+ _channelsByLocalId = /* @__PURE__ */ new Map();
687
+ _channelsByTag = /* @__PURE__ */ new Map();
688
+ _ctx = new Context2(void 0, {
689
+ F: __dxlog_file4,
690
+ L: 108
691
+ });
692
+ _sessionId;
693
+ _nextId = 1;
694
+ _closing = false;
695
+ _destroying = false;
696
+ _disposed = false;
697
+ _lastStats = void 0;
698
+ _lastChannelStats = /* @__PURE__ */ new Map();
699
+ afterClosed = new Event3();
700
+ statsUpdated = new Event3();
701
+ stream = this._balancer.stream;
702
+ constructor() {
703
+ this._balancer.incomingData.on(async (msg) => {
704
+ await this._handleCommand(Command.decode(msg));
705
+ });
706
+ }
746
707
  setSessionId(sessionId) {
747
708
  this._sessionId = sessionId;
748
709
  }
@@ -906,14 +867,16 @@ var Muxer = class {
906
867
  });
907
868
  await this._dispose(err2);
908
869
  });
909
- await asyncTimeout2(this._dispose(err), GRACEFUL_CLOSE_TIMEOUT, new TimeoutError("gracefully closing muxer"));
870
+ await asyncTimeout2(this._dispose(err), GRACEFUL_CLOSE_TIMEOUT, new TimeoutError({
871
+ message: "gracefully closing muxer"
872
+ }));
910
873
  }
911
874
  // force close without confirmation
912
875
  async destroy(err) {
913
876
  if (this._destroying) {
914
877
  log4("already destroying, ignoring destroy request", void 0, {
915
878
  F: __dxlog_file4,
916
- L: 299,
879
+ L: 303,
917
880
  S: this,
918
881
  C: (f, a) => f(...a)
919
882
  });
@@ -924,7 +887,7 @@ var Muxer = class {
924
887
  if (this._closing) {
925
888
  log4("destroy cancelling graceful close", void 0, {
926
889
  F: __dxlog_file4,
927
- L: 305,
890
+ L: 309,
928
891
  S: this,
929
892
  C: (f, a) => f(...a)
930
893
  });
@@ -939,7 +902,7 @@ var Muxer = class {
939
902
  err: err2
940
903
  }, {
941
904
  F: __dxlog_file4,
942
- L: 318,
905
+ L: 322,
943
906
  S: this,
944
907
  C: (f, a) => f(...a)
945
908
  });
@@ -950,7 +913,7 @@ var Muxer = class {
950
913
  err: err2
951
914
  }, {
952
915
  F: __dxlog_file4,
953
- L: 323,
916
+ L: 327,
954
917
  S: this,
955
918
  C: (f, a) => f(...a)
956
919
  });
@@ -961,7 +924,7 @@ var Muxer = class {
961
924
  if (this._disposed) {
962
925
  log4("already destroyed, ignoring dispose request", void 0, {
963
926
  F: __dxlog_file4,
964
- L: 331,
927
+ L: 335,
965
928
  S: this,
966
929
  C: (f, a) => f(...a)
967
930
  });
@@ -984,7 +947,7 @@ var Muxer = class {
984
947
  cmd
985
948
  }, {
986
949
  F: __dxlog_file4,
987
- L: 354,
950
+ L: 358,
988
951
  S: this,
989
952
  C: (f, a) => f(...a)
990
953
  });
@@ -994,7 +957,7 @@ var Muxer = class {
994
957
  if (!this._closing) {
995
958
  log4("received peer close, initiating my own graceful close", void 0, {
996
959
  F: __dxlog_file4,
997
- L: 360,
960
+ L: 364,
998
961
  S: this,
999
962
  C: (f, a) => f(...a)
1000
963
  });
@@ -1002,7 +965,7 @@ var Muxer = class {
1002
965
  } else {
1003
966
  log4("received close from peer, already closing", void 0, {
1004
967
  F: __dxlog_file4,
1005
- L: 363,
968
+ L: 367,
1006
969
  S: this,
1007
970
  C: (f, a) => f(...a)
1008
971
  });
@@ -1031,7 +994,7 @@ var Muxer = class {
1031
994
  tag: stream.tag
1032
995
  }, {
1033
996
  F: __dxlog_file4,
1034
- L: 392,
997
+ L: 396,
1035
998
  S: this,
1036
999
  C: (f, a) => f(...a)
1037
1000
  });
@@ -1086,7 +1049,7 @@ var Muxer = class {
1086
1049
  threshold: MAX_SAFE_FRAME_SIZE
1087
1050
  }, {
1088
1051
  F: __dxlog_file4,
1089
- L: 442,
1052
+ L: 446,
1090
1053
  S: this,
1091
1054
  C: (f, a) => f(...a)
1092
1055
  });
@@ -1109,7 +1072,7 @@ var Muxer = class {
1109
1072
  err
1110
1073
  }, {
1111
1074
  F: __dxlog_file4,
1112
- L: 465,
1075
+ L: 469,
1113
1076
  S: this,
1114
1077
  C: (f, a) => f(...a)
1115
1078
  });
@@ -1170,47 +1133,12 @@ var Muxer = class {
1170
1133
  };
1171
1134
  this.statsUpdated.emit(this._lastStats);
1172
1135
  }
1173
- constructor() {
1174
- _define_property4(this, "_balancer", new Balancer(SYSTEM_CHANNEL_ID));
1175
- _define_property4(this, "_channelsByLocalId", /* @__PURE__ */ new Map());
1176
- _define_property4(this, "_channelsByTag", /* @__PURE__ */ new Map());
1177
- _define_property4(this, "_ctx", new Context2(void 0, {
1178
- F: __dxlog_file4,
1179
- L: 108
1180
- }));
1181
- _define_property4(this, "_sessionId", void 0);
1182
- _define_property4(this, "_nextId", 1);
1183
- _define_property4(this, "_closing", false);
1184
- _define_property4(this, "_destroying", false);
1185
- _define_property4(this, "_disposed", false);
1186
- _define_property4(this, "_lastStats", void 0);
1187
- _define_property4(this, "_lastChannelStats", /* @__PURE__ */ new Map());
1188
- _define_property4(this, "afterClosed", new Event3());
1189
- _define_property4(this, "statsUpdated", new Event3());
1190
- _define_property4(this, "stream", this._balancer.stream);
1191
- this._balancer.incomingData.on(async (msg) => {
1192
- await this._handleCommand(Command.decode(msg));
1193
- });
1194
- }
1195
1136
  };
1196
1137
  _ts_decorate([
1197
1138
  logInfo
1198
1139
  ], Muxer.prototype, "sessionIdString", null);
1199
1140
 
1200
1141
  // src/teleport.ts
1201
- function _define_property5(obj, key, value) {
1202
- if (key in obj) {
1203
- Object.defineProperty(obj, key, {
1204
- value,
1205
- enumerable: true,
1206
- configurable: true,
1207
- writable: true
1208
- });
1209
- } else {
1210
- obj[key] = value;
1211
- }
1212
- return obj;
1213
- }
1214
1142
  function _ts_decorate2(decorators, target, key, desc) {
1215
1143
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1216
1144
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -1221,9 +1149,158 @@ var __dxlog_file5 = "/__w/dxos/dxos/packages/core/mesh/teleport/src/teleport.ts"
1221
1149
  var CONTROL_HEARTBEAT_INTERVAL = 1e4;
1222
1150
  var CONTROL_HEARTBEAT_TIMEOUT = 6e4;
1223
1151
  var Teleport = class {
1152
+ initiator;
1153
+ localPeerId;
1154
+ remotePeerId;
1155
+ _sessionId;
1156
+ _ctx = new Context3({
1157
+ onError: (err) => {
1158
+ log5.info("error in teleport context", {
1159
+ err
1160
+ }, {
1161
+ F: __dxlog_file5,
1162
+ L: 40,
1163
+ S: this,
1164
+ C: (f, a) => f(...a)
1165
+ });
1166
+ void this.destroy(err).catch(() => {
1167
+ log5.error("Error during destroy", err, {
1168
+ F: __dxlog_file5,
1169
+ L: 42,
1170
+ S: this,
1171
+ C: (f, a) => f(...a)
1172
+ });
1173
+ });
1174
+ }
1175
+ }, {
1176
+ F: __dxlog_file5,
1177
+ L: 38
1178
+ });
1179
+ _muxer = new Muxer();
1180
+ _control;
1181
+ _extensions = /* @__PURE__ */ new Map();
1182
+ _remoteExtensions = /* @__PURE__ */ new Set();
1183
+ _open = false;
1184
+ _destroying = false;
1185
+ _aborting = false;
1224
1186
  get isOpen() {
1225
1187
  return this._open;
1226
1188
  }
1189
+ constructor({ initiator, localPeerId, remotePeerId, ...rest }) {
1190
+ invariant4(typeof initiator === "boolean", void 0, {
1191
+ F: __dxlog_file5,
1192
+ L: 63,
1193
+ S: this,
1194
+ A: [
1195
+ "typeof initiator === 'boolean'",
1196
+ ""
1197
+ ]
1198
+ });
1199
+ invariant4(PublicKey.isPublicKey(localPeerId), void 0, {
1200
+ F: __dxlog_file5,
1201
+ L: 64,
1202
+ S: this,
1203
+ A: [
1204
+ "PublicKey.isPublicKey(localPeerId)",
1205
+ ""
1206
+ ]
1207
+ });
1208
+ invariant4(PublicKey.isPublicKey(remotePeerId), void 0, {
1209
+ F: __dxlog_file5,
1210
+ L: 65,
1211
+ S: this,
1212
+ A: [
1213
+ "PublicKey.isPublicKey(remotePeerId)",
1214
+ ""
1215
+ ]
1216
+ });
1217
+ this.initiator = initiator;
1218
+ this.localPeerId = localPeerId;
1219
+ this.remotePeerId = remotePeerId;
1220
+ this._control = new ControlExtension({
1221
+ heartbeatInterval: rest.controlHeartbeatInterval ?? CONTROL_HEARTBEAT_INTERVAL,
1222
+ heartbeatTimeout: rest.controlHeartbeatTimeout ?? CONTROL_HEARTBEAT_TIMEOUT,
1223
+ onTimeout: () => {
1224
+ if (this._destroying || this._aborting) {
1225
+ return;
1226
+ }
1227
+ log5.info("abort teleport due to onTimeout in ControlExtension", void 0, {
1228
+ F: __dxlog_file5,
1229
+ L: 78,
1230
+ S: this,
1231
+ C: (f, a) => f(...a)
1232
+ });
1233
+ this.abort(new TimeoutError2({
1234
+ message: "control extension"
1235
+ })).catch((err) => log5.catch(err, void 0, {
1236
+ F: __dxlog_file5,
1237
+ L: 79,
1238
+ S: this,
1239
+ C: (f, a) => f(...a)
1240
+ }));
1241
+ }
1242
+ }, this.localPeerId, this.remotePeerId);
1243
+ this._control.onExtensionRegistered.set(async (name) => {
1244
+ log5("remote extension", {
1245
+ name
1246
+ }, {
1247
+ F: __dxlog_file5,
1248
+ L: 87,
1249
+ S: this,
1250
+ C: (f, a) => f(...a)
1251
+ });
1252
+ invariant4(!this._remoteExtensions.has(name), "Remote extension already exists", {
1253
+ F: __dxlog_file5,
1254
+ L: 88,
1255
+ S: this,
1256
+ A: [
1257
+ "!this._remoteExtensions.has(name)",
1258
+ "'Remote extension already exists'"
1259
+ ]
1260
+ });
1261
+ this._remoteExtensions.add(name);
1262
+ if (this._extensions.has(name)) {
1263
+ try {
1264
+ await this._openExtension(name);
1265
+ } catch (err) {
1266
+ await this.destroy(err);
1267
+ }
1268
+ }
1269
+ });
1270
+ {
1271
+ this._muxer.stream.on("close", async () => {
1272
+ if (this._destroying || this._aborting) {
1273
+ log5("destroy teleport due to muxer stream close, skipping due to already destroying/aborting", void 0, {
1274
+ F: __dxlog_file5,
1275
+ L: 104,
1276
+ S: this,
1277
+ C: (f, a) => f(...a)
1278
+ });
1279
+ return;
1280
+ }
1281
+ await this.destroy();
1282
+ });
1283
+ this._muxer.stream.on("error", async (err) => {
1284
+ await this.destroy(err);
1285
+ });
1286
+ }
1287
+ this._muxer.statsUpdated.on((stats) => {
1288
+ log5.trace("dxos.mesh.teleport.stats", {
1289
+ localPeerId,
1290
+ remotePeerId,
1291
+ bytesSent: stats.bytesSent,
1292
+ bytesSentRate: stats.bytesSentRate,
1293
+ bytesReceived: stats.bytesReceived,
1294
+ bytesReceivedRate: stats.bytesReceivedRate,
1295
+ channels: stats.channels
1296
+ }, {
1297
+ F: __dxlog_file5,
1298
+ L: 117,
1299
+ S: this,
1300
+ C: (f, a) => f(...a)
1301
+ });
1302
+ });
1303
+ }
1227
1304
  get sessionIdString() {
1228
1305
  return this._sessionId ? this._sessionId.truncate() : "none";
1229
1306
  }
@@ -1434,153 +1511,6 @@ var Teleport = class {
1434
1511
  C: (f, a) => f(...a)
1435
1512
  });
1436
1513
  }
1437
- constructor({ initiator, localPeerId, remotePeerId, ...rest }) {
1438
- _define_property5(this, "initiator", void 0);
1439
- _define_property5(this, "localPeerId", void 0);
1440
- _define_property5(this, "remotePeerId", void 0);
1441
- _define_property5(this, "_sessionId", void 0);
1442
- _define_property5(this, "_ctx", new Context3({
1443
- onError: (err) => {
1444
- log5.info("error in teleport context", {
1445
- err
1446
- }, {
1447
- F: __dxlog_file5,
1448
- L: 40,
1449
- S: this,
1450
- C: (f, a) => f(...a)
1451
- });
1452
- void this.destroy(err).catch(() => {
1453
- log5.error("Error during destroy", err, {
1454
- F: __dxlog_file5,
1455
- L: 42,
1456
- S: this,
1457
- C: (f, a) => f(...a)
1458
- });
1459
- });
1460
- }
1461
- }, {
1462
- F: __dxlog_file5,
1463
- L: 38
1464
- }));
1465
- _define_property5(this, "_muxer", new Muxer());
1466
- _define_property5(this, "_control", void 0);
1467
- _define_property5(this, "_extensions", /* @__PURE__ */ new Map());
1468
- _define_property5(this, "_remoteExtensions", /* @__PURE__ */ new Set());
1469
- _define_property5(this, "_open", false);
1470
- _define_property5(this, "_destroying", false);
1471
- _define_property5(this, "_aborting", false);
1472
- invariant4(typeof initiator === "boolean", void 0, {
1473
- F: __dxlog_file5,
1474
- L: 63,
1475
- S: this,
1476
- A: [
1477
- "typeof initiator === 'boolean'",
1478
- ""
1479
- ]
1480
- });
1481
- invariant4(PublicKey.isPublicKey(localPeerId), void 0, {
1482
- F: __dxlog_file5,
1483
- L: 64,
1484
- S: this,
1485
- A: [
1486
- "PublicKey.isPublicKey(localPeerId)",
1487
- ""
1488
- ]
1489
- });
1490
- invariant4(PublicKey.isPublicKey(remotePeerId), void 0, {
1491
- F: __dxlog_file5,
1492
- L: 65,
1493
- S: this,
1494
- A: [
1495
- "PublicKey.isPublicKey(remotePeerId)",
1496
- ""
1497
- ]
1498
- });
1499
- this.initiator = initiator;
1500
- this.localPeerId = localPeerId;
1501
- this.remotePeerId = remotePeerId;
1502
- this._control = new ControlExtension({
1503
- heartbeatInterval: rest.controlHeartbeatInterval ?? CONTROL_HEARTBEAT_INTERVAL,
1504
- heartbeatTimeout: rest.controlHeartbeatTimeout ?? CONTROL_HEARTBEAT_TIMEOUT,
1505
- onTimeout: () => {
1506
- if (this._destroying || this._aborting) {
1507
- return;
1508
- }
1509
- log5.info("abort teleport due to onTimeout in ControlExtension", void 0, {
1510
- F: __dxlog_file5,
1511
- L: 78,
1512
- S: this,
1513
- C: (f, a) => f(...a)
1514
- });
1515
- this.abort(new TimeoutError2("control extension")).catch((err) => log5.catch(err, void 0, {
1516
- F: __dxlog_file5,
1517
- L: 79,
1518
- S: this,
1519
- C: (f, a) => f(...a)
1520
- }));
1521
- }
1522
- }, this.localPeerId, this.remotePeerId);
1523
- this._control.onExtensionRegistered.set(async (name) => {
1524
- log5("remote extension", {
1525
- name
1526
- }, {
1527
- F: __dxlog_file5,
1528
- L: 87,
1529
- S: this,
1530
- C: (f, a) => f(...a)
1531
- });
1532
- invariant4(!this._remoteExtensions.has(name), "Remote extension already exists", {
1533
- F: __dxlog_file5,
1534
- L: 88,
1535
- S: this,
1536
- A: [
1537
- "!this._remoteExtensions.has(name)",
1538
- "'Remote extension already exists'"
1539
- ]
1540
- });
1541
- this._remoteExtensions.add(name);
1542
- if (this._extensions.has(name)) {
1543
- try {
1544
- await this._openExtension(name);
1545
- } catch (err) {
1546
- await this.destroy(err);
1547
- }
1548
- }
1549
- });
1550
- {
1551
- this._muxer.stream.on("close", async () => {
1552
- if (this._destroying || this._aborting) {
1553
- log5("destroy teleport due to muxer stream close, skipping due to already destroying/aborting", void 0, {
1554
- F: __dxlog_file5,
1555
- L: 104,
1556
- S: this,
1557
- C: (f, a) => f(...a)
1558
- });
1559
- return;
1560
- }
1561
- await this.destroy();
1562
- });
1563
- this._muxer.stream.on("error", async (err) => {
1564
- await this.destroy(err);
1565
- });
1566
- }
1567
- this._muxer.statsUpdated.on((stats) => {
1568
- log5.trace("dxos.mesh.teleport.stats", {
1569
- localPeerId,
1570
- remotePeerId,
1571
- bytesSent: stats.bytesSent,
1572
- bytesSentRate: stats.bytesSentRate,
1573
- bytesReceived: stats.bytesReceived,
1574
- bytesReceivedRate: stats.bytesReceivedRate,
1575
- channels: stats.channels
1576
- }, {
1577
- F: __dxlog_file5,
1578
- L: 117,
1579
- S: this,
1580
- C: (f, a) => f(...a)
1581
- });
1582
- });
1583
- }
1584
1514
  };
1585
1515
  _ts_decorate2([
1586
1516
  logInfo2
@@ -1593,21 +1523,9 @@ _ts_decorate2([
1593
1523
  ], Teleport.prototype, "destroy", null);
1594
1524
 
1595
1525
  // src/testing/test-builder.ts
1596
- function _define_property6(obj, key, value) {
1597
- if (key in obj) {
1598
- Object.defineProperty(obj, key, {
1599
- value,
1600
- enumerable: true,
1601
- configurable: true,
1602
- writable: true
1603
- });
1604
- } else {
1605
- obj[key] = value;
1606
- }
1607
- return obj;
1608
- }
1609
1526
  var __dxlog_file6 = "/__w/dxos/dxos/packages/core/mesh/teleport/src/testing/test-builder.ts";
1610
1527
  var TestBuilder = class {
1528
+ _peers = /* @__PURE__ */ new Set();
1611
1529
  createPeer(opts) {
1612
1530
  const peer = opts.factory();
1613
1531
  this._peers.add(peer);
@@ -1720,11 +1638,13 @@ var TestBuilder = class {
1720
1638
  peer2.closeConnection(connection2)
1721
1639
  ]);
1722
1640
  }
1723
- constructor() {
1724
- _define_property6(this, "_peers", /* @__PURE__ */ new Set());
1725
- }
1726
1641
  };
1727
1642
  var TestPeer = class {
1643
+ peerId;
1644
+ connections = /* @__PURE__ */ new Set();
1645
+ constructor(peerId = PublicKey2.random()) {
1646
+ this.peerId = peerId;
1647
+ }
1728
1648
  async onOpen(connection) {
1729
1649
  }
1730
1650
  async onClose(connection) {
@@ -1766,12 +1686,6 @@ var TestPeer = class {
1766
1686
  await this.closeConnection(teleport);
1767
1687
  }
1768
1688
  }
1769
- constructor(peerId = PublicKey2.random()) {
1770
- _define_property6(this, "peerId", void 0);
1771
- _define_property6(this, "connections", void 0);
1772
- this.peerId = peerId;
1773
- this.connections = /* @__PURE__ */ new Set();
1774
- }
1775
1689
  };
1776
1690
  var pipeStreams = (stream1, stream2) => {
1777
1691
  pipeline(stream1, stream2, (err) => {
@@ -1796,16 +1710,11 @@ var pipeStreams = (stream1, stream2) => {
1796
1710
  });
1797
1711
  };
1798
1712
  var TestConnection = class {
1799
- whenOpen(open) {
1800
- return waitForCondition({
1801
- condition: () => this.teleport.isOpen === open
1802
- });
1803
- }
1713
+ localPeerId;
1714
+ remotePeerId;
1715
+ initiator;
1716
+ teleport;
1804
1717
  constructor(localPeerId, remotePeerId, initiator) {
1805
- _define_property6(this, "localPeerId", void 0);
1806
- _define_property6(this, "remotePeerId", void 0);
1807
- _define_property6(this, "initiator", void 0);
1808
- _define_property6(this, "teleport", void 0);
1809
1718
  this.localPeerId = localPeerId;
1810
1719
  this.remotePeerId = remotePeerId;
1811
1720
  this.initiator = initiator;
@@ -1815,6 +1724,11 @@ var TestConnection = class {
1815
1724
  remotePeerId
1816
1725
  });
1817
1726
  }
1727
+ whenOpen(open) {
1728
+ return waitForCondition({
1729
+ condition: () => this.teleport.isOpen === open
1730
+ });
1731
+ }
1818
1732
  };
1819
1733
 
1820
1734
  // src/testing/test-extension.ts
@@ -1823,21 +1737,17 @@ import { invariant as invariant6 } from "@dxos/invariant";
1823
1737
  import { log as log7 } from "@dxos/log";
1824
1738
  import { schema as schema3 } from "@dxos/protocols/proto";
1825
1739
  import { createProtoRpcPeer as createProtoRpcPeer2 } from "@dxos/rpc";
1826
- function _define_property7(obj, key, value) {
1827
- if (key in obj) {
1828
- Object.defineProperty(obj, key, {
1829
- value,
1830
- enumerable: true,
1831
- configurable: true,
1832
- writable: true
1833
- });
1834
- } else {
1835
- obj[key] = value;
1836
- }
1837
- return obj;
1838
- }
1839
1740
  var __dxlog_file7 = "/__w/dxos/dxos/packages/core/mesh/teleport/src/testing/test-extension.ts";
1840
1741
  var TestExtension = class {
1742
+ callbacks;
1743
+ open = new Trigger2();
1744
+ closed = new Trigger2();
1745
+ aborted = new Trigger2();
1746
+ extensionContext;
1747
+ _rpc;
1748
+ constructor(callbacks = {}) {
1749
+ this.callbacks = callbacks;
1750
+ }
1841
1751
  get remotePeerId() {
1842
1752
  return this.extensionContext?.remotePeerId;
1843
1753
  }
@@ -1928,18 +1838,6 @@ var TestExtension = class {
1928
1838
  async closeConnection(err) {
1929
1839
  this.extensionContext?.close(err);
1930
1840
  }
1931
- constructor(callbacks = {}) {
1932
- _define_property7(this, "callbacks", void 0);
1933
- _define_property7(this, "open", void 0);
1934
- _define_property7(this, "closed", void 0);
1935
- _define_property7(this, "aborted", void 0);
1936
- _define_property7(this, "extensionContext", void 0);
1937
- _define_property7(this, "_rpc", void 0);
1938
- this.callbacks = callbacks;
1939
- this.open = new Trigger2();
1940
- this.closed = new Trigger2();
1941
- this.aborted = new Trigger2();
1942
- }
1943
1841
  };
1944
1842
 
1945
1843
  // src/testing/test-extension-with-streams.ts
@@ -1949,21 +1847,18 @@ import { invariant as invariant7 } from "@dxos/invariant";
1949
1847
  import { log as log8 } from "@dxos/log";
1950
1848
  import { schema as schema4 } from "@dxos/protocols/proto";
1951
1849
  import { createProtoRpcPeer as createProtoRpcPeer3 } from "@dxos/rpc";
1952
- function _define_property8(obj, key, value) {
1953
- if (key in obj) {
1954
- Object.defineProperty(obj, key, {
1955
- value,
1956
- enumerable: true,
1957
- configurable: true,
1958
- writable: true
1959
- });
1960
- } else {
1961
- obj[key] = value;
1962
- }
1963
- return obj;
1964
- }
1965
1850
  var __dxlog_file8 = "/__w/dxos/dxos/packages/core/mesh/teleport/src/testing/test-extension-with-streams.ts";
1966
1851
  var TestExtensionWithStreams = class {
1852
+ callbacks;
1853
+ open = new Trigger3();
1854
+ closed = new Trigger3();
1855
+ aborted = new Trigger3();
1856
+ _streams = /* @__PURE__ */ new Map();
1857
+ extensionContext;
1858
+ _rpc;
1859
+ constructor(callbacks = {}) {
1860
+ this.callbacks = callbacks;
1861
+ }
1967
1862
  get remotePeerId() {
1968
1863
  return this.extensionContext?.remotePeerId;
1969
1864
  }
@@ -2206,20 +2101,6 @@ var TestExtensionWithStreams = class {
2206
2101
  async closeConnection(err) {
2207
2102
  this.extensionContext?.close(err);
2208
2103
  }
2209
- constructor(callbacks = {}) {
2210
- _define_property8(this, "callbacks", void 0);
2211
- _define_property8(this, "open", void 0);
2212
- _define_property8(this, "closed", void 0);
2213
- _define_property8(this, "aborted", void 0);
2214
- _define_property8(this, "_streams", void 0);
2215
- _define_property8(this, "extensionContext", void 0);
2216
- _define_property8(this, "_rpc", void 0);
2217
- this.callbacks = callbacks;
2218
- this.open = new Trigger3();
2219
- this.closed = new Trigger3();
2220
- this.aborted = new Trigger3();
2221
- this._streams = /* @__PURE__ */ new Map();
2222
- }
2223
2104
  };
2224
2105
 
2225
2106
  export {
@@ -2234,4 +2115,4 @@ export {
2234
2115
  TestExtension,
2235
2116
  TestExtensionWithStreams
2236
2117
  };
2237
- //# sourceMappingURL=chunk-32P6LS2F.mjs.map
2118
+ //# sourceMappingURL=chunk-EMGYTZMB.mjs.map