@dxos/network-manager 0.1.56-main.c787da3 → 0.1.56-main.cf6bb41

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 (53) hide show
  1. package/dist/lib/browser/{chunk-MIYEP22D.mjs → chunk-5TRIVLI6.mjs} +850 -346
  2. package/dist/lib/browser/chunk-5TRIVLI6.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +5 -1
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/browser/testing/index.mjs +6 -9
  6. package/dist/lib/browser/testing/index.mjs.map +2 -2
  7. package/dist/lib/node/index.cjs +869 -363
  8. package/dist/lib/node/index.cjs.map +4 -4
  9. package/dist/lib/node/meta.json +1 -1
  10. package/dist/lib/node/testing/index.cjs +803 -360
  11. package/dist/lib/node/testing/index.cjs.map +4 -4
  12. package/dist/types/src/network-manager.d.ts +1 -1
  13. package/dist/types/src/network-manager.d.ts.map +1 -1
  14. package/dist/types/src/signal/swarm-messenger.d.ts.map +1 -1
  15. package/dist/types/src/swarm/connection-limiter.d.ts +28 -0
  16. package/dist/types/src/swarm/connection-limiter.d.ts.map +1 -0
  17. package/dist/types/src/swarm/connection-limiter.test.d.ts +2 -0
  18. package/dist/types/src/swarm/connection-limiter.test.d.ts.map +1 -0
  19. package/dist/types/src/swarm/connection.d.ts +1 -1
  20. package/dist/types/src/swarm/connection.d.ts.map +1 -1
  21. package/dist/types/src/swarm/connection.test.d.ts +2 -0
  22. package/dist/types/src/swarm/connection.test.d.ts.map +1 -0
  23. package/dist/types/src/swarm/index.d.ts +1 -0
  24. package/dist/types/src/swarm/index.d.ts.map +1 -1
  25. package/dist/types/src/swarm/peer.d.ts +3 -1
  26. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  27. package/dist/types/src/swarm/swarm.d.ts +3 -1
  28. package/dist/types/src/swarm/swarm.d.ts.map +1 -1
  29. package/dist/types/src/topology/fully-connected-topology.d.ts.map +1 -1
  30. package/dist/types/src/topology/star-topology.d.ts.map +1 -1
  31. package/dist/types/src/transport/memory-transport.d.ts.map +1 -1
  32. package/dist/types/src/transport/webrtc-transport-proxy.d.ts.map +1 -1
  33. package/dist/types/src/transport/webrtc-transport-service.d.ts.map +1 -1
  34. package/dist/types/src/transport/webrtc-transport.d.ts.map +1 -1
  35. package/package.json +18 -18
  36. package/src/network-manager.ts +19 -6
  37. package/src/signal/swarm-messenger.ts +1 -2
  38. package/src/swarm/connection-limiter.test.ts +44 -0
  39. package/src/swarm/connection-limiter.ts +72 -0
  40. package/src/swarm/connection.test.ts +72 -0
  41. package/src/swarm/connection.ts +18 -5
  42. package/src/swarm/index.ts +1 -0
  43. package/src/swarm/peer.ts +46 -9
  44. package/src/swarm/swarm.test.ts +140 -90
  45. package/src/swarm/swarm.ts +4 -3
  46. package/src/topology/fully-connected-topology.ts +1 -2
  47. package/src/topology/mmst-topology.ts +1 -1
  48. package/src/topology/star-topology.ts +1 -2
  49. package/src/transport/memory-transport.ts +1 -1
  50. package/src/transport/webrtc-transport-proxy.ts +14 -12
  51. package/src/transport/webrtc-transport-service.ts +31 -8
  52. package/src/transport/webrtc-transport.ts +1 -1
  53. package/dist/lib/browser/chunk-MIYEP22D.mjs.map +0 -7
@@ -38,35 +38,35 @@ __export(testing_exports, {
38
38
  module.exports = __toCommonJS(testing_exports);
39
39
 
40
40
  // packages/core/mesh/network-manager/src/testing/test-builder.ts
41
- var import_keys13 = require("@dxos/keys");
41
+ var import_keys14 = require("@dxos/keys");
42
42
  var import_messaging2 = require("@dxos/messaging");
43
43
  var import_protocols6 = require("@dxos/protocols");
44
44
  var import_services2 = require("@dxos/protocols/proto/dxos/client/services");
45
45
  var import_rpc = require("@dxos/rpc");
46
- var import_util10 = require("@dxos/util");
46
+ var import_util11 = require("@dxos/util");
47
47
 
48
48
  // packages/core/mesh/network-manager/src/network-manager.ts
49
- var import_tiny_invariant5 = __toESM(require("tiny-invariant"));
50
- var import_async6 = require("@dxos/async");
51
- var import_keys7 = require("@dxos/keys");
52
- var import_log6 = require("@dxos/log");
49
+ var import_async7 = require("@dxos/async");
50
+ var import_invariant6 = require("@dxos/invariant");
51
+ var import_keys8 = require("@dxos/keys");
52
+ var import_log7 = require("@dxos/log");
53
53
  var import_messaging = require("@dxos/messaging");
54
54
  var import_protocols4 = require("@dxos/protocols");
55
55
  var import_services = require("@dxos/protocols/proto/dxos/client/services");
56
- var import_util5 = require("@dxos/util");
56
+ var import_util6 = require("@dxos/util");
57
57
 
58
58
  // packages/core/mesh/network-manager/src/connection-log.ts
59
- var import_async5 = require("@dxos/async");
59
+ var import_async6 = require("@dxos/async");
60
60
  var import_debug3 = require("@dxos/debug");
61
- var import_keys6 = require("@dxos/keys");
62
- var import_util4 = require("@dxos/util");
61
+ var import_keys7 = require("@dxos/keys");
62
+ var import_util5 = require("@dxos/util");
63
63
 
64
64
  // packages/core/mesh/network-manager/src/swarm/connection.ts
65
- var import_tiny_invariant = __toESM(require("tiny-invariant"));
66
65
  var import_async = require("@dxos/async");
67
66
  var import_context = require("@dxos/context");
68
67
  var import_debug = require("@dxos/debug");
69
68
  var import_errors = require("@dxos/errors");
69
+ var import_invariant = require("@dxos/invariant");
70
70
  var import_keys = require("@dxos/keys");
71
71
  var import_log = require("@dxos/log");
72
72
  var import_protocols = require("@dxos/protocols");
@@ -138,6 +138,18 @@ var Connection = class {
138
138
  await this._flushSignalBuffer();
139
139
  });
140
140
  this._signallingDelay = STARTING_SIGNALLING_DELAY;
141
+ import_log.log.trace("dxos.mesh.connection.construct", {
142
+ sessionId: this.sessionId,
143
+ topic: this.topic,
144
+ localPeerId: this.ownId,
145
+ remotePeerId: this.remoteId,
146
+ initiator: this.initiator
147
+ }, {
148
+ F: __dxlog_file,
149
+ L: 95,
150
+ S: this,
151
+ C: (f, a) => f(...a)
152
+ });
141
153
  }
