@dxos/teleport 0.3.5-main.9c28cb0 → 0.3.5-main.a6efa8a

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.
@@ -2,7 +2,7 @@ import "@dxos/node-std/globals";
2
2
 
3
3
  // packages/core/mesh/teleport/src/testing/test-builder.ts
4
4
  import { pipeline } from "@dxos/node-std/stream";
5
- import { invariant as invariant4 } from "@dxos/invariant";
5
+ import { invariant as invariant5 } from "@dxos/invariant";
6
6
  import { PublicKey as PublicKey2 } from "@dxos/keys";
7
7
  import { log as log5 } from "@dxos/log";
8
8
 
@@ -10,7 +10,7 @@ import { log as log5 } from "@dxos/log";
10
10
  import { asyncTimeout, scheduleTaskInterval as scheduleTaskInterval2, runInContextAsync, synchronized, scheduleTask } from "@dxos/async";
11
11
  import { Context as Context2 } from "@dxos/context";
12
12
  import { failUndefined as failUndefined2 } from "@dxos/debug";
13
- import { invariant as invariant3 } from "@dxos/invariant";
13
+ import { invariant as invariant4 } from "@dxos/invariant";
14
14
  import { PublicKey } from "@dxos/keys";
15
15
  import { log as log4 } from "@dxos/log";
16
16
  import { schema as schema2, RpcClosedError, TimeoutError as TimeoutError2 } from "@dxos/protocols";
@@ -188,13 +188,14 @@ import { Duplex as Duplex2 } from "@dxos/node-std/stream";
188
188
  import { scheduleTaskInterval, Event as Event3, Trigger } from "@dxos/async";
189
189
  import { Context } from "@dxos/context";
190
190
  import { failUndefined } from "@dxos/debug";
191
- import { invariant as invariant2 } from "@dxos/invariant";
191
+ import { invariant as invariant3 } from "@dxos/invariant";
192
192
  import { log as log3 } from "@dxos/log";
193
193
  import { schema, TimeoutError } from "@dxos/protocols";
194
194
 
195
195
  // packages/core/mesh/teleport/src/muxing/balancer.ts
196
196
  import * as varint from "varint";
197
197
  import { Event as Event2 } from "@dxos/async";
198
+ import { invariant as invariant2 } from "@dxos/invariant";
198
199
  import { log as log2 } from "@dxos/log";
199
200
  var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport/src/muxing/balancer.ts";
200
201
  var MAX_CHUNK_SIZE = 8192;
