@dxos/teleport 0.3.3-main.ff5734a → 0.3.3-next.6bede4d

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.
@@ -46,6 +46,7 @@ module.exports = __toCommonJS(src_exports);
46
46
 
47
47
  // packages/core/mesh/teleport/src/muxing/framer.ts
48
48
  var import_node_stream = require("node:stream");
49
+ var import_async = require("@dxos/async");
49
50
  var import_invariant = require("@dxos/invariant");
50
51
  var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/muxing/framer.ts";
51
52
  var FRAME_LENGTH_SIZE = 2;
@@ -58,6 +59,9 @@ var Framer = class {
58
59
  this._sendCallbacks = [];
59
60
  this._bytesSent = 0;
60
61
  this._bytesReceived = 0;
62
+ this._writable = true;
63
+ this.drain = new import_async.Event();
64
+ // TODO(egorgripasov): Consider using a Transform stream if it provides better backpressure handling.
61
65
  this._stream = new import_node_stream.Duplex({
62
66
  objectMode: false,
63
67
  read: () => {
@@ -66,7 +70,7 @@ var Framer = class {
66
70
  write: (chunk, encoding, callback) => {
67
71
  (0, import_invariant.invariant)(!this._subscribeCb, "Internal Framer bug. Concurrent writes detected.", {
68
72
  F: __dxlog_file,
69
- L: 33,
73
+ L: 39,
70
74
  S: this,
71
75
  A: [
72
76
  "!this._subscribeCb",
@@ -99,8 +103,8 @@ var Framer = class {
99
103
  return new Promise((resolve) => {
100
104
  const frame = encodeFrame(message);
101
105
  this._bytesSent += frame.length;
102
- const canContinue = this._stream.push(frame);
103
- if (!canContinue) {
106
+ this._writable = this._stream.push(frame);
107
+ if (!this._writable) {
104
108
  this._sendCallbacks.push(resolve);
105
109
  } else {
106
110
  resolve();
@@ -110,7 +114,7 @@ var Framer = class {
110
114
  subscribe: (callback) => {
111
115
  (0, import_invariant.invariant)(!this._messageCb, "Rpc port already has a message listener.", {
112
116
  F: __dxlog_file,
113
- L: 72,
117
+ L: 78,
114
118
  S: this,
115
119
  A: [
116
120
  "!this._messageCb",
@@ -134,9 +138,14 @@ var Framer = class {
134
138
  get bytesReceived() {
135
139
  return this._bytesReceived;
136
140
  }
141
+ get writable() {
142
+ return this._writable;
143
+ }
137
144
  _processResponseQueue() {
138
145
  const responseQueue = this._sendCallbacks;
139
146
  this._sendCallbacks = [];
147
+ this._writable = true;
148
+ this.drain.emit();
140
149
  responseQueue.forEach((cb) => cb());
141
150
  }
142
151
  /**
@@ -186,7 +195,7 @@ var encodeFrame = (payload) => {
186
195
 
187
196
  // packages/core/mesh/teleport/src/muxing/muxer.ts
188
197
  var import_node_stream2 = require("node:stream");
189
- var import_async2 = require("@dxos/async");
198
+ var import_async3 = require("@dxos/async");
190
199
  var import_context = require("@dxos/context");
191
200
  var import_debug = require("@dxos/debug");
192
201
  var import_invariant2 = require("@dxos/invariant");
@@ -195,7 +204,7 @@ var import_protocols = require("@dxos/protocols");
195
204
 
196
205
  // packages/core/mesh/teleport/src/muxing/balancer.ts
197
206
  var varint = __toESM(require("varint"));
198
- var import_async = require("@dxos/async");
207
+ var import_async2 = require("@dxos/async");
199
208
  var import_log = require("@dxos/log");
200
209
  var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/muxing/balancer.ts";
201
210
  var MAX_CHUNK_SIZE = 8192;
@@ -207,7 +216,7 @@ var Balancer = class {
207
216
  this._framer = new Framer();
208
217
  this._sendBuffers = /* @__PURE__ */ new Map();
209
218
  this._receiveBuffers = /* @__PURE__ */ new Map();
210
- this.incomingData = new import_async.Event();
219
+ this.incomingData = new import_async2.Event();
211
220
  this.stream = this._framer.stream;
212
221
  this._channels.push(_sysChannelId);
213
222
  this._framer.port.subscribe(this._processIncomingMessage.bind(this));
@@ -218,6 +227,9 @@ var Balancer = class {
218
227
  get bytesReceived() {
219
228
  return this._framer.bytesReceived;
220
229
  }
230
+ get buffersCount() {
231
+ return this._sendBuffers.size;
232
+ }
221
233
  addChannel(channel) {
222
234
  this._channels.push(channel);
223
235
  }
@@ -246,9 +258,9 @@ var Balancer = class {
246
258
  });
247
259
  });
248
260
  if (noCalls) {
249
- this._sendChunks().catch((err) => import_log.log.catch(err, void 0, {
261
+ this._sendChunk().catch((err) => import_log.log.catch(err, void 0, {
250
262
  F: __dxlog_file2,
251
- L: 96,
263
+ L: 100,
252
264
  S: this,
253
265
  C: (f, a) => f(...a)
254
266
  }));
@@ -306,10 +318,13 @@ var Balancer = class {
306
318
  }
307
319
  return chunk;
308
320
  }
309
- async _sendChunks() {
321
+ async _sendChunk() {
310
322
  if (this._sendBuffers.size === 0) {
311
323
  return;
312
324
  }
325
+ if (!this._framer.writable) {
326
+ await this._framer.drain.waitForCount(1);
327
+ }
313
328
  const chunk = this._getNextChunk();
314
329
  try {
315
330
  await this._framer.port.send(chunk.msg);
@@ -317,7 +332,7 @@ var Balancer = class {
317
332
  } catch (err) {
318
333
  chunk.trigger?.throw(err);
319
334
  }
320
- await this._sendChunks();
335
+ await this._sendChunk();
321
336
  }
322
337
  };
323
338
  var encodeChunk = ({ channelId, dataLength, chunk }) => {
@@ -364,13 +379,13 @@ var Muxer = class {
364
379
  this._destroying = false;
365
380
  this._lastStats = void 0;
366
381
  this._lastChannelStats = /* @__PURE__ */ new Map();
367
- this.close = new import_async2.Event();
368
- this.statsUpdated = new import_async2.Event();
382
+ this.close = new import_async3.Event();
383
+ this.statsUpdated = new import_async3.Event();
369
384
  this.stream = this._balancer.stream;
370
385
  this._balancer.incomingData.on(async (msg) => {
371
386
  await this._handleCommand(Command.decode(msg));
372
387
  });
373
- (0, import_async2.scheduleTaskInterval)(this._ctx, async () => this._emitStats(), STATS_INTERVAL);
388
+ (0, import_async3.scheduleTaskInterval)(this._ctx, async () => this._emitStats(), STATS_INTERVAL);
374
389
  }
375
390
  /**
376
391
  * Creates a duplex Node.js-style stream.
@@ -575,7 +590,7 @@ var Muxer = class {
575
590
  }
576
591
  async _sendCommand(cmd, channelId = -1) {
577
592
  try {
578
- const trigger = new import_async2.Trigger();
593
+ const trigger = new import_async3.Trigger();
579
594
  this._balancer.pushData(Command.encode(cmd), trigger, channelId);
580
595
  await trigger.wait();
581
596
  } catch (err) {
@@ -678,7 +693,7 @@ var Muxer = class {
678
693
  };
679
694
 
680
695
  // packages/core/mesh/teleport/src/teleport.ts
681
- var import_async3 = require("@dxos/async");
696
+ var import_async4 = require("@dxos/async");
682
697
  var import_context2 = require("@dxos/context");
683
698
  var import_debug2 = require("@dxos/debug");
684
699
  var import_invariant3 = require("@dxos/invariant");
@@ -906,7 +921,7 @@ var Teleport = class {
906
921
  C: (f, a) => f(...a)
907
922
  });
908
923
  this._setExtension(name, extension);
909
- (0, import_async3.scheduleTask)(this._ctx, async () => {
924
+ (0, import_async4.scheduleTask)(this._ctx, async () => {
910
925
  try {
911
926
  await this._control.registerExtension(name);
912
927
  } catch (err) {
@@ -917,7 +932,7 @@ var Teleport = class {
917
932
  }
918
933
  });
919
934
  if (this._remoteExtensions.has(name)) {
920
- (0, import_async3.scheduleTask)(this._ctx, async () => {
935
+ (0, import_async4.scheduleTask)(this._ctx, async () => {
921
936
  await this._openExtension(name);
922
937
  });
923
938
  }
@@ -982,7 +997,7 @@ var Teleport = class {
982
997
  return this._muxer.createStream(`${extensionName}/${channelName}`, opts);
983
998
  },
984
999
  close: (err) => {
985
- void (0, import_async3.runInContextAsync)(this._ctx, async () => {
1000
+ void (0, import_async4.runInContextAsync)(this._ctx, async () => {
986
1001
  await this.close(err);
987
1002
  });
988
1003
  }
@@ -999,10 +1014,10 @@ var Teleport = class {
999
1014
  }
1000
1015
  };
1001
1016
  _ts_decorate([
1002
- import_async3.synchronized
1017
+ import_async4.synchronized
1003
1018
  ], Teleport.prototype, "abort", null);
1004
1019
  _ts_decorate([
1005
- import_async3.synchronized
1020
+ import_async4.synchronized
1006
1021
  ], Teleport.prototype, "destroy", null);
1007
1022
  var ControlExtension = class {
1008
1023
  constructor(opts) {
@@ -1042,9 +1057,9 @@ var ControlExtension = class {
1042
1057
  })
1043
1058
  });
1044
1059
  await this._rpc.open();
1045
- (0, import_async3.scheduleTaskInterval)(this._ctx, async () => {
1060
+ (0, import_async4.scheduleTaskInterval)(this._ctx, async () => {
1046
1061
  try {
1047
- await (0, import_async3.asyncTimeout)(this._rpc.rpc.Control.heartbeat(), this.opts.heartbeatTimeout);
1062
+ await (0, import_async4.asyncTimeout)(this._rpc.rpc.Control.heartbeat(), this.opts.heartbeatTimeout);
1048
1063
  } catch (err) {
1049
1064
  this.opts.onTimeout();
1050
1065
  }
@@ -1266,7 +1281,7 @@ var TestConnection = class {
1266
1281
  };
1267
1282
 
1268
1283
  // packages/core/mesh/teleport/src/testing/test-extension.ts
1269
- var import_async4 = require("@dxos/async");
1284
+ var import_async5 = require("@dxos/async");
1270
1285
  var import_invariant5 = require("@dxos/invariant");
1271
1286
  var import_log5 = require("@dxos/log");
1272
1287
  var import_protocols3 = require("@dxos/protocols");
@@ -1275,9 +1290,9 @@ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src
1275
1290
  var TestExtension = class {
1276
1291
  constructor(callbacks = {}) {
1277
1292
  this.callbacks = callbacks;
1278
- this.open = new import_async4.Trigger();
1279
- this.closed = new import_async4.Trigger();
1280
- this.aborted = new import_async4.Trigger();
1293
+ this.open = new import_async5.Trigger();
1294
+ this.closed = new import_async5.Trigger();
1295
+ this.aborted = new import_async5.Trigger();
1281
1296
  }
1282
1297
  get remotePeerId() {
1283
1298
  return this.extensionContext?.remotePeerId;
@@ -1350,7 +1365,7 @@ var TestExtension = class {
1350
1365
  await this.open.wait({
1351
1366
  timeout: 1500
1352
1367
  });
1353
- const res = await (0, import_async4.asyncTimeout)(this._rpc.rpc.TestService.testCall({
1368
+ const res = await (0, import_async5.asyncTimeout)(this._rpc.rpc.TestService.testCall({
1354
1369
  data: message
1355
1370
  }), 1500);
1356
1371
  (0, import_invariant5.invariant)(res.data === message, void 0, {
@@ -1373,7 +1388,7 @@ var TestExtension = class {
1373
1388
 
1374
1389
  // packages/core/mesh/teleport/src/testing/test-extension-with-streams.ts
1375
1390
  var import_node_crypto = require("node:crypto");
1376
- var import_async5 = require("@dxos/async");
1391
+ var import_async6 = require("@dxos/async");
1377
1392
  var import_invariant6 = require("@dxos/invariant");
1378
1393
  var import_log6 = require("@dxos/log");
1379
1394
  var import_protocols4 = require("@dxos/protocols");
@@ -1382,9 +1397,9 @@ var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src
1382
1397
  var TestExtensionWithStreams = class {
1383
1398
  constructor(callbacks = {}) {
1384
1399
  this.callbacks = callbacks;
1385
- this.open = new import_async5.Trigger();
1386
- this.closed = new import_async5.Trigger();
1387
- this.aborted = new import_async5.Trigger();
1400
+ this.open = new import_async6.Trigger();
1401
+ this.closed = new import_async6.Trigger();
1402
+ this.aborted = new import_async6.Trigger();
1388
1403
  this._streams = /* @__PURE__ */ new Map();
1389
1404
  }
1390
1405
  get remotePeerId() {