@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
@@ -4,15 +4,15 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
4
4
  }) : x)(function(x) {
5
5
  if (typeof require !== "undefined")
6
6
  return require.apply(this, arguments);
7
- throw new Error('Dynamic require of "' + x + '" is not supported');
7
+ throw Error('Dynamic require of "' + x + '" is not supported');
8
8
  });
9
9
 
10
10
  // packages/core/mesh/network-manager/src/swarm/connection.ts
11
- import invariant from "tiny-invariant";
12
11
  import { DeferredTask, Event, sleep, synchronized } from "@dxos/async";
13
12
  import { Context, cancelWithContext } from "@dxos/context";
14
13
  import { ErrorStream } from "@dxos/debug";
15
14
  import { CancelledError } from "@dxos/errors";
15
+ import { invariant } from "@dxos/invariant";
16
16
  import { PublicKey } from "@dxos/keys";
17
17
  import { log } from "@dxos/log";
18
18
  import { trace } from "@dxos/protocols";
@@ -84,6 +84,18 @@ var Connection = class {
84
84
  await this._flushSignalBuffer();
85
85
  });
86
86
  this._signallingDelay = STARTING_SIGNALLING_DELAY;
87
+ log.trace("dxos.mesh.connection.construct", {
88
+ sessionId: this.sessionId,
89
+ topic: this.topic,
90
+ localPeerId: this.ownId,
91
+ remotePeerId: this.remoteId,
92
+ initiator: this.initiator
93
+ }, {
94
+ F: __dxlog_file,
95
+ L: 95,
96
+ S: this,
97
+ C: (f, a) => f(...a)
98
+ });
87
99
  }
