@dxos/teleport 0.8.4-main.c1de068 → 0.8.4-main.c4373fc

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/dist/lib/browser/{chunk-537EXT24.mjs → chunk-32P6LS2F.mjs} +480 -337
  2. package/dist/lib/browser/{chunk-537EXT24.mjs.map → chunk-32P6LS2F.mjs.map} +3 -3
  3. package/dist/lib/browser/index.mjs +22 -5
  4. package/dist/lib/browser/index.mjs.map +3 -3
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/testing/index.mjs +1 -1
  7. package/dist/lib/node-esm/{chunk-4OIMGFCR.mjs → chunk-36FEQDYN.mjs} +480 -337
  8. package/dist/lib/node-esm/{chunk-4OIMGFCR.mjs.map → chunk-36FEQDYN.mjs.map} +3 -3
  9. package/dist/lib/node-esm/index.mjs +22 -5
  10. package/dist/lib/node-esm/index.mjs.map +3 -3
  11. package/dist/lib/node-esm/meta.json +1 -1
  12. package/dist/lib/node-esm/testing/index.mjs +1 -1
  13. package/dist/types/src/muxing/balancer.d.ts +1 -1
  14. package/dist/types/src/muxing/balancer.d.ts.map +1 -1
  15. package/dist/types/src/muxing/muxer.d.ts.map +1 -1
  16. package/dist/types/src/rpc-extension.d.ts.map +1 -1
  17. package/dist/types/src/teleport.d.ts.map +1 -1
  18. package/dist/types/src/testing/test-extension.d.ts.map +1 -1
  19. package/dist/types/tsconfig.tsbuildinfo +1 -1
  20. package/package.json +14 -12
  21. package/src/control-extension.ts +2 -2
  22. package/src/muxing/balancer.test.ts +2 -1
  23. package/src/muxing/balancer.ts +1 -1
  24. package/src/muxing/framer.test.ts +2 -1
  25. package/src/muxing/muxer.test.ts +4 -3
  26. package/src/muxing/muxer.ts +1 -1
  27. package/src/rpc-extension.ts +1 -1
  28. package/src/teleport.test.ts +1 -1
  29. package/src/teleport.ts +1 -1
  30. package/src/testing/test-extension-with-streams.ts +1 -1
  31. package/src/testing/test-extension.ts +2 -2
@@ -116,7 +116,7 @@ import { PublicKey as PublicKey2 } from "@dxos/keys";
116
116
  import { log as log6 } from "@dxos/log";
117
117
 
118
118
  // src/teleport.ts
119
- import { runInContextAsync, synchronized, scheduleTask } from "@dxos/async";
119
+ import { runInContextAsync, scheduleTask, synchronized } from "@dxos/async";
120
120
  import { Context as Context3 } from "@dxos/context";
121
121
  import { failUndefined as failUndefined2 } from "@dxos/debug";
122
122
  import { invariant as invariant4 } from "@dxos/invariant";
@@ -125,31 +125,30 @@ import { log as log5, logInfo as logInfo2 } from "@dxos/log";
125
125
  import { RpcClosedError as RpcClosedError2, TimeoutError as TimeoutError2 } from "@dxos/protocols";
126
126
 
127
127
  // src/control-extension.ts
128
- import { asyncTimeout, scheduleTaskInterval, TimeoutError as AsyncTimeoutError } from "@dxos/async";
128
+ import { TimeoutError as AsyncTimeoutError, asyncTimeout, scheduleTaskInterval } from "@dxos/async";
129
129
  import { Context } from "@dxos/context";
130
130
  import { log } from "@dxos/log";
131
131
  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
+ }
135
148
  var __dxlog_file = "/__w/dxos/dxos/packages/core/mesh/teleport/src/control-extension.ts";
136
149
  var HEARTBEAT_RTT_WARN_THRESH = 1e4;
137
150
  var DEBUG_PRINT_HEARTBEAT = false;
