@dxos/network-manager 0.1.56-main.e47dfd1 → 0.1.56-main.ed2f871

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 (44) hide show
  1. package/dist/lib/browser/{chunk-5R7TDXUU.mjs → chunk-5TRIVLI6.mjs} +657 -247
  2. package/dist/lib/browser/chunk-5TRIVLI6.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +1 -1
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/browser/testing/index.mjs +23 -16
  6. package/dist/lib/browser/testing/index.mjs.map +3 -3
  7. package/dist/lib/node/index.cjs +655 -245
  8. package/dist/lib/node/index.cjs.map +3 -3
  9. package/dist/lib/node/meta.json +1 -1
  10. package/dist/lib/node/testing/index.cjs +603 -242
  11. package/dist/lib/node/testing/index.cjs.map +3 -3
  12. package/dist/types/src/network-manager.d.ts.map +1 -1
  13. package/dist/types/src/signal/swarm-messenger.d.ts.map +1 -1
  14. package/dist/types/src/swarm/connection-limiter.d.ts.map +1 -1
  15. package/dist/types/src/swarm/connection.d.ts.map +1 -1
  16. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  17. package/dist/types/src/swarm/swarm.d.ts.map +1 -1
  18. package/dist/types/src/testing/test-builder.d.ts +4 -3
  19. package/dist/types/src/testing/test-builder.d.ts.map +1 -1
  20. package/dist/types/src/testing/test-wire-protocol.d.ts +13 -2
  21. package/dist/types/src/testing/test-wire-protocol.d.ts.map +1 -1
  22. package/dist/types/src/topology/fully-connected-topology.d.ts.map +1 -1
  23. package/dist/types/src/topology/star-topology.d.ts.map +1 -1
  24. package/dist/types/src/transport/memory-transport.d.ts.map +1 -1
  25. package/dist/types/src/transport/webrtc-transport-proxy.d.ts.map +1 -1
  26. package/dist/types/src/transport/webrtc-transport-service.d.ts.map +1 -1
  27. package/dist/types/src/transport/webrtc-transport.d.ts.map +1 -1
  28. package/package.json +18 -18
  29. package/src/network-manager.ts +1 -2
  30. package/src/signal/swarm-messenger.ts +1 -2
  31. package/src/swarm/connection-limiter.ts +2 -1
  32. package/src/swarm/connection.ts +17 -3
  33. package/src/swarm/peer.ts +24 -2
  34. package/src/swarm/swarm.ts +1 -2
  35. package/src/testing/test-builder.ts +9 -5
  36. package/src/testing/test-wire-protocol.ts +22 -2
  37. package/src/topology/fully-connected-topology.ts +1 -2
  38. package/src/topology/mmst-topology.ts +1 -1
  39. package/src/topology/star-topology.ts +1 -2
  40. package/src/transport/memory-transport.ts +1 -1
  41. package/src/transport/webrtc-transport-proxy.ts +14 -12
  42. package/src/transport/webrtc-transport-service.ts +31 -8
  43. package/src/transport/webrtc-transport.ts +1 -1
  44. package/dist/lib/browser/chunk-5R7TDXUU.mjs.map +0 -7
@@ -61,11 +61,11 @@ var import_keys7 = require("@dxos/keys");
61
61
  var import_util5 = require("@dxos/util");
62
62
 
63
63
  // packages/core/mesh/network-manager/src/swarm/connection.ts
64
- var import_tiny_invariant = __toESM(require("tiny-invariant"));
65
64
  var import_async = require("@dxos/async");
66
65
  var import_context = require("@dxos/context");
67
66
  var import_debug = require("@dxos/debug");
68
67
  var import_errors = require("@dxos/errors");
68
+ var import_invariant = require("@dxos/invariant");
69
69
  var import_keys = require("@dxos/keys");
70
70
  var import_log = require("@dxos/log");
71
71
  var import_protocols = require("@dxos/protocols");
@@ -137,6 +137,18 @@ var Connection = class {
137
137
  await this._flushSignalBuffer();
138
138
  });
139
139
  this._signallingDelay = STARTING_SIGNALLING_DELAY;
140
+ import_log.log.trace("dxos.mesh.connection.construct", {
141
+ sessionId: this.sessionId,
142
+ topic: this.topic,
143
+ localPeerId: this.ownId,
144
+ remotePeerId: this.remoteId,
145
+ initiator: this.initiator
146
+ }, {
147
+ F: __dxlog_file,
148
+ L: 95,
149
+ S: this,
150
+ C: (f, a) => f(...a)
151
+ });
140
152
  }