142
154
  get state() {
143
155
  return this._state;
@@ -151,14 +163,33 @@ var Connection = class {
151
163
  /**
152
164
  * Create an underlying transport and prepares it for the connection.
153
165
  */
154
- // TODO(burdon): Make async?
155
- openConnection() {
156
- (0, import_tiny_invariant.default)(this._state === ConnectionState.INITIAL, "Invalid state.");
166
+ async openConnection() {
167
+ (0, import_invariant.invariant)(this._state === ConnectionState.INITIAL, "Invalid state.", {
168
+ F: __dxlog_file,
169
+ L: 120,
170
+ S: this,
171
+ A: [
172
+ "this._state === ConnectionState.INITIAL",
173
+ "'Invalid state.'"
174
+ ]
175
+ });
157
176
  import_log.log.trace("dxos.mesh.connection.open-connection", import_protocols.trace.begin({
158
177
  id: this._instanceId
159
178
  }), {
160
179
  F: __dxlog_file,
161
- L: 115,
180
+ L: 121,
181
+ S: this,
182
+ C: (f, a) => f(...a)
183
+ });
184
+ import_log.log.trace("dxos.mesh.connection.open", {
185
+ sessionId: this.sessionId,
186
+ topic: this.topic,
187
+ localPeerId: this.ownId,
188
+ remotePeerId: this.remoteId,
189
+ initiator: this.initiator
190
+ }, {
191
+ F: __dxlog_file,
192
+ L: 122,
162
193
  S: this,
163
194
  C: (f, a) => f(...a)
164
195
  });
@@ -169,13 +200,21 @@ var Connection = class {
169
200
  this._protocol.stream.on("close", () => {
170
201
  (0, import_log.log)("protocol stream closed", void 0, {
171
202
  F: __dxlog_file,
172
- L: 126,
203
+ L: 139,
173
204
  S: this,
174
205
  C: (f, a) => f(...a)
175
206
  });
176
207
  this.close().catch((err) => this.errors.raise(err));
177
208
  });
178
- (0, import_tiny_invariant.default)(!this._transport);
209
+ (0, import_invariant.invariant)(!this._transport, void 0, {
210
+ F: __dxlog_file,
211
+ L: 143,
212
+ S: this,
213
+ A: [
214
+ "!this._transport",
215
+ ""
216
+ ]
217
+ });
179
218
  this._transport = this._transportFactory.createTransport({
180
219
  initiator: this.initiator,
181
220
  stream: this._protocol.stream,
@@ -201,13 +240,12 @@ var Connection = class {
201
240
  id: this._instanceId
202
241
  }), {
203
242
  F: __dxlog_file,
204
- L: 159,
243
+ L: 172,
205
244
  S: this,
206
245
  C: (f, a) => f(...a)
207
246
  });
208
247
  }
209
248
  async close() {
210
- var _a2;
211
249
  if (this._state === ConnectionState.CLOSED) {
212
250
  return;
213
251
  }
@@ -217,7 +255,7 @@ var Connection = class {
217
255
  peerId: this.ownId
218
256
  }, {
219
257
  F: __dxlog_file,
220
- L: 171,
258
+ L: 184,
221
259
  S: this,
222
260
  C: (f, a) => f(...a)
223
261
  });
@@ -226,17 +264,17 @@ var Connection = class {
226
264
  } catch (err) {
227
265
  import_log.log.catch(err, void 0, {
228
266
  F: __dxlog_file,
229
- L: 177,
267
+ L: 190,
230
268
  S: this,
231
269
  C: (f, a) => f(...a)
232
270
  });
233
271
  }
234
272
  try {
235
- await ((_a2 = this._transport) == null ? void 0 : _a2.destroy());
273
+ await this._transport?.destroy();
236
274
  } catch (err) {
237
275
  import_log.log.catch(err, void 0, {
238
276
  F: __dxlog_file,
239
- L: 184,
277
+ L: 197,
240
278
  S: this,
241
279
  C: (f, a) => f(...a)
242
280
  });
@@ -245,7 +283,7 @@ var Connection = class {
245
283
  peerId: this.ownId
246
284
  }, {
247
285
  F: __dxlog_file,
248
- L: 187,
286
+ L: 200,
249
287
  S: this,
250
288
  C: (f, a) => f(...a)
251
289
  });
@@ -285,7 +323,7 @@ var Connection = class {
285
323
  err
286
324
  }, {
287
325
  F: __dxlog_file,
288
- L: 221,
326
+ L: 234,
289
327
  S: this,
290
328
  C: (f, a) => f(...a)
291
329
  });
@@ -296,21 +334,52 @@ var Connection = class {
296
334
  * Receive a signal from the remote peer.
297
335
  */
298
336
  async signal(msg) {
299
- var _a2, _b, _c;
300
- (0, import_tiny_invariant.default)(msg.sessionId);
337
+ (0, import_invariant.invariant)(msg.sessionId, void 0, {
338
+ F: __dxlog_file,
339
+ L: 243,
340
+ S: this,
341
+ A: [
342
+ "msg.sessionId",
343
+ ""
344
+ ]
345
+ });
301
346
  if (!msg.sessionId.equals(this.sessionId)) {
302
347
  (0, import_log.log)("dropping signal for incorrect session id", void 0, {
303
348
  F: __dxlog_file,
304
- L: 232,
349
+ L: 245,
305
350
  S: this,
306
351
  C: (f, a) => f(...a)
307
352
  });
308
353
  return;
309
354
  }
310
- (0, import_tiny_invariant.default)(msg.data.signal || msg.data.signalBatch);
311
- (0, import_tiny_invariant.default)((_a2 = msg.author) == null ? void 0 : _a2.equals(this.remoteId));
312
- (0, import_tiny_invariant.default)((_b = msg.recipient) == null ? void 0 : _b.equals(this.ownId));
313
- const signals = msg.data.signalBatch ? (_c = msg.data.signalBatch.signals) != null ? _c : [] : [
355
+ (0, import_invariant.invariant)(msg.data.signal || msg.data.signalBatch, void 0, {
356
+ F: __dxlog_file,
357
+ L: 248,
358
+ S: this,
359
+ A: [
360
+ "msg.data.signal || msg.data.signalBatch",
361
+ ""
362
+ ]
363
+ });
364
+ (0, import_invariant.invariant)(msg.author?.equals(this.remoteId), void 0, {
365
+ F: __dxlog_file,
366
+ L: 249,
367
+ S: this,
368
+ A: [
369
+ "msg.author?.equals(this.remoteId)",
370
+ ""
371
+ ]
372
+ });
373
+ (0, import_invariant.invariant)(msg.recipient?.equals(this.ownId), void 0, {
374
+ F: __dxlog_file,
375
+ L: 250,
376
+ S: this,
377
+ A: [
378
+ "msg.recipient?.equals(this.ownId)",
379
+ ""
380
+ ]
381
+ });
382
+ const signals = msg.data.signalBatch ? msg.data.signalBatch.signals ?? [] : [
314
383
  msg.data.signal
315
384
  ];
316
385
  for (const signal of signals) {
@@ -324,20 +393,28 @@ var Connection = class {
324
393
  msg: msg.data
325
394
  }, {
326
395
  F: __dxlog_file,
327
- L: 246,
396
+ L: 259,
328
397
  S: this,
329
398
  C: (f, a) => f(...a)
330
399
  });
331
400
  this._incomingSignalBuffer.push(signal);
332
401
  } else {
333
- (0, import_tiny_invariant.default)(this._transport, "Connection not ready to accept signals.");
402
+ (0, import_invariant.invariant)(this._transport, "Connection not ready to accept signals.", {
403
+ F: __dxlog_file,
404
+ L: 262,
405
+ S: this,
406
+ A: [
407
+ "this._transport",
408
+ "'Connection not ready to accept signals.'"
409
+ ]
410
+ });
334
411
  (0, import_log.log)("received signal", {
335
412
  peerId: this.ownId,
336
413
  remoteId: this.remoteId,
337
414
  msg: msg.data
338
415
  }, {
339
416
  F: __dxlog_file,
340
- L: 250,
417
+ L: 263,
341
418
  S: this,
342
419
  C: (f, a) => f(...a)
343
420
  });
@@ -352,11 +429,19 @@ var Connection = class {
352
429
  peerId: this.ownId
353
430
  }, {
354
431
  F: __dxlog_file,
355
- L: 257,
432
+ L: 270,
356
433
  S: this,
357
434
  C: (f, a) => f(...a)
358
435
  });
359
- (0, import_tiny_invariant.default)(state !== this._state, "Already in this state.");
436
+ (0, import_invariant.invariant)(state !== this._state, "Already in this state.", {
437
+ F: __dxlog_file,
438
+ L: 271,
439
+ S: this,
440
+ A: [
441
+ "state !== this._state",
442
+ "'Already in this state.'"
443
+ ]
444
+ });
360
445
  this._state = state;
361
446
  this.stateChanged.emit(state);
362
447
  }
@@ -366,18 +451,18 @@ _ts_decorate([
366
451
  ], Connection.prototype, "close", null);
367
452
 
368
453
  // packages/core/mesh/network-manager/src/swarm/swarm.ts
369
- var import_tiny_invariant4 = __toESM(require("tiny-invariant"));
370
454
  var import_async3 = require("@dxos/async");
371
455
  var import_context4 = require("@dxos/context");
372
456
  var import_debug2 = require("@dxos/debug");
457
+ var import_invariant4 = require("@dxos/invariant");
373
458
  var import_keys4 = require("@dxos/keys");
374
459
  var import_log4 = require("@dxos/log");
375
460
  var import_protocols3 = require("@dxos/protocols");
376
461
  var import_util2 = require("@dxos/util");
377
462
 
378
463
  // packages/core/mesh/network-manager/src/signal/swarm-messenger.ts
379
- var import_tiny_invariant2 = __toESM(require("tiny-invariant"));
380
464
  var import_context2 = require("@dxos/context");
465
+ var import_invariant2 = require("@dxos/invariant");
381
466
  var import_keys2 = require("@dxos/keys");
382
467
  var import_log2 = require("@dxos/log");
383
468
  var import_protocols2 = require("@dxos/protocols");
@@ -394,7 +479,6 @@ var SwarmMessenger = class {
394
479
  this._topic = topic;
395
480
  }
396
481
  async receiveMessage({ author, recipient, payload }) {
397
- var _a2, _b, _c, _d;
398
482
  if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
399
483
  return;
400
484
  }
@@ -408,25 +492,25 @@ var SwarmMessenger = class {
408
492
  msg: message
409
493
  }, {
410
494
  F: __dxlog_file2,
411
- L: 70,
495
+ L: 69,
412
496
  S: this,
413
497
  C: (f, a) => f(...a)
414
498
  });
415
- if ((_a2 = message.data) == null ? void 0 : _a2.offer) {
499
+ if (message.data?.offer) {
416
500
  await this._handleOffer({
417
501
  author,
418
502
  recipient,
419
503
  message
420
504
  });
421
- } else if ((_b = message.data) == null ? void 0 : _b.answer) {
505
+ } else if (message.data?.answer) {
422
506
  await this._resolveAnswers(message);
423
- } else if ((_c = message.data) == null ? void 0 : _c.signal) {
507
+ } else if (message.data?.signal) {
424
508
  await this._handleSignal({
425
509
  author,
426
510
  recipient,
427
511
  message
428
512
  });
429
- } else if ((_d = message.data) == null ? void 0 : _d.signalBatch) {
513
+ } else if (message.data?.signalBatch) {
430
514
  await this._handleSignal({
431
515
  author,
432
516
  recipient,
@@ -437,15 +521,22 @@ var SwarmMessenger = class {
437
521
  message
438
522
  }, {
439
523
  F: __dxlog_file2,
440
- L: 81,
524
+ L: 80,
441
525
  S: this,
442
526
  C: (f, a) => f(...a)
443
527
  });
444
528
  }
445
529
  }
446
530
  async signal(message) {
447
- var _a2, _b;
448
- (0, import_tiny_invariant2.default)(((_a2 = message.data) == null ? void 0 : _a2.signal) || ((_b = message.data) == null ? void 0 : _b.signalBatch), "Invalid message");
531
+ (0, import_invariant2.invariant)(message.data?.signal || message.data?.signalBatch, "Invalid message", {
532
+ F: __dxlog_file2,
533
+ L: 85,
534
+ S: this,
535
+ A: [
536
+ "message.data?.signal || message.data?.signalBatch",
537
+ "'Invalid message'"
538
+ ]
539
+ });
449
540
  await this._sendReliableMessage({
450
541
  author: message.author,
451
542
  recipient: message.recipient,
@@ -469,11 +560,10 @@ var SwarmMessenger = class {
469
560
  });
470
561
  }
471
562
  async _sendReliableMessage({ author, recipient, message }) {
472
- var _a2;
473
563
  const networkMessage = {
474
564
  ...message,
475
565
  // Setting unique message_id if it not specified yet.
476
- messageId: (_a2 = message.messageId) != null ? _a2 : import_keys2.PublicKey.random()
566
+ messageId: message.messageId ?? import_keys2.PublicKey.random()
477
567
  };
478
568
  (0, import_log2.log)("sending", {
479
569
  from: author,
@@ -481,7 +571,7 @@ var SwarmMessenger = class {
481
571
  msg: networkMessage
482
572
  }, {
483
573
  F: __dxlog_file2,
484
- L: 124,
574
+ L: 123,
485
575
  S: this,
486
576
  C: (f, a) => f(...a)
487
577
  });
@@ -495,17 +585,32 @@ var SwarmMessenger = class {
495
585
  });
496
586
  }
497
587
  async _resolveAnswers(message) {
498
- var _a2, _b, _c;
499
- (0, import_tiny_invariant2.default)((_b = (_a2 = message.data) == null ? void 0 : _a2.answer) == null ? void 0 : _b.offerMessageId, "No offerMessageId");
588
+ (0, import_invariant2.invariant)(message.data?.answer?.offerMessageId, "No offerMessageId", {
589
+ F: __dxlog_file2,
590
+ L: 135,
591
+ S: this,
592
+ A: [
593
+ "message.data?.answer?.offerMessageId",
594
+ "'No offerMessageId'"
595
+ ]
596
+ });
500
597
  const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
501
598
  if (offerRecord) {
502
599
  this._offerRecords.delete(message.data.answer.offerMessageId);
503
- (0, import_tiny_invariant2.default)((_c = message.data) == null ? void 0 : _c.answer, "No answer");
600
+ (0, import_invariant2.invariant)(message.data?.answer, "No answer", {
601
+ F: __dxlog_file2,
602
+ L: 139,
603
+ S: this,
604
+ A: [
605
+ "message.data?.answer",
606
+ "'No answer'"
607
+ ]
608
+ });
504
609
  (0, import_log2.log)("resolving", {
505
610
  answer: message.data.answer
506
611
  }, {
507
612
  F: __dxlog_file2,
508
- L: 141,
613
+ L: 140,
509
614
  S: this,
510
615
  C: (f, a) => f(...a)
511
616
  });
@@ -513,7 +618,15 @@ var SwarmMessenger = class {
513
618
  }
514
619
  }
515
620
  async _handleOffer({ author, recipient, message }) {
516
- (0, import_tiny_invariant2.default)(message.data.offer, "No offer");
621
+ (0, import_invariant2.invariant)(message.data.offer, "No offer", {
622
+ F: __dxlog_file2,
623
+ L: 154,
624
+ S: this,
625
+ A: [
626
+ "message.data.offer",
627
+ "'No offer'"
628
+ ]
629
+ });
517
630
  const offerMessage = {
518
631
  author,
519
632
  recipient,
@@ -541,15 +654,31 @@ var SwarmMessenger = class {
541
654
  err
542
655
  }, {
543
656
  F: __dxlog_file2,
544
- L: 175,
657
+ L: 174,
545
658
  S: this,
546
659
  C: (f, a) => f(...a)
547
660
  });
548
661
  }
549
662
  }
550
663
  async _handleSignal({ author, recipient, message }) {
551
- (0, import_tiny_invariant2.default)(message.messageId);
552
- (0, import_tiny_invariant2.default)(message.data.signal || message.data.signalBatch, "Invalid message");
664
+ (0, import_invariant2.invariant)(message.messageId, void 0, {
665
+ F: __dxlog_file2,
666
+ L: 187,
667
+ S: this,
668
+ A: [
669
+ "message.messageId",
670
+ ""
671
+ ]
672
+ });
673
+ (0, import_invariant2.invariant)(message.data.signal || message.data.signalBatch, "Invalid message", {
674
+ F: __dxlog_file2,
675
+ L: 188,
676
+ S: this,
677
+ A: [
678
+ "message.data.signal || message.data.signalBatch",
679
+ "'Invalid message'"
680
+ ]
681
+ });
553
682
  const signalMessage = {
554
683
  author,
555
684
  recipient,
@@ -564,9 +693,10 @@ var SwarmMessenger = class {
564
693
  };
565
694
 
566
695
  // packages/core/mesh/network-manager/src/swarm/peer.ts
567
- var import_tiny_invariant3 = __toESM(require("tiny-invariant"));
568
696
  var import_async2 = require("@dxos/async");
569
697
  var import_context3 = require("@dxos/context");
698
+ var import_errors2 = require("@dxos/errors");
699
+ var import_invariant3 = require("@dxos/invariant");
570
700
  var import_keys3 = require("@dxos/keys");
571
701
  var import_log3 = require("@dxos/log");
572
702
  function _ts_decorate2(decorators, target, key, desc) {
@@ -582,13 +712,14 @@ function _ts_decorate2(decorators, target, key, desc) {
582
712
  var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
583
713
  var CONNECTION_COUNTS_STABLE_AFTER = 5e3;
584
714
  var Peer = class {
585
- constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _callbacks) {
715
+ constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _connectionLimiter, _callbacks) {
586
716
  this.id = id;
587
717
  this.topic = topic;
588
718
  this.localPeerId = localPeerId;
589
719
  this._signalMessaging = _signalMessaging;
590
720
  this._protocolProvider = _protocolProvider;
591
721
  this._transportFactory = _transportFactory;
722
+ this._connectionLimiter = _connectionLimiter;
592
723
  this._callbacks = _callbacks;
593
724
  this._availableAfter = 0;
594
725
  this.availableToConnect = true;
@@ -603,13 +734,14 @@ var Peer = class {
603
734
  const remoteId = message.author;
604
735
  if (this.connection || this.initiating) {
605
736
  if (remoteId.toHex() < this.localPeerId.toHex()) {
606
- (0, import_log3.log)("closing local connection and accepting remote peer's offer", {
607
- id: this.id,
737
+ (0, import_log3.log)("close local connection", {
738
+ localPeerId: this.id,
608
739
  topic: this.topic,
609
- peerId: this.localPeerId
740
+ remotePeerId: this.localPeerId,
741
+ sessionId: this.connection?.sessionId
610
742
  }, {
611
743
  F: __dxlog_file3,
612
- L: 105,
744
+ L: 106,
613
745
  S: this,
614
746
  C: (f, a) => f(...a)
615
747
  });
@@ -624,22 +756,33 @@ var Peer = class {
624
756
  }
625
757
  if (await this._callbacks.onOffer(remoteId)) {
626
758
  if (!this.connection) {
627
- (0, import_tiny_invariant3.default)(message.sessionId);
759
+ (0, import_invariant3.invariant)(message.sessionId, void 0, {
760
+ F: __dxlog_file3,
761
+ L: 126,
762
+ S: this,
763
+ A: [
764
+ "message.sessionId",
765
+ ""
766
+ ]
767
+ });
628
768
  const connection = this._createConnection(false, message.sessionId);
629
769
  try {
630
- connection.openConnection();
770
+ await this._connectionLimiter.connecting(message.sessionId);
771
+ await connection.openConnection();
631
772
  } catch (err) {
632
- import_log3.log.warn("connection error", {
633
- topic: this.topic,
634
- peerId: this.localPeerId,
635
- remoteId: this.id,
636
- err
637
- }, {
638
- F: __dxlog_file3,
639
- L: 129,
640
- S: this,
641
- C: (f, a) => f(...a)
642
- });
773
+ if (!(err instanceof import_errors2.CancelledError)) {
774
+ import_log3.log.warn("connection error", {
775
+ topic: this.topic,
776
+ peerId: this.localPeerId,
777
+ remoteId: this.id,
778
+ err
779
+ }, {
780
+ F: __dxlog_file3,
781
+ L: 134,
782
+ S: this,
783
+ C: (f, a) => f(...a)
784
+ });
785
+ }
643
786
  await this.closeConnection();
644
787
  }
645
788
  return {
@@ -655,8 +798,24 @@ var Peer = class {
655
798
  * Initiate a connection to the remote peer.
656
799
  */
657
800
  async initiateConnection() {
658
- (0, import_tiny_invariant3.default)(!this.initiating, "Initiation in progress.");
659
- (0, import_tiny_invariant3.default)(!this.connection, "Already connected.");
801
+ (0, import_invariant3.invariant)(!this.initiating, "Initiation in progress.", {
802
+ F: __dxlog_file3,
803
+ L: 151,
804
+ S: this,
805
+ A: [
806
+ "!this.initiating",
807
+ "'Initiation in progress.'"
808
+ ]
809
+ });
810
+ (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
811
+ F: __dxlog_file3,
812
+ L: 152,
813
+ S: this,
814
+ A: [
815
+ "!this.connection",
816
+ "'Already connected.'"
817
+ ]
818
+ });
660
819
  const sessionId = import_keys3.PublicKey.random();
661
820
  (0, import_log3.log)("initiating...", {
662
821
  id: this.id,
@@ -665,13 +824,14 @@ var Peer = class {
665
824
  sessionId
666
825
  }, {
667
826
  F: __dxlog_file3,
668
- L: 147,
827
+ L: 154,
669
828
  S: this,
670
829
  C: (f, a) => f(...a)
671
830
  });
672
831
  const connection = this._createConnection(true, sessionId);
673
832
  this.initiating = true;
674
833
  try {
834
+ await this._connectionLimiter.connecting(sessionId);
675
835
  const answer = await this._signalMessaging.offer({
676
836
  author: this.localPeerId,
677
837
  recipient: this.id,
@@ -688,14 +848,14 @@ var Peer = class {
688
848
  remoteId: this.id
689
849
  }, {
690
850
  F: __dxlog_file3,
691
- L: 159,
851
+ L: 169,
692
852
  S: this,
693
853
  C: (f, a) => f(...a)
694
854
  });
695
855
  if (connection.state !== ConnectionState.INITIAL) {
696
856
  (0, import_log3.log)("ignoring response", void 0, {
697
857
  F: __dxlog_file3,
698
- L: 161,
858
+ L: 171,
699
859
  S: this,
700
860
  C: (f, a) => f(...a)
701
861
  });
@@ -705,7 +865,7 @@ var Peer = class {
705
865
  this._callbacks.onRejected();
706
866
  return;
707
867
  }
708
- connection.openConnection();
868
+ await connection.openConnection();
709
869
  this._callbacks.onAccepted();
710
870
  } catch (err) {
711
871
  (0, import_log3.log)("initiation error", {
@@ -715,7 +875,7 @@ var Peer = class {
715
875
  remoteId: this.id
716
876
  }, {
717
877
  F: __dxlog_file3,
718
- L: 172,
878
+ L: 182,
719
879
  S: this,
720
880
  C: (f, a) => f(...a)
721
881
  });
@@ -730,7 +890,6 @@ var Peer = class {
730
890
  * Either we're initiating a connection or creating one in response to an offer from the other peer.
731
891
  */
732
892
  _createConnection(initiator, sessionId) {
733
- var _a2;
734
893
  (0, import_log3.log)("creating connection", {
735
894
  topic: this.topic,
736
895
  peerId: this.localPeerId,
@@ -739,11 +898,19 @@ var Peer = class {
739
898
  sessionId
740
899
  }, {
741
900
  F: __dxlog_file3,
742
- L: 186,
901
+ L: 196,
743
902
  S: this,
744
903
  C: (f, a) => f(...a)
745
904
  });
746
- (0, import_tiny_invariant3.default)(!this.connection, "Already connected.");
905
+ (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
906
+ F: __dxlog_file3,
907
+ L: 203,
908
+ S: this,
909
+ A: [
910
+ "!this.connection",
911
+ "'Already connected.'"
912
+ ]
913
+ });
747
914
  const connection = new Connection(
748
915
  this.topic,
749
916
  this.localPeerId,
@@ -761,7 +928,7 @@ var Peer = class {
761
928
  this._transportFactory
762
929
  );
763
930
  this._callbacks.onInitiated(connection);
764
- void ((_a2 = this._connectionCtx) == null ? void 0 : _a2.dispose());
931
+ void this._connectionCtx?.dispose();
765
932
  this._connectionCtx = this._ctx.derive();
766
933
  connection.stateChanged.on((state) => {
767
934
  switch (state) {
@@ -769,6 +936,19 @@ var Peer = class {
769
936
  this.availableToConnect = true;
770
937
  this._lastConnectionTime = Date.now();
771
938
  this._callbacks.onConnected();
939
+ this._connectionLimiter.doneConnecting(sessionId);
940
+ import_log3.log.trace("dxos.mesh.connection.connected", {
941
+ topic: this.topic,
942
+ localPeerId: this.localPeerId,
943
+ remotePeerId: this.id,
944
+ sessionId,
945
+ initiator
946
+ }, {
947
+ F: __dxlog_file3,
948
+ L: 229,
949
+ S: this,
950
+ C: (f, a) => f(...a)
951
+ });
772
952
  break;
773
953
  }
774
954
  case ConnectionState.CLOSED: {
@@ -779,11 +959,31 @@ var Peer = class {
779
959
  initiator
780
960
  }, {
781
961
  F: __dxlog_file3,
782
- L: 221,
962
+ L: 240,
963
+ S: this,
964
+ C: (f, a) => f(...a)
965
+ });
966
+ (0, import_invariant3.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
967
+ F: __dxlog_file3,
968
+ L: 241,
969
+ S: this,
970
+ A: [
971
+ "this.connection === connection",
972
+ "'Connection mismatch (race condition).'"
973
+ ]
974
+ });
975
+ import_log3.log.trace("dxos.mesh.connection.closed", {
976
+ topic: this.topic,
977
+ localPeerId: this.localPeerId,
978
+ remotePeerId: this.id,
979
+ sessionId,
980
+ initiator
981
+ }, {
982
+ F: __dxlog_file3,
983
+ L: 243,
783
984
  S: this,
784
985
  C: (f, a) => f(...a)
785
986
  });
786
- (0, import_tiny_invariant3.default)(this.connection === connection, "Connection mismatch (race condition).");
787
987
  if (this._lastConnectionTime && this._lastConnectionTime + CONNECTION_COUNTS_STABLE_AFTER < Date.now()) {
788
988
  this._availableAfter = 0;
789
989
  } else {
@@ -792,6 +992,7 @@ var Peer = class {
792
992
  }
793
993
  this.connection = void 0;
794
994
  this._callbacks.onDisconnected();
995
+ this._connectionLimiter.doneConnecting(sessionId);
795
996
  (0, import_async2.scheduleTask)(this._connectionCtx, () => {
796
997
  this.availableToConnect = true;
797
998
  this._callbacks.onPeerAvailable();
@@ -809,7 +1010,20 @@ var Peer = class {
809
1010
  err
810
1011
  }, {
811
1012
  F: __dxlog_file3,
812
- L: 249,
1013
+ L: 277,
1014
+ S: this,
1015
+ C: (f, a) => f(...a)
1016
+ });
1017
+ import_log3.log.trace("dxos.mesh.connection.error", {
1018
+ topic: this.topic,
1019
+ localPeerId: this.localPeerId,
1020
+ remotePeerId: this.id,
1021
+ sessionId,
1022
+ initiator,
1023
+ err
1024
+ }, {
1025
+ F: __dxlog_file3,
1026
+ L: 278,
813
1027
  S: this,
814
1028
  C: (f, a) => f(...a)
815
1029
  });
@@ -828,7 +1042,7 @@ var Peer = class {
828
1042
  sessionId: connection.sessionId
829
1043
  }, {
830
1044
  F: __dxlog_file3,
831
- L: 265,
1045
+ L: 301,
832
1046
  S: this,
833
1047
  C: (f, a) => f(...a)
834
1048
  });
@@ -838,7 +1052,7 @@ var Peer = class {
838
1052
  sessionId: connection.sessionId
839
1053
  }, {
840
1054
  F: __dxlog_file3,
841
- L: 271,
1055
+ L: 307,
842
1056
  S: this,
843
1057
  C: (f, a) => f(...a)
844
1058
  });
@@ -849,7 +1063,7 @@ var Peer = class {
849
1063
  message
850
1064
  }, {
851
1065
  F: __dxlog_file3,
852
- L: 276,
1066
+ L: 312,
853
1067
  S: this,
854
1068
  C: (f, a) => f(...a)
855
1069
  });
@@ -858,18 +1072,17 @@ var Peer = class {
858
1072
  await this.connection.signal(message);
859
1073
  }
860
1074
  async destroy() {
861
- var _a2;
862
1075
  await this._ctx.dispose();
863
1076
  (0, import_log3.log)("Destroying peer", {
864
1077
  peerId: this.id,
865
1078
  topic: this.topic
866
1079
  }, {
867
1080
  F: __dxlog_file3,
868
- L: 285,
1081
+ L: 322,
869
1082
  S: this,
870
1083
  C: (f, a) => f(...a)
871
1084
  });
872
- await ((_a2 = this == null ? void 0 : this.connection) == null ? void 0 : _a2.close());
1085
+ await this?.connection?.close();
873
1086
  }
874
1087
  };
875
1088
  _ts_decorate2([
@@ -905,7 +1118,7 @@ var getClassName = (obj) => Object.getPrototypeOf(obj).constructor.name;
905
1118
  var Swarm = class {
906
1119
  // TODO(burdon): Swarm => Peer.create/destroy =< Connection.open/close
907
1120
  // TODO(burdon): Split up properties.
908
- constructor(_topic, _ownPeerId, _topology, _protocolProvider, _messenger, _transportFactory, _label) {
1121
+ constructor(_topic, _ownPeerId, _topology, _protocolProvider, _messenger, _transportFactory, _label, _connectionLimiter) {
909
1122
  this._topic = _topic;
910
1123
  this._ownPeerId = _ownPeerId;
911
1124
  this._topology = _topology;
@@ -913,6 +1126,7 @@ var Swarm = class {
913
1126
  this._messenger = _messenger;
914
1127
  this._transportFactory = _transportFactory;
915
1128
  this._label = _label;
1129
+ this._connectionLimiter = _connectionLimiter;
916
1130
  this._peers = new import_util2.ComplexMap(import_keys4.PublicKey.hash);
917
1131
  this._ctx = new import_context4.Context();
918
1132
  this._listeningHandle = void 0;
@@ -929,7 +1143,7 @@ var Swarm = class {
929
1143
  }
930
1144
  }), {
931
1145
  F: __dxlog_file4,
932
- L: 86,
1146
+ L: 87,
933
1147
  S: this,
934
1148
  C: (f, a) => f(...a)
935
1149
  });
@@ -937,7 +1151,7 @@ var Swarm = class {
937
1151
  peerId: _ownPeerId
938
1152
  }, {
939
1153
  F: __dxlog_file4,
940
- L: 90,
1154
+ L: 91,
941
1155
  S: this,
942
1156
  C: (f, a) => f(...a)
943
1157
  });
@@ -952,7 +1166,7 @@ var Swarm = class {
952
1166
  id: this._instanceId
953
1167
  }), {
954
1168
  F: __dxlog_file4,
955
- L: 99,
1169
+ L: 100,
956
1170
  S: this,
957
1171
  C: (f, a) => f(...a)
958
1172
  });
@@ -973,7 +1187,15 @@ var Swarm = class {
973
1187
  return this._topic;
974
1188
  }
975
1189
  async open() {
976
- (0, import_tiny_invariant4.default)(!this._listeningHandle);
1190
+ (0, import_invariant4.invariant)(!this._listeningHandle, void 0, {
1191
+ F: __dxlog_file4,
1192
+ L: 127,
1193
+ S: this,
1194
+ A: [
1195
+ "!this._listeningHandle",
1196
+ ""
1197
+ ]
1198
+ });
977
1199
  this._listeningHandle = await this._messenger.listen({
978
1200
  peerId: this._ownPeerId,
979
1201
  payloadType: "dxos.mesh.swarm.SwarmMessage",
@@ -982,7 +1204,7 @@ var Swarm = class {
982
1204
  err
983
1205
  }, {
984
1206
  F: __dxlog_file4,
985
- L: 133,
1207
+ L: 134,
986
1208
  S: this,
987
1209
  C: (f, a) => f(...a)
988
1210
  }));
@@ -990,27 +1212,34 @@ var Swarm = class {
990
1212
  });
991
1213
  }
992
1214
  async destroy() {
993
- var _a2;
994
1215
  (0, import_log4.log)("destroying...", void 0, {
995
1216
  F: __dxlog_file4,
996
- L: 139,
1217
+ L: 140,
997
1218
  S: this,
998
1219
  C: (f, a) => f(...a)
999
1220
  });
1000
- await ((_a2 = this._listeningHandle) == null ? void 0 : _a2.unsubscribe());
1221
+ await this._listeningHandle?.unsubscribe();
1001
1222
  this._listeningHandle = void 0;
1002
1223
  await this._ctx.dispose();
1003
1224
  await this._topology.destroy();
1004
1225
  await Promise.all(Array.from(this._peers.keys()).map((key) => this._destroyPeer(key)));
1005
1226
  (0, import_log4.log)("destroyed", void 0, {
1006
1227
  F: __dxlog_file4,
1007
- L: 146,
1228
+ L: 147,
1008
1229
  S: this,
1009
1230
  C: (f, a) => f(...a)
1010
1231
  });
1011
1232
  }
1012
1233
  async setTopology(topology) {
1013
- (0, import_tiny_invariant4.default)(!this._ctx.disposed, "Swarm is offline");
1234
+ (0, import_invariant4.invariant)(!this._ctx.disposed, "Swarm is offline", {
1235
+ F: __dxlog_file4,
1236
+ L: 151,
1237
+ S: this,
1238
+ A: [
1239
+ "!this._ctx.disposed",
1240
+ "'Swarm is offline'"
1241
+ ]
1242
+ });
1014
1243
  if (topology === this._topology) {
1015
1244
  return;
1016
1245
  }
@@ -1019,7 +1248,7 @@ var Swarm = class {
1019
1248
  topology: getClassName(topology)
1020
1249
  }, {
1021
1250
  F: __dxlog_file4,
1022
- L: 154,
1251
+ L: 155,
1023
1252
  S: this,
1024
1253
  C: (f, a) => f(...a)
1025
1254
  });
@@ -1029,19 +1258,18 @@ var Swarm = class {
1029
1258
  this._topology.update();
1030
1259
  }
1031
1260
  onSwarmEvent(swarmEvent) {
1032
- var _a2;
1033
1261
  (0, import_log4.log)("swarm event", {
1034
1262
  swarmEvent
1035
1263
  }, {
1036
1264
  F: __dxlog_file4,
1037
- L: 167,
1265
+ L: 168,
1038
1266
  S: this,
1039
1267
  C: (f, a) => f(...a)
1040
1268
  });
1041
1269
  if (this._ctx.disposed) {
1042
1270
  (0, import_log4.log)("swarm event ignored for disposed swarm", void 0, {
1043
1271
  F: __dxlog_file4,
1044
- L: 170,
1272
+ L: 171,
1045
1273
  S: this,
1046
1274
  C: (f, a) => f(...a)
1047
1275
  });
@@ -1053,7 +1281,7 @@ var Swarm = class {
1053
1281
  peerId
1054
1282
  }, {
1055
1283
  F: __dxlog_file4,
1056
- L: 176,
1284
+ L: 177,
1057
1285
  S: this,
1058
1286
  C: (f, a) => f(...a)
1059
1287
  });
@@ -1065,10 +1293,10 @@ var Swarm = class {
1065
1293
  const peer = this._peers.get(import_keys4.PublicKey.from(swarmEvent.peerLeft.peer));
1066
1294
  if (peer) {
1067
1295
  peer.advertizing = false;
1068
- if (((_a2 = peer.connection) == null ? void 0 : _a2.state) !== ConnectionState.CONNECTED) {
1296
+ if (peer.connection?.state !== ConnectionState.CONNECTED) {
1069
1297
  void this._destroyPeer(peer.id).catch((err) => import_log4.log.catch(err, void 0, {
1070
1298
  F: __dxlog_file4,
1071
- L: 187,
1299
+ L: 188,
1072
1300
  S: this,
1073
1301
  C: (f, a) => f(...a)
1074
1302
  }));
@@ -1078,19 +1306,18 @@ var Swarm = class {
1078
1306
  this._topology.update();
1079
1307
  }
1080
1308
  async onOffer(message) {
1081
- var _a2, _b;
1082
1309
  (0, import_log4.log)("offer", {
1083
1310
  message
1084
1311
  }, {
1085
1312
  F: __dxlog_file4,
1086
- L: 197,
1313
+ L: 198,
1087
1314
  S: this,
1088
1315
  C: (f, a) => f(...a)
1089
1316
  });
1090
1317
  if (this._ctx.disposed) {
1091
1318
  (0, import_log4.log)("ignored for disposed swarm", void 0, {
1092
1319
  F: __dxlog_file4,
1093
- L: 199,
1320
+ L: 200,
1094
1321
  S: this,
1095
1322
  C: (f, a) => f(...a)
1096
1323
  });
@@ -1098,13 +1325,21 @@ var Swarm = class {
1098
1325
  accept: false
1099
1326
  };
1100
1327
  }
1101
- (0, import_tiny_invariant4.default)(message.author);
1102
- if (!((_a2 = message.recipient) == null ? void 0 : _a2.equals(this._ownPeerId))) {
1328
+ (0, import_invariant4.invariant)(message.author, void 0, {
1329
+ F: __dxlog_file4,
1330
+ L: 205,
1331
+ S: this,
1332
+ A: [
1333
+ "message.author",
1334
+ ""
1335
+ ]
1336
+ });
1337
+ if (!message.recipient?.equals(this._ownPeerId)) {
1103
1338
  (0, import_log4.log)("rejecting offer with incorrect peerId", {
1104
1339
  message
1105
1340
  }, {
1106
1341
  F: __dxlog_file4,
1107
- L: 206,
1342
+ L: 207,
1108
1343
  S: this,
1109
1344
  C: (f, a) => f(...a)
1110
1345
  });
@@ -1112,12 +1347,12 @@ var Swarm = class {
1112
1347
  accept: false
1113
1348
  };
1114
1349
  }
1115
- if (!((_b = message.topic) == null ? void 0 : _b.equals(this._topic))) {
1350
+ if (!message.topic?.equals(this._topic)) {
1116
1351
  (0, import_log4.log)("rejecting offer with incorrect topic", {
1117
1352
  message
1118
1353
  }, {
1119
1354
  F: __dxlog_file4,
1120
- L: 210,
1355
+ L: 211,
1121
1356
  S: this,
1122
1357
  C: (f, a) => f(...a)
1123
1358
  });
@@ -1131,7 +1366,6 @@ var Swarm = class {
1131
1366
  return answer;
1132
1367
  }
1133
1368
  async onSignal(message) {
1134
- var _a2, _b;
1135
1369
  (0, import_log4.log)("signal", {
1136
1370
  message
1137
1371
  }, {
@@ -1149,9 +1383,33 @@ var Swarm = class {
1149
1383
  });
1150
1384
  return;
1151
1385
  }
1152
- (0, import_tiny_invariant4.default)((_a2 = message.recipient) == null ? void 0 : _a2.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`);
1153
- (0, import_tiny_invariant4.default)((_b = message.topic) == null ? void 0 : _b.equals(this._topic));
1154
- (0, import_tiny_invariant4.default)(message.author);
1386
+ (0, import_invariant4.invariant)(message.recipient?.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1387
+ F: __dxlog_file4,
1388
+ L: 227,
1389
+ S: this,
1390
+ A: [
1391
+ "message.recipient?.equals(this._ownPeerId)",
1392
+ "`Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`"
1393
+ ]
1394
+ });
1395
+ (0, import_invariant4.invariant)(message.topic?.equals(this._topic), void 0, {
1396
+ F: __dxlog_file4,
1397
+ L: 231,
1398
+ S: this,
1399
+ A: [
1400
+ "message.topic?.equals(this._topic)",
1401
+ ""
1402
+ ]
1403
+ });
1404
+ (0, import_invariant4.invariant)(message.author, void 0, {
1405
+ F: __dxlog_file4,
1406
+ L: 232,
1407
+ S: this,
1408
+ A: [
1409
+ "message.author",
1410
+ ""
1411
+ ]
1412
+ });
1155
1413
  const peer = this._getOrCreatePeer(message.author);
1156
1414
  await peer.onSignal(message);
1157
1415
  }
@@ -1169,7 +1427,7 @@ var Swarm = class {
1169
1427
  _getOrCreatePeer(peerId) {
1170
1428
  let peer = this._peers.get(peerId);
1171
1429
  if (!peer) {
1172
- peer = new Peer(peerId, this._topic, this._ownPeerId, this._swarmMessenger, this._protocolProvider, this._transportFactory, {
1430
+ peer = new Peer(peerId, this._topic, this._ownPeerId, this._swarmMessenger, this._protocolProvider, this._transportFactory, this._connectionLimiter, {
1173
1431
  onInitiated: (connection) => {
1174
1432
  this.connectionAdded.emit(connection);
1175
1433
  },
@@ -1203,7 +1461,15 @@ var Swarm = class {
1203
1461
  return peer;
1204
1462
  }
1205
1463
  async _destroyPeer(peerId) {
1206
- (0, import_tiny_invariant4.default)(this._peers.has(peerId));
1464
+ (0, import_invariant4.invariant)(this._peers.has(peerId), void 0, {
1465
+ F: __dxlog_file4,
1466
+ L: 302,
1467
+ S: this,
1468
+ A: [
1469
+ "this._peers.has(peerId)",
1470
+ ""
1471
+ ]
1472
+ });
1207
1473
  await this._peers.get(peerId).destroy();
1208
1474
  this._peers.delete(peerId);
1209
1475
  }
@@ -1224,7 +1490,7 @@ var Swarm = class {
1224
1490
  } catch (err) {
1225
1491
  (0, import_log4.log)("initiation error", err, {
1226
1492
  F: __dxlog_file4,
1227
- L: 327,
1493
+ L: 328,
1228
1494
  S: this,
1229
1495
  C: (f, a) => f(...a)
1230
1496
  });
@@ -1252,7 +1518,7 @@ var Swarm = class {
1252
1518
  remoteId
1253
1519
  }, {
1254
1520
  F: __dxlog_file4,
1255
- L: 354,
1521
+ L: 355,
1256
1522
  S: this,
1257
1523
  C: (f, a) => f(...a)
1258
1524
  });
@@ -1269,7 +1535,7 @@ var Swarm = class {
1269
1535
  remoteId
1270
1536
  }, {
1271
1537
  F: __dxlog_file4,
1272
- L: 368,
1538
+ L: 369,
1273
1539
  S: this,
1274
1540
  C: (f, a) => f(...a)
1275
1541
  });
@@ -1279,7 +1545,7 @@ var Swarm = class {
1279
1545
  remoteId
1280
1546
  }, {
1281
1547
  F: __dxlog_file4,
1282
- L: 371,
1548
+ L: 372,
1283
1549
  S: this,
1284
1550
  C: (f, a) => f(...a)
1285
1551
  });
@@ -1307,9 +1573,6 @@ _ts_decorate3([
1307
1573
  _ts_decorate3([
1308
1574
  import_async3.synchronized
1309
1575
  ], Swarm.prototype, "onOffer", null);
1310
- _ts_decorate3([
1311
- import_async3.synchronized
1312
- ], Swarm.prototype, "onSignal", null);
1313
1576
  _ts_decorate3([
1314
1577
  import_async3.synchronized
1315
1578
  ], Swarm.prototype, "goOffline", null);
@@ -1341,8 +1604,7 @@ var SwarmMapper = class {
1341
1604
  }));
1342
1605
  }));
1343
1606
  this._subscriptions.add(_swarm.disconnected.on((peerId) => {
1344
- var _a2;
1345
- (_a2 = this._connectionSubscriptions.get(peerId)) == null ? void 0 : _a2();
1607
+ this._connectionSubscriptions.get(peerId)?.();
1346
1608
  this._connectionSubscriptions.delete(peerId);
1347
1609
  this._update();
1348
1610
  }));
@@ -1388,6 +1650,88 @@ var SwarmMapper = class {
1388
1650
  }
1389
1651
  };
1390
1652
 
1653
+ // packages/core/mesh/network-manager/src/swarm/connection-limiter.ts
1654
+ var import_async5 = require("@dxos/async");
1655
+ var import_context5 = require("@dxos/context");
1656
+ var import_errors3 = require("@dxos/errors");
1657
+ var import_invariant5 = require("@dxos/invariant");
1658
+ var import_keys6 = require("@dxos/keys");
1659
+ var import_log6 = require("@dxos/log");
1660
+ var import_util4 = require("@dxos/util");
1661
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
1662
+ var MAX_CONCURRENT_INITIATING_CONNECTIONS = 3;
1663
+ var ConnectionLimiter = class {
1664
+ constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
1665
+ this._ctx = new import_context5.Context();
1666
+ /**
1667
+ * Queue of promises to resolve when initiating connections amount is below the limit.
1668
+ */
1669
+ this._waitingPromises = new import_util4.ComplexMap(import_keys6.PublicKey.hash);
1670
+ this.resolveWaitingPromises = new import_async5.DeferredTask(this._ctx, async () => {
1671
+ Array.from(this._waitingPromises.values()).slice(0, this._maxConcurrentInitConnections).forEach(({ resolve }) => {
1672
+ resolve();
1673
+ });
1674
+ });
1675
+ this._maxConcurrentInitConnections = maxConcurrentInitConnections;
1676
+ }
1677
+ /**
1678
+ * @returns Promise that resolves in queue when connections amount with 'CONNECTING' state is below the limit.
1679
+ */
1680
+ async connecting(sessionId) {
1681
+ (0, import_invariant5.invariant)(!this._waitingPromises.has(sessionId), "Peer is already waiting for connection", {
1682
+ F: __dxlog_file6,
1683
+ L: 48,
1684
+ S: this,
1685
+ A: [
1686
+ "!this._waitingPromises.has(sessionId)",
1687
+ "'Peer is already waiting for connection'"
1688
+ ]
1689
+ });
1690
+ (0, import_log6.log)("waiting", {
1691
+ sessionId
1692
+ }, {
1693
+ F: __dxlog_file6,
1694
+ L: 49,
1695
+ S: this,
1696
+ C: (f, a) => f(...a)
1697
+ });
1698
+ await new Promise((resolve, reject) => {
1699
+ this._waitingPromises.set(sessionId, {
1700
+ resolve,
1701
+ reject
1702
+ });
1703
+ this.resolveWaitingPromises.schedule();
1704
+ });
1705
+ (0, import_log6.log)("allow", {
1706
+ sessionId
1707
+ }, {
1708
+ F: __dxlog_file6,
1709
+ L: 57,
1710
+ S: this,
1711
+ C: (f, a) => f(...a)
1712
+ });
1713
+ }
1714
+ /**
1715
+ * Rejects promise returned by `connecting` method.
1716
+ */
1717
+ doneConnecting(sessionId) {
1718
+ (0, import_log6.log)("done", {
1719
+ sessionId
1720
+ }, {
1721
+ F: __dxlog_file6,
1722
+ L: 64,
1723
+ S: this,
1724
+ C: (f, a) => f(...a)
1725
+ });
1726
+ if (!this._waitingPromises.has(sessionId)) {
1727
+ return;
1728
+ }
1729
+ this._waitingPromises.get(sessionId).reject(new import_errors3.CancelledError());
1730
+ this._waitingPromises.delete(sessionId);
1731
+ this.resolveWaitingPromises.schedule();
1732
+ }
1733
+ };
1734
+
1391
1735
  // packages/core/mesh/network-manager/src/connection-log.ts
1392
1736
  var EventType;
1393
1737
  (function(EventType2) {
@@ -1402,28 +1746,26 @@ var ConnectionLog = class {
1402
1746
  /**
1403
1747
  * SwarmId => info
1404
1748
  */
1405
- this._swarms = new import_util4.ComplexMap(import_keys6.PublicKey.hash);
1406
- this.update = new import_async5.Event();
1749
+ this._swarms = new import_util5.ComplexMap(import_keys7.PublicKey.hash);
1750
+ this.update = new import_async6.Event();
1407
1751
  }
1408
1752
  getSwarmInfo(swarmId) {
1409
- var _a2;
1410
- return (_a2 = this._swarms.get(swarmId)) != null ? _a2 : (0, import_debug3.raise)(new Error(`Swarm not found: ${swarmId}`));
1753
+ return this._swarms.get(swarmId) ?? (0, import_debug3.raise)(new Error(`Swarm not found: ${swarmId}`));
1411
1754
  }
1412
1755
  get swarms() {
1413
1756
  return Array.from(this._swarms.values());
1414
1757
  }
1415
1758
  joinedSwarm(swarm) {
1416
1759
  const info = {
1417
- id: import_keys6.PublicKey.from(swarm._instanceId),
1760
+ id: import_keys7.PublicKey.from(swarm._instanceId),
1418
1761
  topic: swarm.topic,
1419
1762
  isActive: true,
1420
1763
  label: swarm.label,
1421
1764
  connections: []
1422
1765
  };
1423
- this._swarms.set(import_keys6.PublicKey.from(swarm._instanceId), info);
1766
+ this._swarms.set(import_keys7.PublicKey.from(swarm._instanceId), info);
1424
1767
  this.update.emit();
1425
1768
  swarm.connectionAdded.on((connection) => {
1426
- var _a2, _b;
1427
1769
  const connectionInfo = {
1428
1770
  state: ConnectionState.INITIAL,
1429
1771
  remotePeerId: connection.remoteId,
@@ -1442,34 +1784,34 @@ var ConnectionLog = class {
1442
1784
  });
1443
1785
  this.update.emit();
1444
1786
  });
1445
- (_b = (_a2 = connection.protocol) == null ? void 0 : _a2.stats) == null ? void 0 : _b.on((stats) => {
1787
+ connection.protocol?.stats?.on((stats) => {
1446
1788
  connectionInfo.streams = stats.channels;
1447
1789
  this.update.emit();
1448
1790
  });
1449
1791
  });
1450
1792
  }
1451
1793
  leftSwarm(swarm) {
1452
- this.getSwarmInfo(import_keys6.PublicKey.from(swarm._instanceId)).isActive = false;
1794
+ this.getSwarmInfo(import_keys7.PublicKey.from(swarm._instanceId)).isActive = false;
1453
1795
  this.update.emit();
1454
1796
  }
1455
1797
  };
1456
1798
 
1457
1799
  // packages/core/mesh/network-manager/src/network-manager.ts
1458
- var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts";
1800
+ var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts";
1459
1801
  var NetworkManager = class {
1460
1802
  constructor({ transportFactory, signalManager, log: log1 }) {
1461
- this._swarms = new import_util5.ComplexMap(import_keys7.PublicKey.hash);
1462
- this._mappers = new import_util5.ComplexMap(import_keys7.PublicKey.hash);
1803
+ /**
1804
+ * @internal
1805
+ */
1806
+ this._swarms = new import_util6.ComplexMap(import_keys8.PublicKey.hash);
1807
+ this._mappers = new import_util6.ComplexMap(import_keys8.PublicKey.hash);
1463
1808
  this._connectionState = import_services.ConnectionState.ONLINE;
1464
- this.connectionStateChanged = new import_async6.Event();
1465
- this.topicsUpdated = new import_async6.Event();
1466
- this._instanceId = import_keys7.PublicKey.random().toHex();
1809
+ this.connectionStateChanged = new import_async7.Event();
1810
+ this.topicsUpdated = new import_async7.Event();
1811
+ this._instanceId = import_keys8.PublicKey.random().toHex();
1467
1812
  this._transportFactory = transportFactory;
1468
1813
  this._signalManager = signalManager;
1469
- this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => {
1470
- var _a2;
1471
- return (_a2 = this._swarms.get(topic)) == null ? void 0 : _a2.onSwarmEvent(event);
1472
- });
1814
+ this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => this._swarms.get(topic)?.onSwarmEvent(event));
1473
1815
  this._messenger = new import_messaging.Messenger({
1474
1816
  signalManager: this._signalManager
1475
1817
  });
@@ -1477,6 +1819,7 @@ var NetworkManager = class {
1477
1819
  join: (opts) => this._signalManager.join(opts),
1478
1820
  leave: (opts) => this._signalManager.leave(opts)
1479
1821
  };
1822
+ this._connectionLimiter = new ConnectionLimiter();
1480
1823
  if (log1) {
1481
1824
  this._connectionLog = new ConnectionLog();
1482
1825
  }
@@ -1499,21 +1842,21 @@ var NetworkManager = class {
1499
1842
  return this._swarms.get(topic);
1500
1843
  }
1501
1844
  async open() {
1502
- import_log6.log.trace("dxos.mesh.network-manager.open", import_protocols4.trace.begin({
1845
+ import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols4.trace.begin({
1503
1846
  id: this._instanceId
1504
1847
  }), {
1505
- F: __dxlog_file6,
1506
- L: 127,
1848
+ F: __dxlog_file7,
1849
+ L: 130,
1507
1850
  S: this,
1508
1851
  C: (f, a) => f(...a)
1509
1852
  });
1510
1853
  await this._messenger.open();
1511
1854
  await this._signalManager.open();
1512
- import_log6.log.trace("dxos.mesh.network-manager.open", import_protocols4.trace.end({
1855
+ import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols4.trace.end({
1513
1856
  id: this._instanceId
1514
1857
  }), {
1515
- F: __dxlog_file6,
1516
- L: 130,
1858
+ F: __dxlog_file7,
1859
+ L: 133,
1517
1860
  S: this,
1518
1861
  C: (f, a) => f(...a)
1519
1862
  });
@@ -1521,9 +1864,9 @@ var NetworkManager = class {
1521
1864
  async close() {
1522
1865
  for (const topic of this._swarms.keys()) {
1523
1866
  await this.leaveSwarm(topic).catch((err) => {
1524
- (0, import_log6.log)(err, void 0, {
1525
- F: __dxlog_file6,
1526
- L: 136,
1867
+ (0, import_log7.log)(err, void 0, {
1868
+ F: __dxlog_file7,
1869
+ L: 139,
1527
1870
  S: this,
1528
1871
  C: (f, a) => f(...a)
1529
1872
  });
@@ -1536,31 +1879,62 @@ var NetworkManager = class {
1536
1879
  * Join the swarm.
1537
1880
  */
1538
1881
  async joinSwarm({ topic, peerId, topology, protocolProvider: protocol, label }) {
1539
- var _a2;
1540
- (0, import_tiny_invariant5.default)(import_keys7.PublicKey.isPublicKey(topic));
1541
- (0, import_tiny_invariant5.default)(import_keys7.PublicKey.isPublicKey(peerId));
1542
- (0, import_tiny_invariant5.default)(topology);
1543
- (0, import_tiny_invariant5.default)(typeof protocol === "function");
1882
+ (0, import_invariant6.invariant)(import_keys8.PublicKey.isPublicKey(topic), void 0, {
1883
+ F: __dxlog_file7,
1884
+ L: 157,
1885
+ S: this,
1886
+ A: [
1887
+ "PublicKey.isPublicKey(topic)",
1888
+ ""
1889
+ ]
1890
+ });
1891
+ (0, import_invariant6.invariant)(import_keys8.PublicKey.isPublicKey(peerId), void 0, {
1892
+ F: __dxlog_file7,
1893
+ L: 158,
1894
+ S: this,
1895
+ A: [
1896
+ "PublicKey.isPublicKey(peerId)",
1897
+ ""
1898
+ ]
1899
+ });
1900
+ (0, import_invariant6.invariant)(topology, void 0, {
1901
+ F: __dxlog_file7,
1902
+ L: 159,
1903
+ S: this,
1904
+ A: [
1905
+ "topology",
1906
+ ""
1907
+ ]
1908
+ });
1909
+ (0, import_invariant6.invariant)(typeof protocol === "function", void 0, {
1910
+ F: __dxlog_file7,
1911
+ L: 160,
1912
+ S: this,
1913
+ A: [
1914
+ "typeof protocol === 'function'",
1915
+ ""
1916
+ ]
1917
+ });
1544
1918
  if (this._swarms.has(topic)) {
1545
- throw new Error(`Already connected to swarm: ${import_keys7.PublicKey.from(topic)}`);
1919
+ throw new Error(`Already connected to swarm: ${import_keys8.PublicKey.from(topic)}`);
1546
1920
  }
1547
- (0, import_log6.log)("joining", {
1548
- topic: import_keys7.PublicKey.from(topic),
1921
+ (0, import_log7.log)("joining", {
1922
+ topic: import_keys8.PublicKey.from(topic),
1549
1923
  peerId,
1550
1924
  topology: topology.toString()
1551
1925
  }, {
1552
- F: __dxlog_file6,
1553
- L: 162,
1926
+ F: __dxlog_file7,
1927
+ L: 165,
1554
1928
  S: this,
1555
1929
  C: (f, a) => f(...a)
1556
1930
  });
1557
- const swarm = new Swarm(topic, peerId, topology, protocol, this._messenger, this._transportFactory, label);
1931
+ const swarm = new Swarm(topic, peerId, topology, protocol, this._messenger, this._transportFactory, label, this._connectionLimiter);
1558
1932
  swarm.errors.handle((error) => {
1559
- (0, import_log6.log)("swarm error", {
1933
+ (0, import_log7.log)("swarm error", {
1560
1934
  error
1561
1935
  }, {
1562
- F: __dxlog_file6,
1563
- L: 165,
1936
+ F: __dxlog_file7,
1937
+ L: 178,
1564
1938
  S: this,
1565
1939
  C: (f, a) => f(...a)
1566
1940
  });
@@ -1571,20 +1945,20 @@ var NetworkManager = class {
1571
1945
  this._signalConnection.join({
1572
1946
  topic,
1573
1947
  peerId
1574
- }).catch((error) => import_log6.log.catch(error, void 0, {
1575
- F: __dxlog_file6,
1576
- L: 174,
1948
+ }).catch((error) => import_log7.log.catch(error, void 0, {
1949
+ F: __dxlog_file7,
1950
+ L: 187,
1577
1951
  S: this,
1578
1952
  C: (f, a) => f(...a)
1579
1953
  }));
1580
1954
  this.topicsUpdated.emit();
1581
- (_a2 = this._connectionLog) == null ? void 0 : _a2.joinedSwarm(swarm);
1582
- (0, import_log6.log)("joined", {
1583
- topic: import_keys7.PublicKey.from(topic),
1955
+ this._connectionLog?.joinedSwarm(swarm);
1956
+ (0, import_log7.log)("joined", {
1957
+ topic: import_keys8.PublicKey.from(topic),
1584
1958
  count: this._swarms.size
1585
1959
  }, {
1586
- F: __dxlog_file6,
1587
- L: 178,
1960
+ F: __dxlog_file7,
1961
+ L: 191,
1588
1962
  S: this,
1589
1963
  C: (f, a) => f(...a)
1590
1964
  });
@@ -1596,15 +1970,14 @@ var NetworkManager = class {
1596
1970
  * Close the connection.
1597
1971
  */
1598
1972
  async leaveSwarm(topic) {
1599
- var _a2;
1600
1973
  if (!this._swarms.has(topic)) {
1601
1974
  return;
1602
1975
  }
1603
- (0, import_log6.log)("leaving", {
1604
- topic: import_keys7.PublicKey.from(topic)
1976
+ (0, import_log7.log)("leaving", {
1977
+ topic: import_keys8.PublicKey.from(topic)
1605
1978
  }, {
1606
- F: __dxlog_file6,
1607
- L: 194,
1979
+ F: __dxlog_file7,
1980
+ L: 207,
1608
1981
  S: this,
1609
1982
  C: (f, a) => f(...a)
1610
1983
  });
@@ -1616,16 +1989,16 @@ var NetworkManager = class {
1616
1989
  const map = this._mappers.get(topic);
1617
1990
  map.destroy();
1618
1991
  this._mappers.delete(topic);
1619
- (_a2 = this._connectionLog) == null ? void 0 : _a2.leftSwarm(swarm);
1992
+ this._connectionLog?.leftSwarm(swarm);
1620
1993
  await swarm.destroy();
1621
1994
  this._swarms.delete(topic);
1622
1995
  await this.topicsUpdated.emit();
1623
- (0, import_log6.log)("left", {
1624
- topic: import_keys7.PublicKey.from(topic),
1996
+ (0, import_log7.log)("left", {
1997
+ topic: import_keys8.PublicKey.from(topic),
1625
1998
  count: this._swarms.size
1626
1999
  }, {
1627
- F: __dxlog_file6,
1628
- L: 208,
2000
+ F: __dxlog_file7,
2001
+ L: 221,
1629
2002
  S: this,
1630
2003
  C: (f, a) => f(...a)
1631
2004
  });
@@ -1659,17 +2032,34 @@ var NetworkManager = class {
1659
2032
  };
1660
2033
 
1661
2034
  // packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts
1662
- var import_tiny_invariant6 = __toESM(require("tiny-invariant"));
2035
+ var import_invariant7 = require("@dxos/invariant");
2036
+ var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
1663
2037
  var FullyConnectedTopology = class {
1664
2038
  toString() {
1665
2039
  return "FullyConnectedTopology";
1666
2040
  }
1667
2041
  init(controller) {
1668
- (0, import_tiny_invariant6.default)(!this._controller, "Already initialized");
2042
+ (0, import_invariant7.invariant)(!this._controller, "Already initialized", {
2043
+ F: __dxlog_file8,
2044
+ L: 18,
2045
+ S: this,
2046
+ A: [
2047
+ "!this._controller",
2048
+ "'Already initialized'"
2049
+ ]
2050
+ });
1669
2051
  this._controller = controller;
1670
2052
  }
1671
2053
  update() {
1672
- (0, import_tiny_invariant6.default)(this._controller, "Not initialized");
2054
+ (0, import_invariant7.invariant)(this._controller, "Not initialized", {
2055
+ F: __dxlog_file8,
2056
+ L: 23,
2057
+ S: this,
2058
+ A: [
2059
+ "this._controller",
2060
+ "'Not initialized'"
2061
+ ]
2062
+ });
1673
2063
  const { candidates: discovered } = this._controller.getState();
1674
2064
  for (const peer of discovered) {
1675
2065
  this._controller.connect(peer);
@@ -1683,22 +2073,22 @@ var FullyConnectedTopology = class {
1683
2073
  };
1684
2074
 
1685
2075
  // packages/core/mesh/network-manager/src/topology/mmst-topology.ts
1686
- var import_tiny_invariant7 = __toESM(require("tiny-invariant"));
1687
2076
  var import_xor_distance = __toESM(require("xor-distance"));
1688
- var import_log7 = require("@dxos/log");
2077
+ var import_invariant8 = require("@dxos/invariant");
2078
+ var import_log8 = require("@dxos/log");
1689
2079
 
1690
2080
  // packages/core/mesh/network-manager/src/topology/star-topology.ts
1691
- var import_tiny_invariant8 = __toESM(require("tiny-invariant"));
1692
- var import_log8 = require("@dxos/log");
2081
+ var import_invariant9 = require("@dxos/invariant");
2082
+ var import_log9 = require("@dxos/log");
1693
2083
 
1694
2084
  // packages/core/mesh/network-manager/src/transport/memory-transport.ts
1695
2085
  var import_node_stream = require("node:stream");
1696
- var import_tiny_invariant9 = __toESM(require("tiny-invariant"));
1697
- var import_async7 = require("@dxos/async");
2086
+ var import_async8 = require("@dxos/async");
1698
2087
  var import_debug4 = require("@dxos/debug");
1699
- var import_keys8 = require("@dxos/keys");
1700
- var import_log9 = require("@dxos/log");
1701
- var import_util6 = require("@dxos/util");
2088
+ var import_invariant10 = require("@dxos/invariant");
2089
+ var import_keys9 = require("@dxos/keys");
2090
+ var import_log10 = require("@dxos/log");
2091
+ var import_util7 = require("@dxos/util");
1702
2092
  function _ts_decorate4(decorators, target, key, desc) {
1703
2093
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1704
2094
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -1709,7 +2099,7 @@ function _ts_decorate4(decorators, target, key, desc) {
1709
2099
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1710
2100
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1711
2101
  }
1712
- var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
2102
+ var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
1713
2103
  var MEMORY_TRANSPORT_DELAY = 1;
1714
2104
  var createStreamDelay = (delay) => {
1715
2105
  return new import_node_stream.Transform({
@@ -1722,30 +2112,41 @@ var createStreamDelay = (delay) => {
1722
2112
  var MemoryTransportFactory = {
1723
2113
  createTransport: (params) => new MemoryTransport(params)
1724
2114
  };
1725
- var _MemoryTransport = class {
2115
+ var MemoryTransport = class _MemoryTransport {
2116
+ static {
2117
+ // TODO(burdon): Remove static properties (inject context into constructor).
2118
+ this._connections = new import_util7.ComplexMap(import_keys9.PublicKey.hash);
2119
+ }
1726
2120
  constructor(options) {
1727
- var _a2;
1728
2121
  this.options = options;
1729
- this.closed = new import_async7.Event();
1730
- this.connected = new import_async7.Event();
2122
+ this.closed = new import_async8.Event();
2123
+ this.connected = new import_async8.Event();
1731
2124
  this.errors = new import_debug4.ErrorStream();
1732
- this._instanceId = import_keys8.PublicKey.random();
1733
- this._remote = new import_async7.Trigger();
2125
+ this._instanceId = import_keys9.PublicKey.random();
2126
+ this._remote = new import_async8.Trigger();
1734
2127
  this._outgoingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
1735
2128
  this._incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
1736
2129
  this._destroyed = false;
1737
- (0, import_log9.log)("creating", void 0, {
1738
- F: __dxlog_file7,
2130
+ (0, import_log10.log)("creating", void 0, {
2131
+ F: __dxlog_file9,
1739
2132
  L: 64,
1740
2133
  S: this,
1741
2134
  C: (f, a) => f(...a)
1742
2135
  });
1743
- (0, import_tiny_invariant9.default)(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection");
2136
+ (0, import_invariant10.invariant)(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection", {
2137
+ F: __dxlog_file9,
2138
+ L: 66,
2139
+ S: this,
2140
+ A: [
2141
+ "!MemoryTransport._connections.has(this._instanceId)",
2142
+ "'Duplicate memory connection'"
2143
+ ]
2144
+ });
1744
2145
  _MemoryTransport._connections.set(this._instanceId, this);
1745
2146
  if (this.options.initiator) {
1746
2147
  setTimeout(async () => {
1747
- (0, import_log9.log)("sending signal", void 0, {
1748
- F: __dxlog_file7,
2148
+ (0, import_log10.log)("sending signal", void 0, {
2149
+ F: __dxlog_file9,
1749
2150
  L: 73,
1750
2151
  S: this,
1751
2152
  C: (f, a) => f(...a)
@@ -1762,7 +2163,7 @@ var _MemoryTransport = class {
1762
2163
  });
1763
2164
  } else {
1764
2165
  this._remote.wait({
1765
- timeout: (_a2 = this.options.timeout) != null ? _a2 : 1e3
2166
+ timeout: this.options.timeout ?? 1e3
1766
2167
  }).then((remoteId) => {
1767
2168
  if (this._destroyed) {
1768
2169
  return;
@@ -1774,11 +2175,19 @@ var _MemoryTransport = class {
1774
2175
  this.closed.emit();
1775
2176
  return;
1776
2177
  }
1777
- (0, import_tiny_invariant9.default)(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`);
2178
+ (0, import_invariant10.invariant)(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`, {
2179
+ F: __dxlog_file9,
2180
+ L: 99,
2181
+ S: this,
2182
+ A: [
2183
+ "!this._remoteConnection._remoteConnection",
2184
+ "`Remote already connected: ${this._remoteInstanceId}`"
2185
+ ]
2186
+ });
1778
2187
  this._remoteConnection._remoteConnection = this;
1779
2188
  this._remoteConnection._remoteInstanceId = this._instanceId;
1780
- (0, import_log9.log)("connected", void 0, {
1781
- F: __dxlog_file7,
2189
+ (0, import_log10.log)("connected", void 0, {
2190
+ F: __dxlog_file9,
1782
2191
  L: 103,
1783
2192
  S: this,
1784
2193
  C: (f, a) => f(...a)
@@ -1795,8 +2204,8 @@ var _MemoryTransport = class {
1795
2204
  }
1796
2205
  }
1797
2206
  async destroy() {
1798
- (0, import_log9.log)("closing", void 0, {
1799
- F: __dxlog_file7,
2207
+ (0, import_log10.log)("closing", void 0, {
2208
+ F: __dxlog_file9,
1800
2209
  L: 124,
1801
2210
  S: this,
1802
2211
  C: (f, a) => f(...a)
@@ -1804,8 +2213,8 @@ var _MemoryTransport = class {
1804
2213
  this._destroyed = true;
1805
2214
  _MemoryTransport._connections.delete(this._instanceId);
1806
2215
  if (this._remoteConnection) {
1807
- (0, import_log9.log)("closing", void 0, {
1808
- F: __dxlog_file7,
2216
+ (0, import_log10.log)("closing", void 0, {
2217
+ F: __dxlog_file9,
1809
2218
  L: 129,
1810
2219
  S: this,
1811
2220
  C: (f, a) => f(...a)
@@ -1817,99 +2226,95 @@ var _MemoryTransport = class {
1817
2226
  this._remoteConnection.closed.emit();
1818
2227
  this._remoteConnection._remoteConnection = void 0;
1819
2228
  this._remoteConnection = void 0;
1820
- (0, import_log9.log)("closed", void 0, {
1821
- F: __dxlog_file7,
2229
+ (0, import_log10.log)("closed", void 0, {
2230
+ F: __dxlog_file9,
1822
2231
  L: 147,
1823
2232
  S: this,
1824
2233
  C: (f, a) => f(...a)
1825
2234
  });
1826
2235
  }
1827
2236
  this.closed.emit();
1828
- (0, import_log9.log)("closed", void 0, {
1829
- F: __dxlog_file7,
2237
+ (0, import_log10.log)("closed", void 0, {
2238
+ F: __dxlog_file9,
1830
2239
  L: 151,
1831
2240
  S: this,
1832
2241
  C: (f, a) => f(...a)
1833
2242
  });
1834
2243
  }
1835
2244
  signal({ payload }) {
1836
- (0, import_log9.log)("received signal", {
2245
+ (0, import_log10.log)("received signal", {
1837
2246
  payload
1838
2247
  }, {
1839
- F: __dxlog_file7,
2248
+ F: __dxlog_file9,
1840
2249
  L: 155,
1841
2250
  S: this,
1842
2251
  C: (f, a) => f(...a)
1843
2252
  });
1844
- if (!(payload == null ? void 0 : payload.transportId)) {
2253
+ if (!payload?.transportId) {
1845
2254
  return;
1846
2255
  }
1847
2256
  const transportId = payload.transportId;
1848
2257
  if (transportId) {
1849
- const remoteId = import_keys8.PublicKey.fromHex(transportId);
2258
+ const remoteId = import_keys9.PublicKey.fromHex(transportId);
1850
2259
  this._remote.wake(remoteId);
1851
2260
  }
1852
2261
  }
1853
2262
  };
1854
- var MemoryTransport = _MemoryTransport;
1855
- // TODO(burdon): Remove static properties (inject context into constructor).
1856
- MemoryTransport._connections = new import_util6.ComplexMap(import_keys8.PublicKey.hash);
1857
2263
  _ts_decorate4([
1858
- import_log9.logInfo
2264
+ import_log10.logInfo
1859
2265
  ], MemoryTransport.prototype, "_instanceId", void 0);
1860
2266
  _ts_decorate4([
1861
- import_log9.logInfo
2267
+ import_log10.logInfo
1862
2268
  ], MemoryTransport.prototype, "_remoteInstanceId", void 0);
1863
2269
 
1864
2270
  // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
1865
2271
  var import_simple_peer = __toESM(require("simple-peer"));
1866
- var import_tiny_invariant10 = __toESM(require("tiny-invariant"));
1867
- var import_async8 = require("@dxos/async");
2272
+ var import_async9 = require("@dxos/async");
1868
2273
  var import_debug5 = require("@dxos/debug");
1869
- var import_keys9 = require("@dxos/keys");
1870
- var import_log10 = require("@dxos/log");
2274
+ var import_invariant11 = require("@dxos/invariant");
2275
+ var import_keys10 = require("@dxos/keys");
2276
+ var import_log11 = require("@dxos/log");
1871
2277
  var import_protocols5 = require("@dxos/protocols");
1872
2278
 
1873
2279
  // packages/core/mesh/network-manager/src/transport/webrtc.ts
1874
2280
  var wrtc = null;
1875
2281
  try {
1876
2282
  wrtc = require("@koush/wrtc");
1877
- } catch (e) {
2283
+ } catch {
1878
2284
  }
1879
2285
 
1880
2286
  // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
1881
- var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
2287
+ var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
1882
2288
  var WebRTCTransport = class {
1883
2289
  constructor(params) {
1884
- var _a2;
1885
2290
  this.params = params;
1886
2291
  this._closed = false;
1887
- this.closed = new import_async8.Event();
1888
- this.connected = new import_async8.Event();
2292
+ this.closed = new import_async9.Event();
2293
+ this.connected = new import_async9.Event();
1889
2294
  this.errors = new import_debug5.ErrorStream();
1890
- this._instanceId = import_keys9.PublicKey.random().toHex();
1891
- import_log10.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.begin({
2295
+ this._instanceId = import_keys10.PublicKey.random().toHex();
2296
+ import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.begin({
1892
2297
  id: this._instanceId
1893
2298
  }), {
1894
- F: __dxlog_file8,
2299
+ F: __dxlog_file10,
1895
2300
  L: 39,
1896
2301
  S: this,
1897
2302
  C: (f, a) => f(...a)
1898
2303
  });
1899
- (0, import_log10.log)("created connection", params, {
1900
- F: __dxlog_file8,
2304
+ (0, import_log11.log)("created connection", params, {
2305
+ F: __dxlog_file10,
1901
2306
  L: 40,
1902
2307
  S: this,
1903
2308
  C: (f, a) => f(...a)
1904
2309
  });
1905
2310
  this._peer = new import_simple_peer.default({
1906
2311
  initiator: this.params.initiator,
1907
- wrtc: import_simple_peer.default.WEBRTC_SUPPORT ? void 0 : (_a2 = wrtc) != null ? _a2 : (0, import_debug5.raise)(new Error("wrtc not available")),
2312
+ wrtc: import_simple_peer.default.WEBRTC_SUPPORT ? void 0 : wrtc ?? (0, import_debug5.raise)(new Error("wrtc not available")),
1908
2313
  config: this.params.webrtcConfig
1909
2314
  });
1910
2315
  this._peer.on("signal", async (data) => {
1911
- (0, import_log10.log)("signal", data, {
1912
- F: __dxlog_file8,
2316
+ (0, import_log11.log)("signal", data, {
2317
+ F: __dxlog_file10,
1913
2318
  L: 48,
1914
2319
  S: this,
1915
2320
  C: (f, a) => f(...a)
@@ -1921,8 +2326,8 @@ var WebRTCTransport = class {
1921
2326
  });
1922
2327
  });
1923
2328
  this._peer.on("connect", () => {
1924
- (0, import_log10.log)("connected", void 0, {
1925
- F: __dxlog_file8,
2329
+ (0, import_log11.log)("connected", void 0, {
2330
+ F: __dxlog_file10,
1926
2331
  L: 53,
1927
2332
  S: this,
1928
2333
  C: (f, a) => f(...a)
@@ -1931,8 +2336,8 @@ var WebRTCTransport = class {
1931
2336
  this.connected.emit();
1932
2337
  });
1933
2338
  this._peer.on("close", async () => {
1934
- (0, import_log10.log)("closed", void 0, {
1935
- F: __dxlog_file8,
2339
+ (0, import_log11.log)("closed", void 0, {
2340
+ F: __dxlog_file10,
1936
2341
  L: 59,
1937
2342
  S: this,
1938
2343
  C: (f, a) => f(...a)
@@ -1941,38 +2346,37 @@ var WebRTCTransport = class {
1941
2346
  this.closed.emit();
1942
2347
  });
1943
2348
  this._peer.on("error", async (err) => {
1944
- var _a3;
1945
2349
  this.errors.raise(err);
1946
2350
  try {
1947
- if (typeof ((_a3 = this._peer) == null ? void 0 : _a3._pc.getStats) === "function") {
2351
+ if (typeof this._peer?._pc.getStats === "function") {
1948
2352
  this._peer._pc.getStats().then((stats) => {
1949
- import_log10.log.warn("report after webrtc error", {
2353
+ import_log11.log.warn("report after webrtc error", {
1950
2354
  config: this.params.webrtcConfig,
1951
2355
  stats
1952
2356
  }, {
1953
- F: __dxlog_file8,
2357
+ F: __dxlog_file10,
1954
2358
  L: 71,
1955
2359
  S: this,
1956
2360
  C: (f, a) => f(...a)
1957
2361
  });
1958
2362
  });
1959
2363
  }
1960
- } catch (e) {
2364
+ } catch {
1961
2365
  }
1962
2366
  await this.destroy();
1963
2367
  });
1964
- import_log10.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.end({
2368
+ import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.end({
1965
2369
  id: this._instanceId
1966
2370
  }), {
1967
- F: __dxlog_file8,
2371
+ F: __dxlog_file10,
1968
2372
  L: 81,
1969
2373
  S: this,
1970
2374
  C: (f, a) => f(...a)
1971
2375
  });
1972
2376
  }
1973
2377
  async destroy() {
1974
- (0, import_log10.log)("closing...", void 0, {
1975
- F: __dxlog_file8,
2378
+ (0, import_log11.log)("closing...", void 0, {
2379
+ F: __dxlog_file10,
1976
2380
  L: 85,
1977
2381
  S: this,
1978
2382
  C: (f, a) => f(...a)
@@ -1981,8 +2385,8 @@ var WebRTCTransport = class {
1981
2385
  await this._disconnectStreams();
1982
2386
  this._peer.destroy();
1983
2387
  this.closed.emit();
1984
- (0, import_log10.log)("closed", void 0, {
1985
- F: __dxlog_file8,
2388
+ (0, import_log11.log)("closed", void 0, {
2389
+ F: __dxlog_file10,
1986
2390
  L: 90,
1987
2391
  S: this,
1988
2392
  C: (f, a) => f(...a)
@@ -1992,34 +2396,48 @@ var WebRTCTransport = class {
1992
2396
  if (this._closed) {
1993
2397
  return;
1994
2398
  }
1995
- (0, import_tiny_invariant10.default)(signal.payload.data, "Signal message must contain signal data.");
2399
+ (0, import_invariant11.invariant)(signal.payload.data, "Signal message must contain signal data.", {
2400
+ F: __dxlog_file10,
2401
+ L: 98,
2402
+ S: this,
2403
+ A: [
2404
+ "signal.payload.data",
2405
+ "'Signal message must contain signal data.'"
2406
+ ]
2407
+ });
1996
2408
  this._peer.signal(signal.payload.data);
1997
2409
  }
1998
2410
  async _disconnectStreams() {
1999
- var _a2, _b, _c, _d;
2000
- (_d = (_c = (_b = (_a2 = this.params.stream).unpipe) == null ? void 0 : _b.call(_a2, this._peer)) == null ? void 0 : _c.unpipe) == null ? void 0 : _d.call(_c, this.params.stream);
2411
+ this.params.stream.unpipe?.(this._peer)?.unpipe?.(this.params.stream);
2001
2412
  }
2002
2413
  };
2003
2414
 
2004
2415
  // packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts
2005
2416
  var import_node_stream2 = require("node:stream");
2006
- var import_tiny_invariant11 = __toESM(require("tiny-invariant"));
2007
2417
  var import_codec_protobuf = require("@dxos/codec-protobuf");
2008
- var import_keys10 = require("@dxos/keys");
2009
- var import_log11 = require("@dxos/log");
2418
+ var import_invariant12 = require("@dxos/invariant");
2419
+ var import_keys11 = require("@dxos/keys");
2420
+ var import_log12 = require("@dxos/log");
2010
2421
  var import_bridge = require("@dxos/protocols/proto/dxos/mesh/bridge");
2011
- var import_util7 = require("@dxos/util");
2012
- var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts";
2422
+ var import_util8 = require("@dxos/util");
2423
+ var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts";
2013
2424
  var WebRTCTransportService = class {
2014
2425
  // prettier-ignore
2015
2426
  constructor(_webrtcConfig) {
2016
2427
  this._webrtcConfig = _webrtcConfig;
2017
- this.transports = new import_util7.ComplexMap(import_keys10.PublicKey.hash);
2428
+ this.transports = new import_util8.ComplexMap(import_keys11.PublicKey.hash);
2018
2429
  }
2019
2430
  open(request) {
2020
2431
  const rpcStream = new import_codec_protobuf.Stream(({ ready, next, close }) => {
2021
2432
  const duplex = new import_node_stream2.Duplex({
2022
2433
  read: () => {
2434
+ const callbacks = [
2435
+ ...transportState.writeCallbacks
2436
+ ];
2437
+ transportState.writeCallbacks.length = 0;
2438
+ for (const cb of callbacks) {
2439
+ cb();
2440
+ }
2023
2441
  },
2024
2442
  write: function(chunk, _, callback) {
2025
2443
  next({
@@ -2071,30 +2489,53 @@ var WebRTCTransportService = class {
2071
2489
  });
2072
2490
  close();
2073
2491
  });
2074
- ready();
2075
- this.transports.set(request.proxyId, {
2492
+ const transportState = {
2076
2493
  transport,
2077
- stream: duplex
2078
- });
2494
+ stream: duplex,
2495
+ writeCallbacks: []
2496
+ };
2497
+ ready();
2498
+ this.transports.set(request.proxyId, transportState);
2079
2499
  });
2080
2500
  return rpcStream;
2081
2501
  }
2082
2502
  async sendSignal({ proxyId, signal }) {
2083
- (0, import_tiny_invariant11.default)(this.transports.has(proxyId));
2503
+ (0, import_invariant12.invariant)(this.transports.has(proxyId), void 0, {
2504
+ F: __dxlog_file11,
2505
+ L: 113,
2506
+ S: this,
2507
+ A: [
2508
+ "this.transports.has(proxyId)",
2509
+ ""
2510
+ ]
2511
+ });
2084
2512
  await this.transports.get(proxyId).transport.signal(signal);
2085
2513
  }
2086
2514
  async sendData({ proxyId, payload }) {
2087
- (0, import_tiny_invariant11.default)(this.transports.has(proxyId));
2088
- await this.transports.get(proxyId).stream.push(payload);
2515
+ (0, import_invariant12.invariant)(this.transports.has(proxyId), void 0, {
2516
+ F: __dxlog_file11,
2517
+ L: 118,
2518
+ S: this,
2519
+ A: [
2520
+ "this.transports.has(proxyId)",
2521
+ ""
2522
+ ]
2523
+ });
2524
+ const state = this.transports.get(proxyId);
2525
+ const bufferHasSpace = state.stream.push(payload);
2526
+ if (!bufferHasSpace) {
2527
+ await new Promise((resolve) => {
2528
+ state.writeCallbacks.push(resolve);
2529
+ });
2530
+ }
2089
2531
  }
2090
2532
  async close({ proxyId }) {
2091
- var _a2, _b;
2092
- await ((_a2 = this.transports.get(proxyId)) == null ? void 0 : _a2.transport.destroy());
2093
- await ((_b = this.transports.get(proxyId)) == null ? void 0 : _b.stream.end());
2533
+ await this.transports.get(proxyId)?.transport.destroy();
2534
+ await this.transports.get(proxyId)?.stream.end();
2094
2535
  this.transports.delete(proxyId);
2095
- (0, import_log11.log)("Closed.", void 0, {
2096
- F: __dxlog_file9,
2097
- L: 109,
2536
+ (0, import_log12.log)("Closed.", void 0, {
2537
+ F: __dxlog_file11,
2538
+ L: 132,
2098
2539
  S: this,
2099
2540
  C: (f, a) => f(...a)
2100
2541
  });
@@ -2102,24 +2543,25 @@ var WebRTCTransportService = class {
2102
2543
  };
2103
2544
 
2104
2545
  // packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts
2105
- var import_tiny_invariant12 = __toESM(require("tiny-invariant"));
2106
- var import_async9 = require("@dxos/async");
2107
- var import_context5 = require("@dxos/context");
2546
+ var import_node_stream3 = require("node:stream");
2547
+ var import_async10 = require("@dxos/async");
2548
+ var import_context6 = require("@dxos/context");
2108
2549
  var import_debug6 = require("@dxos/debug");
2109
- var import_keys11 = require("@dxos/keys");
2110
- var import_log12 = require("@dxos/log");
2550
+ var import_invariant13 = require("@dxos/invariant");
2551
+ var import_keys12 = require("@dxos/keys");
2552
+ var import_log13 = require("@dxos/log");
2111
2553
  var import_bridge2 = require("@dxos/protocols/proto/dxos/mesh/bridge");
2112
- var import_util8 = require("@dxos/util");
2113
- var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts";
2554
+ var import_util9 = require("@dxos/util");
2555
+ var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts";
2114
2556
  var WebRTCTransportProxy = class {
2115
2557
  // prettier-ignore
2116
2558
  constructor(_params) {
2117
2559
  this._params = _params;
2118
- this._proxyId = import_keys11.PublicKey.random();
2119
- this._ctx = new import_context5.Context();
2120
- this.closed = new import_async9.Event();
2560
+ this._proxyId = import_keys12.PublicKey.random();
2561
+ this._ctx = new import_context6.Context();
2562
+ this.closed = new import_async10.Event();
2121
2563
  this._closed = false;
2122
- this.connected = new import_async9.Event();
2564
+ this.connected = new import_async10.Event();
2123
2565
  this.errors = new import_debug6.ErrorStream();
2124
2566
  this._serviceStream = this._params.bridgeService.open({
2125
2567
  proxyId: this._proxyId,
@@ -2127,9 +2569,9 @@ var WebRTCTransportProxy = class {
2127
2569
  });
2128
2570
  this._serviceStream.waitUntilReady().then(() => {
2129
2571
  this._serviceStream.subscribe(async (event) => {
2130
- (0, import_log12.log)("WebRTCTransportProxy: event", event, {
2131
- F: __dxlog_file10,
2132
- L: 50,
2572
+ (0, import_log13.log)("WebRTCTransportProxy: event", event, {
2573
+ F: __dxlog_file12,
2574
+ L: 51,
2133
2575
  S: this,
2134
2576
  C: (f, a) => f(...a)
2135
2577
  });
@@ -2141,28 +2583,24 @@ var WebRTCTransportProxy = class {
2141
2583
  await this._handleSignal(event.signal);
2142
2584
  }
2143
2585
  });
2144
- const dataListener = async (data) => {
2145
- try {
2146
- await this._params.bridgeService.sendData({
2586
+ this._params.stream.pipe(new import_node_stream3.Writable({
2587
+ write: (chunk, _, callback) => {
2588
+ this._params.bridgeService.sendData({
2147
2589
  proxyId: this._proxyId,
2148
- payload: data
2149
- });
2150
- } catch (err) {
2151
- import_log12.log.catch(err, void 0, {
2152
- F: __dxlog_file10,
2153
- L: 67,
2154
- S: this,
2155
- C: (f, a) => f(...a)
2590
+ payload: chunk
2591
+ }).then(() => callback(), (err) => {
2592
+ import_log13.log.catch(err, void 0, {
2593
+ F: __dxlog_file12,
2594
+ L: 69,
2595
+ S: this,
2596
+ C: (f, a) => f(...a)
2597
+ });
2156
2598
  });
2157
2599
  }
2158
- };
2159
- this._params.stream.on("data", dataListener);
2160
- this._ctx.onDispose(() => {
2161
- this._params.stream.off("data", dataListener);
2162
- });
2163
- }, (error) => import_log12.log.catch(error, void 0, {
2164
- F: __dxlog_file10,
2165
- L: 73,
2600
+ }));
2601
+ }, (error) => import_log13.log.catch(error, void 0, {
2602
+ F: __dxlog_file12,
2603
+ L: 75,
2166
2604
  S: this,
2167
2605
  C: (f, a) => f(...a)
2168
2606
  }));
@@ -2183,7 +2621,7 @@ var WebRTCTransportProxy = class {
2183
2621
  }
2184
2622
  }
2185
2623
  _handleData(dataEvent) {
2186
- this._params.stream.write((0, import_util8.arrayToBuffer)(dataEvent.payload));
2624
+ this._params.stream.write((0, import_util9.arrayToBuffer)(dataEvent.payload));
2187
2625
  }
2188
2626
  async _handleSignal(signalEvent) {
2189
2627
  await this._params.sendSignal(signalEvent.payload);
@@ -2206,9 +2644,9 @@ var WebRTCTransportProxy = class {
2206
2644
  proxyId: this._proxyId
2207
2645
  });
2208
2646
  } catch (err) {
2209
- import_log12.log.catch(err, void 0, {
2210
- F: __dxlog_file10,
2211
- L: 124,
2647
+ import_log13.log.catch(err, void 0, {
2648
+ F: __dxlog_file12,
2649
+ L: 126,
2212
2650
  S: this,
2213
2651
  C: (f, a) => f(...a)
2214
2652
  });
@@ -2242,7 +2680,15 @@ var WebRTCTransportProxyFactory = class {
2242
2680
  return this;
2243
2681
  }
2244
2682
  createTransport(options) {
2245
- (0, import_tiny_invariant12.default)(this._bridgeService, "WebRTCTransportProxyFactory is not ready to open connections");
2683
+ (0, import_invariant13.invariant)(this._bridgeService, "WebRTCTransportProxyFactory is not ready to open connections", {
2684
+ F: __dxlog_file12,
2685
+ L: 163,
2686
+ S: this,
2687
+ A: [
2688
+ "this._bridgeService",
2689
+ "'WebRTCTransportProxyFactory is not ready to open connections'"
2690
+ ]
2691
+ });
2246
2692
  const transport = new WebRTCTransportProxy({
2247
2693
  ...options,
2248
2694
  bridgeService: this._bridgeService
@@ -2254,11 +2700,11 @@ var WebRTCTransportProxyFactory = class {
2254
2700
  };
2255
2701
 
2256
2702
  // packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts
2257
- var import_async10 = require("@dxos/async");
2258
- var import_keys12 = require("@dxos/keys");
2259
- var import_log13 = require("@dxos/log");
2703
+ var import_async11 = require("@dxos/async");
2704
+ var import_keys13 = require("@dxos/keys");
2705
+ var import_log14 = require("@dxos/log");
2260
2706
  var import_teleport2 = require("@dxos/teleport");
2261
- var import_util9 = require("@dxos/util");
2707
+ var import_util10 = require("@dxos/util");
2262
2708
 
2263
2709
  // packages/core/mesh/network-manager/src/wire-protocol.ts
2264
2710
  var import_teleport = require("@dxos/teleport");
@@ -2279,19 +2725,19 @@ var createTeleportProtocolFactory = (onConnection) => {
2279
2725
  };
2280
2726
 
2281
2727
  // packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts
2282
- var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts";
2728
+ var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts";
2283
2729
  var TestWireProtocol = class {
2284
2730
  constructor(peerId) {
2285
2731
  this.peerId = peerId;
2286
- this.connections = new import_util9.ComplexMap(import_keys12.PublicKey.hash);
2287
- this.streamConnections = new import_util9.ComplexMap(import_keys12.PublicKey.hash);
2288
- this.connected = new import_async10.Event();
2289
- this.disconnected = new import_async10.Event();
2732
+ this.connections = new import_util10.ComplexMap(import_keys13.PublicKey.hash);
2733
+ this.streamConnections = new import_util10.ComplexMap(import_keys13.PublicKey.hash);
2734
+ this.connected = new import_async11.Event();
2735
+ this.disconnected = new import_async11.Event();
2290
2736
  this.factory = createTeleportProtocolFactory(async (teleport) => {
2291
- (0, import_log13.log)("create", {
2737
+ (0, import_log14.log)("create", {
2292
2738
  remotePeerId: teleport.remotePeerId
2293
2739
  }, {
2294
- F: __dxlog_file11,
2740
+ F: __dxlog_file13,
2295
2741
  L: 23,
2296
2742
  S: this,
2297
2743
  C: (f, a) => f(...a)
@@ -2318,15 +2764,15 @@ var TestWireProtocol = class {
2318
2764
  if (this.connections.has(peerId)) {
2319
2765
  return this.connections.get(peerId);
2320
2766
  }
2321
- (0, import_log13.log)("waitForConnection", {
2767
+ (0, import_log14.log)("waitForConnection", {
2322
2768
  peerId
2323
2769
  }, {
2324
- F: __dxlog_file11,
2770
+ F: __dxlog_file13,
2325
2771
  L: 47,
2326
2772
  S: this,
2327
2773
  C: (f, a) => f(...a)
2328
2774
  });
2329
- await (0, import_async10.asyncTimeout)(
2775
+ await (0, import_async11.asyncTimeout)(
2330
2776
  this.connected.waitFor((connectedId) => connectedId.equals(peerId)),
2331
2777
  // TODO(nf): Make this configurable.
2332
2778
  1e4
@@ -2354,8 +2800,7 @@ var TestWireProtocol = class {
2354
2800
  };
2355
2801
 
2356
2802
  // packages/core/mesh/network-manager/src/testing/test-builder.ts
2357
- var _a;
2358
- var port = (_a = process.env.SIGNAL_PORT) != null ? _a : 4e3;
2803
+ var port = process.env.SIGNAL_PORT ?? 4e3;
2359
2804
  var TEST_SIGNAL_HOSTS = [
2360
2805
  {
2361
2806
  server: `ws://localhost:${port}/.well-known/dx/signal`
@@ -2372,7 +2817,7 @@ var TestBuilder = class {
2372
2817
  }
2373
2818
  return new import_messaging2.MemorySignalManager(this._signalContext);
2374
2819
  }
2375
- createPeer(peerId = import_keys13.PublicKey.random()) {
2820
+ createPeer(peerId = import_keys14.PublicKey.random()) {
2376
2821
  return new TestPeer(this, peerId);
2377
2822
  }
2378
2823
  };
@@ -2380,7 +2825,7 @@ var TestPeer = class {
2380
2825
  constructor(testBuilder, peerId) {
2381
2826
  this.testBuilder = testBuilder;
2382
2827
  this.peerId = peerId;
2383
- this._swarms = new import_util10.ComplexMap(import_keys13.PublicKey.hash);
2828
+ this._swarms = new import_util11.ComplexMap(import_keys14.PublicKey.hash);
2384
2829
  this._signalManager = this.testBuilder.createSignalManager();
2385
2830
  this._networkManager = this.createNetworkManager();
2386
2831
  }
@@ -2426,17 +2871,15 @@ var TestPeer = class {
2426
2871
  });
2427
2872
  }
2428
2873
  async open() {
2429
- var _a2, _b;
2430
2874
  await this._networkManager.open();
2431
- await ((_a2 = this._proxy) == null ? void 0 : _a2.open());
2432
- await ((_b = this._service) == null ? void 0 : _b.open());
2875
+ await this._proxy?.open();
2876
+ await this._service?.open();
2433
2877
  }
2434
2878
  async close() {
2435
- var _a2, _b;
2436
2879
  await Promise.all(Array.from(this._swarms.values()).map((swarm) => swarm.leave()));
2437
2880
  this._swarms.clear();
2438
- await ((_a2 = this._proxy) == null ? void 0 : _a2.close());
2439
- await ((_b = this._service) == null ? void 0 : _b.close());
2881
+ await this._proxy?.close();
2882
+ await this._service?.close();
2440
2883
  await this._networkManager.close();
2441
2884
  }
2442
2885
  getSwarm(topic) {