138
151
  var ControlExtension = class {
139
- constructor(opts, localPeerId, remotePeerId) {
140
- this.opts = opts;
141
- this.localPeerId = localPeerId;
142
- this.remotePeerId = remotePeerId;
143
- this._ctx = new Context({
144
- onError: (err) => {
145
- this._extensionContext.close(err);
146
- }
147
- }, {
148
- F: __dxlog_file,
149
- L: 31
150
- });
151
- this.onExtensionRegistered = new Callback();
152
- }
153
152
  async registerExtension(name) {
154
153
  await this._rpc.rpc.Control.registerExtension({
155
154
  name
@@ -274,6 +273,27 @@ var ControlExtension = class {
274
273
  await this._ctx.dispose();
275
274
  await this._rpc.abort();
276
275
  }
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
+ }
277
297
  };
278
298
 
279
299
  // src/muxing/framer.ts
@@ -281,21 +301,89 @@ import { Duplex } from "@dxos/node-std/stream";
281
301
  import { Event } from "@dxos/async";
282
302
  import { invariant } from "@dxos/invariant";
283
303
  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
+ }
284
317
  var __dxlog_file2 = "/__w/dxos/dxos/packages/core/mesh/teleport/src/muxing/framer.ts";
285
318
  var FRAME_LENGTH_SIZE = 2;
286
319
  var Framer = class {
287
- constructor() {
288
- // private readonly _tagBuffer = Buffer.alloc(4)
289
- this._messageCb = void 0;
290
- this._subscribeCb = void 0;
291
- this._buffer = void 0;
320
+ get stream() {
321
+ return this._stream;
322
+ }
323
+ get bytesSent() {
324
+ return this._bytesSent;
325
+ }
326
+ get bytesReceived() {
327
+ return this._bytesReceived;
328
+ }
329
+ get writable() {
330
+ return this._writable;
331
+ }
332
+ _processResponseQueue() {
333
+ const responseQueue = this._sendCallbacks;
292
334
  this._sendCallbacks = [];
293
- this._bytesSent = 0;
294
- this._bytesReceived = 0;
295
335
  this._writable = true;
296
- this.drain = new Event();
297
- // TODO(egorgripasov): Consider using a Transform stream if it provides better backpressure handling.
298
- this._stream = new Duplex({
336
+ this.drain.emit();
337
+ responseQueue.forEach((cb) => cb());
338
+ }
339
+ /**
340
+ * Attempts to pop frames from the buffer and call the message callback.
341
+ */
342
+ _popFrames() {
343
+ let offset = 0;
344
+ while (offset < this._buffer.length) {
345
+ const frame = decodeFrame(this._buffer, offset);
346
+ if (!frame) {
347
+ break;
348
+ }
349
+ offset += frame.bytesConsumed;
350
+ this._messageCb(frame.payload);
351
+ }
352
+ if (offset < this._buffer.length) {
353
+ this._buffer = this._buffer.subarray(offset);
354
+ } else {
355
+ this._buffer = void 0;
356
+ }
357
+ }
358
+ destroy() {
359
+ if (this._stream.readableLength > 0) {
360
+ log2("framer destroyed while there are still read bytes in the buffer.", void 0, {
361
+ F: __dxlog_file2,
362
+ L: 140,
363
+ S: this,
364
+ C: (f, a) => f(...a)
365
+ });
366
+ }
367
+ if (this._stream.writableLength > 0) {
368
+ log2.warn("framer destroyed while there are still write bytes in the buffer.", void 0, {
369
+ F: __dxlog_file2,
370
+ L: 143,
371
+ S: this,
372
+ C: (f, a) => f(...a)
373
+ });
374
+ }
375
+ this._stream.destroy();
376
+ }
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({
299
387
  objectMode: false,
300
388
  read: () => {
301
389
  this._processResponseQueue();
@@ -330,8 +418,8 @@ var Framer = class {
330
418
  };
331
419
  }
332
420
  }
333
- });
334
- this.port = {
421
+ }));
422
+ _define_property2(this, "port", {
335
423
  send: (message) => {
336
424
  return new Promise((resolve) => {
337
425
  const frame = encodeFrame(message);
@@ -360,64 +448,7 @@ var Framer = class {
360
448
  this._messageCb = void 0;
361
449
  };
362
450
  }
363
- };
364
- }
365
- get stream() {
366
- return this._stream;
367
- }
368
- get bytesSent() {
369
- return this._bytesSent;
370
- }
371
- get bytesReceived() {
372
- return this._bytesReceived;
373
- }
374
- get writable() {
375
- return this._writable;
376
- }
377
- _processResponseQueue() {
378
- const responseQueue = this._sendCallbacks;
379
- this._sendCallbacks = [];
380
- this._writable = true;
381
- this.drain.emit();
382
- responseQueue.forEach((cb) => cb());
383
- }
384
- /**
385
- * Attempts to pop frames from the buffer and call the message callback.
386
- */
387
- _popFrames() {
388
- let offset = 0;
389
- while (offset < this._buffer.length) {
390
- const frame = decodeFrame(this._buffer, offset);
391
- if (!frame) {
392
- break;
393
- }
394
- offset += frame.bytesConsumed;
395
- this._messageCb(frame.payload);
396
- }
397
- if (offset < this._buffer.length) {
398
- this._buffer = this._buffer.subarray(offset);
399
- } else {
400
- this._buffer = void 0;
401
- }
402
- }
403
- destroy() {
404
- if (this._stream.readableLength > 0) {
405
- log2("framer destroyed while there are still read bytes in the buffer.", void 0, {
406
- F: __dxlog_file2,
407
- L: 140,
408
- S: this,
409
- C: (f, a) => f(...a)
410
- });
411
- }
412
- if (this._stream.writableLength > 0) {
413
- log2.warn("framer destroyed while there are still write bytes in the buffer.", void 0, {
414
- F: __dxlog_file2,
415
- L: 143,
416
- S: this,
417
- C: (f, a) => f(...a)
418
- });
419
- }
420
- this._stream.destroy();
451
+ });
421
452
  }
422
453
  };
423
454
  var decodeFrame = (buffer, offset) => {
@@ -444,7 +475,7 @@ var encodeFrame = (payload) => {
444
475
 
445
476
  // src/muxing/muxer.ts
446
477
  import { Duplex as Duplex2 } from "@dxos/node-std/stream";
447
- import { scheduleTaskInterval as scheduleTaskInterval2, Event as Event3, Trigger, asyncTimeout as asyncTimeout2 } from "@dxos/async";
478
+ import { Event as Event3, Trigger, asyncTimeout as asyncTimeout2, scheduleTaskInterval as scheduleTaskInterval2 } from "@dxos/async";
448
479
  import { Context as Context2 } from "@dxos/context";
449
480
  import { failUndefined } from "@dxos/debug";
450
481
  import { invariant as invariant3 } from "@dxos/invariant";
@@ -457,22 +488,22 @@ var import_varint = __toESM(require_varint(), 1);
457
488
  import { Event as Event2 } from "@dxos/async";
458
489
  import { invariant as invariant2 } from "@dxos/invariant";
459
490
  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
+ }
460
504
  var __dxlog_file3 = "/__w/dxos/dxos/packages/core/mesh/teleport/src/muxing/balancer.ts";
461
505
  var MAX_CHUNK_SIZE = 8192;
462
506
  var Balancer = class {
463
- constructor(_sysChannelId) {
464
- this._sysChannelId = _sysChannelId;
465
- this._lastCallerIndex = 0;
466
- this._channels = [];
467
- this._framer = new Framer();
468
- this._sendBuffers = /* @__PURE__ */ new Map();
469
- this._receiveBuffers = /* @__PURE__ */ new Map();
470
- this._sending = false;
471
- this.incomingData = new Event2();
472
- this.stream = this._framer.stream;
473
- this._channels.push(_sysChannelId);
474
- this._framer.port.subscribe(this._processIncomingMessage.bind(this));
475
- }
476
507
  get bytesSent() {
477
508
  return this._framer.bytesSent;
478
509
  }
@@ -633,6 +664,28 @@ var Balancer = class {
633
664
  });
634
665
  this._sending = false;
635
666
  }
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
+ }
636
689
  };