141
153
  get state() {
142
154
  return this._state;
@@ -151,12 +163,32 @@ var Connection = class {
151
163
  * Create an underlying transport and prepares it for the connection.
152
164
  */
153
165
  async openConnection() {
154
- (0, import_tiny_invariant.default)(this._state === ConnectionState.INITIAL, "Invalid state.");
166
+ (0, import_invariant.invariant)(this._state === ConnectionState.INITIAL, "Invalid state.", {
167
+ F: __dxlog_file,
168
+ L: 120,
169
+ S: this,
170
+ A: [
171
+ "this._state === ConnectionState.INITIAL",
172
+ "'Invalid state.'"
173
+ ]
174
+ });
155
175
  import_log.log.trace("dxos.mesh.connection.open-connection", import_protocols.trace.begin({
156
176
  id: this._instanceId
157
177
  }), {
158
178
  F: __dxlog_file,
159
- L: 114,
179
+ L: 121,
180
+ S: this,
181
+ C: (f, a) => f(...a)
182
+ });
183
+ import_log.log.trace("dxos.mesh.connection.open", {
184
+ sessionId: this.sessionId,
185
+ topic: this.topic,
186
+ localPeerId: this.ownId,
187
+ remotePeerId: this.remoteId,
188
+ initiator: this.initiator
189
+ }, {
190
+ F: __dxlog_file,
191
+ L: 122,
160
192
  S: this,
161
193
  C: (f, a) => f(...a)
162
194
  });
@@ -167,13 +199,21 @@ var Connection = class {
167
199
  this._protocol.stream.on("close", () => {
168
200
  (0, import_log.log)("protocol stream closed", void 0, {
169
201
  F: __dxlog_file,
170
- L: 125,
202
+ L: 139,
171
203
  S: this,
172
204
  C: (f, a) => f(...a)
173
205
  });
174
206
  this.close().catch((err) => this.errors.raise(err));
175
207
  });
176
- (0, import_tiny_invariant.default)(!this._transport);
208
+ (0, import_invariant.invariant)(!this._transport, void 0, {
209
+ F: __dxlog_file,
210
+ L: 143,
211
+ S: this,
212
+ A: [
213
+ "!this._transport",
214
+ ""
215
+ ]
216
+ });
177
217
  this._transport = this._transportFactory.createTransport({
178
218
  initiator: this.initiator,
179
219
  stream: this._protocol.stream,
@@ -199,13 +239,12 @@ var Connection = class {
199
239
  id: this._instanceId
200
240
  }), {
201
241
  F: __dxlog_file,
202
- L: 158,
242
+ L: 172,
203
243
  S: this,
204
244
  C: (f, a) => f(...a)
205
245
  });
206
246
  }
207
247
  async close() {
208
- var _a;
209
248
  if (this._state === ConnectionState.CLOSED) {
210
249
  return;
211
250
  }
@@ -215,7 +254,7 @@ var Connection = class {
215
254
  peerId: this.ownId
216
255
  }, {
217
256
  F: __dxlog_file,
218
- L: 170,
257
+ L: 184,
219
258
  S: this,
220
259
  C: (f, a) => f(...a)
221
260
  });
@@ -224,17 +263,17 @@ var Connection = class {
224
263
  } catch (err) {
225
264
  import_log.log.catch(err, void 0, {
226
265
  F: __dxlog_file,
227
- L: 176,
266
+ L: 190,
228
267
  S: this,
229
268
  C: (f, a) => f(...a)
230
269
  });
231
270
  }
232
271
  try {
233
- await ((_a = this._transport) == null ? void 0 : _a.destroy());
272
+ await this._transport?.destroy();
234
273
  } catch (err) {
235
274
  import_log.log.catch(err, void 0, {
236
275
  F: __dxlog_file,
237
- L: 183,
276
+ L: 197,
238
277
  S: this,
239
278
  C: (f, a) => f(...a)
240
279
  });
@@ -243,7 +282,7 @@ var Connection = class {
243
282
  peerId: this.ownId
244
283
  }, {
245
284
  F: __dxlog_file,
246
- L: 186,
285
+ L: 200,
247
286
  S: this,
248
287
  C: (f, a) => f(...a)
249
288
  });
@@ -283,7 +322,7 @@ var Connection = class {
283
322
  err
284
323
  }, {
285
324
  F: __dxlog_file,
286
- L: 220,
325
+ L: 234,
287
326
  S: this,
288
327
  C: (f, a) => f(...a)
289
328
  });
@@ -294,21 +333,52 @@ var Connection = class {
294
333
  * Receive a signal from the remote peer.
295
334
  */
296
335
  async signal(msg) {
297
- var _a, _b, _c;
298
- (0, import_tiny_invariant.default)(msg.sessionId);
336
+ (0, import_invariant.invariant)(msg.sessionId, void 0, {
337
+ F: __dxlog_file,
338
+ L: 243,
339
+ S: this,
340
+ A: [
341
+ "msg.sessionId",
342
+ ""
343
+ ]
344
+ });
299
345
  if (!msg.sessionId.equals(this.sessionId)) {
300
346
  (0, import_log.log)("dropping signal for incorrect session id", void 0, {
301
347
  F: __dxlog_file,
302
- L: 231,
348
+ L: 245,
303
349
  S: this,
304
350
  C: (f, a) => f(...a)
305
351
  });
306
352
  return;
307
353
  }
308
- (0, import_tiny_invariant.default)(msg.data.signal || msg.data.signalBatch);
309
- (0, import_tiny_invariant.default)((_a = msg.author) == null ? void 0 : _a.equals(this.remoteId));
310
- (0, import_tiny_invariant.default)((_b = msg.recipient) == null ? void 0 : _b.equals(this.ownId));
311
- const signals = msg.data.signalBatch ? (_c = msg.data.signalBatch.signals) != null ? _c : [] : [
354
+ (0, import_invariant.invariant)(msg.data.signal || msg.data.signalBatch, void 0, {
355
+ F: __dxlog_file,
356
+ L: 248,
357
+ S: this,
358
+ A: [
359
+ "msg.data.signal || msg.data.signalBatch",
360
+ ""
361
+ ]
362
+ });
363
+ (0, import_invariant.invariant)(msg.author?.equals(this.remoteId), void 0, {
364
+ F: __dxlog_file,
365
+ L: 249,
366
+ S: this,
367
+ A: [
368
+ "msg.author?.equals(this.remoteId)",
369
+ ""
370
+ ]
371
+ });
372
+ (0, import_invariant.invariant)(msg.recipient?.equals(this.ownId), void 0, {
373
+ F: __dxlog_file,
374
+ L: 250,
375
+ S: this,
376
+ A: [
377
+ "msg.recipient?.equals(this.ownId)",
378
+ ""
379
+ ]
380
+ });
381
+ const signals = msg.data.signalBatch ? msg.data.signalBatch.signals ?? [] : [
312
382
  msg.data.signal
313
383
  ];
314
384
  for (const signal of signals) {
@@ -322,20 +392,28 @@ var Connection = class {
322
392
  msg: msg.data
323
393
  }, {
324
394
  F: __dxlog_file,
325
- L: 245,
395
+ L: 259,
326
396
  S: this,
327
397
  C: (f, a) => f(...a)
328
398
  });
329
399
  this._incomingSignalBuffer.push(signal);
330
400
  } else {
331
- (0, import_tiny_invariant.default)(this._transport, "Connection not ready to accept signals.");
401
+ (0, import_invariant.invariant)(this._transport, "Connection not ready to accept signals.", {
402
+ F: __dxlog_file,
403
+ L: 262,
404
+ S: this,
405
+ A: [
406
+ "this._transport",
407
+ "'Connection not ready to accept signals.'"
408
+ ]
409
+ });
332
410
  (0, import_log.log)("received signal", {
333
411
  peerId: this.ownId,
334
412
  remoteId: this.remoteId,
335
413
  msg: msg.data
336
414
  }, {
337
415
  F: __dxlog_file,
338
- L: 249,
416
+ L: 263,
339
417
  S: this,
340
418
  C: (f, a) => f(...a)
341
419
  });
@@ -350,11 +428,19 @@ var Connection = class {
350
428
  peerId: this.ownId
351
429
  }, {
352
430
  F: __dxlog_file,
353
- L: 256,
431
+ L: 270,
354
432
  S: this,
355
433
  C: (f, a) => f(...a)
356
434
  });
357
- (0, import_tiny_invariant.default)(state !== this._state, "Already in this state.");
435
+ (0, import_invariant.invariant)(state !== this._state, "Already in this state.", {
436
+ F: __dxlog_file,
437
+ L: 271,
438
+ S: this,
439
+ A: [
440
+ "state !== this._state",
441
+ "'Already in this state.'"
442
+ ]
443
+ });
358
444
  this._state = state;
359
445
  this.stateChanged.emit(state);
360
446
  }
@@ -364,18 +450,18 @@ _ts_decorate([
364
450
  ], Connection.prototype, "close", null);
365
451
 
366
452
  // packages/core/mesh/network-manager/src/swarm/swarm.ts
367
- var import_tiny_invariant4 = __toESM(require("tiny-invariant"));
368
453
  var import_async3 = require("@dxos/async");
369
454
  var import_context4 = require("@dxos/context");
370
455
  var import_debug2 = require("@dxos/debug");
456
+ var import_invariant4 = require("@dxos/invariant");
371
457
  var import_keys4 = require("@dxos/keys");
372
458
  var import_log4 = require("@dxos/log");
373
459
  var import_protocols3 = require("@dxos/protocols");
374
460
  var import_util2 = require("@dxos/util");
375
461
 
376
462
  // packages/core/mesh/network-manager/src/signal/swarm-messenger.ts
377
- var import_tiny_invariant2 = __toESM(require("tiny-invariant"));
378
463
  var import_context2 = require("@dxos/context");
464
+ var import_invariant2 = require("@dxos/invariant");
379
465
  var import_keys2 = require("@dxos/keys");
380
466
  var import_log2 = require("@dxos/log");
381
467
  var import_protocols2 = require("@dxos/protocols");
@@ -392,7 +478,6 @@ var SwarmMessenger = class {
392
478
  this._topic = topic;
393
479
  }
394
480
  async receiveMessage({ author, recipient, payload }) {
395
- var _a, _b, _c, _d;
396
481
  if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
397
482
  return;
398
483
  }
@@ -406,25 +491,25 @@ var SwarmMessenger = class {
406
491
  msg: message
407
492
  }, {
408
493
  F: __dxlog_file2,
409
- L: 70,
494
+ L: 69,
410
495
  S: this,
411
496
  C: (f, a) => f(...a)
412
497
  });
413
- if ((_a = message.data) == null ? void 0 : _a.offer) {
498
+ if (message.data?.offer) {
414
499
  await this._handleOffer({
415
500
  author,
416
501
  recipient,
417
502
  message
418
503
  });
419
- } else if ((_b = message.data) == null ? void 0 : _b.answer) {
504
+ } else if (message.data?.answer) {
420
505
  await this._resolveAnswers(message);
421
- } else if ((_c = message.data) == null ? void 0 : _c.signal) {
506
+ } else if (message.data?.signal) {
422
507
  await this._handleSignal({
423
508
  author,
424
509
  recipient,
425
510
  message
426
511
  });
427
- } else if ((_d = message.data) == null ? void 0 : _d.signalBatch) {
512
+ } else if (message.data?.signalBatch) {
428
513
  await this._handleSignal({
429
514
  author,
430
515
  recipient,
@@ -435,15 +520,22 @@ var SwarmMessenger = class {
435
520
  message
436
521
  }, {
437
522
  F: __dxlog_file2,
438
- L: 81,
523
+ L: 80,
439
524
  S: this,
440
525
  C: (f, a) => f(...a)
441
526
  });
442
527
  }
443
528
  }
444
529
  async signal(message) {
445
- var _a, _b;
446
- (0, import_tiny_invariant2.default)(((_a = message.data) == null ? void 0 : _a.signal) || ((_b = message.data) == null ? void 0 : _b.signalBatch), "Invalid message");
530
+ (0, import_invariant2.invariant)(message.data?.signal || message.data?.signalBatch, "Invalid message", {
531
+ F: __dxlog_file2,
532
+ L: 85,
533
+ S: this,
534
+ A: [
535
+ "message.data?.signal || message.data?.signalBatch",
536
+ "'Invalid message'"
537
+ ]
538
+ });
447
539
  await this._sendReliableMessage({
448
540
  author: message.author,
449
541
  recipient: message.recipient,
@@ -467,11 +559,10 @@ var SwarmMessenger = class {
467
559
  });
468
560
  }
469
561
  async _sendReliableMessage({ author, recipient, message }) {
470
- var _a;
471
562
  const networkMessage = {
472
563
  ...message,
473
564
  // Setting unique message_id if it not specified yet.
474
- messageId: (_a = message.messageId) != null ? _a : import_keys2.PublicKey.random()
565
+ messageId: message.messageId ?? import_keys2.PublicKey.random()
475
566
  };
476
567
  (0, import_log2.log)("sending", {
477
568
  from: author,
@@ -479,7 +570,7 @@ var SwarmMessenger = class {
479
570
  msg: networkMessage
480
571
  }, {
481
572
  F: __dxlog_file2,
482
- L: 124,
573
+ L: 123,
483
574
  S: this,
484
575
  C: (f, a) => f(...a)
485
576
  });
@@ -493,17 +584,32 @@ var SwarmMessenger = class {
493
584
  });
494
585
  }
495
586
  async _resolveAnswers(message) {
496
- var _a, _b, _c;
497
- (0, import_tiny_invariant2.default)((_b = (_a = message.data) == null ? void 0 : _a.answer) == null ? void 0 : _b.offerMessageId, "No offerMessageId");
587
+ (0, import_invariant2.invariant)(message.data?.answer?.offerMessageId, "No offerMessageId", {
588
+ F: __dxlog_file2,
589
+ L: 135,
590
+ S: this,
591
+ A: [
592
+ "message.data?.answer?.offerMessageId",
593
+ "'No offerMessageId'"
594
+ ]
595
+ });
498
596
  const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
499
597
  if (offerRecord) {
500
598
  this._offerRecords.delete(message.data.answer.offerMessageId);
501
- (0, import_tiny_invariant2.default)((_c = message.data) == null ? void 0 : _c.answer, "No answer");
599
+ (0, import_invariant2.invariant)(message.data?.answer, "No answer", {
600
+ F: __dxlog_file2,
601
+ L: 139,
602
+ S: this,
603
+ A: [
604
+ "message.data?.answer",
605
+ "'No answer'"
606
+ ]
607
+ });
502
608
  (0, import_log2.log)("resolving", {
503
609
  answer: message.data.answer
504
610
  }, {
505
611
  F: __dxlog_file2,
506
- L: 141,
612
+ L: 140,
507
613
  S: this,
508
614
  C: (f, a) => f(...a)
509
615
  });
@@ -511,7 +617,15 @@ var SwarmMessenger = class {
511
617
  }
512
618
  }
513
619
  async _handleOffer({ author, recipient, message }) {
514
- (0, import_tiny_invariant2.default)(message.data.offer, "No offer");
620
+ (0, import_invariant2.invariant)(message.data.offer, "No offer", {
621
+ F: __dxlog_file2,
622
+ L: 154,
623
+ S: this,
624
+ A: [
625
+ "message.data.offer",
626
+ "'No offer'"
627
+ ]
628
+ });
515
629
  const offerMessage = {
516
630
  author,
517
631
  recipient,
@@ -539,15 +653,31 @@ var SwarmMessenger = class {
539
653
  err
540
654
  }, {
541
655
  F: __dxlog_file2,
542
- L: 175,
656
+ L: 174,
543
657
  S: this,
544
658
  C: (f, a) => f(...a)
545
659
  });
546
660
  }
547
661
  }
548
662
  async _handleSignal({ author, recipient, message }) {
549
- (0, import_tiny_invariant2.default)(message.messageId);
550
- (0, import_tiny_invariant2.default)(message.data.signal || message.data.signalBatch, "Invalid message");
663
+ (0, import_invariant2.invariant)(message.messageId, void 0, {
664
+ F: __dxlog_file2,
665
+ L: 187,
666
+ S: this,
667
+ A: [
668
+ "message.messageId",
669
+ ""
670
+ ]
671
+ });
672
+ (0, import_invariant2.invariant)(message.data.signal || message.data.signalBatch, "Invalid message", {
673
+ F: __dxlog_file2,
674
+ L: 188,
675
+ S: this,
676
+ A: [
677
+ "message.data.signal || message.data.signalBatch",
678
+ "'Invalid message'"
679
+ ]
680
+ });
551
681
  const signalMessage = {
552
682
  author,
553
683
  recipient,
@@ -562,10 +692,10 @@ var SwarmMessenger = class {
562
692
  };
563
693
 
564
694
  // packages/core/mesh/network-manager/src/swarm/peer.ts
565
- var import_tiny_invariant3 = __toESM(require("tiny-invariant"));
566
695
  var import_async2 = require("@dxos/async");
567
696
  var import_context3 = require("@dxos/context");
568
697
  var import_errors2 = require("@dxos/errors");
698
+ var import_invariant3 = require("@dxos/invariant");
569
699
  var import_keys3 = require("@dxos/keys");
570
700
  var import_log3 = require("@dxos/log");
571
701
  function _ts_decorate2(decorators, target, key, desc) {
@@ -600,7 +730,6 @@ var Peer = class {
600
730
  * Respond to remote offer.
601
731
  */
602
732
  async onOffer(message) {
603
- var _a;
604
733
  const remoteId = message.author;
605
734
  if (this.connection || this.initiating) {
606
735
  if (remoteId.toHex() < this.localPeerId.toHex()) {
@@ -608,10 +737,10 @@ var Peer = class {
608
737
  localPeerId: this.id,
609
738
  topic: this.topic,
610
739
  remotePeerId: this.localPeerId,
611
- sessionId: (_a = this.connection) == null ? void 0 : _a.sessionId
740
+ sessionId: this.connection?.sessionId
612
741
  }, {
613
742
  F: __dxlog_file3,
614
- L: 107,
743
+ L: 106,
615
744
  S: this,
616
745
  C: (f, a) => f(...a)
617
746
  });
@@ -626,7 +755,15 @@ var Peer = class {
626
755
  }
627
756
  if (await this._callbacks.onOffer(remoteId)) {
628
757
  if (!this.connection) {
629
- (0, import_tiny_invariant3.default)(message.sessionId);
758
+ (0, import_invariant3.invariant)(message.sessionId, void 0, {
759
+ F: __dxlog_file3,
760
+ L: 126,
761
+ S: this,
762
+ A: [
763
+ "message.sessionId",
764
+ ""
765
+ ]
766
+ });
630
767
  const connection = this._createConnection(false, message.sessionId);
631
768
  try {
632
769
  await this._connectionLimiter.connecting(message.sessionId);
@@ -640,7 +777,7 @@ var Peer = class {
640
777
  err
641
778
  }, {
642
779
  F: __dxlog_file3,
643
- L: 135,
780
+ L: 134,
644
781
  S: this,
645
782
  C: (f, a) => f(...a)
646
783
  });
@@ -660,8 +797,24 @@ var Peer = class {
660
797
  * Initiate a connection to the remote peer.
661
798
  */
662
799
  async initiateConnection() {
663
- (0, import_tiny_invariant3.default)(!this.initiating, "Initiation in progress.");
664
- (0, import_tiny_invariant3.default)(!this.connection, "Already connected.");
800
+ (0, import_invariant3.invariant)(!this.initiating, "Initiation in progress.", {
801
+ F: __dxlog_file3,
802
+ L: 151,
803
+ S: this,
804
+ A: [
805
+ "!this.initiating",
806
+ "'Initiation in progress.'"
807
+ ]
808
+ });
809
+ (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
810
+ F: __dxlog_file3,
811
+ L: 152,
812
+ S: this,
813
+ A: [
814
+ "!this.connection",
815
+ "'Already connected.'"
816
+ ]
817
+ });
665
818
  const sessionId = import_keys3.PublicKey.random();
666
819
  (0, import_log3.log)("initiating...", {
667
820
  id: this.id,
@@ -670,7 +823,7 @@ var Peer = class {
670
823
  sessionId
671
824
  }, {
672
825
  F: __dxlog_file3,
673
- L: 155,
826
+ L: 154,
674
827
  S: this,
675
828
  C: (f, a) => f(...a)
676
829
  });
@@ -694,14 +847,14 @@ var Peer = class {
694
847
  remoteId: this.id
695
848
  }, {
696
849
  F: __dxlog_file3,
697
- L: 170,
850
+ L: 169,
698
851
  S: this,
699
852
  C: (f, a) => f(...a)
700
853
  });
701
854
  if (connection.state !== ConnectionState.INITIAL) {
702
855
  (0, import_log3.log)("ignoring response", void 0, {
703
856
  F: __dxlog_file3,
704
- L: 172,
857
+ L: 171,
705
858
  S: this,
706
859
  C: (f, a) => f(...a)
707
860
  });
@@ -721,7 +874,7 @@ var Peer = class {
721
874
  remoteId: this.id
722
875
  }, {
723
876
  F: __dxlog_file3,
724
- L: 183,
877
+ L: 182,
725
878
  S: this,
726
879
  C: (f, a) => f(...a)
727
880
  });
@@ -736,7 +889,6 @@ var Peer = class {
736
889
  * Either we're initiating a connection or creating one in response to an offer from the other peer.
737
890
  */
738
891
  _createConnection(initiator, sessionId) {
739
- var _a;
740
892
  (0, import_log3.log)("creating connection", {
741
893
  topic: this.topic,
742
894
  peerId: this.localPeerId,
@@ -745,11 +897,19 @@ var Peer = class {
745
897
  sessionId
746
898
  }, {
747
899
  F: __dxlog_file3,
748
- L: 197,
900
+ L: 196,
749
901
  S: this,
750
902
  C: (f, a) => f(...a)
751
903
  });
752
- (0, import_tiny_invariant3.default)(!this.connection, "Already connected.");
904
+ (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
905
+ F: __dxlog_file3,
906
+ L: 203,
907
+ S: this,
908
+ A: [
909
+ "!this.connection",
910
+ "'Already connected.'"
911
+ ]
912
+ });
753
913
  const connection = new Connection(
754
914
  this.topic,
755
915
  this.localPeerId,
@@ -767,7 +927,7 @@ var Peer = class {
767
927
  this._transportFactory
768
928
  );
769
929
  this._callbacks.onInitiated(connection);
770
- void ((_a = this._connectionCtx) == null ? void 0 : _a.dispose());
930
+ void this._connectionCtx?.dispose();
771
931
  this._connectionCtx = this._ctx.derive();
772
932
  connection.stateChanged.on((state) => {
773
933
  switch (state) {
@@ -776,6 +936,18 @@ var Peer = class {
776
936
  this._lastConnectionTime = Date.now();
777
937
  this._callbacks.onConnected();
778
938
  this._connectionLimiter.doneConnecting(sessionId);
939
+ import_log3.log.trace("dxos.mesh.connection.connected", {
940
+ topic: this.topic,
941
+ localPeerId: this.localPeerId,
942
+ remotePeerId: this.id,
943
+ sessionId,
944
+ initiator
945
+ }, {
946
+ F: __dxlog_file3,
947
+ L: 229,
948
+ S: this,
949
+ C: (f, a) => f(...a)
950
+ });
779
951
  break;
780
952
  }
781
953
  case ConnectionState.CLOSED: {
@@ -786,11 +958,31 @@ var Peer = class {
786
958
  initiator
787
959
  }, {
788
960
  F: __dxlog_file3,
789
- L: 234,
961
+ L: 240,
962
+ S: this,
963
+ C: (f, a) => f(...a)
964
+ });
965
+ (0, import_invariant3.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
966
+ F: __dxlog_file3,
967
+ L: 241,
968
+ S: this,
969
+ A: [
970
+ "this.connection === connection",
971
+ "'Connection mismatch (race condition).'"
972
+ ]
973
+ });
974
+ import_log3.log.trace("dxos.mesh.connection.closed", {
975
+ topic: this.topic,
976
+ localPeerId: this.localPeerId,
977
+ remotePeerId: this.id,
978
+ sessionId,
979
+ initiator
980
+ }, {
981
+ F: __dxlog_file3,
982
+ L: 243,
790
983
  S: this,
791
984
  C: (f, a) => f(...a)
792
985
  });
793
- (0, import_tiny_invariant3.default)(this.connection === connection, "Connection mismatch (race condition).");
794
986
  if (this._lastConnectionTime && this._lastConnectionTime + CONNECTION_COUNTS_STABLE_AFTER < Date.now()) {
795
987
  this._availableAfter = 0;
796
988
  } else {
@@ -817,7 +1009,20 @@ var Peer = class {
817
1009
  err
818
1010
  }, {
819
1011
  F: __dxlog_file3,
820
- L: 263,
1012
+ L: 277,
1013
+ S: this,
1014
+ C: (f, a) => f(...a)
1015
+ });
1016
+ import_log3.log.trace("dxos.mesh.connection.error", {
1017
+ topic: this.topic,
1018
+ localPeerId: this.localPeerId,
1019
+ remotePeerId: this.id,
1020
+ sessionId,
1021
+ initiator,
1022
+ err
1023
+ }, {
1024
+ F: __dxlog_file3,
1025
+ L: 278,
821
1026
  S: this,
822
1027
  C: (f, a) => f(...a)
823
1028
  });
@@ -836,7 +1041,7 @@ var Peer = class {
836
1041
  sessionId: connection.sessionId
837
1042
  }, {
838
1043
  F: __dxlog_file3,
839
- L: 279,
1044
+ L: 301,
840
1045
  S: this,
841
1046
  C: (f, a) => f(...a)
842
1047
  });
@@ -846,7 +1051,7 @@ var Peer = class {
846
1051
  sessionId: connection.sessionId
847
1052
  }, {
848
1053
  F: __dxlog_file3,
849
- L: 285,
1054
+ L: 307,
850
1055
  S: this,
851
1056
  C: (f, a) => f(...a)
852
1057
  });
@@ -857,7 +1062,7 @@ var Peer = class {
857
1062
  message
858
1063
  }, {
859
1064
  F: __dxlog_file3,
860
- L: 290,
1065
+ L: 312,
861
1066
  S: this,
862
1067
  C: (f, a) => f(...a)
863
1068
  });
@@ -866,18 +1071,17 @@ var Peer = class {
866
1071
  await this.connection.signal(message);
867
1072
  }
868
1073
  async destroy() {
869
- var _a;
870
1074
  await this._ctx.dispose();
871
1075
  (0, import_log3.log)("Destroying peer", {
872
1076
  peerId: this.id,
873
1077
  topic: this.topic
874
1078
  }, {
875
1079
  F: __dxlog_file3,
876
- L: 300,
1080
+ L: 322,
877
1081
  S: this,
878
1082
  C: (f, a) => f(...a)
879
1083
  });
880
- await ((_a = this == null ? void 0 : this.connection) == null ? void 0 : _a.close());
1084
+ await this?.connection?.close();
881
1085
  }
882
1086
  };
883
1087
  _ts_decorate2([
@@ -938,7 +1142,7 @@ var Swarm = class {
938
1142
  }
939
1143
  }), {
940
1144
  F: __dxlog_file4,
941
- L: 88,
1145
+ L: 87,
942
1146
  S: this,
943
1147
  C: (f, a) => f(...a)
944
1148
  });
@@ -946,7 +1150,7 @@ var Swarm = class {
946
1150
  peerId: _ownPeerId
947
1151
  }, {
948
1152
  F: __dxlog_file4,
949
- L: 92,
1153
+ L: 91,
950
1154
  S: this,
951
1155
  C: (f, a) => f(...a)
952
1156
  });
@@ -961,7 +1165,7 @@ var Swarm = class {
961
1165
  id: this._instanceId
962
1166
  }), {
963
1167
  F: __dxlog_file4,
964
- L: 101,
1168
+ L: 100,
965
1169
  S: this,
966
1170
  C: (f, a) => f(...a)
967
1171
  });
@@ -982,7 +1186,15 @@ var Swarm = class {
982
1186
  return this._topic;
983
1187
  }
984
1188
  async open() {
985
- (0, import_tiny_invariant4.default)(!this._listeningHandle);
1189
+ (0, import_invariant4.invariant)(!this._listeningHandle, void 0, {
1190
+ F: __dxlog_file4,
1191
+ L: 127,
1192
+ S: this,
1193
+ A: [
1194
+ "!this._listeningHandle",
1195
+ ""
1196
+ ]
1197
+ });
986
1198
  this._listeningHandle = await this._messenger.listen({
987
1199
  peerId: this._ownPeerId,
988
1200
  payloadType: "dxos.mesh.swarm.SwarmMessage",
@@ -991,7 +1203,7 @@ var Swarm = class {
991
1203
  err
992
1204
  }, {
993
1205
  F: __dxlog_file4,
994
- L: 135,
1206
+ L: 134,
995
1207
  S: this,
996
1208
  C: (f, a) => f(...a)
997
1209
  }));
@@ -999,27 +1211,34 @@ var Swarm = class {
999
1211
  });
1000
1212
  }
1001
1213
  async destroy() {
1002
- var _a;
1003
1214
  (0, import_log4.log)("destroying...", void 0, {
1004
1215
  F: __dxlog_file4,
1005
- L: 141,
1216
+ L: 140,
1006
1217
  S: this,
1007
1218
  C: (f, a) => f(...a)
1008
1219
  });
1009
- await ((_a = this._listeningHandle) == null ? void 0 : _a.unsubscribe());
1220
+ await this._listeningHandle?.unsubscribe();
1010
1221
  this._listeningHandle = void 0;
1011
1222
  await this._ctx.dispose();
1012
1223
  await this._topology.destroy();
1013
1224
  await Promise.all(Array.from(this._peers.keys()).map((key) => this._destroyPeer(key)));
1014
1225
  (0, import_log4.log)("destroyed", void 0, {
1015
1226
  F: __dxlog_file4,
1016
- L: 148,
1227
+ L: 147,
1017
1228
  S: this,
1018
1229
  C: (f, a) => f(...a)
1019
1230
  });
1020
1231
  }
1021
1232
  async setTopology(topology) {
1022
- (0, import_tiny_invariant4.default)(!this._ctx.disposed, "Swarm is offline");
1233
+ (0, import_invariant4.invariant)(!this._ctx.disposed, "Swarm is offline", {
1234
+ F: __dxlog_file4,
1235
+ L: 151,
1236
+ S: this,
1237
+ A: [
1238
+ "!this._ctx.disposed",
1239
+ "'Swarm is offline'"
1240
+ ]
1241
+ });
1023
1242
  if (topology === this._topology) {
1024
1243
  return;
1025
1244
  }
@@ -1028,7 +1247,7 @@ var Swarm = class {
1028
1247
  topology: getClassName(topology)
1029
1248
  }, {
1030
1249
  F: __dxlog_file4,
1031
- L: 156,
1250
+ L: 155,
1032
1251
  S: this,
1033
1252
  C: (f, a) => f(...a)
1034
1253
  });
@@ -1038,19 +1257,18 @@ var Swarm = class {
1038
1257
  this._topology.update();
1039
1258
  }
1040
1259
  onSwarmEvent(swarmEvent) {
1041
- var _a;
1042
1260
  (0, import_log4.log)("swarm event", {
1043
1261
  swarmEvent
1044
1262
  }, {
1045
1263
  F: __dxlog_file4,
1046
- L: 169,
1264
+ L: 168,
1047
1265
  S: this,
1048
1266
  C: (f, a) => f(...a)
1049
1267
  });
1050
1268
  if (this._ctx.disposed) {
1051
1269
  (0, import_log4.log)("swarm event ignored for disposed swarm", void 0, {
1052
1270
  F: __dxlog_file4,
1053
- L: 172,
1271
+ L: 171,
1054
1272
  S: this,
1055
1273
  C: (f, a) => f(...a)
1056
1274
  });
@@ -1062,7 +1280,7 @@ var Swarm = class {
1062
1280
  peerId
1063
1281
  }, {
1064
1282
  F: __dxlog_file4,
1065
- L: 178,
1283
+ L: 177,
1066
1284
  S: this,
1067
1285
  C: (f, a) => f(...a)
1068
1286
  });
@@ -1074,10 +1292,10 @@ var Swarm = class {
1074
1292
  const peer = this._peers.get(import_keys4.PublicKey.from(swarmEvent.peerLeft.peer));
1075
1293
  if (peer) {
1076
1294
  peer.advertizing = false;
1077
- if (((_a = peer.connection) == null ? void 0 : _a.state) !== ConnectionState.CONNECTED) {
1295
+ if (peer.connection?.state !== ConnectionState.CONNECTED) {
1078
1296
  void this._destroyPeer(peer.id).catch((err) => import_log4.log.catch(err, void 0, {
1079
1297
  F: __dxlog_file4,
1080
- L: 189,
1298
+ L: 188,
1081
1299
  S: this,
1082
1300
  C: (f, a) => f(...a)
1083
1301
  }));
@@ -1087,19 +1305,18 @@ var Swarm = class {
1087
1305
  this._topology.update();
1088
1306
  }
1089
1307
  async onOffer(message) {
1090
- var _a, _b;
1091
1308
  (0, import_log4.log)("offer", {
1092
1309
  message
1093
1310
  }, {
1094
1311
  F: __dxlog_file4,
1095
- L: 199,
1312
+ L: 198,
1096
1313
  S: this,
1097
1314
  C: (f, a) => f(...a)
1098
1315
  });
1099
1316
  if (this._ctx.disposed) {
1100
1317
  (0, import_log4.log)("ignored for disposed swarm", void 0, {
1101
1318
  F: __dxlog_file4,
1102
- L: 201,
1319
+ L: 200,
1103
1320
  S: this,
1104
1321
  C: (f, a) => f(...a)
1105
1322
  });
@@ -1107,13 +1324,21 @@ var Swarm = class {
1107
1324
  accept: false
1108
1325
  };
1109
1326
  }
1110
- (0, import_tiny_invariant4.default)(message.author);
1111
- if (!((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId))) {
1327
+ (0, import_invariant4.invariant)(message.author, void 0, {
1328
+ F: __dxlog_file4,
1329
+ L: 205,
1330
+ S: this,
1331
+ A: [
1332
+ "message.author",
1333
+ ""
1334
+ ]
1335
+ });
1336
+ if (!message.recipient?.equals(this._ownPeerId)) {
1112
1337
  (0, import_log4.log)("rejecting offer with incorrect peerId", {
1113
1338
  message
1114
1339
  }, {
1115
1340
  F: __dxlog_file4,
1116
- L: 208,
1341
+ L: 207,
1117
1342
  S: this,
1118
1343
  C: (f, a) => f(...a)
1119
1344
  });
@@ -1121,12 +1346,12 @@ var Swarm = class {
1121
1346
  accept: false
1122
1347
  };
1123
1348
  }
1124
- if (!((_b = message.topic) == null ? void 0 : _b.equals(this._topic))) {
1349
+ if (!message.topic?.equals(this._topic)) {
1125
1350
  (0, import_log4.log)("rejecting offer with incorrect topic", {
1126
1351
  message
1127
1352
  }, {
1128
1353
  F: __dxlog_file4,
1129
- L: 212,
1354
+ L: 211,
1130
1355
  S: this,
1131
1356
  C: (f, a) => f(...a)
1132
1357
  });
@@ -1140,27 +1365,50 @@ var Swarm = class {
1140
1365
  return answer;
1141
1366
  }
1142
1367
  async onSignal(message) {
1143
- var _a, _b;
1144
1368
  (0, import_log4.log)("signal", {
1145
1369
  message
1146
1370
  }, {
1147
1371
  F: __dxlog_file4,
1148
- L: 223,
1372
+ L: 222,
1149
1373
  S: this,
1150
1374
  C: (f, a) => f(...a)
1151
1375
  });
1152
1376
  if (this._ctx.disposed) {
1153
1377
  import_log4.log.info("ignored for offline swarm", void 0, {
1154
1378
  F: __dxlog_file4,
1155
- L: 225,
1379
+ L: 224,
1156
1380
  S: this,
1157
1381
  C: (f, a) => f(...a)
1158
1382
  });
1159
1383
  return;
1160
1384
  }
1161
- (0, import_tiny_invariant4.default)((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`);
1162
- (0, import_tiny_invariant4.default)((_b = message.topic) == null ? void 0 : _b.equals(this._topic));
1163
- (0, import_tiny_invariant4.default)(message.author);
1385
+ (0, import_invariant4.invariant)(message.recipient?.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1386
+ F: __dxlog_file4,
1387
+ L: 227,
1388
+ S: this,
1389
+ A: [
1390
+ "message.recipient?.equals(this._ownPeerId)",
1391
+ "`Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`"
1392
+ ]
1393
+ });
1394
+ (0, import_invariant4.invariant)(message.topic?.equals(this._topic), void 0, {
1395
+ F: __dxlog_file4,
1396
+ L: 231,
1397
+ S: this,
1398
+ A: [
1399
+ "message.topic?.equals(this._topic)",
1400
+ ""
1401
+ ]
1402
+ });
1403
+ (0, import_invariant4.invariant)(message.author, void 0, {
1404
+ F: __dxlog_file4,
1405
+ L: 232,
1406
+ S: this,
1407
+ A: [
1408
+ "message.author",
1409
+ ""
1410
+ ]
1411
+ });
1164
1412
  const peer = this._getOrCreatePeer(message.author);
1165
1413
  await peer.onSignal(message);
1166
1414
  }
@@ -1212,7 +1460,15 @@ var Swarm = class {
1212
1460
  return peer;
1213
1461
  }
1214
1462
  async _destroyPeer(peerId) {
1215
- (0, import_tiny_invariant4.default)(this._peers.has(peerId));
1463
+ (0, import_invariant4.invariant)(this._peers.has(peerId), void 0, {
1464
+ F: __dxlog_file4,
1465
+ L: 302,
1466
+ S: this,
1467
+ A: [
1468
+ "this._peers.has(peerId)",
1469
+ ""
1470
+ ]
1471
+ });
1216
1472
  await this._peers.get(peerId).destroy();
1217
1473
  this._peers.delete(peerId);
1218
1474
  }
@@ -1233,7 +1489,7 @@ var Swarm = class {
1233
1489
  } catch (err) {
1234
1490
  (0, import_log4.log)("initiation error", err, {
1235
1491
  F: __dxlog_file4,
1236
- L: 329,
1492
+ L: 328,
1237
1493
  S: this,
1238
1494
  C: (f, a) => f(...a)
1239
1495
  });
@@ -1261,7 +1517,7 @@ var Swarm = class {
1261
1517
  remoteId
1262
1518
  }, {
1263
1519
  F: __dxlog_file4,
1264
- L: 356,
1520
+ L: 355,
1265
1521
  S: this,
1266
1522
  C: (f, a) => f(...a)
1267
1523
  });
@@ -1278,7 +1534,7 @@ var Swarm = class {
1278
1534
  remoteId
1279
1535
  }, {
1280
1536
  F: __dxlog_file4,
1281
- L: 370,
1537
+ L: 369,
1282
1538
  S: this,
1283
1539
  C: (f, a) => f(...a)
1284
1540
  });
@@ -1288,7 +1544,7 @@ var Swarm = class {
1288
1544
  remoteId
1289
1545
  }, {
1290
1546
  F: __dxlog_file4,
1291
- L: 373,
1547
+ L: 372,
1292
1548
  S: this,
1293
1549
  C: (f, a) => f(...a)
1294
1550
  });
@@ -1347,8 +1603,7 @@ var SwarmMapper = class {
1347
1603
  }));
1348
1604
  }));
1349
1605
  this._subscriptions.add(_swarm.disconnected.on((peerId) => {
1350
- var _a;
1351
- (_a = this._connectionSubscriptions.get(peerId)) == null ? void 0 : _a();
1606
+ this._connectionSubscriptions.get(peerId)?.();
1352
1607
  this._connectionSubscriptions.delete(peerId);
1353
1608
  this._update();
1354
1609
  }));
@@ -1398,6 +1653,7 @@ var SwarmMapper = class {
1398
1653
  var import_async5 = require("@dxos/async");
1399
1654
  var import_context5 = require("@dxos/context");
1400
1655
  var import_errors3 = require("@dxos/errors");
1656
+ var import_invariant5 = require("@dxos/invariant");
1401
1657
  var import_keys6 = require("@dxos/keys");
1402
1658
  var import_log6 = require("@dxos/log");
1403
1659
  var import_util4 = require("@dxos/util");
@@ -1421,9 +1677,9 @@ var ConnectionLimiter = class {
1421
1677
  * @returns Promise that resolves in queue when connections amount with 'CONNECTING' state is below the limit.
1422
1678
  */
1423
1679
  async connecting(sessionId) {
1424
- (0, import_log6.invariant)(!this._waitingPromises.has(sessionId), "Peer is already waiting for connection", {
1680
+ (0, import_invariant5.invariant)(!this._waitingPromises.has(sessionId), "Peer is already waiting for connection", {
1425
1681
  F: __dxlog_file6,
1426
- L: 47,
1682
+ L: 48,
1427
1683
  S: this,
1428
1684
  A: [
1429
1685
  "!this._waitingPromises.has(sessionId)",
@@ -1434,7 +1690,7 @@ var ConnectionLimiter = class {
1434
1690
  sessionId
1435
1691
  }, {
1436
1692
  F: __dxlog_file6,
1437
- L: 48,
1693
+ L: 49,
1438
1694
  S: this,
1439
1695
  C: (f, a) => f(...a)
1440
1696
  });
@@ -1449,7 +1705,7 @@ var ConnectionLimiter = class {
1449
1705
  sessionId
1450
1706
  }, {
1451
1707
  F: __dxlog_file6,
1452
- L: 56,
1708
+ L: 57,
1453
1709
  S: this,
1454
1710
  C: (f, a) => f(...a)
1455
1711
  });
@@ -1462,7 +1718,7 @@ var ConnectionLimiter = class {
1462
1718
  sessionId
1463
1719
  }, {
1464
1720
  F: __dxlog_file6,
1465
- L: 63,
1721
+ L: 64,
1466
1722
  S: this,
1467
1723
  C: (f, a) => f(...a)
1468
1724
  });
@@ -1493,8 +1749,7 @@ var ConnectionLog = class {
1493
1749
  this.update = new import_async6.Event();
1494
1750
  }
1495
1751
  getSwarmInfo(swarmId) {
1496
- var _a;
1497
- return (_a = this._swarms.get(swarmId)) != null ? _a : (0, import_debug3.raise)(new Error(`Swarm not found: ${swarmId}`));
1752
+ return this._swarms.get(swarmId) ?? (0, import_debug3.raise)(new Error(`Swarm not found: ${swarmId}`));
1498
1753
  }
1499
1754
  get swarms() {
1500
1755
  return Array.from(this._swarms.values());
@@ -1510,7 +1765,6 @@ var ConnectionLog = class {
1510
1765
  this._swarms.set(import_keys7.PublicKey.from(swarm._instanceId), info);
1511
1766
  this.update.emit();
1512
1767
  swarm.connectionAdded.on((connection) => {
1513
- var _a, _b;
1514
1768
  const connectionInfo = {
1515
1769
  state: ConnectionState.INITIAL,
1516
1770
  remotePeerId: connection.remoteId,
@@ -1529,7 +1783,7 @@ var ConnectionLog = class {
1529
1783
  });
1530
1784
  this.update.emit();
1531
1785
  });
1532
- (_b = (_a = connection.protocol) == null ? void 0 : _a.stats) == null ? void 0 : _b.on((stats) => {
1786
+ connection.protocol?.stats?.on((stats) => {
1533
1787
  connectionInfo.streams = stats.channels;
1534
1788
  this.update.emit();
1535
1789
  });
@@ -1542,8 +1796,8 @@ var ConnectionLog = class {
1542
1796
  };
1543
1797
 
1544
1798
  // packages/core/mesh/network-manager/src/network-manager.ts
1545
- var import_tiny_invariant5 = __toESM(require("tiny-invariant"));
1546
1799
  var import_async7 = require("@dxos/async");
1800
+ var import_invariant6 = require("@dxos/invariant");
1547
1801
  var import_keys8 = require("@dxos/keys");
1548
1802
  var import_log7 = require("@dxos/log");
1549
1803
  var import_messaging = require("@dxos/messaging");
@@ -1564,10 +1818,7 @@ var NetworkManager = class {
1564
1818
  this._instanceId = import_keys8.PublicKey.random().toHex();
1565
1819
  this._transportFactory = transportFactory;
1566
1820
  this._signalManager = signalManager;
1567
- this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => {
1568
- var _a;
1569
- return (_a = this._swarms.get(topic)) == null ? void 0 : _a.onSwarmEvent(event);
1570
- });
1821
+ this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => this._swarms.get(topic)?.onSwarmEvent(event));
1571
1822
  this._messenger = new import_messaging.Messenger({
1572
1823
  signalManager: this._signalManager
1573
1824
  });
@@ -1602,7 +1853,7 @@ var NetworkManager = class {
1602
1853
  id: this._instanceId
1603
1854
  }), {
1604
1855
  F: __dxlog_file7,
1605
- L: 131,
1856
+ L: 130,
1606
1857
  S: this,
1607
1858
  C: (f, a) => f(...a)
1608
1859
  });
@@ -1612,7 +1863,7 @@ var NetworkManager = class {
1612
1863
  id: this._instanceId
1613
1864
  }), {
1614
1865
  F: __dxlog_file7,
1615
- L: 134,
1866
+ L: 133,
1616
1867
  S: this,
1617
1868
  C: (f, a) => f(...a)
1618
1869
  });
@@ -1622,7 +1873,7 @@ var NetworkManager = class {
1622
1873
  await this.leaveSwarm(topic).catch((err) => {
1623
1874
  (0, import_log7.log)(err, void 0, {
1624
1875
  F: __dxlog_file7,
1625
- L: 140,
1876
+ L: 139,
1626
1877
  S: this,
1627
1878
  C: (f, a) => f(...a)
1628
1879
  });
@@ -1635,11 +1886,42 @@ var NetworkManager = class {
1635
1886
  * Join the swarm.
1636
1887
  */
1637
1888
  async joinSwarm({ topic, peerId, topology, protocolProvider: protocol, label }) {
1638
- var _a;
1639
- (0, import_tiny_invariant5.default)(import_keys8.PublicKey.isPublicKey(topic));
1640
- (0, import_tiny_invariant5.default)(import_keys8.PublicKey.isPublicKey(peerId));
1641
- (0, import_tiny_invariant5.default)(topology);
1642
- (0, import_tiny_invariant5.default)(typeof protocol === "function");
1889
+ (0, import_invariant6.invariant)(import_keys8.PublicKey.isPublicKey(topic), void 0, {
1890
+ F: __dxlog_file7,
1891
+ L: 157,
1892
+ S: this,
1893
+ A: [
1894
+ "PublicKey.isPublicKey(topic)",
1895
+ ""
1896
+ ]
1897
+ });
1898
+ (0, import_invariant6.invariant)(import_keys8.PublicKey.isPublicKey(peerId), void 0, {
1899
+ F: __dxlog_file7,
1900
+ L: 158,
1901
+ S: this,
1902
+ A: [
1903
+ "PublicKey.isPublicKey(peerId)",
1904
+ ""
1905
+ ]
1906
+ });
1907
+ (0, import_invariant6.invariant)(topology, void 0, {
1908
+ F: __dxlog_file7,
1909
+ L: 159,
1910
+ S: this,
1911
+ A: [
1912
+ "topology",
1913
+ ""
1914
+ ]
1915
+ });
1916
+ (0, import_invariant6.invariant)(typeof protocol === "function", void 0, {
1917
+ F: __dxlog_file7,
1918
+ L: 160,
1919
+ S: this,
1920
+ A: [
1921
+ "typeof protocol === 'function'",
1922
+ ""
1923
+ ]
1924
+ });
1643
1925
  if (this._swarms.has(topic)) {
1644
1926
  throw new Error(`Already connected to swarm: ${import_keys8.PublicKey.from(topic)}`);
1645
1927
  }
@@ -1649,7 +1931,7 @@ var NetworkManager = class {
1649
1931
  topology: topology.toString()
1650
1932
  }, {
1651
1933
  F: __dxlog_file7,
1652
- L: 166,
1934
+ L: 165,
1653
1935
  S: this,
1654
1936
  C: (f, a) => f(...a)
1655
1937
  });
@@ -1659,7 +1941,7 @@ var NetworkManager = class {
1659
1941
  error
1660
1942
  }, {
1661
1943
  F: __dxlog_file7,
1662
- L: 179,
1944
+ L: 178,
1663
1945
  S: this,
1664
1946
  C: (f, a) => f(...a)
1665
1947
  });
@@ -1672,18 +1954,18 @@ var NetworkManager = class {
1672
1954
  peerId
1673
1955
  }).catch((error) => import_log7.log.catch(error, void 0, {
1674
1956
  F: __dxlog_file7,
1675
- L: 188,
1957
+ L: 187,
1676
1958
  S: this,
1677
1959
  C: (f, a) => f(...a)
1678
1960
  }));
1679
1961
  this.topicsUpdated.emit();
1680
- (_a = this._connectionLog) == null ? void 0 : _a.joinedSwarm(swarm);
1962
+ this._connectionLog?.joinedSwarm(swarm);
1681
1963
  (0, import_log7.log)("joined", {
1682
1964
  topic: import_keys8.PublicKey.from(topic),
1683
1965
  count: this._swarms.size
1684
1966
  }, {
1685
1967
  F: __dxlog_file7,
1686
- L: 192,
1968
+ L: 191,
1687
1969
  S: this,
1688
1970
  C: (f, a) => f(...a)
1689
1971
  });
@@ -1695,7 +1977,6 @@ var NetworkManager = class {
1695
1977
  * Close the connection.
1696
1978
  */
1697
1979
  async leaveSwarm(topic) {
1698
- var _a;
1699
1980
  if (!this._swarms.has(topic)) {
1700
1981
  return;
1701
1982
  }
@@ -1703,7 +1984,7 @@ var NetworkManager = class {
1703
1984
  topic: import_keys8.PublicKey.from(topic)
1704
1985
  }, {
1705
1986
  F: __dxlog_file7,
1706
- L: 208,
1987
+ L: 207,
1707
1988
  S: this,
1708
1989
  C: (f, a) => f(...a)
1709
1990
  });
@@ -1715,7 +1996,7 @@ var NetworkManager = class {
1715
1996
  const map = this._mappers.get(topic);
1716
1997
  map.destroy();
1717
1998
  this._mappers.delete(topic);
1718
- (_a = this._connectionLog) == null ? void 0 : _a.leftSwarm(swarm);
1999
+ this._connectionLog?.leftSwarm(swarm);
1719
2000
  await swarm.destroy();
1720
2001
  this._swarms.delete(topic);
1721
2002
  await this.topicsUpdated.emit();
@@ -1724,7 +2005,7 @@ var NetworkManager = class {
1724
2005
  count: this._swarms.size
1725
2006
  }, {
1726
2007
  F: __dxlog_file7,
1727
- L: 222,
2008
+ L: 221,
1728
2009
  S: this,
1729
2010
  C: (f, a) => f(...a)
1730
2011
  });
@@ -1758,17 +2039,34 @@ var NetworkManager = class {
1758
2039
  };
1759
2040
 
1760
2041
  // packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts
1761
- var import_tiny_invariant6 = __toESM(require("tiny-invariant"));
2042
+ var import_invariant7 = require("@dxos/invariant");
2043
+ var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
1762
2044
  var FullyConnectedTopology = class {
1763
2045
  toString() {
1764
2046
  return "FullyConnectedTopology";
1765
2047
  }
1766
2048
  init(controller) {
1767
- (0, import_tiny_invariant6.default)(!this._controller, "Already initialized");
2049
+ (0, import_invariant7.invariant)(!this._controller, "Already initialized", {
2050
+ F: __dxlog_file8,
2051
+ L: 18,
2052
+ S: this,
2053
+ A: [
2054
+ "!this._controller",
2055
+ "'Already initialized'"
2056
+ ]
2057
+ });
1768
2058
  this._controller = controller;
1769
2059
  }
1770
2060
  update() {
1771
- (0, import_tiny_invariant6.default)(this._controller, "Not initialized");
2061
+ (0, import_invariant7.invariant)(this._controller, "Not initialized", {
2062
+ F: __dxlog_file8,
2063
+ L: 23,
2064
+ S: this,
2065
+ A: [
2066
+ "this._controller",
2067
+ "'Not initialized'"
2068
+ ]
2069
+ });
1772
2070
  const { candidates: discovered } = this._controller.getState();
1773
2071
  for (const peer of discovered) {
1774
2072
  this._controller.connect(peer);
@@ -1782,10 +2080,10 @@ var FullyConnectedTopology = class {
1782
2080
  };
1783
2081
 
1784
2082
  // packages/core/mesh/network-manager/src/topology/mmst-topology.ts
1785
- var import_tiny_invariant7 = __toESM(require("tiny-invariant"));
1786
2083
  var import_xor_distance = __toESM(require("xor-distance"));
2084
+ var import_invariant8 = require("@dxos/invariant");
1787
2085
  var import_log8 = require("@dxos/log");
1788
- var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/mmst-topology.ts";
2086
+ var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/mmst-topology.ts";
1789
2087
  var MMSTTopology = class {
1790
2088
  constructor({ originateConnections = 2, maxPeers = 4, sampleSize = 10 } = {}) {
1791
2089
  this._sampleCollected = false;
@@ -1794,15 +2092,31 @@ var MMSTTopology = class {
1794
2092
  this._sampleSize = sampleSize;
1795
2093
  }
1796
2094
  init(controller) {
1797
- (0, import_tiny_invariant7.default)(!this._controller, "Already initialized");
2095
+ (0, import_invariant8.invariant)(!this._controller, "Already initialized", {
2096
+ F: __dxlog_file9,
2097
+ L: 46,
2098
+ S: this,
2099
+ A: [
2100
+ "!this._controller",
2101
+ "'Already initialized'"
2102
+ ]
2103
+ });
1798
2104
  this._controller = controller;
1799
2105
  }
1800
2106
  update() {
1801
- (0, import_tiny_invariant7.default)(this._controller, "Not initialized");
2107
+ (0, import_invariant8.invariant)(this._controller, "Not initialized", {
2108
+ F: __dxlog_file9,
2109
+ L: 51,
2110
+ S: this,
2111
+ A: [
2112
+ "this._controller",
2113
+ "'Not initialized'"
2114
+ ]
2115
+ });
1802
2116
  const { connected, candidates } = this._controller.getState();
1803
2117
  if (this._sampleCollected || connected.length > this._maxPeers || candidates.length > 0) {
1804
2118
  (0, import_log8.log)("Running the algorithm.", void 0, {
1805
- F: __dxlog_file8,
2119
+ F: __dxlog_file9,
1806
2120
  L: 55,
1807
2121
  S: this,
1808
2122
  C: (f, a) => f(...a)
@@ -1812,11 +2126,19 @@ var MMSTTopology = class {
1812
2126
  }
1813
2127
  }
1814
2128
  async onOffer(peer) {
1815
- (0, import_tiny_invariant7.default)(this._controller, "Not initialized");
2129
+ (0, import_invariant8.invariant)(this._controller, "Not initialized", {
2130
+ F: __dxlog_file9,
2131
+ L: 62,
2132
+ S: this,
2133
+ A: [
2134
+ "this._controller",
2135
+ "'Not initialized'"
2136
+ ]
2137
+ });
1816
2138
  const { connected } = this._controller.getState();
1817
2139
  const accept = connected.length < this._maxPeers;
1818
2140
  (0, import_log8.log)(`Offer ${peer} accept=${accept}`, void 0, {
1819
- F: __dxlog_file8,
2141
+ F: __dxlog_file9,
1820
2142
  L: 65,
1821
2143
  S: this,
1822
2144
  C: (f, a) => f(...a)
@@ -1826,13 +2148,21 @@ var MMSTTopology = class {
1826
2148
  async destroy() {
1827
2149
  }
1828
2150
  _runAlgorithm() {
1829
- (0, import_tiny_invariant7.default)(this._controller, "Not initialized");
2151
+ (0, import_invariant8.invariant)(this._controller, "Not initialized", {
2152
+ F: __dxlog_file9,
2153
+ L: 74,
2154
+ S: this,
2155
+ A: [
2156
+ "this._controller",
2157
+ "'Not initialized'"
2158
+ ]
2159
+ });
1830
2160
  const { connected, candidates, ownPeerId } = this._controller.getState();
1831
2161
  if (connected.length > this._maxPeers) {
1832
2162
  const sorted = sortByXorDistance(connected, ownPeerId).reverse().slice(0, this._maxPeers - connected.length);
1833
2163
  for (const peer of sorted) {
1834
2164
  (0, import_log8.log)(`Disconnect ${peer}.`, void 0, {
1835
- F: __dxlog_file8,
2165
+ F: __dxlog_file9,
1836
2166
  L: 83,
1837
2167
  S: this,
1838
2168
  C: (f, a) => f(...a)
@@ -1844,7 +2174,7 @@ var MMSTTopology = class {
1844
2174
  const sorted = sortByXorDistance(sample, ownPeerId).slice(0, this._originateConnections - connected.length);
1845
2175
  for (const peer of sorted) {
1846
2176
  (0, import_log8.log)(`Connect ${peer}.`, void 0, {
1847
- F: __dxlog_file8,
2177
+ F: __dxlog_file9,
1848
2178
  L: 91,
1849
2179
  S: this,
1850
2180
  C: (f, a) => f(...a)
@@ -1860,9 +2190,9 @@ var MMSTTopology = class {
1860
2190
  var sortByXorDistance = (keys, reference) => keys.sort((a, b) => import_xor_distance.default.gt((0, import_xor_distance.default)(a.asBuffer(), reference.asBuffer()), (0, import_xor_distance.default)(b.asBuffer(), reference.asBuffer())));
1861
2191
 
1862
2192
  // packages/core/mesh/network-manager/src/topology/star-topology.ts
1863
- var import_tiny_invariant8 = __toESM(require("tiny-invariant"));
2193
+ var import_invariant9 = require("@dxos/invariant");
1864
2194
  var import_log9 = require("@dxos/log");
1865
- var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
2195
+ var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
1866
2196
  var StarTopology = class {
1867
2197
  // prettier-ignore
1868
2198
  constructor(_centralPeer) {
@@ -1872,16 +2202,32 @@ var StarTopology = class {
1872
2202
  return `StarTopology(${this._centralPeer.truncate()})`;
1873
2203
  }
1874
2204
  init(controller) {
1875
- (0, import_tiny_invariant8.default)(!this._controller, "Already initialized.");
2205
+ (0, import_invariant9.invariant)(!this._controller, "Already initialized.", {
2206
+ F: __dxlog_file10,
2207
+ L: 24,
2208
+ S: this,
2209
+ A: [
2210
+ "!this._controller",
2211
+ "'Already initialized.'"
2212
+ ]
2213
+ });
1876
2214
  this._controller = controller;
1877
2215
  }
1878
2216
  update() {
1879
- (0, import_tiny_invariant8.default)(this._controller, "Not initialized.");
2217
+ (0, import_invariant9.invariant)(this._controller, "Not initialized.", {
2218
+ F: __dxlog_file10,
2219
+ L: 29,
2220
+ S: this,
2221
+ A: [
2222
+ "this._controller",
2223
+ "'Not initialized.'"
2224
+ ]
2225
+ });
1880
2226
  const { candidates, connected, ownPeerId } = this._controller.getState();
1881
2227
  if (!ownPeerId.equals(this._centralPeer)) {
1882
2228
  (0, import_log9.log)("leaf peer dropping all connections apart from central peer.", void 0, {
1883
- F: __dxlog_file9,
1884
- L: 33,
2229
+ F: __dxlog_file10,
2230
+ L: 32,
1885
2231
  S: this,
1886
2232
  C: (f, a) => f(...a)
1887
2233
  });
@@ -1890,8 +2236,8 @@ var StarTopology = class {
1890
2236
  (0, import_log9.log)("dropping connection", {
1891
2237
  peer
1892
2238
  }, {
1893
- F: __dxlog_file9,
1894
- L: 38,
2239
+ F: __dxlog_file10,
2240
+ L: 37,
1895
2241
  S: this,
1896
2242
  C: (f, a) => f(...a)
1897
2243
  });
@@ -1904,8 +2250,8 @@ var StarTopology = class {
1904
2250
  (0, import_log9.log)("connecting to peer", {
1905
2251
  peer
1906
2252
  }, {
1907
- F: __dxlog_file9,
1908
- L: 47,
2253
+ F: __dxlog_file10,
2254
+ L: 46,
1909
2255
  S: this,
1910
2256
  C: (f, a) => f(...a)
1911
2257
  });
@@ -1914,15 +2260,23 @@ var StarTopology = class {
1914
2260
  }
1915
2261
  }
1916
2262
  async onOffer(peer) {
1917
- (0, import_tiny_invariant8.default)(this._controller, "Not initialized.");
2263
+ (0, import_invariant9.invariant)(this._controller, "Not initialized.", {
2264
+ F: __dxlog_file10,
2265
+ L: 53,
2266
+ S: this,
2267
+ A: [
2268
+ "this._controller",
2269
+ "'Not initialized.'"
2270
+ ]
2271
+ });
1918
2272
  const { ownPeerId } = this._controller.getState();
1919
2273
  (0, import_log9.log)("offer", {
1920
2274
  peer,
1921
2275
  isCentral: peer.equals(this._centralPeer),
1922
2276
  isSelfCentral: ownPeerId.equals(this._centralPeer)
1923
2277
  }, {
1924
- F: __dxlog_file9,
1925
- L: 56,
2278
+ F: __dxlog_file10,
2279
+ L: 55,
1926
2280
  S: this,
1927
2281
  C: (f, a) => f(...a)
1928
2282
  });
@@ -1934,9 +2288,9 @@ var StarTopology = class {
1934
2288
 
1935
2289
  // packages/core/mesh/network-manager/src/transport/memory-transport.ts
1936
2290
  var import_node_stream = require("node:stream");
1937
- var import_tiny_invariant9 = __toESM(require("tiny-invariant"));
1938
2291
  var import_async8 = require("@dxos/async");
1939
2292
  var import_debug4 = require("@dxos/debug");
2293
+ var import_invariant10 = require("@dxos/invariant");
1940
2294
  var import_keys9 = require("@dxos/keys");
1941
2295
  var import_log10 = require("@dxos/log");
1942
2296
  var import_util7 = require("@dxos/util");
@@ -1950,7 +2304,7 @@ function _ts_decorate4(decorators, target, key, desc) {
1950
2304
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1951
2305
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1952
2306
  }
1953
- var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
2307
+ var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
1954
2308
  var MEMORY_TRANSPORT_DELAY = 1;
1955
2309
  var createStreamDelay = (delay) => {
1956
2310
  return new import_node_stream.Transform({
@@ -1963,9 +2317,12 @@ var createStreamDelay = (delay) => {
1963
2317
  var MemoryTransportFactory = {
1964
2318
  createTransport: (params) => new MemoryTransport(params)
1965
2319
  };
1966
- var _MemoryTransport = class {
2320
+ var MemoryTransport = class _MemoryTransport {
2321
+ static {
2322
+ // TODO(burdon): Remove static properties (inject context into constructor).
2323
+ this._connections = new import_util7.ComplexMap(import_keys9.PublicKey.hash);
2324
+ }
1967
2325
  constructor(options) {
1968
- var _a;
1969
2326
  this.options = options;
1970
2327
  this.closed = new import_async8.Event();
1971
2328
  this.connected = new import_async8.Event();
@@ -1976,17 +2333,25 @@ var _MemoryTransport = class {
1976
2333
  this._incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
1977
2334
  this._destroyed = false;
1978
2335
  (0, import_log10.log)("creating", void 0, {
1979
- F: __dxlog_file10,
2336
+ F: __dxlog_file11,
1980
2337
  L: 64,
1981
2338
  S: this,
1982
2339
  C: (f, a) => f(...a)
1983
2340
  });
1984
- (0, import_tiny_invariant9.default)(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection");
2341
+ (0, import_invariant10.invariant)(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection", {
2342
+ F: __dxlog_file11,
2343
+ L: 66,
2344
+ S: this,
2345
+ A: [
2346
+ "!MemoryTransport._connections.has(this._instanceId)",
2347
+ "'Duplicate memory connection'"
2348
+ ]
2349
+ });
1985
2350
  _MemoryTransport._connections.set(this._instanceId, this);
1986
2351
  if (this.options.initiator) {
1987
2352
  setTimeout(async () => {
1988
2353
  (0, import_log10.log)("sending signal", void 0, {
1989
- F: __dxlog_file10,
2354
+ F: __dxlog_file11,
1990
2355
  L: 73,
1991
2356
  S: this,
1992
2357
  C: (f, a) => f(...a)
@@ -2003,7 +2368,7 @@ var _MemoryTransport = class {
2003
2368
  });
2004
2369
  } else {
2005
2370
  this._remote.wait({
2006
- timeout: (_a = this.options.timeout) != null ? _a : 1e3
2371
+ timeout: this.options.timeout ?? 1e3
2007
2372
  }).then((remoteId) => {
2008
2373
  if (this._destroyed) {
2009
2374
  return;
@@ -2015,11 +2380,19 @@ var _MemoryTransport = class {
2015
2380
  this.closed.emit();
2016
2381
  return;
2017
2382
  }
2018
- (0, import_tiny_invariant9.default)(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`);
2383
+ (0, import_invariant10.invariant)(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`, {
2384
+ F: __dxlog_file11,
2385
+ L: 99,
2386
+ S: this,
2387
+ A: [
2388
+ "!this._remoteConnection._remoteConnection",
2389
+ "`Remote already connected: ${this._remoteInstanceId}`"
2390
+ ]
2391
+ });
2019
2392
  this._remoteConnection._remoteConnection = this;
2020
2393
  this._remoteConnection._remoteInstanceId = this._instanceId;
2021
2394
  (0, import_log10.log)("connected", void 0, {
2022
- F: __dxlog_file10,
2395
+ F: __dxlog_file11,
2023
2396
  L: 103,
2024
2397
  S: this,
2025
2398
  C: (f, a) => f(...a)
@@ -2037,7 +2410,7 @@ var _MemoryTransport = class {
2037
2410
  }
2038
2411
  async destroy() {
2039
2412
  (0, import_log10.log)("closing", void 0, {
2040
- F: __dxlog_file10,
2413
+ F: __dxlog_file11,
2041
2414
  L: 124,
2042
2415
  S: this,
2043
2416
  C: (f, a) => f(...a)
@@ -2046,7 +2419,7 @@ var _MemoryTransport = class {
2046
2419
  _MemoryTransport._connections.delete(this._instanceId);
2047
2420
  if (this._remoteConnection) {
2048
2421
  (0, import_log10.log)("closing", void 0, {
2049
- F: __dxlog_file10,
2422
+ F: __dxlog_file11,
2050
2423
  L: 129,
2051
2424
  S: this,
2052
2425
  C: (f, a) => f(...a)
@@ -2059,7 +2432,7 @@ var _MemoryTransport = class {
2059
2432
  this._remoteConnection._remoteConnection = void 0;
2060
2433
  this._remoteConnection = void 0;
2061
2434
  (0, import_log10.log)("closed", void 0, {
2062
- F: __dxlog_file10,
2435
+ F: __dxlog_file11,
2063
2436
  L: 147,
2064
2437
  S: this,
2065
2438
  C: (f, a) => f(...a)
@@ -2067,7 +2440,7 @@ var _MemoryTransport = class {
2067
2440
  }
2068
2441
  this.closed.emit();
2069
2442
  (0, import_log10.log)("closed", void 0, {
2070
- F: __dxlog_file10,
2443
+ F: __dxlog_file11,
2071
2444
  L: 151,
2072
2445
  S: this,
2073
2446
  C: (f, a) => f(...a)
@@ -2077,12 +2450,12 @@ var _MemoryTransport = class {
2077
2450
  (0, import_log10.log)("received signal", {
2078
2451
  payload
2079
2452
  }, {
2080
- F: __dxlog_file10,
2453
+ F: __dxlog_file11,
2081
2454
  L: 155,
2082
2455
  S: this,
2083
2456
  C: (f, a) => f(...a)
2084
2457
  });
2085
- if (!(payload == null ? void 0 : payload.transportId)) {
2458
+ if (!payload?.transportId) {
2086
2459
  return;
2087
2460
  }
2088
2461
  const transportId = payload.transportId;
@@ -2092,9 +2465,6 @@ var _MemoryTransport = class {
2092
2465
  }
2093
2466
  }
2094
2467
  };
2095
- var MemoryTransport = _MemoryTransport;
2096
- // TODO(burdon): Remove static properties (inject context into constructor).
2097
- MemoryTransport._connections = new import_util7.ComplexMap(import_keys9.PublicKey.hash);
2098
2468
  _ts_decorate4([
2099
2469
  import_log10.logInfo
2100
2470
  ], MemoryTransport.prototype, "_instanceId", void 0);
@@ -2104,9 +2474,9 @@ _ts_decorate4([
2104
2474
 
2105
2475
  // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
2106
2476
  var import_simple_peer = __toESM(require("simple-peer"));
2107
- var import_tiny_invariant10 = __toESM(require("tiny-invariant"));
2108
2477
  var import_async9 = require("@dxos/async");
2109
2478
  var import_debug5 = require("@dxos/debug");
2479
+ var import_invariant11 = require("@dxos/invariant");
2110
2480
  var import_keys10 = require("@dxos/keys");
2111
2481
  var import_log11 = require("@dxos/log");
2112
2482
  var import_protocols5 = require("@dxos/protocols");
@@ -2115,14 +2485,13 @@ var import_protocols5 = require("@dxos/protocols");
2115
2485
  var wrtc = null;
2116
2486
  try {
2117
2487
  wrtc = require("@koush/wrtc");
2118
- } catch (e) {
2488
+ } catch {
2119
2489
  }
2120
2490
 
2121
2491
  // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
2122
- var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
2492
+ var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
2123
2493
  var WebRTCTransport = class {
2124
2494
  constructor(params) {
2125
- var _a;
2126
2495
  this.params = params;
2127
2496
  this._closed = false;
2128
2497
  this.closed = new import_async9.Event();
@@ -2132,25 +2501,25 @@ var WebRTCTransport = class {
2132
2501
  import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.begin({
2133
2502
  id: this._instanceId
2134
2503
  }), {
2135
- F: __dxlog_file11,
2504
+ F: __dxlog_file12,
2136
2505
  L: 39,
2137
2506
  S: this,
2138
2507
  C: (f, a) => f(...a)
2139
2508
  });
2140
2509
  (0, import_log11.log)("created connection", params, {
2141
- F: __dxlog_file11,
2510
+ F: __dxlog_file12,
2142
2511
  L: 40,
2143
2512
  S: this,
2144
2513
  C: (f, a) => f(...a)
2145
2514
  });
2146
2515
  this._peer = new import_simple_peer.default({
2147
2516
  initiator: this.params.initiator,
2148
- wrtc: import_simple_peer.default.WEBRTC_SUPPORT ? void 0 : (_a = wrtc) != null ? _a : (0, import_debug5.raise)(new Error("wrtc not available")),
2517
+ wrtc: import_simple_peer.default.WEBRTC_SUPPORT ? void 0 : wrtc ?? (0, import_debug5.raise)(new Error("wrtc not available")),
2149
2518
  config: this.params.webrtcConfig
2150
2519
  });
2151
2520
  this._peer.on("signal", async (data) => {
2152
2521
  (0, import_log11.log)("signal", data, {
2153
- F: __dxlog_file11,
2522
+ F: __dxlog_file12,
2154
2523
  L: 48,
2155
2524
  S: this,
2156
2525
  C: (f, a) => f(...a)
@@ -2163,7 +2532,7 @@ var WebRTCTransport = class {
2163
2532
  });
2164
2533
  this._peer.on("connect", () => {
2165
2534
  (0, import_log11.log)("connected", void 0, {
2166
- F: __dxlog_file11,
2535
+ F: __dxlog_file12,
2167
2536
  L: 53,
2168
2537
  S: this,
2169
2538
  C: (f, a) => f(...a)
@@ -2173,7 +2542,7 @@ var WebRTCTransport = class {
2173
2542
  });
2174
2543
  this._peer.on("close", async () => {
2175
2544
  (0, import_log11.log)("closed", void 0, {
2176
- F: __dxlog_file11,
2545
+ F: __dxlog_file12,
2177
2546
  L: 59,
2178
2547
  S: this,
2179
2548
  C: (f, a) => f(...a)
@@ -2182,30 +2551,29 @@ var WebRTCTransport = class {
2182
2551
  this.closed.emit();
2183
2552
  });
2184
2553
  this._peer.on("error", async (err) => {
2185
- var _a2;
2186
2554
  this.errors.raise(err);
2187
2555
  try {
2188
- if (typeof ((_a2 = this._peer) == null ? void 0 : _a2._pc.getStats) === "function") {
2556
+ if (typeof this._peer?._pc.getStats === "function") {
2189
2557
  this._peer._pc.getStats().then((stats) => {
2190
2558
  import_log11.log.warn("report after webrtc error", {
2191
2559
  config: this.params.webrtcConfig,
2192
2560
  stats
2193
2561
  }, {
2194
- F: __dxlog_file11,
2562
+ F: __dxlog_file12,
2195
2563
  L: 71,
2196
2564
  S: this,
2197
2565
  C: (f, a) => f(...a)
2198
2566
  });
2199
2567
  });
2200
2568
  }
2201
- } catch (e) {
2569
+ } catch {
2202
2570
  }
2203
2571
  await this.destroy();
2204
2572
  });
2205
2573
  import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.end({
2206
2574
  id: this._instanceId
2207
2575
  }), {
2208
- F: __dxlog_file11,
2576
+ F: __dxlog_file12,
2209
2577
  L: 81,
2210
2578
  S: this,
2211
2579
  C: (f, a) => f(...a)
@@ -2213,7 +2581,7 @@ var WebRTCTransport = class {
2213
2581
  }
2214
2582
  async destroy() {
2215
2583
  (0, import_log11.log)("closing...", void 0, {
2216
- F: __dxlog_file11,
2584
+ F: __dxlog_file12,
2217
2585
  L: 85,
2218
2586
  S: this,
2219
2587
  C: (f, a) => f(...a)
@@ -2223,7 +2591,7 @@ var WebRTCTransport = class {
2223
2591
  this._peer.destroy();
2224
2592
  this.closed.emit();
2225
2593
  (0, import_log11.log)("closed", void 0, {
2226
- F: __dxlog_file11,
2594
+ F: __dxlog_file12,
2227
2595
  L: 90,
2228
2596
  S: this,
2229
2597
  C: (f, a) => f(...a)
@@ -2233,12 +2601,19 @@ var WebRTCTransport = class {
2233
2601
  if (this._closed) {
2234
2602
  return;
2235
2603
  }
2236
- (0, import_tiny_invariant10.default)(signal.payload.data, "Signal message must contain signal data.");
2604
+ (0, import_invariant11.invariant)(signal.payload.data, "Signal message must contain signal data.", {
2605
+ F: __dxlog_file12,
2606
+ L: 98,
2607
+ S: this,
2608
+ A: [
2609
+ "signal.payload.data",
2610
+ "'Signal message must contain signal data.'"
2611
+ ]
2612
+ });
2237
2613
  this._peer.signal(signal.payload.data);
2238
2614
  }
2239
2615
  async _disconnectStreams() {
2240
- var _a, _b, _c, _d;
2241
- (_d = (_c = (_b = (_a = this.params.stream).unpipe) == null ? void 0 : _b.call(_a, this._peer)) == null ? void 0 : _c.unpipe) == null ? void 0 : _d.call(_c, this.params.stream);
2616
+ this.params.stream.unpipe?.(this._peer)?.unpipe?.(this.params.stream);
2242
2617
  }
2243
2618
  };
2244
2619
  var createWebRTCTransportFactory = (webrtcConfig) => ({
@@ -2250,13 +2625,13 @@ var createWebRTCTransportFactory = (webrtcConfig) => ({
2250
2625
 
2251
2626
  // packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts
2252
2627
  var import_node_stream2 = require("node:stream");
2253
- var import_tiny_invariant11 = __toESM(require("tiny-invariant"));
2254
2628
  var import_codec_protobuf = require("@dxos/codec-protobuf");
2629
+ var import_invariant12 = require("@dxos/invariant");
2255
2630
  var import_keys11 = require("@dxos/keys");
2256
2631
  var import_log12 = require("@dxos/log");
2257
2632
  var import_bridge = require("@dxos/protocols/proto/dxos/mesh/bridge");
2258
2633
  var import_util8 = require("@dxos/util");
2259
- var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts";
2634
+ var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts";
2260
2635
  var WebRTCTransportService = class {
2261
2636
  // prettier-ignore
2262
2637
  constructor(_webrtcConfig) {
@@ -2267,6 +2642,13 @@ var WebRTCTransportService = class {
2267
2642
  const rpcStream = new import_codec_protobuf.Stream(({ ready, next, close }) => {
2268
2643
  const duplex = new import_node_stream2.Duplex({
2269
2644
  read: () => {
2645
+ const callbacks = [
2646
+ ...transportState.writeCallbacks
2647
+ ];
2648
+ transportState.writeCallbacks.length = 0;
2649
+ for (const cb of callbacks) {
2650
+ cb();
2651
+ }
2270
2652
  },
2271
2653
  write: function(chunk, _, callback) {
2272
2654
  next({
@@ -2318,30 +2700,53 @@ var WebRTCTransportService = class {
2318
2700
  });
2319
2701
  close();
2320
2702
  });
2321
- ready();
2322
- this.transports.set(request.proxyId, {
2703
+ const transportState = {
2323
2704
  transport,
2324
- stream: duplex
2325
- });
2705
+ stream: duplex,
2706
+ writeCallbacks: []
2707
+ };
2708
+ ready();
2709
+ this.transports.set(request.proxyId, transportState);
2326
2710
  });
2327
2711
  return rpcStream;
2328
2712
  }
2329
2713
  async sendSignal({ proxyId, signal }) {
2330
- (0, import_tiny_invariant11.default)(this.transports.has(proxyId));
2714
+ (0, import_invariant12.invariant)(this.transports.has(proxyId), void 0, {
2715
+ F: __dxlog_file13,
2716
+ L: 113,
2717
+ S: this,
2718
+ A: [
2719
+ "this.transports.has(proxyId)",
2720
+ ""
2721
+ ]
2722
+ });
2331
2723
  await this.transports.get(proxyId).transport.signal(signal);
2332
2724
  }
2333
2725
  async sendData({ proxyId, payload }) {
2334
- (0, import_tiny_invariant11.default)(this.transports.has(proxyId));
2335
- await this.transports.get(proxyId).stream.push(payload);
2726
+ (0, import_invariant12.invariant)(this.transports.has(proxyId), void 0, {
2727
+ F: __dxlog_file13,
2728
+ L: 118,
2729
+ S: this,
2730
+ A: [
2731
+ "this.transports.has(proxyId)",
2732
+ ""
2733
+ ]
2734
+ });
2735
+ const state = this.transports.get(proxyId);
2736
+ const bufferHasSpace = state.stream.push(payload);
2737
+ if (!bufferHasSpace) {
2738
+ await new Promise((resolve) => {
2739
+ state.writeCallbacks.push(resolve);
2740
+ });
2741
+ }
2336
2742
  }
2337
2743
  async close({ proxyId }) {
2338
- var _a, _b;
2339
- await ((_a = this.transports.get(proxyId)) == null ? void 0 : _a.transport.destroy());
2340
- await ((_b = this.transports.get(proxyId)) == null ? void 0 : _b.stream.end());
2744
+ await this.transports.get(proxyId)?.transport.destroy();
2745
+ await this.transports.get(proxyId)?.stream.end();
2341
2746
  this.transports.delete(proxyId);
2342
2747
  (0, import_log12.log)("Closed.", void 0, {
2343
- F: __dxlog_file12,
2344
- L: 109,
2748
+ F: __dxlog_file13,
2749
+ L: 132,
2345
2750
  S: this,
2346
2751
  C: (f, a) => f(...a)
2347
2752
  });
@@ -2349,15 +2754,16 @@ var WebRTCTransportService = class {
2349
2754
  };
2350
2755
 
2351
2756
  // packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts
2352
- var import_tiny_invariant12 = __toESM(require("tiny-invariant"));
2757
+ var import_node_stream3 = require("node:stream");
2353
2758
  var import_async10 = require("@dxos/async");
2354
2759
  var import_context6 = require("@dxos/context");
2355
2760
  var import_debug6 = require("@dxos/debug");
2761
+ var import_invariant13 = require("@dxos/invariant");
2356
2762
  var import_keys12 = require("@dxos/keys");
2357
2763
  var import_log13 = require("@dxos/log");
2358
2764
  var import_bridge2 = require("@dxos/protocols/proto/dxos/mesh/bridge");
2359
2765
  var import_util9 = require("@dxos/util");
2360
- var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts";
2766
+ var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts";
2361
2767
  var WebRTCTransportProxy = class {
2362
2768
  // prettier-ignore
2363
2769
  constructor(_params) {
@@ -2375,8 +2781,8 @@ var WebRTCTransportProxy = class {
2375
2781
  this._serviceStream.waitUntilReady().then(() => {
2376
2782
  this._serviceStream.subscribe(async (event) => {
2377
2783
  (0, import_log13.log)("WebRTCTransportProxy: event", event, {
2378
- F: __dxlog_file13,
2379
- L: 50,
2784
+ F: __dxlog_file14,
2785
+ L: 51,
2380
2786
  S: this,
2381
2787
  C: (f, a) => f(...a)
2382
2788
  });
@@ -2388,28 +2794,24 @@ var WebRTCTransportProxy = class {
2388
2794
  await this._handleSignal(event.signal);
2389
2795
  }
2390
2796
  });
2391
- const dataListener = async (data) => {
2392
- try {
2393
- await this._params.bridgeService.sendData({
2797
+ this._params.stream.pipe(new import_node_stream3.Writable({
2798
+ write: (chunk, _, callback) => {
2799
+ this._params.bridgeService.sendData({
2394
2800
  proxyId: this._proxyId,
2395
- payload: data
2396
- });
2397
- } catch (err) {
2398
- import_log13.log.catch(err, void 0, {
2399
- F: __dxlog_file13,
2400
- L: 67,
2401
- S: this,
2402
- C: (f, a) => f(...a)
2801
+ payload: chunk
2802
+ }).then(() => callback(), (err) => {
2803
+ import_log13.log.catch(err, void 0, {
2804
+ F: __dxlog_file14,
2805
+ L: 69,
2806
+ S: this,
2807
+ C: (f, a) => f(...a)
2808
+ });
2403
2809
  });
2404
2810
  }
2405
- };
2406
- this._params.stream.on("data", dataListener);
2407
- this._ctx.onDispose(() => {
2408
- this._params.stream.off("data", dataListener);
2409
- });
2811
+ }));
2410
2812
  }, (error) => import_log13.log.catch(error, void 0, {
2411
- F: __dxlog_file13,
2412
- L: 73,
2813
+ F: __dxlog_file14,
2814
+ L: 75,
2413
2815
  S: this,
2414
2816
  C: (f, a) => f(...a)
2415
2817
  }));
@@ -2454,8 +2856,8 @@ var WebRTCTransportProxy = class {
2454
2856
  });
2455
2857
  } catch (err) {
2456
2858
  import_log13.log.catch(err, void 0, {
2457
- F: __dxlog_file13,
2458
- L: 124,
2859
+ F: __dxlog_file14,
2860
+ L: 126,
2459
2861
  S: this,
2460
2862
  C: (f, a) => f(...a)
2461
2863
  });
@@ -2489,7 +2891,15 @@ var WebRTCTransportProxyFactory = class {
2489
2891
  return this;
2490
2892
  }
2491
2893
  createTransport(options) {
2492
- (0, import_tiny_invariant12.default)(this._bridgeService, "WebRTCTransportProxyFactory is not ready to open connections");
2894
+ (0, import_invariant13.invariant)(this._bridgeService, "WebRTCTransportProxyFactory is not ready to open connections", {
2895
+ F: __dxlog_file14,
2896
+ L: 163,
2897
+ S: this,
2898
+ A: [
2899
+ "this._bridgeService",
2900
+ "'WebRTCTransportProxyFactory is not ready to open connections'"
2901
+ ]
2902
+ });
2493
2903
  const transport = new WebRTCTransportProxy({
2494
2904
  ...options,
2495
2905
  bridgeService: this._bridgeService