88
100
  get state() {
89
101
  return this._state;
@@ -98,12 +110,32 @@ var Connection = class {
98
110
  * Create an underlying transport and prepares it for the connection.
99
111
  */
100
112
  async openConnection() {
101
- invariant(this._state === ConnectionState.INITIAL, "Invalid state.");
113
+ invariant(this._state === ConnectionState.INITIAL, "Invalid state.", {
114
+ F: __dxlog_file,
115
+ L: 120,
116
+ S: this,
117
+ A: [
118
+ "this._state === ConnectionState.INITIAL",
119
+ "'Invalid state.'"
120
+ ]
121
+ });
102
122
  log.trace("dxos.mesh.connection.open-connection", trace.begin({
103
123
  id: this._instanceId
104
124
  }), {
105
125
  F: __dxlog_file,
106
- L: 114,
126
+ L: 121,
127
+ S: this,
128
+ C: (f, a) => f(...a)
129
+ });
130
+ log.trace("dxos.mesh.connection.open", {
131
+ sessionId: this.sessionId,
132
+ topic: this.topic,
133
+ localPeerId: this.ownId,
134
+ remotePeerId: this.remoteId,
135
+ initiator: this.initiator
136
+ }, {
137
+ F: __dxlog_file,
138
+ L: 122,
107
139
  S: this,
108
140
  C: (f, a) => f(...a)
109
141
  });
@@ -114,13 +146,21 @@ var Connection = class {
114
146
  this._protocol.stream.on("close", () => {
115
147
  log("protocol stream closed", void 0, {
116
148
  F: __dxlog_file,
117
- L: 125,
149
+ L: 139,
118
150
  S: this,
119
151
  C: (f, a) => f(...a)
120
152
  });
121
153
  this.close().catch((err) => this.errors.raise(err));
122
154
  });
123
- invariant(!this._transport);
155
+ invariant(!this._transport, void 0, {
156
+ F: __dxlog_file,
157
+ L: 143,
158
+ S: this,
159
+ A: [
160
+ "!this._transport",
161
+ ""
162
+ ]
163
+ });
124
164
  this._transport = this._transportFactory.createTransport({
125
165
  initiator: this.initiator,
126
166
  stream: this._protocol.stream,
@@ -146,13 +186,12 @@ var Connection = class {
146
186
  id: this._instanceId
147
187
  }), {
148
188
  F: __dxlog_file,
149
- L: 158,
189
+ L: 172,
150
190
  S: this,
151
191
  C: (f, a) => f(...a)
152
192
  });
153
193
  }
154
194
  async close() {
155
- var _a;
156
195
  if (this._state === ConnectionState.CLOSED) {
157
196
  return;
158
197
  }
@@ -162,7 +201,7 @@ var Connection = class {
162
201
  peerId: this.ownId
163
202
  }, {
164
203
  F: __dxlog_file,
165
- L: 170,
204
+ L: 184,
166
205
  S: this,
167
206
  C: (f, a) => f(...a)
168
207
  });
@@ -171,17 +210,17 @@ var Connection = class {
171
210
  } catch (err) {
172
211
  log.catch(err, void 0, {
173
212
  F: __dxlog_file,
174
- L: 176,
213
+ L: 190,
175
214
  S: this,
176
215
  C: (f, a) => f(...a)
177
216
  });
178
217
  }
179
218
  try {
180
- await ((_a = this._transport) == null ? void 0 : _a.destroy());
219
+ await this._transport?.destroy();
181
220
  } catch (err) {
182
221
  log.catch(err, void 0, {
183
222
  F: __dxlog_file,
184
- L: 183,
223
+ L: 197,
185
224
  S: this,
186
225
  C: (f, a) => f(...a)
187
226
  });
@@ -190,7 +229,7 @@ var Connection = class {
190
229
  peerId: this.ownId
191
230
  }, {
192
231
  F: __dxlog_file,
193
- L: 186,
232
+ L: 200,
194
233
  S: this,
195
234
  C: (f, a) => f(...a)
196
235
  });
@@ -230,7 +269,7 @@ var Connection = class {
230
269
  err
231
270
  }, {
232
271
  F: __dxlog_file,
233
- L: 220,
272
+ L: 234,
234
273
  S: this,
235
274
  C: (f, a) => f(...a)
236
275
  });
@@ -241,21 +280,52 @@ var Connection = class {
241
280
  * Receive a signal from the remote peer.
242
281
  */
243
282
  async signal(msg) {
244
- var _a, _b, _c;
245
- invariant(msg.sessionId);
283
+ invariant(msg.sessionId, void 0, {
284
+ F: __dxlog_file,
285
+ L: 243,
286
+ S: this,
287
+ A: [
288
+ "msg.sessionId",
289
+ ""
290
+ ]
291
+ });
246
292
  if (!msg.sessionId.equals(this.sessionId)) {
247
293
  log("dropping signal for incorrect session id", void 0, {
248
294
  F: __dxlog_file,
249
- L: 231,
295
+ L: 245,
250
296
  S: this,
251
297
  C: (f, a) => f(...a)
252
298
  });
253
299
  return;
254
300
  }
255
- invariant(msg.data.signal || msg.data.signalBatch);
256
- invariant((_a = msg.author) == null ? void 0 : _a.equals(this.remoteId));
257
- invariant((_b = msg.recipient) == null ? void 0 : _b.equals(this.ownId));
258
- const signals = msg.data.signalBatch ? (_c = msg.data.signalBatch.signals) != null ? _c : [] : [
301
+ invariant(msg.data.signal || msg.data.signalBatch, void 0, {
302
+ F: __dxlog_file,
303
+ L: 248,
304
+ S: this,
305
+ A: [
306
+ "msg.data.signal || msg.data.signalBatch",
307
+ ""
308
+ ]
309
+ });
310
+ invariant(msg.author?.equals(this.remoteId), void 0, {
311
+ F: __dxlog_file,
312
+ L: 249,
313
+ S: this,
314
+ A: [
315
+ "msg.author?.equals(this.remoteId)",
316
+ ""
317
+ ]
318
+ });
319
+ invariant(msg.recipient?.equals(this.ownId), void 0, {
320
+ F: __dxlog_file,
321
+ L: 250,
322
+ S: this,
323
+ A: [
324
+ "msg.recipient?.equals(this.ownId)",
325
+ ""
326
+ ]
327
+ });
328
+ const signals = msg.data.signalBatch ? msg.data.signalBatch.signals ?? [] : [
259
329
  msg.data.signal
260
330
  ];
261
331
  for (const signal of signals) {
@@ -269,20 +339,28 @@ var Connection = class {
269
339
  msg: msg.data
270
340
  }, {
271
341
  F: __dxlog_file,
272
- L: 245,
342
+ L: 259,
273
343
  S: this,
274
344
  C: (f, a) => f(...a)
275
345
  });
276
346
  this._incomingSignalBuffer.push(signal);
277
347
  } else {
278
- invariant(this._transport, "Connection not ready to accept signals.");
348
+ invariant(this._transport, "Connection not ready to accept signals.", {
349
+ F: __dxlog_file,
350
+ L: 262,
351
+ S: this,
352
+ A: [
353
+ "this._transport",
354
+ "'Connection not ready to accept signals.'"
355
+ ]
356
+ });
279
357
  log("received signal", {
280
358
  peerId: this.ownId,
281
359
  remoteId: this.remoteId,
282
360
  msg: msg.data
283
361
  }, {
284
362
  F: __dxlog_file,
285
- L: 249,
363
+ L: 263,
286
364
  S: this,
287
365
  C: (f, a) => f(...a)
288
366
  });
@@ -297,11 +375,19 @@ var Connection = class {
297
375
  peerId: this.ownId
298
376
  }, {
299
377
  F: __dxlog_file,
300
- L: 256,
378
+ L: 270,
301
379
  S: this,
302
380
  C: (f, a) => f(...a)
303
381
  });
304
- invariant(state !== this._state, "Already in this state.");
382
+ invariant(state !== this._state, "Already in this state.", {
383
+ F: __dxlog_file,
384
+ L: 271,
385
+ S: this,
386
+ A: [
387
+ "state !== this._state",
388
+ "'Already in this state.'"
389
+ ]
390
+ });
305
391
  this._state = state;
306
392
  this.stateChanged.emit(state);
307
393
  }
@@ -311,8 +397,8 @@ _ts_decorate([
311
397
  ], Connection.prototype, "close", null);
312
398
 
313
399
  // packages/core/mesh/network-manager/src/signal/swarm-messenger.ts
314
- import invariant2 from "tiny-invariant";
315
400
  import { Context as Context2 } from "@dxos/context";
401
+ import { invariant as invariant2 } from "@dxos/invariant";
316
402
  import { PublicKey as PublicKey2 } from "@dxos/keys";
317
403
  import { log as log2 } from "@dxos/log";
318
404
  import { schema } from "@dxos/protocols";
@@ -329,7 +415,6 @@ var SwarmMessenger = class {
329
415
  this._topic = topic;
330
416
  }
331
417
  async receiveMessage({ author, recipient, payload }) {
332
- var _a, _b, _c, _d;
333
418
  if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
334
419
  return;
335
420
  }
@@ -343,25 +428,25 @@ var SwarmMessenger = class {
343
428
  msg: message
344
429
  }, {
345
430
  F: __dxlog_file2,
346
- L: 70,
431
+ L: 69,
347
432
  S: this,
348
433
  C: (f, a) => f(...a)
349
434
  });
350
- if ((_a = message.data) == null ? void 0 : _a.offer) {
435
+ if (message.data?.offer) {
351
436
  await this._handleOffer({
352
437
  author,
353
438
  recipient,
354
439
  message
355
440
  });
356
- } else if ((_b = message.data) == null ? void 0 : _b.answer) {
441
+ } else if (message.data?.answer) {
357
442
  await this._resolveAnswers(message);
358
- } else if ((_c = message.data) == null ? void 0 : _c.signal) {
443
+ } else if (message.data?.signal) {
359
444
  await this._handleSignal({
360
445
  author,
361
446
  recipient,
362
447
  message
363
448
  });
364
- } else if ((_d = message.data) == null ? void 0 : _d.signalBatch) {
449
+ } else if (message.data?.signalBatch) {
365
450
  await this._handleSignal({
366
451
  author,
367
452
  recipient,
@@ -372,15 +457,22 @@ var SwarmMessenger = class {
372
457
  message
373
458
  }, {
374
459
  F: __dxlog_file2,
375
- L: 81,
460
+ L: 80,
376
461
  S: this,
377
462
  C: (f, a) => f(...a)
378
463
  });
379
464
  }
380
465
  }
381
466
  async signal(message) {
382
- var _a, _b;
383
- invariant2(((_a = message.data) == null ? void 0 : _a.signal) || ((_b = message.data) == null ? void 0 : _b.signalBatch), "Invalid message");
467
+ invariant2(message.data?.signal || message.data?.signalBatch, "Invalid message", {
468
+ F: __dxlog_file2,
469
+ L: 85,
470
+ S: this,
471
+ A: [
472
+ "message.data?.signal || message.data?.signalBatch",
473
+ "'Invalid message'"
474
+ ]
475
+ });
384
476
  await this._sendReliableMessage({
385
477
  author: message.author,
386
478
  recipient: message.recipient,
@@ -404,11 +496,10 @@ var SwarmMessenger = class {
404
496
  });
405
497
  }
406
498
  async _sendReliableMessage({ author, recipient, message }) {
407
- var _a;
408
499
  const networkMessage = {
409
500
  ...message,
410
501
  // Setting unique message_id if it not specified yet.
411
- messageId: (_a = message.messageId) != null ? _a : PublicKey2.random()
502
+ messageId: message.messageId ?? PublicKey2.random()
412
503
  };
413
504
  log2("sending", {
414
505
  from: author,
@@ -416,7 +507,7 @@ var SwarmMessenger = class {
416
507
  msg: networkMessage
417
508
  }, {
418
509
  F: __dxlog_file2,
419
- L: 124,
510
+ L: 123,
420
511
  S: this,
421
512
  C: (f, a) => f(...a)
422
513
  });
@@ -430,17 +521,32 @@ var SwarmMessenger = class {
430
521
  });
431
522
  }
432
523
  async _resolveAnswers(message) {
433
- var _a, _b, _c;
434
- invariant2((_b = (_a = message.data) == null ? void 0 : _a.answer) == null ? void 0 : _b.offerMessageId, "No offerMessageId");
524
+ invariant2(message.data?.answer?.offerMessageId, "No offerMessageId", {
525
+ F: __dxlog_file2,
526
+ L: 135,
527
+ S: this,
528
+ A: [
529
+ "message.data?.answer?.offerMessageId",
530
+ "'No offerMessageId'"
531
+ ]
532
+ });
435
533
  const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
436
534
  if (offerRecord) {
437
535
  this._offerRecords.delete(message.data.answer.offerMessageId);
438
- invariant2((_c = message.data) == null ? void 0 : _c.answer, "No answer");
536
+ invariant2(message.data?.answer, "No answer", {
537
+ F: __dxlog_file2,
538
+ L: 139,
539
+ S: this,
540
+ A: [
541
+ "message.data?.answer",
542
+ "'No answer'"
543
+ ]
544
+ });
439
545
  log2("resolving", {
440
546
  answer: message.data.answer
441
547
  }, {
442
548
  F: __dxlog_file2,
443
- L: 141,
549
+ L: 140,
444
550
  S: this,
445
551
  C: (f, a) => f(...a)
446
552
  });
@@ -448,7 +554,15 @@ var SwarmMessenger = class {
448
554
  }
449
555
  }
450
556
  async _handleOffer({ author, recipient, message }) {
451
- invariant2(message.data.offer, "No offer");
557
+ invariant2(message.data.offer, "No offer", {
558
+ F: __dxlog_file2,
559
+ L: 154,
560
+ S: this,
561
+ A: [
562
+ "message.data.offer",
563
+ "'No offer'"
564
+ ]
565
+ });
452
566
  const offerMessage = {
453
567
  author,
454
568
  recipient,
@@ -476,15 +590,31 @@ var SwarmMessenger = class {
476
590
  err
477
591
  }, {
478
592
  F: __dxlog_file2,
479
- L: 175,
593
+ L: 174,
480
594
  S: this,
481
595
  C: (f, a) => f(...a)
482
596
  });
483
597
  }
484
598
  }
485
599
  async _handleSignal({ author, recipient, message }) {
486
- invariant2(message.messageId);
487
- invariant2(message.data.signal || message.data.signalBatch, "Invalid message");
600
+ invariant2(message.messageId, void 0, {
601
+ F: __dxlog_file2,
602
+ L: 187,
603
+ S: this,
604
+ A: [
605
+ "message.messageId",
606
+ ""
607
+ ]
608
+ });
609
+ invariant2(message.data.signal || message.data.signalBatch, "Invalid message", {
610
+ F: __dxlog_file2,
611
+ L: 188,
612
+ S: this,
613
+ A: [
614
+ "message.data.signal || message.data.signalBatch",
615
+ "'Invalid message'"
616
+ ]
617
+ });
488
618
  const signalMessage = {
489
619
  author,
490
620
  recipient,
@@ -499,20 +629,20 @@ var SwarmMessenger = class {
499
629
  };
500
630
 
501
631
  // packages/core/mesh/network-manager/src/swarm/swarm.ts
502
- import invariant4 from "tiny-invariant";
503
632
  import { Event as Event2, scheduleTask as scheduleTask2, sleep as sleep2, synchronized as synchronized3 } from "@dxos/async";
504
633
  import { Context as Context4 } from "@dxos/context";
505
634
  import { ErrorStream as ErrorStream2 } from "@dxos/debug";
635
+ import { invariant as invariant4 } from "@dxos/invariant";
506
636
  import { PublicKey as PublicKey4 } from "@dxos/keys";
507
637
  import { log as log4, logInfo } from "@dxos/log";
508
638
  import { trace as trace2 } from "@dxos/protocols";
509
639
  import { ComplexMap as ComplexMap2, isNotNullOrUndefined } from "@dxos/util";
510
640
 
511
641
  // packages/core/mesh/network-manager/src/swarm/peer.ts
512
- import invariant3 from "tiny-invariant";
513
642
  import { scheduleTask, synchronized as synchronized2 } from "@dxos/async";
514
643
  import { Context as Context3 } from "@dxos/context";
515
644
  import { CancelledError as CancelledError2 } from "@dxos/errors";
645
+ import { invariant as invariant3 } from "@dxos/invariant";
516
646
  import { PublicKey as PublicKey3 } from "@dxos/keys";
517
647
  import { log as log3 } from "@dxos/log";
518
648
  function _ts_decorate2(decorators, target, key, desc) {
@@ -547,7 +677,6 @@ var Peer = class {
547
677
  * Respond to remote offer.
548
678
  */
549
679
  async onOffer(message) {
550
- var _a;
551
680
  const remoteId = message.author;
552
681
  if (this.connection || this.initiating) {
553
682
  if (remoteId.toHex() < this.localPeerId.toHex()) {
@@ -555,10 +684,10 @@ var Peer = class {
555
684
  localPeerId: this.id,
556
685
  topic: this.topic,
557
686
  remotePeerId: this.localPeerId,
558
- sessionId: (_a = this.connection) == null ? void 0 : _a.sessionId
687
+ sessionId: this.connection?.sessionId
559
688
  }, {
560
689
  F: __dxlog_file3,
561
- L: 107,
690
+ L: 106,
562
691
  S: this,
563
692
  C: (f, a) => f(...a)
564
693
  });
@@ -573,7 +702,15 @@ var Peer = class {
573
702
  }
574
703
  if (await this._callbacks.onOffer(remoteId)) {
575
704
  if (!this.connection) {
576
- invariant3(message.sessionId);
705
+ invariant3(message.sessionId, void 0, {
706
+ F: __dxlog_file3,
707
+ L: 126,
708
+ S: this,
709
+ A: [
710
+ "message.sessionId",
711
+ ""
712
+ ]
713
+ });
577
714
  const connection = this._createConnection(false, message.sessionId);
578
715
  try {
579
716
  await this._connectionLimiter.connecting(message.sessionId);
@@ -587,7 +724,7 @@ var Peer = class {
587
724
  err
588
725
  }, {
589
726
  F: __dxlog_file3,
590
- L: 135,
727
+ L: 134,
591
728
  S: this,
592
729
  C: (f, a) => f(...a)
593
730
  });
@@ -607,8 +744,24 @@ var Peer = class {
607
744
  * Initiate a connection to the remote peer.
608
745
  */
609
746
  async initiateConnection() {
610
- invariant3(!this.initiating, "Initiation in progress.");
611
- invariant3(!this.connection, "Already connected.");
747
+ invariant3(!this.initiating, "Initiation in progress.", {
748
+ F: __dxlog_file3,
749
+ L: 151,
750
+ S: this,
751
+ A: [
752
+ "!this.initiating",
753
+ "'Initiation in progress.'"
754
+ ]
755
+ });
756
+ invariant3(!this.connection, "Already connected.", {
757
+ F: __dxlog_file3,
758
+ L: 152,
759
+ S: this,
760
+ A: [
761
+ "!this.connection",
762
+ "'Already connected.'"
763
+ ]
764
+ });
612
765
  const sessionId = PublicKey3.random();
613
766
  log3("initiating...", {
614
767
  id: this.id,
@@ -617,7 +770,7 @@ var Peer = class {
617
770
  sessionId
618
771
  }, {
619
772
  F: __dxlog_file3,
620
- L: 155,
773
+ L: 154,
621
774
  S: this,
622
775
  C: (f, a) => f(...a)
623
776
  });
@@ -641,14 +794,14 @@ var Peer = class {
641
794
  remoteId: this.id
642
795
  }, {
643
796
  F: __dxlog_file3,
644
- L: 170,
797
+ L: 169,
645
798
  S: this,
646
799
  C: (f, a) => f(...a)
647
800
  });
648
801
  if (connection.state !== ConnectionState.INITIAL) {
649
802
  log3("ignoring response", void 0, {
650
803
  F: __dxlog_file3,
651
- L: 172,
804
+ L: 171,
652
805
  S: this,
653
806
  C: (f, a) => f(...a)
654
807
  });
@@ -668,7 +821,7 @@ var Peer = class {
668
821
  remoteId: this.id
669
822
  }, {
670
823
  F: __dxlog_file3,
671
- L: 183,
824
+ L: 182,
672
825
  S: this,
673
826
  C: (f, a) => f(...a)
674
827
  });
@@ -683,7 +836,6 @@ var Peer = class {
683
836
  * Either we're initiating a connection or creating one in response to an offer from the other peer.
684
837
  */
685
838
  _createConnection(initiator, sessionId) {
686
- var _a;
687
839
  log3("creating connection", {
688
840
  topic: this.topic,
689
841
  peerId: this.localPeerId,
@@ -692,11 +844,19 @@ var Peer = class {
692
844
  sessionId
693
845
  }, {
694
846
  F: __dxlog_file3,
695
- L: 197,
847
+ L: 196,
696
848
  S: this,
697
849
  C: (f, a) => f(...a)
698
850
  });
699
- invariant3(!this.connection, "Already connected.");
851
+ invariant3(!this.connection, "Already connected.", {
852
+ F: __dxlog_file3,
853
+ L: 203,
854
+ S: this,
855
+ A: [
856
+ "!this.connection",
857
+ "'Already connected.'"
858
+ ]
859
+ });
700
860
  const connection = new Connection(
701
861
  this.topic,
702
862
  this.localPeerId,
@@ -714,7 +874,7 @@ var Peer = class {
714
874
  this._transportFactory
715
875
  );
716
876
  this._callbacks.onInitiated(connection);
717
- void ((_a = this._connectionCtx) == null ? void 0 : _a.dispose());
877
+ void this._connectionCtx?.dispose();
718
878
  this._connectionCtx = this._ctx.derive();
719
879
  connection.stateChanged.on((state) => {
720
880
  switch (state) {
@@ -723,6 +883,18 @@ var Peer = class {
723
883
  this._lastConnectionTime = Date.now();
724
884
  this._callbacks.onConnected();
725
885
  this._connectionLimiter.doneConnecting(sessionId);
886
+ log3.trace("dxos.mesh.connection.connected", {
887
+ topic: this.topic,
888
+ localPeerId: this.localPeerId,
889
+ remotePeerId: this.id,
890
+ sessionId,
891
+ initiator
892
+ }, {
893
+ F: __dxlog_file3,
894
+ L: 229,
895
+ S: this,
896
+ C: (f, a) => f(...a)
897
+ });
726
898
  break;
727
899
  }
728
900
  case ConnectionState.CLOSED: {
@@ -733,11 +905,31 @@ var Peer = class {
733
905
  initiator
734
906
  }, {
735
907
  F: __dxlog_file3,
736
- L: 234,
908
+ L: 240,
909
+ S: this,
910
+ C: (f, a) => f(...a)
911
+ });
912
+ invariant3(this.connection === connection, "Connection mismatch (race condition).", {
913
+ F: __dxlog_file3,
914
+ L: 241,
915
+ S: this,
916
+ A: [
917
+ "this.connection === connection",
918
+ "'Connection mismatch (race condition).'"
919
+ ]
920
+ });
921
+ log3.trace("dxos.mesh.connection.closed", {
922
+ topic: this.topic,
923
+ localPeerId: this.localPeerId,
924
+ remotePeerId: this.id,
925
+ sessionId,
926
+ initiator
927
+ }, {
928
+ F: __dxlog_file3,
929
+ L: 243,
737
930
  S: this,
738
931
  C: (f, a) => f(...a)
739
932
  });
740
- invariant3(this.connection === connection, "Connection mismatch (race condition).");
741
933
  if (this._lastConnectionTime && this._lastConnectionTime + CONNECTION_COUNTS_STABLE_AFTER < Date.now()) {
742
934
  this._availableAfter = 0;
743
935
  } else {
@@ -764,7 +956,20 @@ var Peer = class {
764
956
  err
765
957
  }, {
766
958
  F: __dxlog_file3,
767
- L: 263,
959
+ L: 277,
960
+ S: this,
961
+ C: (f, a) => f(...a)
962
+ });
963
+ log3.trace("dxos.mesh.connection.error", {
964
+ topic: this.topic,
965
+ localPeerId: this.localPeerId,
966
+ remotePeerId: this.id,
967
+ sessionId,
968
+ initiator,
969
+ err
970
+ }, {
971
+ F: __dxlog_file3,
972
+ L: 278,
768
973
  S: this,
769
974
  C: (f, a) => f(...a)
770
975
  });
@@ -783,7 +988,7 @@ var Peer = class {
783
988
  sessionId: connection.sessionId
784
989
  }, {
785
990
  F: __dxlog_file3,
786
- L: 279,
991
+ L: 301,
787
992
  S: this,
788
993
  C: (f, a) => f(...a)
789
994
  });
@@ -793,7 +998,7 @@ var Peer = class {
793
998
  sessionId: connection.sessionId
794
999
  }, {
795
1000
  F: __dxlog_file3,
796
- L: 285,
1001
+ L: 307,
797
1002
  S: this,
798
1003
  C: (f, a) => f(...a)
799
1004
  });
@@ -804,7 +1009,7 @@ var Peer = class {
804
1009
  message
805
1010
  }, {
806
1011
  F: __dxlog_file3,
807
- L: 290,
1012
+ L: 312,
808
1013
  S: this,
809
1014
  C: (f, a) => f(...a)
810
1015
  });
@@ -813,18 +1018,17 @@ var Peer = class {
813
1018
  await this.connection.signal(message);
814
1019
  }
815
1020
  async destroy() {
816
- var _a;
817
1021
  await this._ctx.dispose();
818
1022
  log3("Destroying peer", {
819
1023
  peerId: this.id,
820
1024
  topic: this.topic
821
1025
  }, {
822
1026
  F: __dxlog_file3,
823
- L: 300,
1027
+ L: 322,
824
1028
  S: this,
825
1029
  C: (f, a) => f(...a)
826
1030
  });
827
- await ((_a = this == null ? void 0 : this.connection) == null ? void 0 : _a.close());
1031
+ await this?.connection?.close();
828
1032
  }
829
1033
  };
830
1034
  _ts_decorate2([
@@ -885,7 +1089,7 @@ var Swarm = class {
885
1089
  }
886
1090
  }), {
887
1091
  F: __dxlog_file4,
888
- L: 88,
1092
+ L: 87,
889
1093
  S: this,
890
1094
  C: (f, a) => f(...a)
891
1095
  });
@@ -893,7 +1097,7 @@ var Swarm = class {
893
1097
  peerId: _ownPeerId
894
1098
  }, {
895
1099
  F: __dxlog_file4,
896
- L: 92,
1100
+ L: 91,
897
1101
  S: this,
898
1102
  C: (f, a) => f(...a)
899
1103
  });
@@ -908,7 +1112,7 @@ var Swarm = class {
908
1112
  id: this._instanceId
909
1113
  }), {
910
1114
  F: __dxlog_file4,
911
- L: 101,
1115
+ L: 100,
912
1116
  S: this,
913
1117
  C: (f, a) => f(...a)
914
1118
  });
@@ -929,7 +1133,15 @@ var Swarm = class {
929
1133
  return this._topic;
930
1134
  }
931
1135
  async open() {
932
- invariant4(!this._listeningHandle);
1136
+ invariant4(!this._listeningHandle, void 0, {
1137
+ F: __dxlog_file4,
1138
+ L: 127,
1139
+ S: this,
1140
+ A: [
1141
+ "!this._listeningHandle",
1142
+ ""
1143
+ ]
1144
+ });
933
1145
  this._listeningHandle = await this._messenger.listen({
934
1146
  peerId: this._ownPeerId,
935
1147
  payloadType: "dxos.mesh.swarm.SwarmMessage",
@@ -938,7 +1150,7 @@ var Swarm = class {
938
1150
  err
939
1151
  }, {
940
1152
  F: __dxlog_file4,
941
- L: 135,
1153
+ L: 134,
942
1154
  S: this,
943
1155
  C: (f, a) => f(...a)
944
1156
  }));
@@ -946,27 +1158,34 @@ var Swarm = class {
946
1158
  });
947
1159
  }
948
1160
  async destroy() {
949
- var _a;
950
1161
  log4("destroying...", void 0, {
951
1162
  F: __dxlog_file4,
952
- L: 141,
1163
+ L: 140,
953
1164
  S: this,
954
1165
  C: (f, a) => f(...a)
955
1166
  });
956
- await ((_a = this._listeningHandle) == null ? void 0 : _a.unsubscribe());
1167
+ await this._listeningHandle?.unsubscribe();
957
1168
  this._listeningHandle = void 0;
958
1169
  await this._ctx.dispose();
959
1170
  await this._topology.destroy();
960
1171
  await Promise.all(Array.from(this._peers.keys()).map((key) => this._destroyPeer(key)));
961
1172
  log4("destroyed", void 0, {
962
1173
  F: __dxlog_file4,
963
- L: 148,
1174
+ L: 147,
964
1175
  S: this,
965
1176
  C: (f, a) => f(...a)
966
1177
  });
967
1178
  }
968
1179
  async setTopology(topology) {
969
- invariant4(!this._ctx.disposed, "Swarm is offline");
1180
+ invariant4(!this._ctx.disposed, "Swarm is offline", {
1181
+ F: __dxlog_file4,
1182
+ L: 151,
1183
+ S: this,
1184
+ A: [
1185
+ "!this._ctx.disposed",
1186
+ "'Swarm is offline'"
1187
+ ]
1188
+ });
970
1189
  if (topology === this._topology) {
971
1190
  return;
972
1191
  }
@@ -975,7 +1194,7 @@ var Swarm = class {
975
1194
  topology: getClassName(topology)
976
1195
  }, {
977
1196
  F: __dxlog_file4,
978
- L: 156,
1197
+ L: 155,
979
1198
  S: this,
980
1199
  C: (f, a) => f(...a)
981
1200
  });
@@ -985,19 +1204,18 @@ var Swarm = class {
985
1204
  this._topology.update();
986
1205
  }
987
1206
  onSwarmEvent(swarmEvent) {
988
- var _a;
989
1207
  log4("swarm event", {
990
1208
  swarmEvent
991
1209
  }, {
992
1210
  F: __dxlog_file4,
993
- L: 169,
1211
+ L: 168,
994
1212
  S: this,
995
1213
  C: (f, a) => f(...a)
996
1214
  });
997
1215
  if (this._ctx.disposed) {
998
1216
  log4("swarm event ignored for disposed swarm", void 0, {
999
1217
  F: __dxlog_file4,
1000
- L: 172,
1218
+ L: 171,
1001
1219
  S: this,
1002
1220
  C: (f, a) => f(...a)
1003
1221
  });
@@ -1009,7 +1227,7 @@ var Swarm = class {
1009
1227
  peerId
1010
1228
  }, {
1011
1229
  F: __dxlog_file4,
1012
- L: 178,
1230
+ L: 177,
1013
1231
  S: this,
1014
1232
  C: (f, a) => f(...a)
1015
1233
  });
@@ -1021,10 +1239,10 @@ var Swarm = class {
1021
1239
  const peer = this._peers.get(PublicKey4.from(swarmEvent.peerLeft.peer));
1022
1240
  if (peer) {
1023
1241
  peer.advertizing = false;
1024
- if (((_a = peer.connection) == null ? void 0 : _a.state) !== ConnectionState.CONNECTED) {
1242
+ if (peer.connection?.state !== ConnectionState.CONNECTED) {
1025
1243
  void this._destroyPeer(peer.id).catch((err) => log4.catch(err, void 0, {
1026
1244
  F: __dxlog_file4,
1027
- L: 189,
1245
+ L: 188,
1028
1246
  S: this,
1029
1247
  C: (f, a) => f(...a)
1030
1248
  }));
@@ -1034,19 +1252,18 @@ var Swarm = class {
1034
1252
  this._topology.update();
1035
1253
  }
1036
1254
  async onOffer(message) {
1037
- var _a, _b;
1038
1255
  log4("offer", {
1039
1256
  message
1040
1257
  }, {
1041
1258
  F: __dxlog_file4,
1042
- L: 199,
1259
+ L: 198,
1043
1260
  S: this,
1044
1261
  C: (f, a) => f(...a)
1045
1262
  });
1046
1263
  if (this._ctx.disposed) {
1047
1264
  log4("ignored for disposed swarm", void 0, {
1048
1265
  F: __dxlog_file4,
1049
- L: 201,
1266
+ L: 200,
1050
1267
  S: this,
1051
1268
  C: (f, a) => f(...a)
1052
1269
  });
@@ -1054,13 +1271,21 @@ var Swarm = class {
1054
1271
  accept: false
1055
1272
  };
1056
1273
  }
1057
- invariant4(message.author);
1058
- if (!((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId))) {
1274
+ invariant4(message.author, void 0, {
1275
+ F: __dxlog_file4,
1276
+ L: 205,
1277
+ S: this,
1278
+ A: [
1279
+ "message.author",
1280
+ ""
1281
+ ]
1282
+ });
1283
+ if (!message.recipient?.equals(this._ownPeerId)) {
1059
1284
  log4("rejecting offer with incorrect peerId", {
1060
1285
  message
1061
1286
  }, {
1062
1287
  F: __dxlog_file4,
1063
- L: 208,
1288
+ L: 207,
1064
1289
  S: this,
1065
1290
  C: (f, a) => f(...a)
1066
1291
  });
@@ -1068,12 +1293,12 @@ var Swarm = class {
1068
1293
  accept: false
1069
1294
  };
1070
1295
  }
1071
- if (!((_b = message.topic) == null ? void 0 : _b.equals(this._topic))) {
1296
+ if (!message.topic?.equals(this._topic)) {
1072
1297
  log4("rejecting offer with incorrect topic", {
1073
1298
  message
1074
1299
  }, {
1075
1300
  F: __dxlog_file4,
1076
- L: 212,
1301
+ L: 211,
1077
1302
  S: this,
1078
1303
  C: (f, a) => f(...a)
1079
1304
  });
@@ -1087,27 +1312,50 @@ var Swarm = class {
1087
1312
  return answer;
1088
1313
  }
1089
1314
  async onSignal(message) {
1090
- var _a, _b;
1091
1315
  log4("signal", {
1092
1316
  message
1093
1317
  }, {
1094
1318
  F: __dxlog_file4,
1095
- L: 223,
1319
+ L: 222,
1096
1320
  S: this,
1097
1321
  C: (f, a) => f(...a)
1098
1322
  });
1099
1323
  if (this._ctx.disposed) {
1100
1324
  log4.info("ignored for offline swarm", void 0, {
1101
1325
  F: __dxlog_file4,
1102
- L: 225,
1326
+ L: 224,
1103
1327
  S: this,
1104
1328
  C: (f, a) => f(...a)
1105
1329
  });
1106
1330
  return;
1107
1331
  }
1108
- invariant4((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`);
1109
- invariant4((_b = message.topic) == null ? void 0 : _b.equals(this._topic));
1110
- invariant4(message.author);
1332
+ invariant4(message.recipient?.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1333
+ F: __dxlog_file4,
1334
+ L: 227,
1335
+ S: this,
1336
+ A: [
1337
+ "message.recipient?.equals(this._ownPeerId)",
1338
+ "`Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`"
1339
+ ]
1340
+ });
1341
+ invariant4(message.topic?.equals(this._topic), void 0, {
1342
+ F: __dxlog_file4,
1343
+ L: 231,
1344
+ S: this,
1345
+ A: [
1346
+ "message.topic?.equals(this._topic)",
1347
+ ""
1348
+ ]
1349
+ });
1350
+ invariant4(message.author, void 0, {
1351
+ F: __dxlog_file4,
1352
+ L: 232,
1353
+ S: this,
1354
+ A: [
1355
+ "message.author",
1356
+ ""
1357
+ ]
1358
+ });
1111
1359
  const peer = this._getOrCreatePeer(message.author);
1112
1360
  await peer.onSignal(message);
1113
1361
  }
@@ -1159,7 +1407,15 @@ var Swarm = class {
1159
1407
  return peer;
1160
1408
  }
1161
1409
  async _destroyPeer(peerId) {
1162
- invariant4(this._peers.has(peerId));
1410
+ invariant4(this._peers.has(peerId), void 0, {
1411
+ F: __dxlog_file4,
1412
+ L: 302,
1413
+ S: this,
1414
+ A: [
1415
+ "this._peers.has(peerId)",
1416
+ ""
1417
+ ]
1418
+ });
1163
1419
  await this._peers.get(peerId).destroy();
1164
1420
  this._peers.delete(peerId);
1165
1421
  }
@@ -1180,7 +1436,7 @@ var Swarm = class {
1180
1436
  } catch (err) {
1181
1437
  log4("initiation error", err, {
1182
1438
  F: __dxlog_file4,
1183
- L: 329,
1439
+ L: 328,
1184
1440
  S: this,
1185
1441
  C: (f, a) => f(...a)
1186
1442
  });
@@ -1208,7 +1464,7 @@ var Swarm = class {
1208
1464
  remoteId
1209
1465
  }, {
1210
1466
  F: __dxlog_file4,
1211
- L: 356,
1467
+ L: 355,
1212
1468
  S: this,
1213
1469
  C: (f, a) => f(...a)
1214
1470
  });
@@ -1225,7 +1481,7 @@ var Swarm = class {
1225
1481
  remoteId
1226
1482
  }, {
1227
1483
  F: __dxlog_file4,
1228
- L: 370,
1484
+ L: 369,
1229
1485
  S: this,
1230
1486
  C: (f, a) => f(...a)
1231
1487
  });
@@ -1235,7 +1491,7 @@ var Swarm = class {
1235
1491
  remoteId
1236
1492
  }, {
1237
1493
  F: __dxlog_file4,
1238
- L: 373,
1494
+ L: 372,
1239
1495
  S: this,
1240
1496
  C: (f, a) => f(...a)
1241
1497
  });
@@ -1294,8 +1550,7 @@ var SwarmMapper = class {
1294
1550
  }));
1295
1551
  }));
1296
1552
  this._subscriptions.add(_swarm.disconnected.on((peerId) => {
1297
- var _a;
1298
- (_a = this._connectionSubscriptions.get(peerId)) == null ? void 0 : _a();
1553
+ this._connectionSubscriptions.get(peerId)?.();
1299
1554
  this._connectionSubscriptions.delete(peerId);
1300
1555
  this._update();
1301
1556
  }));
@@ -1345,8 +1600,9 @@ var SwarmMapper = class {
1345
1600
  import { DeferredTask as DeferredTask2 } from "@dxos/async";
1346
1601
  import { Context as Context5 } from "@dxos/context";
1347
1602
  import { CancelledError as CancelledError3 } from "@dxos/errors";
1603
+ import { invariant as invariant5 } from "@dxos/invariant";
1348
1604
  import { PublicKey as PublicKey6 } from "@dxos/keys";
1349
- import { invariant as invariant5, log as log6 } from "@dxos/log";
1605
+ import { log as log6 } from "@dxos/log";
1350
1606
  import { ComplexMap as ComplexMap4 } from "@dxos/util";
1351
1607
  var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
1352
1608
  var MAX_CONCURRENT_INITIATING_CONNECTIONS = 3;
@@ -1370,7 +1626,7 @@ var ConnectionLimiter = class {
1370
1626
  async connecting(sessionId) {
1371
1627
  invariant5(!this._waitingPromises.has(sessionId), "Peer is already waiting for connection", {
1372
1628
  F: __dxlog_file6,
1373
- L: 47,
1629
+ L: 48,
1374
1630
  S: this,
1375
1631
  A: [
1376
1632
  "!this._waitingPromises.has(sessionId)",
@@ -1381,7 +1637,7 @@ var ConnectionLimiter = class {
1381
1637
  sessionId
1382
1638
  }, {
1383
1639
  F: __dxlog_file6,
1384
- L: 48,
1640
+ L: 49,
1385
1641
  S: this,
1386
1642
  C: (f, a) => f(...a)
1387
1643
  });
@@ -1396,7 +1652,7 @@ var ConnectionLimiter = class {
1396
1652
  sessionId
1397
1653
  }, {
1398
1654
  F: __dxlog_file6,
1399
- L: 56,
1655
+ L: 57,
1400
1656
  S: this,
1401
1657
  C: (f, a) => f(...a)
1402
1658
  });
@@ -1409,7 +1665,7 @@ var ConnectionLimiter = class {
1409
1665
  sessionId
1410
1666
  }, {
1411
1667
  F: __dxlog_file6,
1412
- L: 63,
1668
+ L: 64,
1413
1669
  S: this,
1414
1670
  C: (f, a) => f(...a)
1415
1671
  });
@@ -1444,8 +1700,7 @@ var ConnectionLog = class {
1444
1700
  this.update = new Event4();
1445
1701
  }
1446
1702
  getSwarmInfo(swarmId) {
1447
- var _a;
1448
- return (_a = this._swarms.get(swarmId)) != null ? _a : raise(new Error(`Swarm not found: ${swarmId}`));
1703
+ return this._swarms.get(swarmId) ?? raise(new Error(`Swarm not found: ${swarmId}`));
1449
1704
  }
1450
1705
  get swarms() {
1451
1706
  return Array.from(this._swarms.values());
@@ -1461,7 +1716,6 @@ var ConnectionLog = class {
1461
1716
  this._swarms.set(PublicKey7.from(swarm._instanceId), info);
1462
1717
  this.update.emit();
1463
1718
  swarm.connectionAdded.on((connection) => {
1464
- var _a, _b;
1465
1719
  const connectionInfo = {
1466
1720
  state: ConnectionState.INITIAL,
1467
1721
  remotePeerId: connection.remoteId,
@@ -1480,7 +1734,7 @@ var ConnectionLog = class {
1480
1734
  });
1481
1735
  this.update.emit();
1482
1736
  });
1483
- (_b = (_a = connection.protocol) == null ? void 0 : _a.stats) == null ? void 0 : _b.on((stats) => {
1737
+ connection.protocol?.stats?.on((stats) => {
1484
1738
  connectionInfo.streams = stats.channels;
1485
1739
  this.update.emit();
1486
1740
  });
@@ -1493,8 +1747,8 @@ var ConnectionLog = class {
1493
1747
  };
1494
1748
 
1495
1749
  // packages/core/mesh/network-manager/src/network-manager.ts
1496
- import invariant6 from "tiny-invariant";
1497
1750
  import { Event as Event5 } from "@dxos/async";
1751
+ import { invariant as invariant6 } from "@dxos/invariant";
1498
1752
  import { PublicKey as PublicKey8 } from "@dxos/keys";
1499
1753
  import { log as log7 } from "@dxos/log";
1500
1754
  import { Messenger } from "@dxos/messaging";
@@ -1515,10 +1769,7 @@ var NetworkManager = class {
1515
1769
  this._instanceId = PublicKey8.random().toHex();
1516
1770
  this._transportFactory = transportFactory;
1517
1771
  this._signalManager = signalManager;
1518
- this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => {
1519
- var _a;
1520
- return (_a = this._swarms.get(topic)) == null ? void 0 : _a.onSwarmEvent(event);
1521
- });
1772
+ this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => this._swarms.get(topic)?.onSwarmEvent(event));
1522
1773
  this._messenger = new Messenger({
1523
1774
  signalManager: this._signalManager
1524
1775
  });
@@ -1553,7 +1804,7 @@ var NetworkManager = class {
1553
1804
  id: this._instanceId
1554
1805
  }), {
1555
1806
  F: __dxlog_file7,
1556
- L: 131,
1807
+ L: 130,
1557
1808
  S: this,
1558
1809
  C: (f, a) => f(...a)
1559
1810
  });
@@ -1563,7 +1814,7 @@ var NetworkManager = class {
1563
1814
  id: this._instanceId
1564
1815
  }), {
1565
1816
  F: __dxlog_file7,
1566
- L: 134,
1817
+ L: 133,
1567
1818
  S: this,
1568
1819
  C: (f, a) => f(...a)
1569
1820
  });
@@ -1573,7 +1824,7 @@ var NetworkManager = class {
1573
1824
  await this.leaveSwarm(topic).catch((err) => {
1574
1825
  log7(err, void 0, {
1575
1826
  F: __dxlog_file7,
1576
- L: 140,
1827
+ L: 139,
1577
1828
  S: this,
1578
1829
  C: (f, a) => f(...a)
1579
1830
  });
@@ -1586,11 +1837,42 @@ var NetworkManager = class {
1586
1837
  * Join the swarm.
1587
1838
  */
1588
1839
  async joinSwarm({ topic, peerId, topology, protocolProvider: protocol, label }) {
1589
- var _a;
1590
- invariant6(PublicKey8.isPublicKey(topic));
1591
- invariant6(PublicKey8.isPublicKey(peerId));
1592
- invariant6(topology);
1593
- invariant6(typeof protocol === "function");
1840
+ invariant6(PublicKey8.isPublicKey(topic), void 0, {
1841
+ F: __dxlog_file7,
1842
+ L: 157,
1843
+ S: this,
1844
+ A: [
1845
+ "PublicKey.isPublicKey(topic)",
1846
+ ""
1847
+ ]
1848
+ });
1849
+ invariant6(PublicKey8.isPublicKey(peerId), void 0, {
1850
+ F: __dxlog_file7,
1851
+ L: 158,
1852
+ S: this,
1853
+ A: [
1854
+ "PublicKey.isPublicKey(peerId)",
1855
+ ""
1856
+ ]
1857
+ });
1858
+ invariant6(topology, void 0, {
1859
+ F: __dxlog_file7,
1860
+ L: 159,
1861
+ S: this,
1862
+ A: [
1863
+ "topology",
1864
+ ""
1865
+ ]
1866
+ });
1867
+ invariant6(typeof protocol === "function", void 0, {
1868
+ F: __dxlog_file7,
1869
+ L: 160,
1870
+ S: this,
1871
+ A: [
1872
+ "typeof protocol === 'function'",
1873
+ ""
1874
+ ]
1875
+ });
1594
1876
  if (this._swarms.has(topic)) {
1595
1877
  throw new Error(`Already connected to swarm: ${PublicKey8.from(topic)}`);
1596
1878
  }
@@ -1600,7 +1882,7 @@ var NetworkManager = class {
1600
1882
  topology: topology.toString()
1601
1883
  }, {
1602
1884
  F: __dxlog_file7,
1603
- L: 166,
1885
+ L: 165,
1604
1886
  S: this,
1605
1887
  C: (f, a) => f(...a)
1606
1888
  });
@@ -1610,7 +1892,7 @@ var NetworkManager = class {
1610
1892
  error
1611
1893
  }, {
1612
1894
  F: __dxlog_file7,
1613
- L: 179,
1895
+ L: 178,
1614
1896
  S: this,
1615
1897
  C: (f, a) => f(...a)
1616
1898
  });
@@ -1623,18 +1905,18 @@ var NetworkManager = class {
1623
1905
  peerId
1624
1906
  }).catch((error) => log7.catch(error, void 0, {
1625
1907
  F: __dxlog_file7,
1626
- L: 188,
1908
+ L: 187,
1627
1909
  S: this,
1628
1910
  C: (f, a) => f(...a)
1629
1911
  }));
1630
1912
  this.topicsUpdated.emit();
1631
- (_a = this._connectionLog) == null ? void 0 : _a.joinedSwarm(swarm);
1913
+ this._connectionLog?.joinedSwarm(swarm);
1632
1914
  log7("joined", {
1633
1915
  topic: PublicKey8.from(topic),
1634
1916
  count: this._swarms.size
1635
1917
  }, {
1636
1918
  F: __dxlog_file7,
1637
- L: 192,
1919
+ L: 191,
1638
1920
  S: this,
1639
1921
  C: (f, a) => f(...a)
1640
1922
  });
@@ -1646,7 +1928,6 @@ var NetworkManager = class {
1646
1928
  * Close the connection.
1647
1929
  */
1648
1930
  async leaveSwarm(topic) {
1649
- var _a;
1650
1931
  if (!this._swarms.has(topic)) {
1651
1932
  return;
1652
1933
  }
@@ -1654,7 +1935,7 @@ var NetworkManager = class {
1654
1935
  topic: PublicKey8.from(topic)
1655
1936
  }, {
1656
1937
  F: __dxlog_file7,
1657
- L: 208,
1938
+ L: 207,
1658
1939
  S: this,
1659
1940
  C: (f, a) => f(...a)
1660
1941
  });
@@ -1666,7 +1947,7 @@ var NetworkManager = class {
1666
1947
  const map = this._mappers.get(topic);
1667
1948
  map.destroy();
1668
1949
  this._mappers.delete(topic);
1669
- (_a = this._connectionLog) == null ? void 0 : _a.leftSwarm(swarm);
1950
+ this._connectionLog?.leftSwarm(swarm);
1670
1951
  await swarm.destroy();
1671
1952
  this._swarms.delete(topic);
1672
1953
  await this.topicsUpdated.emit();
@@ -1675,7 +1956,7 @@ var NetworkManager = class {
1675
1956
  count: this._swarms.size
1676
1957
  }, {
1677
1958
  F: __dxlog_file7,
1678
- L: 222,
1959
+ L: 221,
1679
1960
  S: this,
1680
1961
  C: (f, a) => f(...a)
1681
1962
  });
@@ -1709,17 +1990,34 @@ var NetworkManager = class {
1709
1990
  };
1710
1991
 
1711
1992
  // packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts
1712
- import invariant7 from "tiny-invariant";
1993
+ import { invariant as invariant7 } from "@dxos/invariant";
1994
+ var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
1713
1995
  var FullyConnectedTopology = class {
1714
1996
  toString() {
1715
1997
  return "FullyConnectedTopology";
1716
1998
  }
1717
1999
  init(controller) {
1718
- invariant7(!this._controller, "Already initialized");
2000
+ invariant7(!this._controller, "Already initialized", {
2001
+ F: __dxlog_file8,
2002
+ L: 18,
2003
+ S: this,
2004
+ A: [
2005
+ "!this._controller",
2006
+ "'Already initialized'"
2007
+ ]
2008
+ });
1719
2009
  this._controller = controller;
1720
2010
  }
1721
2011
  update() {
1722
- invariant7(this._controller, "Not initialized");
2012
+ invariant7(this._controller, "Not initialized", {
2013
+ F: __dxlog_file8,
2014
+ L: 23,
2015
+ S: this,
2016
+ A: [
2017
+ "this._controller",
2018
+ "'Not initialized'"
2019
+ ]
2020
+ });
1723
2021
  const { candidates: discovered } = this._controller.getState();
1724
2022
  for (const peer of discovered) {
1725
2023
  this._controller.connect(peer);
@@ -1733,10 +2031,10 @@ var FullyConnectedTopology = class {
1733
2031
  };
1734
2032
 
1735
2033
  // packages/core/mesh/network-manager/src/topology/mmst-topology.ts
1736
- import invariant8 from "tiny-invariant";
1737
2034
  import distance from "xor-distance";
2035
+ import { invariant as invariant8 } from "@dxos/invariant";
1738
2036
  import { log as log8 } from "@dxos/log";
1739
- var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/mmst-topology.ts";
2037
+ var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/mmst-topology.ts";
1740
2038
  var MMSTTopology = class {
1741
2039
  constructor({ originateConnections = 2, maxPeers = 4, sampleSize = 10 } = {}) {
1742
2040
  this._sampleCollected = false;
@@ -1745,15 +2043,31 @@ var MMSTTopology = class {
1745
2043
  this._sampleSize = sampleSize;
1746
2044
  }
1747
2045
  init(controller) {
1748
- invariant8(!this._controller, "Already initialized");
2046
+ invariant8(!this._controller, "Already initialized", {
2047
+ F: __dxlog_file9,
2048
+ L: 46,
2049
+ S: this,
2050
+ A: [
2051
+ "!this._controller",
2052
+ "'Already initialized'"
2053
+ ]
2054
+ });
1749
2055
  this._controller = controller;
1750
2056
  }
1751
2057
  update() {
1752
- invariant8(this._controller, "Not initialized");
2058
+ invariant8(this._controller, "Not initialized", {
2059
+ F: __dxlog_file9,
2060
+ L: 51,
2061
+ S: this,
2062
+ A: [
2063
+ "this._controller",
2064
+ "'Not initialized'"
2065
+ ]
2066
+ });
1753
2067
  const { connected, candidates } = this._controller.getState();
1754
2068
  if (this._sampleCollected || connected.length > this._maxPeers || candidates.length > 0) {
1755
2069
  log8("Running the algorithm.", void 0, {
1756
- F: __dxlog_file8,
2070
+ F: __dxlog_file9,
1757
2071
  L: 55,
1758
2072
  S: this,
1759
2073
  C: (f, a) => f(...a)
@@ -1763,11 +2077,19 @@ var MMSTTopology = class {
1763
2077
  }
1764
2078
  }
1765
2079
  async onOffer(peer) {
1766
- invariant8(this._controller, "Not initialized");
2080
+ invariant8(this._controller, "Not initialized", {
2081
+ F: __dxlog_file9,
2082
+ L: 62,
2083
+ S: this,
2084
+ A: [
2085
+ "this._controller",
2086
+ "'Not initialized'"
2087
+ ]
2088
+ });
1767
2089
  const { connected } = this._controller.getState();
1768
2090
  const accept = connected.length < this._maxPeers;
1769
2091
  log8(`Offer ${peer} accept=${accept}`, void 0, {
1770
- F: __dxlog_file8,
2092
+ F: __dxlog_file9,
1771
2093
  L: 65,
1772
2094
  S: this,
1773
2095
  C: (f, a) => f(...a)
@@ -1777,13 +2099,21 @@ var MMSTTopology = class {
1777
2099
  async destroy() {
1778
2100
  }
1779
2101
  _runAlgorithm() {
1780
- invariant8(this._controller, "Not initialized");
2102
+ invariant8(this._controller, "Not initialized", {
2103
+ F: __dxlog_file9,
2104
+ L: 74,
2105
+ S: this,
2106
+ A: [
2107
+ "this._controller",
2108
+ "'Not initialized'"
2109
+ ]
2110
+ });
1781
2111
  const { connected, candidates, ownPeerId } = this._controller.getState();
1782
2112
  if (connected.length > this._maxPeers) {
1783
2113
  const sorted = sortByXorDistance(connected, ownPeerId).reverse().slice(0, this._maxPeers - connected.length);
1784
2114
  for (const peer of sorted) {
1785
2115
  log8(`Disconnect ${peer}.`, void 0, {
1786
- F: __dxlog_file8,
2116
+ F: __dxlog_file9,
1787
2117
  L: 83,
1788
2118
  S: this,
1789
2119
  C: (f, a) => f(...a)
@@ -1795,7 +2125,7 @@ var MMSTTopology = class {
1795
2125
  const sorted = sortByXorDistance(sample, ownPeerId).slice(0, this._originateConnections - connected.length);
1796
2126
  for (const peer of sorted) {
1797
2127
  log8(`Connect ${peer}.`, void 0, {
1798
- F: __dxlog_file8,
2128
+ F: __dxlog_file9,
1799
2129
  L: 91,
1800
2130
  S: this,
1801
2131
  C: (f, a) => f(...a)
@@ -1811,9 +2141,9 @@ var MMSTTopology = class {
1811
2141
  var sortByXorDistance = (keys, reference) => keys.sort((a, b) => distance.gt(distance(a.asBuffer(), reference.asBuffer()), distance(b.asBuffer(), reference.asBuffer())));
1812
2142
 
1813
2143
  // packages/core/mesh/network-manager/src/topology/star-topology.ts
1814
- import invariant9 from "tiny-invariant";
2144
+ import { invariant as invariant9 } from "@dxos/invariant";
1815
2145
  import { log as log9 } from "@dxos/log";
1816
- var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
2146
+ var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
1817
2147
  var StarTopology = class {
1818
2148
  // prettier-ignore
1819
2149
  constructor(_centralPeer) {
@@ -1823,16 +2153,32 @@ var StarTopology = class {
1823
2153
  return `StarTopology(${this._centralPeer.truncate()})`;
1824
2154
  }
1825
2155
  init(controller) {
1826
- invariant9(!this._controller, "Already initialized.");
2156
+ invariant9(!this._controller, "Already initialized.", {
2157
+ F: __dxlog_file10,
2158
+ L: 24,
2159
+ S: this,
2160
+ A: [
2161
+ "!this._controller",
2162
+ "'Already initialized.'"
2163
+ ]
2164
+ });
1827
2165
  this._controller = controller;
1828
2166
  }
1829
2167
  update() {
1830
- invariant9(this._controller, "Not initialized.");
2168
+ invariant9(this._controller, "Not initialized.", {
2169
+ F: __dxlog_file10,
2170
+ L: 29,
2171
+ S: this,
2172
+ A: [
2173
+ "this._controller",
2174
+ "'Not initialized.'"
2175
+ ]
2176
+ });
1831
2177
  const { candidates, connected, ownPeerId } = this._controller.getState();
1832
2178
  if (!ownPeerId.equals(this._centralPeer)) {
1833
2179
  log9("leaf peer dropping all connections apart from central peer.", void 0, {
1834
- F: __dxlog_file9,
1835
- L: 33,
2180
+ F: __dxlog_file10,
2181
+ L: 32,
1836
2182
  S: this,
1837
2183
  C: (f, a) => f(...a)
1838
2184
  });
@@ -1841,8 +2187,8 @@ var StarTopology = class {
1841
2187
  log9("dropping connection", {
1842
2188
  peer
1843
2189
  }, {
1844
- F: __dxlog_file9,
1845
- L: 38,
2190
+ F: __dxlog_file10,
2191
+ L: 37,
1846
2192
  S: this,
1847
2193
  C: (f, a) => f(...a)
1848
2194
  });
@@ -1855,8 +2201,8 @@ var StarTopology = class {
1855
2201
  log9("connecting to peer", {
1856
2202
  peer
1857
2203
  }, {
1858
- F: __dxlog_file9,
1859
- L: 47,
2204
+ F: __dxlog_file10,
2205
+ L: 46,
1860
2206
  S: this,
1861
2207
  C: (f, a) => f(...a)
1862
2208
  });
@@ -1865,15 +2211,23 @@ var StarTopology = class {
1865
2211
  }
1866
2212
  }
1867
2213
  async onOffer(peer) {
1868
- invariant9(this._controller, "Not initialized.");
2214
+ invariant9(this._controller, "Not initialized.", {
2215
+ F: __dxlog_file10,
2216
+ L: 53,
2217
+ S: this,
2218
+ A: [
2219
+ "this._controller",
2220
+ "'Not initialized.'"
2221
+ ]
2222
+ });
1869
2223
  const { ownPeerId } = this._controller.getState();
1870
2224
  log9("offer", {
1871
2225
  peer,
1872
2226
  isCentral: peer.equals(this._centralPeer),
1873
2227
  isSelfCentral: ownPeerId.equals(this._centralPeer)
1874
2228
  }, {
1875
- F: __dxlog_file9,
1876
- L: 56,
2229
+ F: __dxlog_file10,
2230
+ L: 55,
1877
2231
  S: this,
1878
2232
  C: (f, a) => f(...a)
1879
2233
  });
@@ -1885,9 +2239,9 @@ var StarTopology = class {
1885
2239
 
1886
2240
  // packages/core/mesh/network-manager/src/transport/memory-transport.ts
1887
2241
  import { Transform } from "@dxos/node-std/stream";
1888
- import invariant10 from "tiny-invariant";
1889
2242
  import { Event as Event6, Trigger } from "@dxos/async";
1890
2243
  import { ErrorStream as ErrorStream3 } from "@dxos/debug";
2244
+ import { invariant as invariant10 } from "@dxos/invariant";
1891
2245
  import { PublicKey as PublicKey9 } from "@dxos/keys";
1892
2246
  import { log as log10, logInfo as logInfo2 } from "@dxos/log";
1893
2247
  import { ComplexMap as ComplexMap7 } from "@dxos/util";
@@ -1901,7 +2255,7 @@ function _ts_decorate4(decorators, target, key, desc) {
1901
2255
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1902
2256
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1903
2257
  }
1904
- var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
2258
+ var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
1905
2259
  var MEMORY_TRANSPORT_DELAY = 1;
1906
2260
  var createStreamDelay = (delay) => {
1907
2261
  return new Transform({
@@ -1914,9 +2268,12 @@ var createStreamDelay = (delay) => {
1914
2268
  var MemoryTransportFactory = {
1915
2269
  createTransport: (params) => new MemoryTransport(params)
1916
2270
  };
1917
- var _MemoryTransport = class {
2271
+ var MemoryTransport = class _MemoryTransport {
2272
+ static {
2273
+ // TODO(burdon): Remove static properties (inject context into constructor).
2274
+ this._connections = new ComplexMap7(PublicKey9.hash);
2275
+ }
1918
2276
  constructor(options) {
1919
- var _a;
1920
2277
  this.options = options;
1921
2278
  this.closed = new Event6();
1922
2279
  this.connected = new Event6();
@@ -1927,17 +2284,25 @@ var _MemoryTransport = class {
1927
2284
  this._incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
1928
2285
  this._destroyed = false;
1929
2286
  log10("creating", void 0, {
1930
- F: __dxlog_file10,
2287
+ F: __dxlog_file11,
1931
2288
  L: 64,
1932
2289
  S: this,
1933
2290
  C: (f, a) => f(...a)
1934
2291
  });
1935
- invariant10(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection");
2292
+ invariant10(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection", {
2293
+ F: __dxlog_file11,
2294
+ L: 66,
2295
+ S: this,
2296
+ A: [
2297
+ "!MemoryTransport._connections.has(this._instanceId)",
2298
+ "'Duplicate memory connection'"
2299
+ ]
2300
+ });
1936
2301
  _MemoryTransport._connections.set(this._instanceId, this);
1937
2302
  if (this.options.initiator) {
1938
2303
  setTimeout(async () => {
1939
2304
  log10("sending signal", void 0, {
1940
- F: __dxlog_file10,
2305
+ F: __dxlog_file11,
1941
2306
  L: 73,
1942
2307
  S: this,
1943
2308
  C: (f, a) => f(...a)
@@ -1954,7 +2319,7 @@ var _MemoryTransport = class {
1954
2319
  });
1955
2320
  } else {
1956
2321
  this._remote.wait({
1957
- timeout: (_a = this.options.timeout) != null ? _a : 1e3
2322
+ timeout: this.options.timeout ?? 1e3
1958
2323
  }).then((remoteId) => {
1959
2324
  if (this._destroyed) {
1960
2325
  return;
@@ -1966,11 +2331,19 @@ var _MemoryTransport = class {
1966
2331
  this.closed.emit();
1967
2332
  return;
1968
2333
  }
1969
- invariant10(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`);
2334
+ invariant10(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`, {
2335
+ F: __dxlog_file11,
2336
+ L: 99,
2337
+ S: this,
2338
+ A: [
2339
+ "!this._remoteConnection._remoteConnection",
2340
+ "`Remote already connected: ${this._remoteInstanceId}`"
2341
+ ]
2342
+ });
1970
2343
  this._remoteConnection._remoteConnection = this;
1971
2344
  this._remoteConnection._remoteInstanceId = this._instanceId;
1972
2345
  log10("connected", void 0, {
1973
- F: __dxlog_file10,
2346
+ F: __dxlog_file11,
1974
2347
  L: 103,
1975
2348
  S: this,
1976
2349
  C: (f, a) => f(...a)
@@ -1988,7 +2361,7 @@ var _MemoryTransport = class {
1988
2361
  }
1989
2362
  async destroy() {
1990
2363
  log10("closing", void 0, {
1991
- F: __dxlog_file10,
2364
+ F: __dxlog_file11,
1992
2365
  L: 124,
1993
2366
  S: this,
1994
2367
  C: (f, a) => f(...a)
@@ -1997,7 +2370,7 @@ var _MemoryTransport = class {
1997
2370
  _MemoryTransport._connections.delete(this._instanceId);
1998
2371
  if (this._remoteConnection) {
1999
2372
  log10("closing", void 0, {
2000
- F: __dxlog_file10,
2373
+ F: __dxlog_file11,
2001
2374
  L: 129,
2002
2375
  S: this,
2003
2376
  C: (f, a) => f(...a)
@@ -2010,7 +2383,7 @@ var _MemoryTransport = class {
2010
2383
  this._remoteConnection._remoteConnection = void 0;
2011
2384
  this._remoteConnection = void 0;
2012
2385
  log10("closed", void 0, {
2013
- F: __dxlog_file10,
2386
+ F: __dxlog_file11,
2014
2387
  L: 147,
2015
2388
  S: this,
2016
2389
  C: (f, a) => f(...a)
@@ -2018,7 +2391,7 @@ var _MemoryTransport = class {
2018
2391
  }
2019
2392
  this.closed.emit();
2020
2393
  log10("closed", void 0, {
2021
- F: __dxlog_file10,
2394
+ F: __dxlog_file11,
2022
2395
  L: 151,
2023
2396
  S: this,
2024
2397
  C: (f, a) => f(...a)
@@ -2028,12 +2401,12 @@ var _MemoryTransport = class {
2028
2401
  log10("received signal", {
2029
2402
  payload
2030
2403
  }, {
2031
- F: __dxlog_file10,
2404
+ F: __dxlog_file11,
2032
2405
  L: 155,
2033
2406
  S: this,
2034
2407
  C: (f, a) => f(...a)
2035
2408
  });
2036
- if (!(payload == null ? void 0 : payload.transportId)) {
2409
+ if (!payload?.transportId) {
2037
2410
  return;
2038
2411
  }
2039
2412
  const transportId = payload.transportId;
@@ -2043,9 +2416,6 @@ var _MemoryTransport = class {
2043
2416
  }
2044
2417
  }
2045
2418
  };
2046
- var MemoryTransport = _MemoryTransport;
2047
- // TODO(burdon): Remove static properties (inject context into constructor).
2048
- MemoryTransport._connections = new ComplexMap7(PublicKey9.hash);
2049
2419
  _ts_decorate4([
2050
2420
  logInfo2
2051
2421
  ], MemoryTransport.prototype, "_instanceId", void 0);
@@ -2055,9 +2425,9 @@ _ts_decorate4([
2055
2425
 
2056
2426
  // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
2057
2427
  import SimplePeerConstructor from "simple-peer";
2058
- import invariant11 from "tiny-invariant";
2059
2428
  import { Event as Event7 } from "@dxos/async";
2060
2429
  import { ErrorStream as ErrorStream4, raise as raise2 } from "@dxos/debug";
2430
+ import { invariant as invariant11 } from "@dxos/invariant";
2061
2431
  import { PublicKey as PublicKey10 } from "@dxos/keys";
2062
2432
  import { log as log11 } from "@dxos/log";
2063
2433
  import { trace as trace4 } from "@dxos/protocols";
@@ -2066,14 +2436,13 @@ import { trace as trace4 } from "@dxos/protocols";
2066
2436
  var wrtc = null;
2067
2437
  try {
2068
2438
  wrtc = __require("@koush/wrtc");
2069
- } catch (e) {
2439
+ } catch {
2070
2440
  }
2071
2441
 
2072
2442
  // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
2073
- var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
2443
+ var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
2074
2444
  var WebRTCTransport = class {
2075
2445
  constructor(params) {
2076
- var _a;
2077
2446
  this.params = params;
2078
2447
  this._closed = false;
2079
2448
  this.closed = new Event7();
@@ -2083,25 +2452,25 @@ var WebRTCTransport = class {
2083
2452
  log11.trace("dxos.mesh.webrtc-transport.constructor", trace4.begin({
2084
2453
  id: this._instanceId
2085
2454
  }), {
2086
- F: __dxlog_file11,
2455
+ F: __dxlog_file12,
2087
2456
  L: 39,
2088
2457
  S: this,
2089
2458
  C: (f, a) => f(...a)
2090
2459
  });
2091
2460
  log11("created connection", params, {
2092
- F: __dxlog_file11,
2461
+ F: __dxlog_file12,
2093
2462
  L: 40,
2094
2463
  S: this,
2095
2464
  C: (f, a) => f(...a)
2096
2465
  });
2097
2466
  this._peer = new SimplePeerConstructor({
2098
2467
  initiator: this.params.initiator,
2099
- wrtc: SimplePeerConstructor.WEBRTC_SUPPORT ? void 0 : (_a = wrtc) != null ? _a : raise2(new Error("wrtc not available")),
2468
+ wrtc: SimplePeerConstructor.WEBRTC_SUPPORT ? void 0 : wrtc ?? raise2(new Error("wrtc not available")),
2100
2469
  config: this.params.webrtcConfig
2101
2470
  });
2102
2471
  this._peer.on("signal", async (data) => {
2103
2472
  log11("signal", data, {
2104
- F: __dxlog_file11,
2473
+ F: __dxlog_file12,
2105
2474
  L: 48,
2106
2475
  S: this,
2107
2476
  C: (f, a) => f(...a)
@@ -2114,7 +2483,7 @@ var WebRTCTransport = class {
2114
2483
  });
2115
2484
  this._peer.on("connect", () => {
2116
2485
  log11("connected", void 0, {
2117
- F: __dxlog_file11,
2486
+ F: __dxlog_file12,
2118
2487
  L: 53,
2119
2488
  S: this,
2120
2489
  C: (f, a) => f(...a)
@@ -2124,7 +2493,7 @@ var WebRTCTransport = class {
2124
2493
  });
2125
2494
  this._peer.on("close", async () => {
2126
2495
  log11("closed", void 0, {
2127
- F: __dxlog_file11,
2496
+ F: __dxlog_file12,
2128
2497
  L: 59,
2129
2498
  S: this,
2130
2499
  C: (f, a) => f(...a)
@@ -2133,30 +2502,29 @@ var WebRTCTransport = class {
2133
2502
  this.closed.emit();
2134
2503
  });
2135
2504
  this._peer.on("error", async (err) => {
2136
- var _a2;
2137
2505
  this.errors.raise(err);
2138
2506
  try {
2139
- if (typeof ((_a2 = this._peer) == null ? void 0 : _a2._pc.getStats) === "function") {
2507
+ if (typeof this._peer?._pc.getStats === "function") {
2140
2508
  this._peer._pc.getStats().then((stats) => {
2141
2509
  log11.warn("report after webrtc error", {
2142
2510
  config: this.params.webrtcConfig,
2143
2511
  stats
2144
2512
  }, {
2145
- F: __dxlog_file11,
2513
+ F: __dxlog_file12,
2146
2514
  L: 71,
2147
2515
  S: this,
2148
2516
  C: (f, a) => f(...a)
2149
2517
  });
2150
2518
  });
2151
2519
  }
2152
- } catch (e) {
2520
+ } catch {
2153
2521
  }
2154
2522
  await this.destroy();
2155
2523
  });
2156
2524
  log11.trace("dxos.mesh.webrtc-transport.constructor", trace4.end({
2157
2525
  id: this._instanceId
2158
2526
  }), {
2159
- F: __dxlog_file11,
2527
+ F: __dxlog_file12,
2160
2528
  L: 81,
2161
2529
  S: this,
2162
2530
  C: (f, a) => f(...a)
@@ -2164,7 +2532,7 @@ var WebRTCTransport = class {
2164
2532
  }
2165
2533
  async destroy() {
2166
2534
  log11("closing...", void 0, {
2167
- F: __dxlog_file11,
2535
+ F: __dxlog_file12,
2168
2536
  L: 85,
2169
2537
  S: this,
2170
2538
  C: (f, a) => f(...a)
@@ -2174,7 +2542,7 @@ var WebRTCTransport = class {
2174
2542
  this._peer.destroy();
2175
2543
  this.closed.emit();
2176
2544
  log11("closed", void 0, {
2177
- F: __dxlog_file11,
2545
+ F: __dxlog_file12,
2178
2546
  L: 90,
2179
2547
  S: this,
2180
2548
  C: (f, a) => f(...a)
@@ -2184,12 +2552,19 @@ var WebRTCTransport = class {
2184
2552
  if (this._closed) {
2185
2553
  return;
2186
2554
  }
2187
- invariant11(signal.payload.data, "Signal message must contain signal data.");
2555
+ invariant11(signal.payload.data, "Signal message must contain signal data.", {
2556
+ F: __dxlog_file12,
2557
+ L: 98,
2558
+ S: this,
2559
+ A: [
2560
+ "signal.payload.data",
2561
+ "'Signal message must contain signal data.'"
2562
+ ]
2563
+ });
2188
2564
  this._peer.signal(signal.payload.data);
2189
2565
  }
2190
2566
  async _disconnectStreams() {
2191
- var _a, _b, _c, _d;
2192
- (_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);
2567
+ this.params.stream.unpipe?.(this._peer)?.unpipe?.(this.params.stream);
2193
2568
  }
2194
2569
  };
2195
2570
  var createWebRTCTransportFactory = (webrtcConfig) => ({
@@ -2201,13 +2576,13 @@ var createWebRTCTransportFactory = (webrtcConfig) => ({
2201
2576
 
2202
2577
  // packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts
2203
2578
  import { Duplex } from "@dxos/node-std/stream";
2204
- import invariant12 from "tiny-invariant";
2205
2579
  import { Stream } from "@dxos/codec-protobuf";
2580
+ import { invariant as invariant12 } from "@dxos/invariant";
2206
2581
  import { PublicKey as PublicKey11 } from "@dxos/keys";
2207
2582
  import { log as log12 } from "@dxos/log";
2208
2583
  import { ConnectionState as ConnectionState3 } from "@dxos/protocols/proto/dxos/mesh/bridge";
2209
2584
  import { ComplexMap as ComplexMap8 } from "@dxos/util";
2210
- var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts";
2585
+ var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts";
2211
2586
  var WebRTCTransportService = class {
2212
2587
  // prettier-ignore
2213
2588
  constructor(_webrtcConfig) {
@@ -2218,6 +2593,13 @@ var WebRTCTransportService = class {
2218
2593
  const rpcStream = new Stream(({ ready, next, close }) => {
2219
2594
  const duplex = new Duplex({
2220
2595
  read: () => {
2596
+ const callbacks = [
2597
+ ...transportState.writeCallbacks
2598
+ ];
2599
+ transportState.writeCallbacks.length = 0;
2600
+ for (const cb of callbacks) {
2601
+ cb();
2602
+ }
2221
2603
  },
2222
2604
  write: function(chunk, _, callback) {
2223
2605
  next({
@@ -2269,30 +2651,53 @@ var WebRTCTransportService = class {
2269
2651
  });
2270
2652
  close();
2271
2653
  });
2272
- ready();
2273
- this.transports.set(request.proxyId, {
2654
+ const transportState = {
2274
2655
  transport,
2275
- stream: duplex
2276
- });
2656
+ stream: duplex,
2657
+ writeCallbacks: []
2658
+ };
2659
+ ready();
2660
+ this.transports.set(request.proxyId, transportState);
2277
2661
  });
2278
2662
  return rpcStream;
2279
2663
  }
2280
2664
  async sendSignal({ proxyId, signal }) {
2281
- invariant12(this.transports.has(proxyId));
2665
+ invariant12(this.transports.has(proxyId), void 0, {
2666
+ F: __dxlog_file13,
2667
+ L: 113,
2668
+ S: this,
2669
+ A: [
2670
+ "this.transports.has(proxyId)",
2671
+ ""
2672
+ ]
2673
+ });
2282
2674
  await this.transports.get(proxyId).transport.signal(signal);
2283
2675
  }
2284
2676
  async sendData({ proxyId, payload }) {
2285
- invariant12(this.transports.has(proxyId));
2286
- await this.transports.get(proxyId).stream.push(payload);
2677
+ invariant12(this.transports.has(proxyId), void 0, {
2678
+ F: __dxlog_file13,
2679
+ L: 118,
2680
+ S: this,
2681
+ A: [
2682
+ "this.transports.has(proxyId)",
2683
+ ""
2684
+ ]
2685
+ });
2686
+ const state = this.transports.get(proxyId);
2687
+ const bufferHasSpace = state.stream.push(payload);
2688
+ if (!bufferHasSpace) {
2689
+ await new Promise((resolve) => {
2690
+ state.writeCallbacks.push(resolve);
2691
+ });
2692
+ }
2287
2693
  }
2288
2694
  async close({ proxyId }) {
2289
- var _a, _b;
2290
- await ((_a = this.transports.get(proxyId)) == null ? void 0 : _a.transport.destroy());
2291
- await ((_b = this.transports.get(proxyId)) == null ? void 0 : _b.stream.end());
2695
+ await this.transports.get(proxyId)?.transport.destroy();
2696
+ await this.transports.get(proxyId)?.stream.end();
2292
2697
  this.transports.delete(proxyId);
2293
2698
  log12("Closed.", void 0, {
2294
- F: __dxlog_file12,
2295
- L: 109,
2699
+ F: __dxlog_file13,
2700
+ L: 132,
2296
2701
  S: this,
2297
2702
  C: (f, a) => f(...a)
2298
2703
  });
@@ -2300,15 +2705,16 @@ var WebRTCTransportService = class {
2300
2705
  };
2301
2706
 
2302
2707
  // packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts
2303
- import invariant13 from "tiny-invariant";
2708
+ import { Writable } from "@dxos/node-std/stream";
2304
2709
  import { Event as Event8 } from "@dxos/async";
2305
2710
  import { Context as Context6 } from "@dxos/context";
2306
2711
  import { ErrorStream as ErrorStream5 } from "@dxos/debug";
2712
+ import { invariant as invariant13 } from "@dxos/invariant";
2307
2713
  import { PublicKey as PublicKey12 } from "@dxos/keys";
2308
2714
  import { log as log13 } from "@dxos/log";
2309
2715
  import { ConnectionState as ConnectionState4 } from "@dxos/protocols/proto/dxos/mesh/bridge";
2310
2716
  import { arrayToBuffer } from "@dxos/util";
2311
- var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts";
2717
+ var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts";
2312
2718
  var WebRTCTransportProxy = class {
2313
2719
  // prettier-ignore
2314
2720
  constructor(_params) {
@@ -2326,8 +2732,8 @@ var WebRTCTransportProxy = class {
2326
2732
  this._serviceStream.waitUntilReady().then(() => {
2327
2733
  this._serviceStream.subscribe(async (event) => {
2328
2734
  log13("WebRTCTransportProxy: event", event, {
2329
- F: __dxlog_file13,
2330
- L: 50,
2735
+ F: __dxlog_file14,
2736
+ L: 51,
2331
2737
  S: this,
2332
2738
  C: (f, a) => f(...a)
2333
2739
  });
@@ -2339,28 +2745,24 @@ var WebRTCTransportProxy = class {
2339
2745
  await this._handleSignal(event.signal);
2340
2746
  }
2341
2747
  });
2342
- const dataListener = async (data) => {
2343
- try {
2344
- await this._params.bridgeService.sendData({
2748
+ this._params.stream.pipe(new Writable({
2749
+ write: (chunk, _, callback) => {
2750
+ this._params.bridgeService.sendData({
2345
2751
  proxyId: this._proxyId,
2346
- payload: data
2347
- });
2348
- } catch (err) {
2349
- log13.catch(err, void 0, {
2350
- F: __dxlog_file13,
2351
- L: 67,
2352
- S: this,
2353
- C: (f, a) => f(...a)
2752
+ payload: chunk
2753
+ }).then(() => callback(), (err) => {
2754
+ log13.catch(err, void 0, {
2755
+ F: __dxlog_file14,
2756
+ L: 69,
2757
+ S: this,
2758
+ C: (f, a) => f(...a)
2759
+ });
2354
2760
  });
2355
2761
  }
2356
- };
2357
- this._params.stream.on("data", dataListener);
2358
- this._ctx.onDispose(() => {
2359
- this._params.stream.off("data", dataListener);
2360
- });
2762
+ }));
2361
2763
  }, (error) => log13.catch(error, void 0, {
2362
- F: __dxlog_file13,
2363
- L: 73,
2764
+ F: __dxlog_file14,
2765
+ L: 75,
2364
2766
  S: this,
2365
2767
  C: (f, a) => f(...a)
2366
2768
  }));
@@ -2405,8 +2807,8 @@ var WebRTCTransportProxy = class {
2405
2807
  });
2406
2808
  } catch (err) {
2407
2809
  log13.catch(err, void 0, {
2408
- F: __dxlog_file13,
2409
- L: 124,
2810
+ F: __dxlog_file14,
2811
+ L: 126,
2410
2812
  S: this,
2411
2813
  C: (f, a) => f(...a)
2412
2814
  });
@@ -2440,7 +2842,15 @@ var WebRTCTransportProxyFactory = class {
2440
2842
  return this;
2441
2843
  }
2442
2844
  createTransport(options) {
2443
- invariant13(this._bridgeService, "WebRTCTransportProxyFactory is not ready to open connections");
2845
+ invariant13(this._bridgeService, "WebRTCTransportProxyFactory is not ready to open connections", {
2846
+ F: __dxlog_file14,
2847
+ L: 163,
2848
+ S: this,
2849
+ A: [
2850
+ "this._bridgeService",
2851
+ "'WebRTCTransportProxyFactory is not ready to open connections'"
2852
+ ]
2853
+ });
2444
2854
  const transport = new WebRTCTransportProxy({
2445
2855
  ...options,
2446
2856
  bridgeService: this._bridgeService
@@ -2492,4 +2902,4 @@ export {
2492
2902
  WebRTCTransportProxyFactory,
2493
2903
  createTeleportProtocolFactory
2494
2904
  };
2495
- //# sourceMappingURL=chunk-5R7TDXUU.mjs.map
2905
+ //# sourceMappingURL=chunk-5TRIVLI6.mjs.map