637
690
  var encodeChunk = ({ channelId, dataLength, chunk }) => {
638
691
  const channelTagLength = import_varint.default.encodingLength(channelId);
@@ -662,6 +715,19 @@ var decodeChunk = (data, withLength) => {
662
715
  };
663
716
 
664
717
  // 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
+ }
665
731
  function _ts_decorate(decorators, target, key, desc) {
666
732
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
667
733
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -677,27 +743,6 @@ var MAX_SAFE_FRAME_SIZE = 1e6;
677
743
  var SYSTEM_CHANNEL_ID = 0;
678
744
  var GRACEFUL_CLOSE_TIMEOUT = 3e3;
679
745
  var Muxer = class {
680
- constructor() {
681
- this._balancer = new Balancer(SYSTEM_CHANNEL_ID);
682
- this._channelsByLocalId = /* @__PURE__ */ new Map();
683
- this._channelsByTag = /* @__PURE__ */ new Map();
684
- this._ctx = new Context2(void 0, {
685
- F: __dxlog_file4,
686
- L: 108
687
- });
688
- this._nextId = 1;
689
- this._closing = false;
690
- this._destroying = false;
691
- this._disposed = false;
692
- this._lastStats = void 0;
693
- this._lastChannelStats = /* @__PURE__ */ new Map();
694
- this.afterClosed = new Event3();
695
- this.statsUpdated = new Event3();
696
- this.stream = this._balancer.stream;
697
- this._balancer.incomingData.on(async (msg) => {
698
- await this._handleCommand(Command.decode(msg));
699
- });
700
- }
701
746
  setSessionId(sessionId) {
702
747
  this._sessionId = sessionId;
703
748
  }
@@ -1125,12 +1170,47 @@ var Muxer = class {
1125
1170
  };
1126
1171
  this.statsUpdated.emit(this._lastStats);
1127
1172
  }
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
+ }
1128
1195
  };