@@ -206,6 +207,7 @@ var Balancer = class {
206
207
  this._framer = new Framer();
207
208
  this._sendBuffers = /* @__PURE__ */ new Map();
208
209
  this._receiveBuffers = /* @__PURE__ */ new Map();
210
+ this._sending = false;
209
211
  this.incomingData = new Event2();
210
212
  this.stream = this._framer.stream;
211
213
  this._channels.push(_sysChannelId);
@@ -224,43 +226,19 @@ var Balancer = class {
224
226
  this._channels.push(channel);
225
227
  }
226
228
  pushData(data, trigger, channelId) {
227
- const noCalls = this._sendBuffers.size === 0;
228
- if (!this._channels.includes(channelId)) {
229
- throw new Error(`Unknown channel ${channelId}`);
230
- }
231
- if (!this._sendBuffers.has(channelId)) {
232
- this._sendBuffers.set(channelId, []);
233
- }
234
- const sendBuffer = this._sendBuffers.get(channelId);
235
- const chunks = [];
236
- for (let idx = 0; idx < data.length; idx += MAX_CHUNK_SIZE) {
237
- chunks.push(data.subarray(idx, idx + MAX_CHUNK_SIZE));
238
- }
239
- chunks.forEach((chunk, index) => {
240
- const msg = encodeChunk({
241
- chunk,
242
- channelId,
243
- dataLength: index === 0 ? data.length : void 0
244
- });
245
- sendBuffer.push({
246
- msg,
247
- trigger: index === chunks.length - 1 ? trigger : void 0
248
- });
249
- });
250
- if (noCalls) {
251
- this._sendChunk().catch((err) => log2.catch(err, void 0, {
252
- F: __dxlog_file2,
253
- L: 100,
254
- S: this,
255
- C: (f, a) => f(...a)
256
- }));
257
- }
229
+ this._enqueueChunk(data, trigger, channelId);
230
+ this._sendChunks().catch((err) => log2.catch(err, void 0, {
231
+ F: __dxlog_file2,
232
+ L: 75,
233
+ S: this,
234
+ C: (f, a) => f(...a)
235
+ }));
258
236
  }
259
237
  destroy() {
260
238
  if (this._sendBuffers.size !== 0) {
261
239
  log2.warn("destroying balancer with pending calls", void 0, {
262
240
  F: __dxlog_file2,
263
- L: 106,
241
+ L: 80,
264
242
  S: this,
265
243
  C: (f, a) => f(...a)
266
244
  });
@@ -301,36 +279,99 @@ var Balancer = class {
301
279
  this._lastCallerIndex = (this._lastCallerIndex + 1) % this._channels.length;
302
280
  return this._channels[index];
303
281
  }
282
+ _enqueueChunk(data, trigger, channelId) {
283
+ if (!this._channels.includes(channelId)) {
284
+ throw new Error(`Unknown channel ${channelId}`);
285
+ }
286
+ if (!this._sendBuffers.has(channelId)) {
287
+ this._sendBuffers.set(channelId, []);
288
+ }
289
+ const sendBuffer = this._sendBuffers.get(channelId);
290
+ const chunks = [];
291
+ for (let idx = 0; idx < data.length; idx += MAX_CHUNK_SIZE) {
292
+ chunks.push(data.subarray(idx, idx + MAX_CHUNK_SIZE));
293
+ }
294
+ chunks.forEach((chunk, index) => {
295
+ const msg = encodeChunk({
296
+ chunk,
297
+ channelId,
298
+ dataLength: index === 0 ? data.length : void 0
299
+ });
300
+ sendBuffer.push({
301
+ msg,
302
+ trigger: index === chunks.length - 1 ? trigger : void 0
303
+ });
304
+ });
305
+ }
306
+ // get the next chunk or null if there are no chunks remaining
304
307
  _getNextChunk() {
305
308
  let chunk;
306
- while (!chunk) {
309
+ while (this._sendBuffers.size > 0) {
307
310
  const channelId = this._getNextCallerId();
308
311
  const sendBuffer = this._sendBuffers.get(channelId);
309
312
  if (!sendBuffer) {
310
313
  continue;
311
314
  }
312
315
  chunk = sendBuffer.shift();
316
+ if (!chunk) {
317
+ continue;
318
+ }
313
319
  if (sendBuffer.length === 0) {
314
320
  this._sendBuffers.delete(channelId);
315
321
  }
322
+ return chunk;
316
323
  }
317
- return chunk;
324
+ return null;
318
325
  }
319
- async _sendChunk() {
320
- if (this._sendBuffers.size === 0) {
326
+ async _sendChunks() {
327
+ if (this._sending) {
321
328
  return;
322
329
  }
323
- if (!this._framer.writable) {
324
- await this._framer.drain.waitForCount(1);
325
- }
326
- const chunk = this._getNextChunk();
327
- try {
328
- await this._framer.port.send(chunk.msg);
329
- chunk.trigger?.wake();
330
- } catch (err) {
331
- chunk.trigger?.throw(err);
330
+ this._sending = true;
331
+ let chunk;
332
+ chunk = this._getNextChunk();
333
+ while (chunk) {
334
+ if (!this._framer.writable) {
335
+ log2("PAUSE for drain", void 0, {
336
+ F: __dxlog_file2,
337
+ L: 179,
338
+ S: this,
339
+ C: (f, a) => f(...a)
340
+ });
341
+ await this._framer.drain.waitForCount(1);
342
+ log2("RESUME for drain", void 0, {
343
+ F: __dxlog_file2,
344
+ L: 181,
345
+ S: this,
346
+ C: (f, a) => f(...a)
347
+ });
348
+ }
349
+ try {
350
+ await this._framer.port.send(chunk.msg);
351
+ chunk.trigger?.wake();
352
+ } catch (err) {
353
+ log2("Error sending chunk", {
354
+ err
355
+ }, {
356
+ F: __dxlog_file2,
357
+ L: 187,
358
+ S: this,
359
+ C: (f, a) => f(...a)
360
+ });
361
+ chunk.trigger?.throw(err);
362
+ }
363
+ chunk = this._getNextChunk();
332
364
  }
333
- await this._sendChunk();
365
+ invariant2(this._sendBuffers.size === 0, "sendBuffers not empty", {
366
+ F: __dxlog_file2,
367
+ L: 192,
368
+ S: this,
369
+ A: [
370
+ "this._sendBuffers.size === 0",
371
+ "'sendBuffers not empty'"
372
+ ]
373
+ });
374
+ this._sending = false;
334
375
  }
335
376
  };
336
377
  var encodeChunk = ({ channelId, dataLength, chunk }) => {
@@ -399,7 +440,7 @@ var Muxer = class {
399
440
  tag,
400
441
  contentType: opts.contentType
401
442
  });
402
- invariant2(!channel.push, `Channel already open: ${tag}`, {
443
+ invariant3(!channel.push, `Channel already open: ${tag}`, {
403
444
  F: __dxlog_file3,
404
445
  L: 140,
405
446
  S: this,
@@ -447,7 +488,7 @@ var Muxer = class {
447
488
  tag,
448
489
  contentType: opts.contentType
449
490
  });
450
- invariant2(!channel.push, `Channel already open: ${tag}`, {
491
+ invariant3(!channel.push, `Channel already open: ${tag}`, {
451
492
  F: __dxlog_file3,
452
493
  L: 192,
453
494
  S: this,
@@ -471,7 +512,7 @@ var Muxer = class {
471
512
  await this._sendData(channel, data, timeout);
472
513
  },
473
514
  subscribe: (cb) => {
474
- invariant2(!callback, "Only one subscriber is allowed", {
515
+ invariant3(!callback, "Only one subscriber is allowed", {
475
516
  F: __dxlog_file3,
476
517
  L: 214,
477
518
  S: this,
@@ -843,7 +884,7 @@ var Teleport = class {
843
884
  this._open = false;
844
885
  this._destroying = false;
845
886
  this._aborting = false;
846
- invariant3(typeof initiator === "boolean", void 0, {
887
+ invariant4(typeof initiator === "boolean", void 0, {
847
888
  F: __dxlog_file4,
848
889
  L: 75,
849
890
  S: this,
@@ -852,7 +893,7 @@ var Teleport = class {
852
893
  ""
853
894
  ]
854
895
  });
855
- invariant3(PublicKey.isPublicKey(localPeerId), void 0, {
896
+ invariant4(PublicKey.isPublicKey(localPeerId), void 0, {
856
897
  F: __dxlog_file4,
857
898
  L: 76,
858
899
  S: this,
@@ -861,7 +902,7 @@ var Teleport = class {
861
902
  ""
862
903
  ]
863
904
  });
864
- invariant3(PublicKey.isPublicKey(remotePeerId), void 0, {
905
+ invariant4(PublicKey.isPublicKey(remotePeerId), void 0, {
865
906
  F: __dxlog_file4,
866
907
  L: 77,
867
908
  S: this,
@@ -882,7 +923,7 @@ var Teleport = class {
882
923
  S: this,
883
924
  C: (f, a) => f(...a)
884
925
  });
885
- invariant3(!this._remoteExtensions.has(name), "Remote extension already exists", {
926
+ invariant4(!this._remoteExtensions.has(name), "Remote extension already exists", {
886
927
  F: __dxlog_file4,
887
928
  L: 84,
888
929
  S: this,
@@ -1027,7 +1068,7 @@ var Teleport = class {
1027
1068
  }
1028
1069
  }
1029
1070
  _setExtension(extensionName, extension) {
1030
- invariant3(!extensionName.includes("/"), "Invalid extension name", {
1071
+ invariant4(!extensionName.includes("/"), "Invalid extension name", {
1031
1072
  F: __dxlog_file4,
1032
1073
  L: 226,
1033
1074
  S: this,
@@ -1036,7 +1077,7 @@ var Teleport = class {
1036
1077
  "'Invalid extension name'"
1037
1078
  ]
1038
1079
  });
1039
- invariant3(!this._extensions.has(extensionName), "Extension already exists", {
1080
+ invariant4(!this._extensions.has(extensionName), "Extension already exists", {
1040
1081
  F: __dxlog_file4,
1041
1082
  L: 227,
1042
1083
  S: this,
@@ -1062,7 +1103,7 @@ var Teleport = class {
1062
1103
  localPeerId: this.localPeerId,
1063
1104
  remotePeerId: this.remotePeerId,
1064
1105
  createPort: async (channelName, opts) => {
1065
- invariant3(!channelName.includes("/"), "Invalid channel name", {
1106
+ invariant4(!channelName.includes("/"), "Invalid channel name", {
1066
1107
  F: __dxlog_file4,
1067
1108
  L: 240,
1068
1109
  S: this,
@@ -1074,7 +1115,7 @@ var Teleport = class {
1074
1115
  return this._muxer.createPort(`${extensionName}/${channelName}`, opts);
1075
1116
  },
1076
1117
  createStream: async (channelName, opts) => {
1077
- invariant3(!channelName.includes("/"), "Invalid channel name", {
1118
+ invariant4(!channelName.includes("/"), "Invalid channel name", {
1078
1119
  F: __dxlog_file4,
1079
1120
  L: 244,
1080
1121
  S: this,
@@ -1184,7 +1225,7 @@ var TestBuilder = class {
1184
1225
  await Promise.all(Array.from(this._peers).map((agent) => agent.destroy()));
1185
1226
  }
1186
1227
  async connect(peer1, peer2) {
1187
- invariant4(peer1 !== peer2, void 0, {
1228
+ invariant5(peer1 !== peer2, void 0, {
1188
1229
  F: __dxlog_file5,
1189
1230
  L: 37,
1190
1231
  S: this,
@@ -1193,7 +1234,7 @@ var TestBuilder = class {
1193
1234
  ""
1194
1235
  ]
1195
1236
  });
1196
- invariant4(this._peers.has(peer1), void 0, {
1237
+ invariant5(this._peers.has(peer1), void 0, {
1197
1238
  F: __dxlog_file5,
1198
1239
  L: 38,
1199
1240
  S: this,
@@ -1202,7 +1243,7 @@ var TestBuilder = class {
1202
1243
  ""
1203
1244
  ]
1204
1245
  });
1205
- invariant4(this._peers.has(peer1), void 0, {
1246
+ invariant5(this._peers.has(peer1), void 0, {
1206
1247
  F: __dxlog_file5,
1207
1248
  L: 39,
1208
1249
  S: this,
@@ -1230,7 +1271,7 @@ var TestBuilder = class {
1230
1271
  ];
1231
1272
  }
1232
1273
  async disconnect(peer1, peer2) {
1233
- invariant4(peer1 !== peer2, void 0, {
1274
+ invariant5(peer1 !== peer2, void 0, {
1234
1275
  F: __dxlog_file5,
1235
1276
  L: 51,
1236
1277
  S: this,
@@ -1239,7 +1280,7 @@ var TestBuilder = class {
1239
1280
  ""
1240
1281
  ]
1241
1282
  });
1242
- invariant4(this._peers.has(peer1), void 0, {
1283
+ invariant5(this._peers.has(peer1), void 0, {
1243
1284
  F: __dxlog_file5,
1244
1285
  L: 52,
1245
1286
  S: this,
@@ -1248,7 +1289,7 @@ var TestBuilder = class {
1248
1289
  ""
1249
1290
  ]
1250
1291
  });
1251
- invariant4(this._peers.has(peer1), void 0, {
1292
+ invariant5(this._peers.has(peer1), void 0, {
1252
1293
  F: __dxlog_file5,
1253
1294
  L: 53,
1254
1295
  S: this,
@@ -1259,7 +1300,7 @@ var TestBuilder = class {
1259
1300
  });
1260
1301
  const connection1 = Array.from(peer1.connections).find((connection) => connection.remotePeerId.equals(peer2.peerId));
1261
1302
  const connection2 = Array.from(peer2.connections).find((connection) => connection.remotePeerId.equals(peer1.peerId));
1262
- invariant4(connection1, void 0, {
1303
+ invariant5(connection1, void 0, {
1263
1304
  F: __dxlog_file5,
1264
1305
  L: 62,
1265
1306
  S: this,
@@ -1268,7 +1309,7 @@ var TestBuilder = class {
1268
1309
  ""
1269
1310
  ]
1270
1311
  });
1271
- invariant4(connection2, void 0, {
1312
+ invariant5(connection2, void 0, {
1272
1313
  F: __dxlog_file5,
1273
1314
  L: 63,
1274
1315
  S: this,
@@ -1298,7 +1339,7 @@ var TestPeer = class {
1298
1339
  return connection;
1299
1340
  }
1300
1341
  async openConnection(connection) {
1301
- invariant4(this.connections.has(connection), void 0, {
1342
+ invariant5(this.connections.has(connection), void 0, {
1302
1343
  F: __dxlog_file5,
1303
1344
  L: 84,
1304
1345
  S: this,
@@ -1311,7 +1352,7 @@ var TestPeer = class {
1311
1352
  await this.onOpen(connection);
1312
1353
  }
1313
1354
  async closeConnection(connection) {
1314
- invariant4(this.connections.has(connection), void 0, {
1355
+ invariant5(this.connections.has(connection), void 0, {
1315
1356
  F: __dxlog_file5,
1316
1357
  L: 90,
1317
1358
  S: this,
@@ -1367,7 +1408,7 @@ var TestConnection = class {
1367
1408
 
1368
1409
  // packages/core/mesh/teleport/src/testing/test-extension.ts
1369
1410
  import { asyncTimeout as asyncTimeout2, Trigger as Trigger2 } from "@dxos/async";
1370
- import { invariant as invariant5 } from "@dxos/invariant";
1411
+ import { invariant as invariant6 } from "@dxos/invariant";
1371
1412
  import { log as log6 } from "@dxos/log";
1372
1413
  import { schema as schema3 } from "@dxos/protocols";
1373
1414
  import { createProtoRpcPeer as createProtoRpcPeer2 } from "@dxos/rpc";
@@ -1453,7 +1494,7 @@ var TestExtension = class {
1453
1494
  const res = await asyncTimeout2(this._rpc.rpc.TestService.testCall({
1454
1495
  data: message
1455
1496
  }), 1500);
1456
- invariant5(res.data === message, void 0, {
1497
+ invariant6(res.data === message, void 0, {
1457
1498
  F: __dxlog_file6,
1458
1499
  L: 84,
1459
1500
  S: this,
@@ -1474,7 +1515,7 @@ var TestExtension = class {
1474
1515
  // packages/core/mesh/teleport/src/testing/test-extension-with-streams.ts
1475
1516
  import { randomBytes } from "@dxos/node-std/crypto";
1476
1517
  import { Trigger as Trigger3 } from "@dxos/async";
1477
- import { invariant as invariant6 } from "@dxos/invariant";
1518
+ import { invariant as invariant7 } from "@dxos/invariant";
1478
1519
  import { log as log7 } from "@dxos/log";
1479
1520
  import { schema as schema4 } from "@dxos/protocols";
1480
1521
  import { createProtoRpcPeer as createProtoRpcPeer3 } from "@dxos/rpc";
@@ -1491,7 +1532,7 @@ var TestExtensionWithStreams = class {
1491
1532
  return this.extensionContext?.remotePeerId;
1492
1533
  }
1493
1534
  async _openStream(streamTag, interval = 5, chunkSize = 2048) {
1494
- invariant6(!this._streams.has(streamTag), `Stream already exists: ${streamTag}`, {
1535
+ invariant7(!this._streams.has(streamTag), `Stream already exists: ${streamTag}`, {
1495
1536
  F: __dxlog_file7,
1496
1537
  L: 39,
1497
1538
  S: this,
@@ -1557,7 +1598,7 @@ var TestExtensionWithStreams = class {
1557
1598
  }, 100);
1558
1599
  }
1559
1600
  _closeStream(streamTag) {
1560
- invariant6(this._streams.has(streamTag), `Stream does not exist: ${streamTag}`, {
1601
+ invariant7(this._streams.has(streamTag), `Stream does not exist: ${streamTag}`, {
1561
1602
  F: __dxlog_file7,
1562
1603
  L: 106,
1563
1604
  S: this,
@@ -1680,7 +1721,7 @@ var TestExtensionWithStreams = class {
1680
1721
  streamLoadInterval,
1681
1722
  streamLoadChunkSize
1682
1723
  });
1683
- invariant6(data === streamTag, void 0, {
1724
+ invariant7(data === streamTag, void 0, {
1684
1725
  F: __dxlog_file7,
1685
1726
  L: 207,
1686
1727
  S: this,
@@ -1699,7 +1740,7 @@ var TestExtensionWithStreams = class {
1699
1740
  const { data, bytesSent, bytesReceived, sendErrors, receiveErrors, runningTime } = await this._rpc.rpc.TestServiceWithStreams.closeTestStream({
1700
1741
  data: streamTag
1701
1742
  });
1702
- invariant6(data === streamTag, void 0, {
1743
+ invariant7(data === streamTag, void 0, {
1703
1744
  F: __dxlog_file7,
1704
1745
  L: 220,
1705
1746
  S: this,
@@ -1743,4 +1784,4 @@ export {
1743
1784
  TestExtension,
1744
1785
  TestExtensionWithStreams
1745
1786
  };
1746
- //# sourceMappingURL=chunk-3PM5BABC.mjs.map
1787
+ //# sourceMappingURL=chunk-VXXLKKBF.mjs.map