1129
1196
  _ts_decorate([
1130
1197
  logInfo
1131
1198
  ], Muxer.prototype, "sessionIdString", null);
1132
1199
 
1133
1200
  // 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
+ }
1134
1214
  function _ts_decorate2(decorators, target, key, desc) {
1135
1215
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1136
1216
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -1141,196 +1221,54 @@ var __dxlog_file5 = "/__w/dxos/dxos/packages/core/mesh/teleport/src/teleport.ts"
1141
1221
  var CONTROL_HEARTBEAT_INTERVAL = 1e4;
1142
1222
  var CONTROL_HEARTBEAT_TIMEOUT = 6e4;
1143
1223
  var Teleport = class {
1144
- constructor({ initiator, localPeerId, remotePeerId, ...rest }) {
1145
- this._ctx = new Context3({
1146
- onError: (err) => {
1147
- log5.info("error in teleport context", {
1148
- err
1149
- }, {
1224
+ get isOpen() {
1225
+ return this._open;
1226
+ }
1227
+ get sessionIdString() {
1228
+ return this._sessionId ? this._sessionId.truncate() : "none";
1229
+ }
1230
+ get stream() {
1231
+ return this._muxer.stream;
1232
+ }
1233
+ get stats() {
1234
+ return this._muxer.statsUpdated;
1235
+ }
1236
+ /**
1237
+ * Blocks until the handshake is complete.
1238
+ */
1239
+ async open(sessionId = PublicKey.random()) {
1240
+ this._sessionId = sessionId;
1241
+ log5("open", void 0, {
1242
+ F: __dxlog_file5,
1243
+ L: 151,
1244
+ S: this,
1245
+ C: (f, a) => f(...a)
1246
+ });
1247
+ this._setExtension("dxos.mesh.teleport.control", this._control);
1248
+ await this._openExtension("dxos.mesh.teleport.control");
1249
+ this._open = true;
1250
+ this._muxer.setSessionId(sessionId);
1251
+ }
1252
+ async close(err) {
1253
+ await this.destroy(err);
1254
+ }
1255
+ async abort(err) {
1256
+ if (this._aborting || this._destroying) {
1257
+ return;
1258
+ }
1259
+ this._aborting = true;
1260
+ this._open = false;
1261
+ if (this._ctx.disposed) {
1262
+ return;
1263
+ }
1264
+ await this._ctx.dispose();
1265
+ for (const extension of this._extensions.values()) {
1266
+ try {
1267
+ await extension.onAbort(err);
1268
+ } catch (err2) {
1269
+ log5.catch(err2, void 0, {
1150
1270
  F: __dxlog_file5,
1151
- L: 40,
1152
- S: this,
1153
- C: (f, a) => f(...a)
1154
- });
1155
- void this.destroy(err).catch(() => {
1156
- log5.error("Error during destroy", err, {
1157
- F: __dxlog_file5,
1158
- L: 42,
1159
- S: this,
1160
- C: (f, a) => f(...a)
1161
- });
1162
- });
1163
- }
1164
- }, {
1165
- F: __dxlog_file5,
1166
- L: 38
1167
- });
1168
- this._muxer = new Muxer();
1169
- this._extensions = /* @__PURE__ */ new Map();
1170
- this._remoteExtensions = /* @__PURE__ */ new Set();
1171
- this._open = false;
1172
- this._destroying = false;
1173
- this._aborting = false;
1174
- invariant4(typeof initiator === "boolean", void 0, {
1175
- F: __dxlog_file5,
1176
- L: 63,
1177
- S: this,
1178
- A: [
1179
- "typeof initiator === 'boolean'",
1180
- ""
1181
- ]
1182
- });
1183
- invariant4(PublicKey.isPublicKey(localPeerId), void 0, {
1184
- F: __dxlog_file5,
1185
- L: 64,
1186
- S: this,
1187
- A: [
1188
- "PublicKey.isPublicKey(localPeerId)",
1189
- ""
1190
- ]
1191
- });
1192
- invariant4(PublicKey.isPublicKey(remotePeerId), void 0, {
1193
- F: __dxlog_file5,
1194
- L: 65,
1195
- S: this,
1196
- A: [
1197
- "PublicKey.isPublicKey(remotePeerId)",
1198
- ""
1199
- ]
1200
- });
1201
- this.initiator = initiator;
1202
- this.localPeerId = localPeerId;
1203
- this.remotePeerId = remotePeerId;
1204
- this._control = new ControlExtension({
1205
- heartbeatInterval: rest.controlHeartbeatInterval ?? CONTROL_HEARTBEAT_INTERVAL,
1206
- heartbeatTimeout: rest.controlHeartbeatTimeout ?? CONTROL_HEARTBEAT_TIMEOUT,
1207
- onTimeout: () => {
1208
- if (this._destroying || this._aborting) {
1209
- return;
1210
- }
1211
- log5.info("abort teleport due to onTimeout in ControlExtension", void 0, {
1212
- F: __dxlog_file5,
1213
- L: 78,
1214
- S: this,
1215
- C: (f, a) => f(...a)
1216
- });
1217
- this.abort(new TimeoutError2("control extension")).catch((err) => log5.catch(err, void 0, {
1218
- F: __dxlog_file5,
1219
- L: 79,
1220
- S: this,
1221
- C: (f, a) => f(...a)
1222
- }));
1223
- }
1224
- }, this.localPeerId, this.remotePeerId);
1225
- this._control.onExtensionRegistered.set(async (name) => {
1226
- log5("remote extension", {
1227
- name
1228
- }, {
1229
- F: __dxlog_file5,
1230
- L: 87,
1231
- S: this,
1232
- C: (f, a) => f(...a)
1233
- });
1234
- invariant4(!this._remoteExtensions.has(name), "Remote extension already exists", {
1235
- F: __dxlog_file5,
1236
- L: 88,
1237
- S: this,
1238
- A: [
1239
- "!this._remoteExtensions.has(name)",
1240
- "'Remote extension already exists'"
1241
- ]
1242
- });
1243
- this._remoteExtensions.add(name);
1244
- if (this._extensions.has(name)) {
1245
- try {
1246
- await this._openExtension(name);
1247
- } catch (err) {
1248
- await this.destroy(err);
1249
- }
1250
- }
1251
- });
1252
- {
1253
- this._muxer.stream.on("close", async () => {
1254
- if (this._destroying || this._aborting) {
1255
- log5("destroy teleport due to muxer stream close, skipping due to already destroying/aborting", void 0, {
1256
- F: __dxlog_file5,
1257
- L: 104,
1258
- S: this,
1259
- C: (f, a) => f(...a)
1260
- });
1261
- return;
1262
- }
1263
- await this.destroy();
1264
- });
1265
- this._muxer.stream.on("error", async (err) => {
1266
- await this.destroy(err);
1267
- });
1268
- }
1269
- this._muxer.statsUpdated.on((stats) => {
1270
- log5.trace("dxos.mesh.teleport.stats", {
1271
- localPeerId,
1272
- remotePeerId,
1273
- bytesSent: stats.bytesSent,
1274
- bytesSentRate: stats.bytesSentRate,
1275
- bytesReceived: stats.bytesReceived,
1276
- bytesReceivedRate: stats.bytesReceivedRate,
1277
- channels: stats.channels
1278
- }, {
1279
- F: __dxlog_file5,
1280
- L: 117,
1281
- S: this,
1282
- C: (f, a) => f(...a)
1283
- });
1284
- });
1285
- }
1286
- get isOpen() {
1287
- return this._open;
1288
- }
1289
- get sessionIdString() {
1290
- return this._sessionId ? this._sessionId.truncate() : "none";
1291
- }
1292
- get stream() {
1293
- return this._muxer.stream;
1294
- }
1295
- get stats() {
1296
- return this._muxer.statsUpdated;
1297
- }
1298
- /**
1299
- * Blocks until the handshake is complete.
1300
- */
1301
- async open(sessionId = PublicKey.random()) {
1302
- this._sessionId = sessionId;
1303
- log5("open", void 0, {
1304
- F: __dxlog_file5,
1305
- L: 151,
1306
- S: this,
1307
- C: (f, a) => f(...a)
1308
- });
1309
- this._setExtension("dxos.mesh.teleport.control", this._control);
1310
- await this._openExtension("dxos.mesh.teleport.control");
1311
- this._open = true;
1312
- this._muxer.setSessionId(sessionId);
1313
- }
1314
- async close(err) {
1315
- await this.destroy(err);
1316
- }
1317
- async abort(err) {
1318
- if (this._aborting || this._destroying) {
1319
- return;
1320
- }
1321
- this._aborting = true;
1322
- this._open = false;
1323
- if (this._ctx.disposed) {
1324
- return;
1325
- }
1326
- await this._ctx.dispose();
1327
- for (const extension of this._extensions.values()) {
1328
- try {
1329
- await extension.onAbort(err);
1330
- } catch (err2) {
1331
- log5.catch(err2, void 0, {
1332
- F: __dxlog_file5,
1333
- L: 181,
1271
+ L: 181,
1334
1272
  S: this,
1335
1273
  C: (f, a) => f(...a)
1336
1274
  });
@@ -1496,6 +1434,153 @@ var Teleport = class {
1496
1434
  C: (f, a) => f(...a)
1497
1435
  });
1498
1436
  }
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
+ }
1499
1584
  };
1500
1585
  _ts_decorate2([
1501
1586
  logInfo2
@@ -1508,11 +1593,21 @@ _ts_decorate2([
1508
1593
  ], Teleport.prototype, "destroy", null);
1509
1594
 
1510
1595
  // 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
+ }
1511
1609
  var __dxlog_file6 = "/__w/dxos/dxos/packages/core/mesh/teleport/src/testing/test-builder.ts";
1512
1610
  var TestBuilder = class {
1513
- constructor() {
1514
- this._peers = /* @__PURE__ */ new Set();
1515
- }
1516
1611
  createPeer(opts) {
1517
1612
  const peer = opts.factory();
1518
1613
  this._peers.add(peer);
@@ -1625,12 +1720,11 @@ var TestBuilder = class {
1625
1720
  peer2.closeConnection(connection2)
1626
1721
  ]);
1627
1722
  }
1723
+ constructor() {
1724
+ _define_property6(this, "_peers", /* @__PURE__ */ new Set());
1725
+ }
1628
1726
  };
1629
1727
  var TestPeer = class {
1630
- constructor(peerId = PublicKey2.random()) {
1631
- this.peerId = peerId;
1632
- this.connections = /* @__PURE__ */ new Set();
1633
- }
1634
1728
  async onOpen(connection) {
1635
1729
  }
1636
1730
  async onClose(connection) {
@@ -1672,6 +1766,12 @@ var TestPeer = class {
1672
1766
  await this.closeConnection(teleport);
1673
1767
  }
1674
1768
  }
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
+ }
1675
1775
  };
1676
1776
  var pipeStreams = (stream1, stream2) => {
1677
1777
  pipeline(stream1, stream2, (err) => {
@@ -1696,7 +1796,16 @@ var pipeStreams = (stream1, stream2) => {
1696
1796
  });
1697
1797
  };
1698
1798
  var TestConnection = class {
1799
+ whenOpen(open) {
1800
+ return waitForCondition({
1801
+ condition: () => this.teleport.isOpen === open
1802
+ });
1803
+ }
1699
1804
  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);
1700
1809
  this.localPeerId = localPeerId;
1701
1810
  this.remotePeerId = remotePeerId;
1702
1811
  this.initiator = initiator;
@@ -1706,27 +1815,29 @@ var TestConnection = class {
1706
1815
  remotePeerId
1707
1816
  });
1708
1817
  }
1709
- whenOpen(open) {
1710
- return waitForCondition({
1711
- condition: () => this.teleport.isOpen === open
1712
- });
1713
- }
1714
1818
  };
1715
1819
 
1716
1820
  // src/testing/test-extension.ts
1717
- import { asyncTimeout as asyncTimeout3, Trigger as Trigger2 } from "@dxos/async";
1821
+ import { Trigger as Trigger2, asyncTimeout as asyncTimeout3 } from "@dxos/async";
1718
1822
  import { invariant as invariant6 } from "@dxos/invariant";
1719
1823
  import { log as log7 } from "@dxos/log";
1720
1824
  import { schema as schema3 } from "@dxos/protocols/proto";
1721
1825
  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
+ }
1722
1839
  var __dxlog_file7 = "/__w/dxos/dxos/packages/core/mesh/teleport/src/testing/test-extension.ts";
1723
1840
  var TestExtension = class {
1724
- constructor(callbacks = {}) {
1725
- this.callbacks = callbacks;
1726
- this.open = new Trigger2();
1727
- this.closed = new Trigger2();
1728
- this.aborted = new Trigger2();
1729
- }
1730
1841
  get remotePeerId() {
1731
1842
  return this.extensionContext?.remotePeerId;
1732
1843
  }
@@ -1817,6 +1928,18 @@ var TestExtension = class {
1817
1928
  async closeConnection(err) {
1818
1929
  this.extensionContext?.close(err);
1819
1930
  }
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
+ }
1820
1943
  };
1821
1944
 
1822
1945
  // src/testing/test-extension-with-streams.ts
@@ -1826,15 +1949,21 @@ import { invariant as invariant7 } from "@dxos/invariant";
1826
1949
  import { log as log8 } from "@dxos/log";
1827
1950
  import { schema as schema4 } from "@dxos/protocols/proto";
1828
1951
  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
+ }
1829
1965
  var __dxlog_file8 = "/__w/dxos/dxos/packages/core/mesh/teleport/src/testing/test-extension-with-streams.ts";
1830
1966
  var TestExtensionWithStreams = class {
1831
- constructor(callbacks = {}) {
1832
- this.callbacks = callbacks;
1833
- this.open = new Trigger3();
1834
- this.closed = new Trigger3();
1835
- this.aborted = new Trigger3();
1836
- this._streams = /* @__PURE__ */ new Map();
1837
- }
1838
1967
  get remotePeerId() {
1839
1968
  return this.extensionContext?.remotePeerId;
1840
1969
  }
@@ -2077,6 +2206,20 @@ var TestExtensionWithStreams = class {
2077
2206
  async closeConnection(err) {
2078
2207
  this.extensionContext?.close(err);
2079
2208
  }
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
+ }
2080
2223
  };
2081
2224
 
2082
2225
  export {
@@ -2091,4 +2234,4 @@ export {
2091
2234
  TestExtension,
2092
2235
  TestExtensionWithStreams
2093
2236
  };
2094
- //# sourceMappingURL=chunk-537EXT24.mjs.map
2237
+ //# sourceMappingURL=chunk-32P6LS2F.mjs.map