@dxos/network-manager 0.8.4-main.406dc2a → 0.8.4-main.422d1c7879

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 (83) hide show
  1. package/dist/lib/browser/{chunk-NRRQ4H6U.mjs → chunk-Y2FDDNGM.mjs} +898 -1439
  2. package/dist/lib/browser/chunk-Y2FDDNGM.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +342 -4
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/testing/index.mjs +98 -116
  7. package/dist/lib/browser/testing/index.mjs.map +3 -3
  8. package/dist/lib/browser/transport/tcp/index.mjs +2 -48
  9. package/dist/lib/browser/transport/tcp/index.mjs.map +4 -4
  10. package/dist/lib/browser/transport/tcp/tcp-transport.browser.mjs +36 -0
  11. package/dist/lib/browser/transport/tcp/tcp-transport.browser.mjs.map +7 -0
  12. package/dist/lib/browser/transport/tcp/tcp-transport.mjs +160 -0
  13. package/dist/lib/browser/transport/tcp/tcp-transport.mjs.map +7 -0
  14. package/dist/lib/node-esm/{chunk-AX4TJ2DL.mjs → chunk-OU3FYVBA.mjs} +898 -1439
  15. package/dist/lib/node-esm/chunk-OU3FYVBA.mjs.map +7 -0
  16. package/dist/lib/node-esm/index.mjs +342 -4
  17. package/dist/lib/node-esm/index.mjs.map +4 -4
  18. package/dist/lib/node-esm/meta.json +1 -1
  19. package/dist/lib/node-esm/testing/index.mjs +98 -116
  20. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  21. package/dist/lib/node-esm/transport/tcp/index.mjs +2 -177
  22. package/dist/lib/node-esm/transport/tcp/index.mjs.map +4 -4
  23. package/dist/lib/node-esm/transport/tcp/tcp-transport.browser.mjs +36 -0
  24. package/dist/lib/node-esm/transport/tcp/tcp-transport.browser.mjs.map +7 -0
  25. package/dist/lib/node-esm/transport/tcp/tcp-transport.mjs +160 -0
  26. package/dist/lib/node-esm/transport/tcp/tcp-transport.mjs.map +7 -0
  27. package/dist/types/src/network-manager.d.ts +5 -4
  28. package/dist/types/src/network-manager.d.ts.map +1 -1
  29. package/dist/types/src/signal/signal-connection.d.ts +3 -2
  30. package/dist/types/src/signal/signal-connection.d.ts.map +1 -1
  31. package/dist/types/src/signal/signal-messenger.d.ts +3 -2
  32. package/dist/types/src/signal/signal-messenger.d.ts.map +1 -1
  33. package/dist/types/src/signal/swarm-messenger.d.ts +7 -7
  34. package/dist/types/src/signal/swarm-messenger.d.ts.map +1 -1
  35. package/dist/types/src/swarm/connection.d.ts +2 -1
  36. package/dist/types/src/swarm/connection.d.ts.map +1 -1
  37. package/dist/types/src/swarm/peer.d.ts +4 -3
  38. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  39. package/dist/types/src/swarm/swarm.d.ts +3 -2
  40. package/dist/types/src/swarm/swarm.d.ts.map +1 -1
  41. package/dist/types/src/testing/test-builder.d.ts.map +1 -1
  42. package/dist/types/src/tests/basic-test-suite.d.ts.map +1 -1
  43. package/dist/types/src/tests/property-test-suite.d.ts.map +1 -1
  44. package/dist/types/src/transport/tcp/index.d.ts +1 -1
  45. package/dist/types/src/transport/tcp/index.d.ts.map +1 -1
  46. package/dist/types/src/transport/webrtc/rtc-peer-connection.d.ts.map +1 -1
  47. package/dist/types/src/transport/webrtc/rtc-transport-channel.d.ts.map +1 -1
  48. package/dist/types/src/transport/webrtc/rtc-transport-factory.d.ts.map +1 -1
  49. package/dist/types/src/transport/webrtc/rtc-transport-proxy.d.ts.map +1 -1
  50. package/dist/types/src/transport/webrtc/rtc-transport-service.d.ts.map +1 -1
  51. package/dist/types/src/wire-protocol.d.ts +5 -5
  52. package/dist/types/src/wire-protocol.d.ts.map +1 -1
  53. package/dist/types/tsconfig.tsbuildinfo +1 -1
  54. package/package.json +32 -32
  55. package/src/network-manager.ts +19 -15
  56. package/src/signal/integration.node.test.ts +11 -10
  57. package/src/signal/signal-connection.ts +3 -2
  58. package/src/signal/signal-messenger.ts +3 -2
  59. package/src/signal/swarm-messenger.node.test.ts +25 -24
  60. package/src/signal/swarm-messenger.ts +65 -55
  61. package/src/swarm/connection.test.ts +8 -8
  62. package/src/swarm/connection.ts +12 -10
  63. package/src/swarm/peer.ts +7 -7
  64. package/src/swarm/swarm.test.ts +4 -4
  65. package/src/swarm/swarm.ts +9 -10
  66. package/src/testing/test-builder.ts +4 -4
  67. package/src/tests/basic-test-suite.ts +0 -1
  68. package/src/tests/memory-transport.test.ts +0 -2
  69. package/src/tests/property-test-suite.ts +4 -3
  70. package/src/tests/tcp-transport.node.test.ts +0 -2
  71. package/src/tests/webrtc-transport.test.ts +4 -3
  72. package/src/transport/tcp/index.ts +1 -1
  73. package/src/transport/webrtc/rtc-peer-connection.ts +1 -2
  74. package/src/transport/webrtc/rtc-transport-channel.test.ts +0 -2
  75. package/src/transport/webrtc/rtc-transport-channel.ts +1 -2
  76. package/src/transport/webrtc/rtc-transport-factory.ts +0 -1
  77. package/src/transport/webrtc/rtc-transport-proxy.test.ts +2 -3
  78. package/src/transport/webrtc/rtc-transport-proxy.ts +6 -4
  79. package/src/transport/webrtc/rtc-transport-service.ts +0 -1
  80. package/src/transport/webrtc/rtc-transport.test.ts +2 -2
  81. package/src/wire-protocol.ts +6 -6
  82. package/dist/lib/browser/chunk-NRRQ4H6U.mjs.map +0 -7
  83. package/dist/lib/node-esm/chunk-AX4TJ2DL.mjs.map +0 -7
@@ -8,19 +8,6 @@ import { invariant } from "@dxos/invariant";
8
8
  import { PublicKey } from "@dxos/keys";
9
9
  import { log, logInfo } from "@dxos/log";
10
10
  import { CancelledError, ConnectionResetError, ConnectivityError, ProtocolError, TimeoutError, trace } from "@dxos/protocols";
11
- function _define_property(obj, key, value) {
12
- if (key in obj) {
13
- Object.defineProperty(obj, key, {
14
- value,
15
- enumerable: true,
16
- configurable: true,
17
- writable: true
18
- });
19
- } else {
20
- obj[key] = value;
21
- }
22
- return obj;
23
- }
24
11
  function _ts_decorate(decorators, target, key, desc) {
25
12
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
26
13
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -44,6 +31,61 @@ var ConnectionState = /* @__PURE__ */ (function(ConnectionState5) {
44
31
  return ConnectionState5;
45
32
  })({});
46
33
  var Connection = class {
34
+ topic;
35
+ localInfo;
36
+ remoteInfo;
37
+ sessionId;
38
+ initiator;
39
+ _signalMessaging;
40
+ _protocol;
41
+ _transportFactory;
42
+ _callbacks;
43
+ _ctx = new Context(void 0, {
44
+ F: __dxlog_file,
45
+ L: 100
46
+ });
47
+ connectedTimeoutContext = new Context(void 0, {
48
+ F: __dxlog_file,
49
+ L: 101
50
+ });
51
+ _protocolClosed = new Trigger();
52
+ _transportClosed = new Trigger();
53
+ _state = "CREATED";
54
+ _transport;
55
+ closeReason;
56
+ _incomingSignalBuffer = [];
57
+ _outgoingSignalBuffer = [];
58
+ stateChanged = new Event();
59
+ errors = new ErrorStream();
60
+ _instanceId = PublicKey.random().toHex();
61
+ transportStats = new Event();
62
+ _signalSendTask = new DeferredTask(this._ctx, async () => {
63
+ await this._flushSignalBuffer(this._ctx);
64
+ });
65
+ _signallingDelay = STARTING_SIGNALLING_DELAY;
66
+ constructor(topic, localInfo, remoteInfo, sessionId, initiator, _signalMessaging, _protocol, _transportFactory, _callbacks) {
67
+ this.topic = topic;
68
+ this.localInfo = localInfo;
69
+ this.remoteInfo = remoteInfo;
70
+ this.sessionId = sessionId;
71
+ this.initiator = initiator;
72
+ this._signalMessaging = _signalMessaging;
73
+ this._protocol = _protocol;
74
+ this._transportFactory = _transportFactory;
75
+ this._callbacks = _callbacks;
76
+ log.trace("dxos.mesh.connection.construct", {
77
+ sessionId: this.sessionId,
78
+ topic: this.topic,
79
+ localPeer: this.localInfo,
80
+ remotePeer: this.remoteInfo,
81
+ initiator: this.initiator
82
+ }, {
83
+ F: __dxlog_file,
84
+ L: 137,
85
+ S: this,
86
+ C: (f, a) => f(...a)
87
+ });
88
+ }
47
89
  get sessionIdString() {
48
90
  return this.sessionId.truncate();
49
91
  }
@@ -102,21 +144,25 @@ var Connection = class {
102
144
  });
103
145
  this._protocolClosed.wake();
104
146
  this.close({
105
- error: new ProtocolError("protocol stream closed")
147
+ error: new ProtocolError({
148
+ message: "protocol stream closed"
149
+ })
106
150
  }).catch((err) => this.errors.raise(err));
107
151
  });
108
152
  scheduleTask(this.connectedTimeoutContext, async () => {
109
153
  log.info(`timeout waiting ${TRANSPORT_CONNECTION_TIMEOUT / 1e3}s for transport to connect, aborting`, void 0, {
110
154
  F: __dxlog_file,
111
- L: 194,
155
+ L: 196,
112
156
  S: this,
113
157
  C: (f, a) => f(...a)
114
158
  });
115
- await this.abort(new TimeoutError(`${TRANSPORT_CONNECTION_TIMEOUT / 1e3}s for transport to connect`)).catch((err) => this.errors.raise(err));
159
+ await this.abort(new TimeoutError({
160
+ message: `${TRANSPORT_CONNECTION_TIMEOUT / 1e3}s for transport to connect`
161
+ })).catch((err) => this.errors.raise(err));
116
162
  }, TRANSPORT_CONNECTION_TIMEOUT);
117
163
  invariant(!this._transport, void 0, {
118
164
  F: __dxlog_file,
119
- L: 202,
165
+ L: 204,
120
166
  S: this,
121
167
  A: [
122
168
  "!this._transport",
@@ -143,7 +189,7 @@ var Connection = class {
143
189
  this._transportClosed.wake();
144
190
  log("abort triggered by transport close", void 0, {
145
191
  F: __dxlog_file,
146
- L: 224,
192
+ L: 226,
147
193
  S: this,
148
194
  C: (f, a) => f(...a)
149
195
  });
@@ -154,7 +200,7 @@ var Connection = class {
154
200
  err
155
201
  }, {
156
202
  F: __dxlog_file,
157
- L: 229,
203
+ L: 231,
158
204
  S: this,
159
205
  C: (f, a) => f(...a)
160
206
  });
@@ -164,7 +210,7 @@ var Connection = class {
164
210
  if (err instanceof ConnectionResetError) {
165
211
  log.info("aborting due to transport ConnectionResetError", void 0, {
166
212
  F: __dxlog_file,
167
- L: 236,
213
+ L: 238,
168
214
  S: this,
169
215
  C: (f, a) => f(...a)
170
216
  });
@@ -172,7 +218,7 @@ var Connection = class {
172
218
  } else if (err instanceof ConnectivityError) {
173
219
  log.info("aborting due to transport ConnectivityError", void 0, {
174
220
  F: __dxlog_file,
175
- L: 239,
221
+ L: 241,
176
222
  S: this,
177
223
  C: (f, a) => f(...a)
178
224
  });
@@ -192,7 +238,7 @@ var Connection = class {
192
238
  id: this._instanceId
193
239
  }), {
194
240
  F: __dxlog_file,
195
- L: 258,
241
+ L: 260,
196
242
  S: this,
197
243
  C: (f, a) => f(...a)
198
244
  });
@@ -202,14 +248,14 @@ var Connection = class {
202
248
  err
203
249
  }, {
204
250
  F: __dxlog_file,
205
- L: 265,
251
+ L: 267,
206
252
  S: this,
207
253
  C: (f, a) => f(...a)
208
254
  });
209
255
  if (this._state === "CLOSED" || this._state === "ABORTED") {
210
256
  log(`abort ignored: already ${this._state}`, this.closeReason, {
211
257
  F: __dxlog_file,
212
- L: 267,
258
+ L: 269,
213
259
  S: this,
214
260
  C: (f, a) => f(...a)
215
261
  });
@@ -226,7 +272,7 @@ var Connection = class {
226
272
  err
227
273
  }, {
228
274
  F: __dxlog_file,
229
- L: 279,
275
+ L: 281,
230
276
  S: this,
231
277
  C: (f, a) => f(...a)
232
278
  });
@@ -237,7 +283,7 @@ var Connection = class {
237
283
  } catch (err2) {
238
284
  log.catch(err2, void 0, {
239
285
  F: __dxlog_file,
240
- L: 285,
286
+ L: 287,
241
287
  S: this,
242
288
  C: (f, a) => f(...a)
243
289
  });
@@ -247,7 +293,7 @@ var Connection = class {
247
293
  } catch (err2) {
248
294
  log.catch(err2, void 0, {
249
295
  F: __dxlog_file,
250
- L: 292,
296
+ L: 294,
251
297
  S: this,
252
298
  C: (f, a) => f(...a)
253
299
  });
@@ -257,7 +303,7 @@ var Connection = class {
257
303
  } catch (err2) {
258
304
  log.catch(err2, void 0, {
259
305
  F: __dxlog_file,
260
- L: 298,
306
+ L: 300,
261
307
  S: this,
262
308
  C: (f, a) => f(...a)
263
309
  });
@@ -269,7 +315,7 @@ var Connection = class {
269
315
  error
270
316
  }, {
271
317
  F: __dxlog_file,
272
- L: 305,
318
+ L: 307,
273
319
  S: this,
274
320
  C: (f, a) => f(...a)
275
321
  });
@@ -284,7 +330,7 @@ var Connection = class {
284
330
  error
285
331
  }, {
286
332
  F: __dxlog_file,
287
- L: 316,
333
+ L: 318,
288
334
  S: this,
289
335
  C: (f, a) => f(...a)
290
336
  });
@@ -298,7 +344,7 @@ var Connection = class {
298
344
  if (lastState !== "CONNECTED" || error != null) {
299
345
  log(`graceful close requested when we were in ${lastState} state? aborting`, void 0, {
300
346
  F: __dxlog_file,
301
- L: 327,
347
+ L: 329,
302
348
  S: this,
303
349
  C: (f, a) => f(...a)
304
350
  });
@@ -310,7 +356,7 @@ var Connection = class {
310
356
  error
311
357
  }, {
312
358
  F: __dxlog_file,
313
- L: 331,
359
+ L: 333,
314
360
  S: this,
315
361
  C: (f, a) => f(...a)
316
362
  });
@@ -321,7 +367,7 @@ var Connection = class {
321
367
  } catch (err) {
322
368
  log.catch(err, void 0, {
323
369
  F: __dxlog_file,
324
- L: 336,
370
+ L: 338,
325
371
  S: this,
326
372
  C: (f, a) => f(...a)
327
373
  });
@@ -331,7 +377,7 @@ var Connection = class {
331
377
  } catch (err) {
332
378
  log.catch(err, void 0, {
333
379
  F: __dxlog_file,
334
- L: 342,
380
+ L: 344,
335
381
  S: this,
336
382
  C: (f, a) => f(...a)
337
383
  });
@@ -340,7 +386,7 @@ var Connection = class {
340
386
  peerId: this.localInfo
341
387
  }, {
342
388
  F: __dxlog_file,
343
- L: 345,
389
+ L: 347,
344
390
  S: this,
345
391
  C: (f, a) => f(...a)
346
392
  });
@@ -350,7 +396,7 @@ var Connection = class {
350
396
  async _closeProtocol(options) {
351
397
  log("closing protocol", options, {
352
398
  F: __dxlog_file,
353
- L: 351,
399
+ L: 353,
354
400
  S: this,
355
401
  C: (f, a) => f(...a)
356
402
  });
@@ -360,7 +406,7 @@ var Connection = class {
360
406
  ]);
361
407
  log("protocol closed", options, {
362
408
  F: __dxlog_file,
363
- L: 353,
409
+ L: 355,
364
410
  S: this,
365
411
  C: (f, a) => f(...a)
366
412
  });
@@ -368,7 +414,7 @@ var Connection = class {
368
414
  async _closeTransport() {
369
415
  log("closing transport", void 0, {
370
416
  F: __dxlog_file,
371
- L: 357,
417
+ L: 359,
372
418
  S: this,
373
419
  C: (f, a) => f(...a)
374
420
  });
@@ -378,7 +424,7 @@ var Connection = class {
378
424
  ]);
379
425
  log("transport closed", void 0, {
380
426
  F: __dxlog_file,
381
- L: 359,
427
+ L: 361,
382
428
  S: this,
383
429
  C: (f, a) => f(...a)
384
430
  });
@@ -387,20 +433,20 @@ var Connection = class {
387
433
  this._outgoingSignalBuffer.push(signal);
388
434
  this._signalSendTask.schedule();
389
435
  }
390
- async _flushSignalBuffer() {
436
+ async _flushSignalBuffer(ctx) {
391
437
  if (this._outgoingSignalBuffer.length === 0) {
392
438
  return;
393
439
  }
394
440
  try {
395
441
  if (true) {
396
- await cancelWithContext(this._ctx, sleep(this._signallingDelay));
442
+ await cancelWithContext(ctx, sleep(this._signallingDelay));
397
443
  this._signallingDelay = Math.min(this._signallingDelay * 2, MAX_SIGNALLING_DELAY);
398
444
  }
399
445
  const signals = [
400
446
  ...this._outgoingSignalBuffer
401
447
  ];
402
448
  this._outgoingSignalBuffer.length = 0;
403
- await this._signalMessaging.signal({
449
+ await this._signalMessaging.signal(ctx, {
404
450
  author: this.localInfo,
405
451
  recipient: this.remoteInfo,
406
452
  sessionId: this.sessionId,
@@ -419,22 +465,25 @@ var Connection = class {
419
465
  err
420
466
  }, {
421
467
  F: __dxlog_file,
422
- L: 399,
468
+ L: 401,
423
469
  S: this,
424
470
  C: (f, a) => f(...a)
425
471
  });
426
472
  await this.close({
427
- error: new ConnectivityError("signal message failed to deliver", err)
473
+ error: new ConnectivityError({
474
+ message: "signal message failed to deliver",
475
+ cause: err
476
+ })
428
477
  });
429
478
  }
430
479
  }
431
480
  /**
432
481
  * Receive a signal from the remote peer.
433
482
  */
434
- async signal(msg) {
483
+ async signal(_ctx, msg) {
435
484
  invariant(msg.sessionId, void 0, {
436
485
  F: __dxlog_file,
437
- L: 408,
486
+ L: 410,
438
487
  S: this,
439
488
  A: [
440
489
  "msg.sessionId",
@@ -444,7 +493,7 @@ var Connection = class {
444
493
  if (!msg.sessionId.equals(this.sessionId)) {
445
494
  log("dropping signal for incorrect session id", void 0, {
446
495
  F: __dxlog_file,
447
- L: 410,
496
+ L: 412,
448
497
  S: this,
449
498
  C: (f, a) => f(...a)
450
499
  });
@@ -452,7 +501,7 @@ var Connection = class {
452
501
  }
453
502
  invariant(msg.data.signal || msg.data.signalBatch, void 0, {
454
503
  F: __dxlog_file,
455
- L: 413,
504
+ L: 415,
456
505
  S: this,
457
506
  A: [
458
507
  "msg.data.signal || msg.data.signalBatch",
@@ -461,7 +510,7 @@ var Connection = class {
461
510
  });
462
511
  invariant(msg.author.peerKey === this.remoteInfo.peerKey, void 0, {
463
512
  F: __dxlog_file,
464
- L: 414,
513
+ L: 416,
465
514
  S: this,
466
515
  A: [
467
516
  "msg.author.peerKey === this.remoteInfo.peerKey",
@@ -470,7 +519,7 @@ var Connection = class {
470
519
  });
471
520
  invariant(msg.recipient.peerKey === this.localInfo.peerKey, void 0, {
472
521
  F: __dxlog_file,
473
- L: 415,
522
+ L: 417,
474
523
  S: this,
475
524
  A: [
476
525
  "msg.recipient.peerKey === this.localInfo.peerKey",
@@ -494,7 +543,7 @@ var Connection = class {
494
543
  msg: msg.data
495
544
  }, {
496
545
  F: __dxlog_file,
497
- L: 424,
546
+ L: 426,
498
547
  S: this,
499
548
  C: (f, a) => f(...a)
500
549
  });
@@ -502,7 +551,7 @@ var Connection = class {
502
551
  } else {
503
552
  invariant(this._transport, "Connection not ready to accept signals.", {
504
553
  F: __dxlog_file,
505
- L: 427,
554
+ L: 429,
506
555
  S: this,
507
556
  A: [
508
557
  "this._transport",
@@ -515,7 +564,7 @@ var Connection = class {
515
564
  msg: msg.data
516
565
  }, {
517
566
  F: __dxlog_file,
518
- L: 428,
567
+ L: 430,
519
568
  S: this,
520
569
  C: (f, a) => f(...a)
521
570
  });
@@ -533,13 +582,13 @@ var Connection = class {
533
582
  peerId: this.localInfo
534
583
  }, {
535
584
  F: __dxlog_file,
536
- L: 439,
585
+ L: 441,
537
586
  S: this,
538
587
  C: (f, a) => f(...a)
539
588
  });
540
589
  invariant(state !== this._state, "Already in this state.", {
541
590
  F: __dxlog_file,
542
- L: 440,
591
+ L: 442,
543
592
  S: this,
544
593
  A: [
545
594
  "state !== this._state",
@@ -555,74 +604,6 @@ var Connection = class {
555
604
  this.transportStats.emit(stats);
556
605
  }
557
606
  }
558
- constructor(topic, localInfo, remoteInfo, sessionId, initiator, _signalMessaging, _protocol, _transportFactory, _callbacks) {
559
- _define_property(this, "topic", void 0);
560
- _define_property(this, "localInfo", void 0);
561
- _define_property(this, "remoteInfo", void 0);
562
- _define_property(this, "sessionId", void 0);
563
- _define_property(this, "initiator", void 0);
564
- _define_property(this, "_signalMessaging", void 0);
565
- _define_property(this, "_protocol", void 0);
566
- _define_property(this, "_transportFactory", void 0);
567
- _define_property(this, "_callbacks", void 0);
568
- _define_property(this, "_ctx", void 0);
569
- _define_property(this, "connectedTimeoutContext", void 0);
570
- _define_property(this, "_protocolClosed", void 0);
571
- _define_property(this, "_transportClosed", void 0);
572
- _define_property(this, "_state", void 0);
573
- _define_property(this, "_transport", void 0);
574
- _define_property(this, "closeReason", void 0);
575
- _define_property(this, "_incomingSignalBuffer", void 0);
576
- _define_property(this, "_outgoingSignalBuffer", void 0);
577
- _define_property(this, "stateChanged", void 0);
578
- _define_property(this, "errors", void 0);
579
- _define_property(this, "_instanceId", void 0);
580
- _define_property(this, "transportStats", void 0);
581
- _define_property(this, "_signalSendTask", void 0);
582
- _define_property(this, "_signallingDelay", void 0);
583
- this.topic = topic;
584
- this.localInfo = localInfo;
585
- this.remoteInfo = remoteInfo;
586
- this.sessionId = sessionId;
587
- this.initiator = initiator;
588
- this._signalMessaging = _signalMessaging;
589
- this._protocol = _protocol;
590
- this._transportFactory = _transportFactory;
591
- this._callbacks = _callbacks;
592
- this._ctx = new Context(void 0, {
593
- F: __dxlog_file,
594
- L: 100
595
- });
596
- this.connectedTimeoutContext = new Context(void 0, {
597
- F: __dxlog_file,
598
- L: 101
599
- });
600
- this._protocolClosed = new Trigger();
601
- this._transportClosed = new Trigger();
602
- this._state = "CREATED";
603
- this._incomingSignalBuffer = [];
604
- this._outgoingSignalBuffer = [];
605
- this.stateChanged = new Event();
606
- this.errors = new ErrorStream();
607
- this._instanceId = PublicKey.random().toHex();
608
- this.transportStats = new Event();
609
- this._signalSendTask = new DeferredTask(this._ctx, async () => {
610
- await this._flushSignalBuffer();
611
- });
612
- this._signallingDelay = STARTING_SIGNALLING_DELAY;
613
- log.trace("dxos.mesh.connection.construct", {
614
- sessionId: this.sessionId,
615
- topic: this.topic,
616
- localPeer: this.localInfo,
617
- remotePeer: this.remoteInfo,
618
- initiator: this.initiator
619
- }, {
620
- F: __dxlog_file,
621
- L: 137,
622
- S: this,
623
- C: (f, a) => f(...a)
624
- });
625
- }
626
607
  };
627
608
  _ts_decorate([
628
609
  logInfo
@@ -634,64 +615,28 @@ _ts_decorate([
634
615
  synchronized
635
616
  ], Connection.prototype, "close", null);
636
617
 
637
- // src/signal/ice.ts
638
- import { asyncTimeout } from "@dxos/async";
639
- import { log as log2 } from "@dxos/log";
640
- import { isNonNullable } from "@dxos/util";
641
- var __dxlog_file2 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/signal/ice.ts";
642
- var createIceProvider = (iceProviders) => {
643
- let cachedIceServers;
644
- return {
645
- getIceServers: async () => {
646
- if (cachedIceServers) {
647
- return cachedIceServers;
648
- }
649
- cachedIceServers = (await Promise.all(iceProviders.map(({ urls }) => asyncTimeout(fetch(urls, {
650
- method: "GET"
651
- }), 1e4).then((response) => response.json()).catch((err) => {
652
- const isDev = typeof window !== "undefined" && window.location.href.includes("localhost");
653
- if (!isDev) {
654
- log2.error("Failed to fetch ICE servers from provider", {
655
- urls,
656
- err
657
- }, {
658
- F: __dxlog_file2,
659
- L: 30,
660
- S: void 0,
661
- C: (f, a) => f(...a)
662
- });
663
- }
664
- })))).filter(isNonNullable).map(({ iceServers }) => iceServers).flat();
665
- return cachedIceServers;
666
- }
667
- };
668
- };
669
-
670
618
  // src/signal/swarm-messenger.ts
671
- import { Context as Context2 } from "@dxos/context";
672
619
  import { invariant as invariant2 } from "@dxos/invariant";
673
620
  import { PublicKey as PublicKey2 } from "@dxos/keys";
674
- import { log as log3 } from "@dxos/log";
621
+ import { log as log2 } from "@dxos/log";
675
622
  import { TimeoutError as TimeoutError2 } from "@dxos/protocols";
676
623
  import { schema } from "@dxos/protocols/proto";
677
624
  import { ComplexMap } from "@dxos/util";
678
- function _define_property2(obj, key, value) {
679
- if (key in obj) {
680
- Object.defineProperty(obj, key, {
681
- value,
682
- enumerable: true,
683
- configurable: true,
684
- writable: true
685
- });
686
- } else {
687
- obj[key] = value;
688
- }
689
- return obj;
690
- }
691
- var __dxlog_file3 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/signal/swarm-messenger.ts";
625
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/signal/swarm-messenger.ts";
692
626
  var SwarmMessage = schema.getCodecForType("dxos.mesh.swarm.SwarmMessage");
693
627
  var SwarmMessenger = class {
694
- async receiveMessage({ author, recipient, payload }) {
628
+ _sendMessage;
629
+ _onSignal;
630
+ _onOffer;
631
+ _topic;
632
+ _offerRecords = new ComplexMap((key) => key.toHex());
633
+ constructor({ sendMessage, onSignal, onOffer, topic }) {
634
+ this._sendMessage = sendMessage;
635
+ this._onSignal = onSignal;
636
+ this._onOffer = onOffer;
637
+ this._topic = topic;
638
+ }
639
+ async receiveMessage(ctx, { author, recipient, payload }) {
695
640
  if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
696
641
  return;
697
642
  }
@@ -699,18 +644,18 @@ var SwarmMessenger = class {
699
644
  if (!this._topic.equals(message.topic)) {
700
645
  return;
701
646
  }
702
- log3("received", {
647
+ log2("received", {
703
648
  from: author,
704
649
  to: recipient,
705
650
  msg: message
706
651
  }, {
707
- F: __dxlog_file3,
708
- L: 71,
652
+ F: __dxlog_file2,
653
+ L: 72,
709
654
  S: this,
710
655
  C: (f, a) => f(...a)
711
656
  });
712
657
  if (message.data?.offer) {
713
- await this._handleOffer({
658
+ await this._handleOffer(ctx, {
714
659
  author,
715
660
  recipient,
716
661
  message
@@ -718,45 +663,45 @@ var SwarmMessenger = class {
718
663
  } else if (message.data?.answer) {
719
664
  await this._resolveAnswers(message);
720
665
  } else if (message.data?.signal) {
721
- await this._handleSignal({
666
+ await this._handleSignal(ctx, {
722
667
  author,
723
668
  recipient,
724
669
  message
725
670
  });
726
671
  } else if (message.data?.signalBatch) {
727
- await this._handleSignal({
672
+ await this._handleSignal(ctx, {
728
673
  author,
729
674
  recipient,
730
675
  message
731
676
  });
732
677
  } else {
733
- log3.warn("unknown message", {
678
+ log2.warn("unknown message", {
734
679
  message
735
680
  }, {
736
- F: __dxlog_file3,
737
- L: 82,
681
+ F: __dxlog_file2,
682
+ L: 83,
738
683
  S: this,
739
684
  C: (f, a) => f(...a)
740
685
  });
741
686
  }
742
687
  }
743
- async signal(message) {
688
+ async signal(ctx, message) {
744
689
  invariant2(message.data?.signal || message.data?.signalBatch, "Invalid message", {
745
- F: __dxlog_file3,
746
- L: 87,
690
+ F: __dxlog_file2,
691
+ L: 88,
747
692
  S: this,
748
693
  A: [
749
694
  "message.data?.signal || message.data?.signalBatch",
750
695
  "'Invalid message'"
751
696
  ]
752
697
  });
753
- await this._sendReliableMessage({
698
+ await this._sendReliableMessage(ctx, {
754
699
  author: message.author,
755
700
  recipient: message.recipient,
756
701
  message
757
702
  });
758
703
  }
759
- async offer(message) {
704
+ async offer(ctx, message) {
760
705
  const networkMessage = {
761
706
  ...message,
762
707
  messageId: PublicKey2.random()
@@ -765,30 +710,30 @@ var SwarmMessenger = class {
765
710
  this._offerRecords.set(networkMessage.messageId, {
766
711
  resolve
767
712
  });
768
- this._sendReliableMessage({
713
+ this._sendReliableMessage(ctx, {
769
714
  author: message.author,
770
715
  recipient: message.recipient,
771
716
  message: networkMessage
772
717
  }).catch((err) => reject(err));
773
718
  });
774
719
  }
775
- async _sendReliableMessage({ author, recipient, message }) {
720
+ async _sendReliableMessage(ctx, { author, recipient, message }) {
776
721
  const networkMessage = {
777
722
  ...message,
778
723
  // Setting unique message_id if it not specified yet.
779
724
  messageId: message.messageId ?? PublicKey2.random()
780
725
  };
781
- log3("sending", {
726
+ log2("sending", {
782
727
  from: author,
783
728
  to: recipient,
784
729
  msg: networkMessage
785
730
  }, {
786
- F: __dxlog_file3,
787
- L: 125,
731
+ F: __dxlog_file2,
732
+ L: 129,
788
733
  S: this,
789
734
  C: (f, a) => f(...a)
790
735
  });
791
- await this._sendMessage({
736
+ await this._sendMessage(ctx, {
792
737
  author,
793
738
  recipient,
794
739
  payload: {
@@ -799,8 +744,8 @@ var SwarmMessenger = class {
799
744
  }
800
745
  async _resolveAnswers(message) {
801
746
  invariant2(message.data?.answer?.offerMessageId, "No offerMessageId", {
802
- F: __dxlog_file3,
803
- L: 137,
747
+ F: __dxlog_file2,
748
+ L: 141,
804
749
  S: this,
805
750
  A: [
806
751
  "message.data?.answer?.offerMessageId",
@@ -811,29 +756,29 @@ var SwarmMessenger = class {
811
756
  if (offerRecord) {
812
757
  this._offerRecords.delete(message.data.answer.offerMessageId);
813
758
  invariant2(message.data?.answer, "No answer", {
814
- F: __dxlog_file3,
815
- L: 141,
759
+ F: __dxlog_file2,
760
+ L: 145,
816
761
  S: this,
817
762
  A: [
818
763
  "message.data?.answer",
819
764
  "'No answer'"
820
765
  ]
821
766
  });
822
- log3("resolving", {
767
+ log2("resolving", {
823
768
  answer: message.data.answer
824
769
  }, {
825
- F: __dxlog_file3,
826
- L: 142,
770
+ F: __dxlog_file2,
771
+ L: 146,
827
772
  S: this,
828
773
  C: (f, a) => f(...a)
829
774
  });
830
775
  offerRecord.resolve(message.data.answer);
831
776
  }
832
777
  }
833
- async _handleOffer({ author, recipient, message }) {
778
+ async _handleOffer(ctx, { author, recipient, message }) {
834
779
  invariant2(message.data.offer, "No offer", {
835
- F: __dxlog_file3,
836
- L: 156,
780
+ F: __dxlog_file2,
781
+ L: 163,
837
782
  S: this,
838
783
  A: [
839
784
  "message.data.offer",
@@ -848,10 +793,10 @@ var SwarmMessenger = class {
848
793
  offer: message.data.offer
849
794
  }
850
795
  };
851
- const answer = await this._onOffer(offerMessage);
796
+ const answer = await this._onOffer(ctx, offerMessage);
852
797
  answer.offerMessageId = message.messageId;
853
798
  try {
854
- await this._sendReliableMessage({
799
+ await this._sendReliableMessage(ctx, {
855
800
  author: recipient,
856
801
  recipient: author,
857
802
  message: {
@@ -864,30 +809,30 @@ var SwarmMessenger = class {
864
809
  });
865
810
  } catch (err) {
866
811
  if (err instanceof TimeoutError2) {
867
- log3.info("timeout sending answer to offer", {
812
+ log2.info("timeout sending answer to offer", {
868
813
  err
869
814
  }, {
870
- F: __dxlog_file3,
871
- L: 177,
815
+ F: __dxlog_file2,
816
+ L: 184,
872
817
  S: this,
873
818
  C: (f, a) => f(...a)
874
819
  });
875
820
  } else {
876
- log3.info("error sending answer to offer", {
821
+ log2.info("error sending answer to offer", {
877
822
  err
878
823
  }, {
879
- F: __dxlog_file3,
880
- L: 179,
824
+ F: __dxlog_file2,
825
+ L: 186,
881
826
  S: this,
882
827
  C: (f, a) => f(...a)
883
828
  });
884
829
  }
885
830
  }
886
831
  }
887
- async _handleSignal({ author, recipient, message }) {
832
+ async _handleSignal(ctx, { author, recipient, message }) {
888
833
  invariant2(message.messageId, void 0, {
889
- F: __dxlog_file3,
890
- L: 193,
834
+ F: __dxlog_file2,
835
+ L: 203,
891
836
  S: this,
892
837
  A: [
893
838
  "message.messageId",
@@ -895,8 +840,8 @@ var SwarmMessenger = class {
895
840
  ]
896
841
  });
897
842
  invariant2(message.data.signal || message.data.signalBatch, "Invalid message", {
898
- F: __dxlog_file3,
899
- L: 194,
843
+ F: __dxlog_file2,
844
+ L: 204,
900
845
  S: this,
901
846
  A: [
902
847
  "message.data.signal || message.data.signalBatch",
@@ -912,83 +857,93 @@ var SwarmMessenger = class {
912
857
  signalBatch: message.data.signalBatch
913
858
  }
914
859
  };
915
- await this._onSignal(signalMessage);
916
- }
917
- constructor({ sendMessage, onSignal, onOffer, topic }) {
918
- _define_property2(this, "_ctx", new Context2(void 0, {
919
- F: __dxlog_file3,
920
- L: 35
921
- }));
922
- _define_property2(this, "_sendMessage", void 0);
923
- _define_property2(this, "_onSignal", void 0);
924
- _define_property2(this, "_onOffer", void 0);
925
- _define_property2(this, "_topic", void 0);
926
- _define_property2(this, "_offerRecords", new ComplexMap((key) => key.toHex()));
927
- this._sendMessage = sendMessage;
928
- this._onSignal = onSignal;
929
- this._onOffer = onOffer;
930
- this._topic = topic;
860
+ await this._onSignal(ctx, signalMessage);
931
861
  }
932
862
  };
933
863
 
934
864
  // src/swarm/swarm.ts
935
865
  import { Event as Event3, scheduleTask as scheduleTask3, sleep as sleep2, synchronized as synchronized3 } from "@dxos/async";
936
- import { Context as Context4 } from "@dxos/context";
866
+ import { Context as Context3 } from "@dxos/context";
937
867
  import { ErrorStream as ErrorStream2 } from "@dxos/debug";
938
868
  import { invariant as invariant4 } from "@dxos/invariant";
939
869
  import { PublicKey as PublicKey4 } from "@dxos/keys";
940
- import { log as log5, logInfo as logInfo2 } from "@dxos/log";
870
+ import { log as log4, logInfo as logInfo2 } from "@dxos/log";
941
871
  import { PeerInfoHash } from "@dxos/messaging";
942
872
  import { trace as trace2 } from "@dxos/protocols";
943
- import { ComplexMap as ComplexMap2, isNonNullable as isNonNullable2 } from "@dxos/util";
873
+ import { ComplexMap as ComplexMap2, isNonNullable } from "@dxos/util";
944
874
 
945
875
  // src/swarm/peer.ts
946
876
  import { Event as Event2, scheduleTask as scheduleTask2, synchronized as synchronized2 } from "@dxos/async";
947
- import { Context as Context3 } from "@dxos/context";
877
+ import { Context as Context2 } from "@dxos/context";
948
878
  import { invariant as invariant3 } from "@dxos/invariant";
949
879
  import { PublicKey as PublicKey3 } from "@dxos/keys";
950
- import { log as log4 } from "@dxos/log";
880
+ import { log as log3 } from "@dxos/log";
951
881
  import { CancelledError as CancelledError2, SystemError } from "@dxos/protocols";
952
- function _define_property3(obj, key, value) {
953
- if (key in obj) {
954
- Object.defineProperty(obj, key, {
955
- value,
956
- enumerable: true,
957
- configurable: true,
958
- writable: true
959
- });
960
- } else {
961
- obj[key] = value;
962
- }
963
- return obj;
964
- }
965
882
  function _ts_decorate2(decorators, target, key, desc) {
966
883
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
967
884
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
968
885
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
969
886
  return c > 3 && r && Object.defineProperty(target, key, r), r;
970
887
  }
971
- var __dxlog_file4 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
888
+ var __dxlog_file3 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
972
889
  var ConnectionDisplacedError = class extends SystemError {
973
890
  constructor() {
974
- super("Connection displaced by remote initiator.");
891
+ super({
892
+ message: "Connection displaced by remote initiator."
893
+ });
975
894
  }
976
895
  };
977
896
  var CONNECTION_COUNTS_STABLE_AFTER = 5e3;
978
897
  var Peer = class {
898
+ remoteInfo;
899
+ topic;
900
+ localInfo;
901
+ _signalMessaging;
902
+ _protocolProvider;
903
+ _transportFactory;
904
+ _connectionLimiter;
905
+ _callbacks;
906
+ /**
907
+ * Will be available to connect after this time.
908
+ */
909
+ _availableAfter = 0;
910
+ availableToConnect = true;
911
+ _lastConnectionTime;
912
+ _ctx = new Context2(void 0, {
913
+ F: __dxlog_file3,
914
+ L: 80
915
+ });
916
+ _connectionCtx;
917
+ connection;
918
+ /**
919
+ * Whether the peer is currently advertizing itself on the signal-network.
920
+ */
921
+ advertizing = false;
922
+ initiating = false;
923
+ connectionDisplaced = new Event2();
924
+ constructor(remoteInfo, topic, localInfo, _signalMessaging, _protocolProvider, _transportFactory, _connectionLimiter, _callbacks) {
925
+ this.remoteInfo = remoteInfo;
926
+ this.topic = topic;
927
+ this.localInfo = localInfo;
928
+ this._signalMessaging = _signalMessaging;
929
+ this._protocolProvider = _protocolProvider;
930
+ this._transportFactory = _transportFactory;
931
+ this._connectionLimiter = _connectionLimiter;
932
+ this._callbacks = _callbacks;
933
+ }
979
934
  /**
980
935
  * Respond to remote offer.
981
936
  */
982
- async onOffer(message) {
937
+ async onOffer(_ctx, message) {
983
938
  const remote = message.author;
984
939
  if (this.connection && ![
985
940
  ConnectionState.CREATED,
986
941
  ConnectionState.INITIAL,
987
942
  ConnectionState.CONNECTING
988
943
  ].includes(this.connection.state)) {
989
- log4.info(`received offer when connection already in ${this.connection.state} state`, void 0, {
990
- F: __dxlog_file4,
991
- L: 116,
944
+ log3.info(`received offer when connection already in ${this.connection.state} state`, void 0, {
945
+ F: __dxlog_file3,
946
+ L: 115,
992
947
  S: this,
993
948
  C: (f, a) => f(...a)
994
949
  });
@@ -998,14 +953,14 @@ var Peer = class {
998
953
  }
999
954
  if (this.connection || this.initiating) {
1000
955
  if (remote.peerKey < this.localInfo.peerKey) {
1001
- log4("close local connection", {
956
+ log3("close local connection", {
1002
957
  localPeer: this.localInfo,
1003
958
  topic: this.topic,
1004
959
  remotePeer: this.remoteInfo,
1005
960
  sessionId: this.connection?.sessionId
1006
961
  }, {
1007
- F: __dxlog_file4,
1008
- L: 125,
962
+ F: __dxlog_file3,
963
+ L: 124,
1009
964
  S: this,
1010
965
  C: (f, a) => f(...a)
1011
966
  });
@@ -1021,8 +976,8 @@ var Peer = class {
1021
976
  if (await this._callbacks.onOffer(remote)) {
1022
977
  if (!this.connection) {
1023
978
  invariant3(message.sessionId, void 0, {
1024
- F: __dxlog_file4,
1025
- L: 145,
979
+ F: __dxlog_file3,
980
+ L: 144,
1026
981
  S: this,
1027
982
  A: [
1028
983
  "message.sessionId",
@@ -1036,14 +991,14 @@ var Peer = class {
1036
991
  await connection.openConnection();
1037
992
  } catch (err) {
1038
993
  if (!(err instanceof CancelledError2)) {
1039
- log4.info("connection error", {
994
+ log3.info("connection error", {
1040
995
  topic: this.topic,
1041
996
  peerId: this.localInfo,
1042
997
  remoteId: this.remoteInfo,
1043
998
  err
1044
999
  }, {
1045
- F: __dxlog_file4,
1046
- L: 155,
1000
+ F: __dxlog_file3,
1001
+ L: 154,
1047
1002
  S: this,
1048
1003
  C: (f, a) => f(...a)
1049
1004
  });
@@ -1062,9 +1017,9 @@ var Peer = class {
1062
1017
  /**
1063
1018
  * Initiate a connection to the remote peer.
1064
1019
  */
1065
- async initiateConnection() {
1020
+ async initiateConnection(ctx) {
1066
1021
  invariant3(!this.initiating, "Initiation in progress.", {
1067
- F: __dxlog_file4,
1022
+ F: __dxlog_file3,
1068
1023
  L: 172,
1069
1024
  S: this,
1070
1025
  A: [
@@ -1073,7 +1028,7 @@ var Peer = class {
1073
1028
  ]
1074
1029
  });
1075
1030
  invariant3(!this.connection, "Already connected.", {
1076
- F: __dxlog_file4,
1031
+ F: __dxlog_file3,
1077
1032
  L: 173,
1078
1033
  S: this,
1079
1034
  A: [
@@ -1082,13 +1037,13 @@ var Peer = class {
1082
1037
  ]
1083
1038
  });
1084
1039
  const sessionId = PublicKey3.random();
1085
- log4("initiating...", {
1040
+ log3("initiating...", {
1086
1041
  local: this.localInfo,
1087
1042
  topic: this.topic,
1088
1043
  remote: this.remoteInfo,
1089
1044
  sessionId
1090
1045
  }, {
1091
- F: __dxlog_file4,
1046
+ F: __dxlog_file3,
1092
1047
  L: 175,
1093
1048
  S: this,
1094
1049
  C: (f, a) => f(...a)
@@ -1099,7 +1054,7 @@ var Peer = class {
1099
1054
  try {
1100
1055
  await this._connectionLimiter.connecting(sessionId);
1101
1056
  connection.initiate();
1102
- answer = await this._signalMessaging.offer({
1057
+ answer = await this._signalMessaging.offer(ctx, {
1103
1058
  author: this.localInfo,
1104
1059
  recipient: this.remoteInfo,
1105
1060
  sessionId,
@@ -1108,20 +1063,20 @@ var Peer = class {
1108
1063
  offer: {}
1109
1064
  }
1110
1065
  });
1111
- log4("received", {
1066
+ log3("received", {
1112
1067
  answer,
1113
1068
  topic: this.topic,
1114
1069
  local: this.localInfo,
1115
1070
  remote: this.remoteInfo
1116
1071
  }, {
1117
- F: __dxlog_file4,
1072
+ F: __dxlog_file3,
1118
1073
  L: 192,
1119
1074
  S: this,
1120
1075
  C: (f, a) => f(...a)
1121
1076
  });
1122
1077
  if (connection.state !== ConnectionState.INITIAL) {
1123
- log4("ignoring response", void 0, {
1124
- F: __dxlog_file4,
1078
+ log3("ignoring response", void 0, {
1079
+ F: __dxlog_file3,
1125
1080
  L: 194,
1126
1081
  S: this,
1127
1082
  C: (f, a) => f(...a)
@@ -1129,13 +1084,13 @@ var Peer = class {
1129
1084
  return;
1130
1085
  }
1131
1086
  } catch (err) {
1132
- log4("initiation error: send offer", {
1087
+ log3("initiation error: send offer", {
1133
1088
  err,
1134
1089
  topic: this.topic,
1135
1090
  local: this.localInfo,
1136
1091
  remote: this.remoteInfo
1137
1092
  }, {
1138
- F: __dxlog_file4,
1093
+ F: __dxlog_file3,
1139
1094
  L: 198,
1140
1095
  S: this,
1141
1096
  C: (f, a) => f(...a)
@@ -1151,13 +1106,13 @@ var Peer = class {
1151
1106
  return;
1152
1107
  }
1153
1108
  } catch (err) {
1154
- log4("initiation error: accept answer", {
1109
+ log3("initiation error: accept answer", {
1155
1110
  err,
1156
1111
  topic: this.topic,
1157
1112
  local: this.localInfo,
1158
1113
  remote: this.remoteInfo
1159
1114
  }, {
1160
- F: __dxlog_file4,
1115
+ F: __dxlog_file3,
1161
1116
  L: 211,
1162
1117
  S: this,
1163
1118
  C: (f, a) => f(...a)
@@ -1168,8 +1123,8 @@ var Peer = class {
1168
1123
  this.initiating = false;
1169
1124
  }
1170
1125
  try {
1171
- log4("opening connection as initiator", void 0, {
1172
- F: __dxlog_file4,
1126
+ log3("opening connection as initiator", void 0, {
1127
+ F: __dxlog_file3,
1173
1128
  L: 224,
1174
1129
  S: this,
1175
1130
  C: (f, a) => f(...a)
@@ -1177,21 +1132,21 @@ var Peer = class {
1177
1132
  await connection.openConnection();
1178
1133
  this._callbacks.onAccepted();
1179
1134
  } catch (err) {
1180
- log4("initiation error: open connection", {
1135
+ log3("initiation error: open connection", {
1181
1136
  err,
1182
1137
  topic: this.topic,
1183
1138
  local: this.localInfo,
1184
1139
  remote: this.remoteInfo
1185
1140
  }, {
1186
- F: __dxlog_file4,
1141
+ F: __dxlog_file3,
1187
1142
  L: 228,
1188
1143
  S: this,
1189
1144
  C: (f, a) => f(...a)
1190
1145
  });
1191
- log4.warn("closing connection due to unhandled error on openConnection", {
1146
+ log3.warn("closing connection due to unhandled error on openConnection", {
1192
1147
  err
1193
1148
  }, {
1194
- F: __dxlog_file4,
1149
+ F: __dxlog_file3,
1195
1150
  L: 235,
1196
1151
  S: this,
1197
1152
  C: (f, a) => f(...a)
@@ -1207,20 +1162,20 @@ var Peer = class {
1207
1162
  * Either we're initiating a connection or creating one in response to an offer from the other peer.
1208
1163
  */
1209
1164
  _createConnection(initiator, sessionId) {
1210
- log4("creating connection", {
1165
+ log3("creating connection", {
1211
1166
  topic: this.topic,
1212
1167
  peerId: this.localInfo,
1213
1168
  remoteId: this.remoteInfo,
1214
1169
  initiator,
1215
1170
  sessionId
1216
1171
  }, {
1217
- F: __dxlog_file4,
1172
+ F: __dxlog_file3,
1218
1173
  L: 249,
1219
1174
  S: this,
1220
1175
  C: (f, a) => f(...a)
1221
1176
  });
1222
1177
  invariant3(!this.connection, "Already connected.", {
1223
- F: __dxlog_file4,
1178
+ F: __dxlog_file3,
1224
1179
  L: 256,
1225
1180
  S: this,
1226
1181
  A: [
@@ -1249,14 +1204,14 @@ var Peer = class {
1249
1204
  this._lastConnectionTime = Date.now();
1250
1205
  this._callbacks.onConnected();
1251
1206
  this._connectionLimiter.doneConnecting(sessionId);
1252
- log4.trace("dxos.mesh.connection.connected", {
1207
+ log3.trace("dxos.mesh.connection.connected", {
1253
1208
  topic: this.topic,
1254
1209
  localPeerId: this.localInfo,
1255
1210
  remotePeerId: this.remoteInfo,
1256
1211
  sessionId,
1257
1212
  initiator
1258
1213
  }, {
1259
- F: __dxlog_file4,
1214
+ F: __dxlog_file3,
1260
1215
  L: 280,
1261
1216
  S: this,
1262
1217
  C: (f, a) => f(...a)
@@ -1269,15 +1224,15 @@ var Peer = class {
1269
1224
  remoteId: this.remoteInfo,
1270
1225
  initiator
1271
1226
  };
1272
- log4("connection closed", logMeta, {
1273
- F: __dxlog_file4,
1227
+ log3("connection closed", logMeta, {
1228
+ F: __dxlog_file3,
1274
1229
  L: 290,
1275
1230
  S: this,
1276
1231
  C: (f, a) => f(...a)
1277
1232
  });
1278
1233
  this._connectionLimiter.doneConnecting(sessionId);
1279
1234
  invariant3(this.connection === connection, "Connection mismatch (race condition).", {
1280
- F: __dxlog_file4,
1235
+ F: __dxlog_file3,
1281
1236
  L: 295,
1282
1237
  S: this,
1283
1238
  A: [
@@ -1285,14 +1240,14 @@ var Peer = class {
1285
1240
  "'Connection mismatch (race condition).'"
1286
1241
  ]
1287
1242
  });
1288
- log4.trace("dxos.mesh.connection.closed", {
1243
+ log3.trace("dxos.mesh.connection.closed", {
1289
1244
  topic: this.topic,
1290
1245
  localPeerId: this.localInfo,
1291
1246
  remotePeerId: this.remoteInfo,
1292
1247
  sessionId,
1293
1248
  initiator
1294
1249
  }, {
1295
- F: __dxlog_file4,
1250
+ F: __dxlog_file3,
1296
1251
  L: 297,
1297
1252
  S: this,
1298
1253
  C: (f, a) => f(...a)
@@ -1308,8 +1263,8 @@ var Peer = class {
1308
1263
  }
1309
1264
  this._callbacks.onDisconnected();
1310
1265
  scheduleTask2(this._connectionCtx, () => {
1311
- log4("peer became available", logMeta, {
1312
- F: __dxlog_file4,
1266
+ log3("peer became available", logMeta, {
1267
+ F: __dxlog_file3,
1313
1268
  L: 321,
1314
1269
  S: this,
1315
1270
  C: (f, a) => f(...a)
@@ -1326,19 +1281,19 @@ var Peer = class {
1326
1281
  void this._connectionCtx?.dispose();
1327
1282
  this._connectionCtx = this._ctx.derive();
1328
1283
  connection.errors.handle((err) => {
1329
- log4.info("connection error, closing", {
1284
+ log3.info("connection error, closing", {
1330
1285
  topic: this.topic,
1331
1286
  peerId: this.localInfo,
1332
1287
  remoteId: this.remoteInfo,
1333
1288
  initiator,
1334
1289
  err
1335
1290
  }, {
1336
- F: __dxlog_file4,
1291
+ F: __dxlog_file3,
1337
1292
  L: 339,
1338
1293
  S: this,
1339
1294
  C: (f, a) => f(...a)
1340
1295
  });
1341
- log4.trace("dxos.mesh.connection.error", {
1296
+ log3.trace("dxos.mesh.connection.error", {
1342
1297
  topic: this.topic,
1343
1298
  localPeerId: this.localInfo,
1344
1299
  remotePeerId: this.remoteInfo,
@@ -1346,7 +1301,7 @@ var Peer = class {
1346
1301
  initiator,
1347
1302
  err
1348
1303
  }, {
1349
- F: __dxlog_file4,
1304
+ F: __dxlog_file3,
1350
1305
  L: 346,
1351
1306
  S: this,
1352
1307
  C: (f, a) => f(...a)
@@ -1361,11 +1316,11 @@ var Peer = class {
1361
1316
  return;
1362
1317
  }
1363
1318
  const connection = this.connection;
1364
- log4("closing...", {
1319
+ log3("closing...", {
1365
1320
  peerId: this.remoteInfo,
1366
1321
  sessionId: connection.sessionId
1367
1322
  }, {
1368
- F: __dxlog_file4,
1323
+ F: __dxlog_file3,
1369
1324
  L: 371,
1370
1325
  S: this,
1371
1326
  C: (f, a) => f(...a)
@@ -1373,37 +1328,37 @@ var Peer = class {
1373
1328
  await connection.close({
1374
1329
  error: err
1375
1330
  });
1376
- log4("closed", {
1331
+ log3("closed", {
1377
1332
  peerId: this.remoteInfo,
1378
1333
  sessionId: connection.sessionId
1379
1334
  }, {
1380
- F: __dxlog_file4,
1335
+ F: __dxlog_file3,
1381
1336
  L: 377,
1382
1337
  S: this,
1383
1338
  C: (f, a) => f(...a)
1384
1339
  });
1385
1340
  }
1386
- async onSignal(message) {
1341
+ async onSignal(ctx, message) {
1387
1342
  if (!this.connection) {
1388
- log4("dropping signal message for non-existent connection", {
1343
+ log3("dropping signal message for non-existent connection", {
1389
1344
  message
1390
1345
  }, {
1391
- F: __dxlog_file4,
1346
+ F: __dxlog_file3,
1392
1347
  L: 382,
1393
1348
  S: this,
1394
1349
  C: (f, a) => f(...a)
1395
1350
  });
1396
1351
  return;
1397
1352
  }
1398
- await this.connection.signal(message);
1353
+ await this.connection.signal(ctx, message);
1399
1354
  }
1400
1355
  async safeDestroy(reason) {
1401
1356
  await this._ctx.dispose();
1402
- log4("Destroying peer", {
1357
+ log3("Destroying peer", {
1403
1358
  peerId: this.remoteInfo,
1404
1359
  topic: this.topic
1405
1360
  }, {
1406
- F: __dxlog_file4,
1361
+ F: __dxlog_file3,
1407
1362
  L: 392,
1408
1363
  S: this,
1409
1364
  C: (f, a) => f(...a)
@@ -1412,42 +1367,6 @@ var Peer = class {
1412
1367
  reason
1413
1368
  });
1414
1369
  }
1415
- constructor(remoteInfo, topic, localInfo, _signalMessaging, _protocolProvider, _transportFactory, _connectionLimiter, _callbacks) {
1416
- _define_property3(this, "remoteInfo", void 0);
1417
- _define_property3(this, "topic", void 0);
1418
- _define_property3(this, "localInfo", void 0);
1419
- _define_property3(this, "_signalMessaging", void 0);
1420
- _define_property3(this, "_protocolProvider", void 0);
1421
- _define_property3(this, "_transportFactory", void 0);
1422
- _define_property3(this, "_connectionLimiter", void 0);
1423
- _define_property3(this, "_callbacks", void 0);
1424
- _define_property3(this, "_availableAfter", void 0);
1425
- _define_property3(this, "availableToConnect", void 0);
1426
- _define_property3(this, "_lastConnectionTime", void 0);
1427
- _define_property3(this, "_ctx", void 0);
1428
- _define_property3(this, "_connectionCtx", void 0);
1429
- _define_property3(this, "connection", void 0);
1430
- _define_property3(this, "advertizing", void 0);
1431
- _define_property3(this, "initiating", void 0);
1432
- _define_property3(this, "connectionDisplaced", void 0);
1433
- this.remoteInfo = remoteInfo;
1434
- this.topic = topic;
1435
- this.localInfo = localInfo;
1436
- this._signalMessaging = _signalMessaging;
1437
- this._protocolProvider = _protocolProvider;
1438
- this._transportFactory = _transportFactory;
1439
- this._connectionLimiter = _connectionLimiter;
1440
- this._callbacks = _callbacks;
1441
- this._availableAfter = 0;
1442
- this.availableToConnect = true;
1443
- this._ctx = new Context3(void 0, {
1444
- F: __dxlog_file4,
1445
- L: 81
1446
- });
1447
- this.advertizing = false;
1448
- this.initiating = false;
1449
- this.connectionDisplaced = new Event2();
1450
- }
1451
1370
  };
1452
1371
  _ts_decorate2([
1453
1372
  synchronized2
@@ -1466,31 +1385,106 @@ var increaseInterval = (interval) => {
1466
1385
  };
1467
1386
 
1468
1387
  // src/swarm/swarm.ts
1469
- function _define_property4(obj, key, value) {
1470
- if (key in obj) {
1471
- Object.defineProperty(obj, key, {
1472
- value,
1473
- enumerable: true,
1474
- configurable: true,
1475
- writable: true
1476
- });
1477
- } else {
1478
- obj[key] = value;
1479
- }
1480
- return obj;
1481
- }
1482
1388
  function _ts_decorate3(decorators, target, key, desc) {
1483
1389
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1484
1390
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1485
1391
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1486
1392
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1487
1393
  }
1488
- var __dxlog_file5 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts";
1394
+ var __dxlog_file4 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts";
1489
1395
  var INITIATION_DELAY = 100;
1490
1396
  var getClassName = (obj) => Object.getPrototypeOf(obj).constructor.name;
1491
1397
  var Swarm = class {
1398
+ _topic;
1399
+ _ownPeer;
1400
+ _topology;
1401
+ _protocolProvider;
1402
+ _messenger;
1403
+ _transportFactory;
1404
+ _label;
1405
+ _connectionLimiter;
1406
+ _initiationDelay;
1407
+ _swarmMessenger;
1408
+ _ctx = new Context3(void 0, {
1409
+ F: __dxlog_file4,
1410
+ L: 38
1411
+ });
1412
+ _listeningHandle = void 0;
1413
+ /**
1414
+ * PeerInfo -> Peer.
1415
+ * @internal
1416
+ */
1417
+ _peers = new ComplexMap2(PeerInfoHash);
1418
+ /**
1419
+ * Unique id of the swarm, local to the current peer, generated when swarm is joined.
1420
+ */
1421
+ _instanceId = PublicKey4.random().toHex();
1422
+ /**
1423
+ * New connection to a peer is started.
1424
+ * @internal
1425
+ */
1426
+ connectionAdded = new Event3();
1427
+ /**
1428
+ * Connection to a peer is dropped.
1429
+ * @internal
1430
+ */
1431
+ disconnected = new Event3();
1432
+ /**
1433
+ * Connection is established to a new peer.
1434
+ * @internal
1435
+ */
1436
+ connected = new Event3();
1437
+ errors = new ErrorStream2();
1438
+ // TODO(burdon): Swarm => Peer.create/destroy =< Connection.open/close
1439
+ // TODO(burdon): Pass in object.
1440
+ constructor(_topic, _ownPeer, _topology, _protocolProvider, _messenger, _transportFactory, _label, _connectionLimiter, _initiationDelay = INITIATION_DELAY) {
1441
+ this._topic = _topic;
1442
+ this._ownPeer = _ownPeer;
1443
+ this._topology = _topology;
1444
+ this._protocolProvider = _protocolProvider;
1445
+ this._messenger = _messenger;
1446
+ this._transportFactory = _transportFactory;
1447
+ this._label = _label;
1448
+ this._connectionLimiter = _connectionLimiter;
1449
+ this._initiationDelay = _initiationDelay;
1450
+ log4.trace("dxos.mesh.swarm.constructor", trace2.begin({
1451
+ id: this._instanceId,
1452
+ data: {
1453
+ topic: this._topic.toHex(),
1454
+ peer: this._ownPeer
1455
+ }
1456
+ }), {
1457
+ F: __dxlog_file4,
1458
+ L: 88,
1459
+ S: this,
1460
+ C: (f, a) => f(...a)
1461
+ });
1462
+ log4("creating swarm", {
1463
+ peerId: _ownPeer
1464
+ }, {
1465
+ F: __dxlog_file4,
1466
+ L: 92,
1467
+ S: this,
1468
+ C: (f, a) => f(...a)
1469
+ });
1470
+ _topology.init(this._getSwarmController());
1471
+ this._swarmMessenger = new SwarmMessenger({
1472
+ sendMessage: async (ctx, msg) => await this._messenger.sendMessage(ctx, msg),
1473
+ onSignal: async (ctx, msg) => await this.onSignal(ctx, msg),
1474
+ onOffer: async (ctx, msg) => await this.onOffer(ctx, msg),
1475
+ topic: this._topic
1476
+ });
1477
+ log4.trace("dxos.mesh.swarm.constructor", trace2.end({
1478
+ id: this._instanceId
1479
+ }), {
1480
+ F: __dxlog_file4,
1481
+ L: 101,
1482
+ S: this,
1483
+ C: (f, a) => f(...a)
1484
+ });
1485
+ }
1492
1486
  get connections() {
1493
- return Array.from(this._peers.values()).map((peer) => peer.connection).filter(isNonNullable2);
1487
+ return Array.from(this._peers.values()).map((peer) => peer.connection).filter(isNonNullable);
1494
1488
  }
1495
1489
  get ownPeerId() {
1496
1490
  return PublicKey4.from(this._ownPeer.peerKey);
@@ -1509,8 +1503,8 @@ var Swarm = class {
1509
1503
  }
1510
1504
  async open() {
1511
1505
  invariant4(!this._listeningHandle, void 0, {
1512
- F: __dxlog_file5,
1513
- L: 133,
1506
+ F: __dxlog_file4,
1507
+ L: 132,
1514
1508
  S: this,
1515
1509
  A: [
1516
1510
  "!this._listeningHandle",
@@ -1521,11 +1515,11 @@ var Swarm = class {
1521
1515
  peer: this._ownPeer,
1522
1516
  payloadType: "dxos.mesh.swarm.SwarmMessage",
1523
1517
  onMessage: async (message) => {
1524
- await this._swarmMessenger.receiveMessage(message).catch((err) => log5.info("Error while receiving message", {
1518
+ await this._swarmMessenger.receiveMessage(this._ctx, message).catch((err) => log4.info("Error while receiving message", {
1525
1519
  err
1526
1520
  }, {
1527
- F: __dxlog_file5,
1528
- L: 141,
1521
+ F: __dxlog_file4,
1522
+ L: 140,
1529
1523
  S: this,
1530
1524
  C: (f, a) => f(...a)
1531
1525
  }));
@@ -1533,9 +1527,9 @@ var Swarm = class {
1533
1527
  });
1534
1528
  }
1535
1529
  async destroy() {
1536
- log5("destroying...", void 0, {
1537
- F: __dxlog_file5,
1538
- L: 147,
1530
+ log4("destroying...", void 0, {
1531
+ F: __dxlog_file4,
1532
+ L: 146,
1539
1533
  S: this,
1540
1534
  C: (f, a) => f(...a)
1541
1535
  });
@@ -1544,17 +1538,17 @@ var Swarm = class {
1544
1538
  await this._ctx.dispose();
1545
1539
  await this._topology.destroy();
1546
1540
  await Promise.all(Array.from(this._peers.keys()).map((key) => this._destroyPeer(key, "swarm destroyed")));
1547
- log5("destroyed", void 0, {
1548
- F: __dxlog_file5,
1549
- L: 154,
1541
+ log4("destroyed", void 0, {
1542
+ F: __dxlog_file4,
1543
+ L: 153,
1550
1544
  S: this,
1551
1545
  C: (f, a) => f(...a)
1552
1546
  });
1553
1547
  }
1554
1548
  async setTopology(topology) {
1555
1549
  invariant4(!this._ctx.disposed, "Swarm is offline", {
1556
- F: __dxlog_file5,
1557
- L: 158,
1550
+ F: __dxlog_file4,
1551
+ L: 157,
1558
1552
  S: this,
1559
1553
  A: [
1560
1554
  "!this._ctx.disposed",
@@ -1564,12 +1558,12 @@ var Swarm = class {
1564
1558
  if (topology === this._topology) {
1565
1559
  return;
1566
1560
  }
1567
- log5("setting topology", {
1561
+ log4("setting topology", {
1568
1562
  previous: getClassName(this._topology),
1569
1563
  topology: getClassName(topology)
1570
1564
  }, {
1571
- F: __dxlog_file5,
1572
- L: 162,
1565
+ F: __dxlog_file4,
1566
+ L: 161,
1573
1567
  S: this,
1574
1568
  C: (f, a) => f(...a)
1575
1569
  });
@@ -1579,18 +1573,18 @@ var Swarm = class {
1579
1573
  this._topology.update();
1580
1574
  }
1581
1575
  async onSwarmEvent(swarmEvent) {
1582
- log5("swarm event", {
1576
+ log4("swarm event", {
1583
1577
  swarmEvent
1584
1578
  }, {
1585
- F: __dxlog_file5,
1586
- L: 175,
1579
+ F: __dxlog_file4,
1580
+ L: 174,
1587
1581
  S: this,
1588
1582
  C: (f, a) => f(...a)
1589
1583
  });
1590
1584
  if (this._ctx.disposed) {
1591
- log5("swarm event ignored for disposed swarm", void 0, {
1592
- F: __dxlog_file5,
1593
- L: 178,
1585
+ log4("swarm event ignored for disposed swarm", void 0, {
1586
+ F: __dxlog_file4,
1587
+ L: 177,
1594
1588
  S: this,
1595
1589
  C: (f, a) => f(...a)
1596
1590
  });
@@ -1599,11 +1593,11 @@ var Swarm = class {
1599
1593
  if (swarmEvent.peerAvailable) {
1600
1594
  const peerId = swarmEvent.peerAvailable.peer.peerKey;
1601
1595
  if (peerId !== this._ownPeer.peerKey) {
1602
- log5("new peer", {
1596
+ log4("new peer", {
1603
1597
  peerId
1604
1598
  }, {
1605
- F: __dxlog_file5,
1606
- L: 185,
1599
+ F: __dxlog_file4,
1600
+ L: 184,
1607
1601
  S: this,
1608
1602
  C: (f, a) => f(...a)
1609
1603
  });
@@ -1615,25 +1609,25 @@ var Swarm = class {
1615
1609
  if (peer) {
1616
1610
  peer.advertizing = false;
1617
1611
  if (this._isConnectionEstablishmentInProgress(peer)) {
1618
- log5(`destroying peer, state: ${peer.connection?.state}`, void 0, {
1619
- F: __dxlog_file5,
1620
- L: 196,
1612
+ log4(`destroying peer, state: ${peer.connection?.state}`, void 0, {
1613
+ F: __dxlog_file4,
1614
+ L: 195,
1621
1615
  S: this,
1622
1616
  C: (f, a) => f(...a)
1623
1617
  });
1624
- void this._destroyPeer(swarmEvent.peerLeft.peer, "peer left").catch((err) => log5.catch(err, void 0, {
1625
- F: __dxlog_file5,
1626
- L: 197,
1618
+ void this._destroyPeer(swarmEvent.peerLeft.peer, "peer left").catch((err) => log4.catch(err, void 0, {
1619
+ F: __dxlog_file4,
1620
+ L: 196,
1627
1621
  S: this,
1628
1622
  C: (f, a) => f(...a)
1629
1623
  }));
1630
1624
  }
1631
1625
  } else {
1632
- log5("received peerLeft but no peer found", {
1626
+ log4("received peerLeft but no peer found", {
1633
1627
  peer: swarmEvent.peerLeft.peer.peerKey
1634
1628
  }, {
1635
- F: __dxlog_file5,
1636
- L: 200,
1629
+ F: __dxlog_file4,
1630
+ L: 199,
1637
1631
  S: this,
1638
1632
  C: (f, a) => f(...a)
1639
1633
  });
@@ -1641,19 +1635,19 @@ var Swarm = class {
1641
1635
  }
1642
1636
  this._topology.update();
1643
1637
  }
1644
- async onOffer(message) {
1645
- log5("offer", {
1638
+ async onOffer(ctx, message) {
1639
+ log4("offer", {
1646
1640
  message
1647
1641
  }, {
1648
- F: __dxlog_file5,
1649
- L: 209,
1642
+ F: __dxlog_file4,
1643
+ L: 208,
1650
1644
  S: this,
1651
1645
  C: (f, a) => f(...a)
1652
1646
  });
1653
1647
  if (this._ctx.disposed) {
1654
- log5("ignored for disposed swarm", void 0, {
1655
- F: __dxlog_file5,
1656
- L: 211,
1648
+ log4("ignored for disposed swarm", void 0, {
1649
+ F: __dxlog_file4,
1650
+ L: 210,
1657
1651
  S: this,
1658
1652
  C: (f, a) => f(...a)
1659
1653
  });
@@ -1662,8 +1656,8 @@ var Swarm = class {
1662
1656
  };
1663
1657
  }
1664
1658
  invariant4(message.author, void 0, {
1665
- F: __dxlog_file5,
1666
- L: 216,
1659
+ F: __dxlog_file4,
1660
+ L: 215,
1667
1661
  S: this,
1668
1662
  A: [
1669
1663
  "message.author",
@@ -1671,11 +1665,11 @@ var Swarm = class {
1671
1665
  ]
1672
1666
  });
1673
1667
  if (message.recipient.peerKey !== this._ownPeer.peerKey) {
1674
- log5("rejecting offer with incorrect peerId", {
1668
+ log4("rejecting offer with incorrect peerId", {
1675
1669
  message
1676
1670
  }, {
1677
- F: __dxlog_file5,
1678
- L: 218,
1671
+ F: __dxlog_file4,
1672
+ L: 217,
1679
1673
  S: this,
1680
1674
  C: (f, a) => f(...a)
1681
1675
  });
@@ -1684,11 +1678,11 @@ var Swarm = class {
1684
1678
  };
1685
1679
  }
1686
1680
  if (!message.topic?.equals(this._topic)) {
1687
- log5("rejecting offer with incorrect topic", {
1681
+ log4("rejecting offer with incorrect topic", {
1688
1682
  message
1689
1683
  }, {
1690
- F: __dxlog_file5,
1691
- L: 222,
1684
+ F: __dxlog_file4,
1685
+ L: 221,
1692
1686
  S: this,
1693
1687
  C: (f, a) => f(...a)
1694
1688
  });
@@ -1697,7 +1691,7 @@ var Swarm = class {
1697
1691
  };
1698
1692
  }
1699
1693
  const peer = this._getOfferSenderPeer(message.author);
1700
- const answer = await peer.onOffer(message);
1694
+ const answer = await peer.onOffer(ctx, message);
1701
1695
  this._topology.update();
1702
1696
  return answer;
1703
1697
  }
@@ -1711,27 +1705,27 @@ var Swarm = class {
1711
1705
  }
1712
1706
  return peer;
1713
1707
  }
1714
- async onSignal(message) {
1715
- log5("signal", {
1708
+ async onSignal(ctx, message) {
1709
+ log4("signal", {
1716
1710
  message
1717
1711
  }, {
1718
- F: __dxlog_file5,
1719
- L: 247,
1712
+ F: __dxlog_file4,
1713
+ L: 246,
1720
1714
  S: this,
1721
1715
  C: (f, a) => f(...a)
1722
1716
  });
1723
1717
  if (this._ctx.disposed) {
1724
- log5.info("ignored for offline swarm", void 0, {
1725
- F: __dxlog_file5,
1726
- L: 249,
1718
+ log4.info("ignored for offline swarm", void 0, {
1719
+ F: __dxlog_file4,
1720
+ L: 248,
1727
1721
  S: this,
1728
1722
  C: (f, a) => f(...a)
1729
1723
  });
1730
1724
  return;
1731
1725
  }
1732
1726
  invariant4(message.recipient.peerKey === this._ownPeer.peerKey, `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1733
- F: __dxlog_file5,
1734
- L: 252,
1727
+ F: __dxlog_file4,
1728
+ L: 251,
1735
1729
  S: this,
1736
1730
  A: [
1737
1731
  "message.recipient.peerKey === this._ownPeer.peerKey",
@@ -1739,8 +1733,8 @@ var Swarm = class {
1739
1733
  ]
1740
1734
  });
1741
1735
  invariant4(message.topic?.equals(this._topic), void 0, {
1742
- F: __dxlog_file5,
1743
- L: 256,
1736
+ F: __dxlog_file4,
1737
+ L: 255,
1744
1738
  S: this,
1745
1739
  A: [
1746
1740
  "message.topic?.equals(this._topic)",
@@ -1748,8 +1742,8 @@ var Swarm = class {
1748
1742
  ]
1749
1743
  });
1750
1744
  invariant4(message.author, void 0, {
1751
- F: __dxlog_file5,
1752
- L: 257,
1745
+ F: __dxlog_file4,
1746
+ L: 256,
1753
1747
  S: this,
1754
1748
  A: [
1755
1749
  "message.author",
@@ -1757,7 +1751,7 @@ var Swarm = class {
1757
1751
  ]
1758
1752
  });
1759
1753
  const peer = this._getOrCreatePeer(message.author);
1760
- await peer.onSignal(message);
1754
+ await peer.onSignal(ctx, message);
1761
1755
  }
1762
1756
  // For debug purposes
1763
1757
  async goOffline() {
@@ -1768,15 +1762,15 @@ var Swarm = class {
1768
1762
  }
1769
1763
  // For debug purposes
1770
1764
  async goOnline() {
1771
- this._ctx = new Context4(void 0, {
1772
- F: __dxlog_file5,
1773
- L: 273
1765
+ this._ctx = new Context3(void 0, {
1766
+ F: __dxlog_file4,
1767
+ L: 272
1774
1768
  });
1775
1769
  }
1776
1770
  _getOrCreatePeer(peerInfo) {
1777
1771
  invariant4(peerInfo.peerKey, "PeerInfo.peerKey is required", {
1778
- F: __dxlog_file5,
1779
- L: 277,
1772
+ F: __dxlog_file4,
1773
+ L: 276,
1780
1774
  S: this,
1781
1775
  A: [
1782
1776
  "peerInfo.peerKey",
@@ -1794,9 +1788,9 @@ var Swarm = class {
1794
1788
  },
1795
1789
  onDisconnected: async () => {
1796
1790
  if (this._isUnregistered(peer)) {
1797
- log5.verbose("ignored onDisconnected for unregistered peer", void 0, {
1798
- F: __dxlog_file5,
1799
- L: 297,
1791
+ log4.verbose("ignored onDisconnected for unregistered peer", void 0, {
1792
+ F: __dxlog_file4,
1793
+ L: 296,
1800
1794
  S: this,
1801
1795
  C: (f, a) => f(...a)
1802
1796
  });
@@ -1810,11 +1804,11 @@ var Swarm = class {
1810
1804
  },
1811
1805
  onRejected: () => {
1812
1806
  if (!this._isUnregistered(peer)) {
1813
- log5("peer rejected connection", {
1807
+ log4("peer rejected connection", {
1814
1808
  peerInfo
1815
1809
  }, {
1816
- F: __dxlog_file5,
1817
- L: 311,
1810
+ F: __dxlog_file4,
1811
+ L: 310,
1818
1812
  S: this,
1819
1813
  C: (f, a) => f(...a)
1820
1814
  });
@@ -1836,19 +1830,19 @@ var Swarm = class {
1836
1830
  return peer;
1837
1831
  }
1838
1832
  async _destroyPeer(peerInfo, reason) {
1839
- log5("destroy peer", {
1833
+ log4("destroy peer", {
1840
1834
  peerKey: peerInfo.peerKey,
1841
1835
  reason
1842
1836
  }, {
1843
- F: __dxlog_file5,
1844
- L: 333,
1837
+ F: __dxlog_file4,
1838
+ L: 332,
1845
1839
  S: this,
1846
1840
  C: (f, a) => f(...a)
1847
1841
  });
1848
1842
  const peer = this._peers.get(peerInfo);
1849
1843
  invariant4(peer, void 0, {
1850
- F: __dxlog_file5,
1851
- L: 335,
1844
+ F: __dxlog_file4,
1845
+ L: 334,
1852
1846
  S: this,
1853
1847
  A: [
1854
1848
  "peer",
@@ -1876,9 +1870,9 @@ var Swarm = class {
1876
1870
  peerKey: peer.toHex()
1877
1871
  });
1878
1872
  } catch (err) {
1879
- log5("initiation error", err, {
1880
- F: __dxlog_file5,
1881
- L: 362,
1873
+ log4("initiation error", err, {
1874
+ F: __dxlog_file4,
1875
+ L: 361,
1882
1876
  S: this,
1883
1877
  C: (f, a) => f(...a)
1884
1878
  });
@@ -1905,11 +1899,11 @@ var Swarm = class {
1905
1899
  const ctx = this._ctx;
1906
1900
  const peer = this._getOrCreatePeer(remotePeer);
1907
1901
  if (remotePeer.peerKey < this._ownPeer.peerKey) {
1908
- log5("initiation delay", {
1902
+ log4("initiation delay", {
1909
1903
  remotePeer
1910
1904
  }, {
1911
- F: __dxlog_file5,
1912
- L: 390,
1905
+ F: __dxlog_file4,
1906
+ L: 389,
1913
1907
  S: this,
1914
1908
  C: (f, a) => f(...a)
1915
1909
  });
@@ -1924,21 +1918,21 @@ var Swarm = class {
1924
1918
  if (peer.connection) {
1925
1919
  return;
1926
1920
  }
1927
- log5("initiating connection...", {
1921
+ log4("initiating connection...", {
1928
1922
  remotePeer
1929
1923
  }, {
1930
- F: __dxlog_file5,
1931
- L: 406,
1924
+ F: __dxlog_file4,
1925
+ L: 405,
1932
1926
  S: this,
1933
1927
  C: (f, a) => f(...a)
1934
1928
  });
1935
- await peer.initiateConnection();
1929
+ await peer.initiateConnection(ctx);
1936
1930
  this._topology.update();
1937
- log5("initiated", {
1931
+ log4("initiated", {
1938
1932
  remotePeer
1939
1933
  }, {
1940
- F: __dxlog_file5,
1941
- L: 409,
1934
+ F: __dxlog_file4,
1935
+ L: 408,
1942
1936
  S: this,
1943
1937
  C: (f, a) => f(...a)
1944
1938
  });
@@ -1963,83 +1957,6 @@ var Swarm = class {
1963
1957
  _isUnregistered(peer) {
1964
1958
  return !peer || this._peers.get(peer.remoteInfo) !== peer;
1965
1959
  }
1966
- // TODO(burdon): Swarm => Peer.create/destroy =< Connection.open/close
1967
- // TODO(burdon): Pass in object.
1968
- constructor(_topic, _ownPeer, _topology, _protocolProvider, _messenger, _transportFactory, _label, _connectionLimiter, _initiationDelay = INITIATION_DELAY) {
1969
- _define_property4(this, "_topic", void 0);
1970
- _define_property4(this, "_ownPeer", void 0);
1971
- _define_property4(this, "_topology", void 0);
1972
- _define_property4(this, "_protocolProvider", void 0);
1973
- _define_property4(this, "_messenger", void 0);
1974
- _define_property4(this, "_transportFactory", void 0);
1975
- _define_property4(this, "_label", void 0);
1976
- _define_property4(this, "_connectionLimiter", void 0);
1977
- _define_property4(this, "_initiationDelay", void 0);
1978
- _define_property4(this, "_swarmMessenger", void 0);
1979
- _define_property4(this, "_ctx", void 0);
1980
- _define_property4(this, "_listeningHandle", void 0);
1981
- _define_property4(this, "_peers", void 0);
1982
- _define_property4(this, "_instanceId", void 0);
1983
- _define_property4(this, "connectionAdded", void 0);
1984
- _define_property4(this, "disconnected", void 0);
1985
- _define_property4(this, "connected", void 0);
1986
- _define_property4(this, "errors", void 0);
1987
- this._topic = _topic;
1988
- this._ownPeer = _ownPeer;
1989
- this._topology = _topology;
1990
- this._protocolProvider = _protocolProvider;
1991
- this._messenger = _messenger;
1992
- this._transportFactory = _transportFactory;
1993
- this._label = _label;
1994
- this._connectionLimiter = _connectionLimiter;
1995
- this._initiationDelay = _initiationDelay;
1996
- this._ctx = new Context4(void 0, {
1997
- F: __dxlog_file5,
1998
- L: 39
1999
- });
2000
- this._listeningHandle = void 0;
2001
- this._peers = new ComplexMap2(PeerInfoHash);
2002
- this._instanceId = PublicKey4.random().toHex();
2003
- this.connectionAdded = new Event3();
2004
- this.disconnected = new Event3();
2005
- this.connected = new Event3();
2006
- this.errors = new ErrorStream2();
2007
- log5.trace("dxos.mesh.swarm.constructor", trace2.begin({
2008
- id: this._instanceId,
2009
- data: {
2010
- topic: this._topic.toHex(),
2011
- peer: this._ownPeer
2012
- }
2013
- }), {
2014
- F: __dxlog_file5,
2015
- L: 89,
2016
- S: this,
2017
- C: (f, a) => f(...a)
2018
- });
2019
- log5("creating swarm", {
2020
- peerId: _ownPeer
2021
- }, {
2022
- F: __dxlog_file5,
2023
- L: 93,
2024
- S: this,
2025
- C: (f, a) => f(...a)
2026
- });
2027
- _topology.init(this._getSwarmController());
2028
- this._swarmMessenger = new SwarmMessenger({
2029
- sendMessage: async (msg) => await this._messenger.sendMessage(msg),
2030
- onSignal: async (msg) => await this.onSignal(msg),
2031
- onOffer: async (msg) => await this.onOffer(msg),
2032
- topic: this._topic
2033
- });
2034
- log5.trace("dxos.mesh.swarm.constructor", trace2.end({
2035
- id: this._instanceId
2036
- }), {
2037
- F: __dxlog_file5,
2038
- L: 102,
2039
- S: this,
2040
- C: (f, a) => f(...a)
2041
- });
2042
- }
2043
1960
  };
2044
1961
  _ts_decorate3([
2045
1962
  logInfo2
@@ -2066,30 +1983,37 @@ _ts_decorate3([
2066
1983
  // src/swarm/swarm-mapper.ts
2067
1984
  import { Event as Event4, SubscriptionList } from "@dxos/async";
2068
1985
  import { PublicKey as PublicKey5 } from "@dxos/keys";
2069
- import { log as log6 } from "@dxos/log";
1986
+ import { log as log5 } from "@dxos/log";
2070
1987
  import { PeerInfoHash as PeerInfoHash2 } from "@dxos/messaging";
2071
1988
  import { ComplexMap as ComplexMap3 } from "@dxos/util";
2072
- function _define_property5(obj, key, value) {
2073
- if (key in obj) {
2074
- Object.defineProperty(obj, key, {
2075
- value,
2076
- enumerable: true,
2077
- configurable: true,
2078
- writable: true
2079
- });
2080
- } else {
2081
- obj[key] = value;
2082
- }
2083
- return obj;
2084
- }
2085
- var __dxlog_file6 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts";
1989
+ var __dxlog_file5 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts";
2086
1990
  var SwarmMapper = class {
1991
+ _swarm;
1992
+ _subscriptions = new SubscriptionList();
1993
+ _connectionSubscriptions = new ComplexMap3(PeerInfoHash2);
1994
+ _peers = new ComplexMap3(PeerInfoHash2);
1995
+ mapUpdated = new Event4();
2087
1996
  get peers() {
2088
1997
  return Array.from(this._peers.values());
2089
1998
  }
1999
+ constructor(_swarm) {
2000
+ this._swarm = _swarm;
2001
+ this._subscriptions.add(_swarm.connectionAdded.on((connection) => {
2002
+ this._update();
2003
+ this._connectionSubscriptions.set(connection.remoteInfo, connection.stateChanged.on(() => {
2004
+ this._update();
2005
+ }));
2006
+ }));
2007
+ this._subscriptions.add(_swarm.disconnected.on((peerId) => {
2008
+ this._connectionSubscriptions.get(peerId)?.();
2009
+ this._connectionSubscriptions.delete(peerId);
2010
+ this._update();
2011
+ }));
2012
+ this._update();
2013
+ }
2090
2014
  _update() {
2091
- log6("updating swarm", void 0, {
2092
- F: __dxlog_file6,
2015
+ log5("updating swarm", void 0, {
2016
+ F: __dxlog_file5,
2093
2017
  L: 71,
2094
2018
  S: this,
2095
2019
  C: (f, a) => f(...a)
@@ -2109,11 +2033,11 @@ var SwarmMapper = class {
2109
2033
  ]
2110
2034
  });
2111
2035
  }
2112
- log6("graph changed", {
2036
+ log5("graph changed", {
2113
2037
  directConnections: this._swarm.connections.length,
2114
2038
  totalPeersInSwarm: this._peers.size
2115
2039
  }, {
2116
- F: __dxlog_file6,
2040
+ F: __dxlog_file5,
2117
2041
  L: 112,
2118
2042
  S: this,
2119
2043
  C: (f, a) => f(...a)
@@ -2126,62 +2050,42 @@ var SwarmMapper = class {
2126
2050
  this._connectionSubscriptions.clear();
2127
2051
  this._subscriptions.clear();
2128
2052
  }
2129
- constructor(_swarm) {
2130
- _define_property5(this, "_swarm", void 0);
2131
- _define_property5(this, "_subscriptions", void 0);
2132
- _define_property5(this, "_connectionSubscriptions", void 0);
2133
- _define_property5(this, "_peers", void 0);
2134
- _define_property5(this, "mapUpdated", void 0);
2135
- this._swarm = _swarm;
2136
- this._subscriptions = new SubscriptionList();
2137
- this._connectionSubscriptions = new ComplexMap3(PeerInfoHash2);
2138
- this._peers = new ComplexMap3(PeerInfoHash2);
2139
- this.mapUpdated = new Event4();
2140
- this._subscriptions.add(_swarm.connectionAdded.on((connection) => {
2141
- this._update();
2142
- this._connectionSubscriptions.set(connection.remoteInfo, connection.stateChanged.on(() => {
2143
- this._update();
2144
- }));
2145
- }));
2146
- this._subscriptions.add(_swarm.disconnected.on((peerId) => {
2147
- this._connectionSubscriptions.get(peerId)?.();
2148
- this._connectionSubscriptions.delete(peerId);
2149
- this._update();
2150
- }));
2151
- this._update();
2152
- }
2153
2053
  };
2154
2054
 
2155
2055
  // src/swarm/connection-limiter.ts
2156
2056
  import { DeferredTask as DeferredTask2 } from "@dxos/async";
2157
- import { Context as Context5 } from "@dxos/context";
2057
+ import { Context as Context4 } from "@dxos/context";
2158
2058
  import { invariant as invariant5 } from "@dxos/invariant";
2159
2059
  import { PublicKey as PublicKey6 } from "@dxos/keys";
2160
- import { log as log7 } from "@dxos/log";
2060
+ import { log as log6 } from "@dxos/log";
2161
2061
  import { CancelledError as CancelledError3 } from "@dxos/protocols";
2162
2062
  import { ComplexMap as ComplexMap4 } from "@dxos/util";
2163
- function _define_property6(obj, key, value) {
2164
- if (key in obj) {
2165
- Object.defineProperty(obj, key, {
2166
- value,
2167
- enumerable: true,
2168
- configurable: true,
2169
- writable: true
2170
- });
2171
- } else {
2172
- obj[key] = value;
2173
- }
2174
- return obj;
2175
- }
2176
- var __dxlog_file7 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
2063
+ var __dxlog_file6 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
2177
2064
  var MAX_CONCURRENT_INITIATING_CONNECTIONS = 50;
2178
2065
  var ConnectionLimiter = class {
2066
+ _ctx = new Context4(void 0, {
2067
+ F: __dxlog_file6,
2068
+ L: 23
2069
+ });
2070
+ _maxConcurrentInitConnections;
2071
+ /**
2072
+ * Queue of promises to resolve when initiating connections amount is below the limit.
2073
+ */
2074
+ _waitingPromises = new ComplexMap4(PublicKey6.hash);
2075
+ resolveWaitingPromises = new DeferredTask2(this._ctx, async () => {
2076
+ Array.from(this._waitingPromises.values()).slice(0, this._maxConcurrentInitConnections).forEach(({ resolve }) => {
2077
+ resolve();
2078
+ });
2079
+ });
2080
+ constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
2081
+ this._maxConcurrentInitConnections = maxConcurrentInitConnections;
2082
+ }
2179
2083
  /**
2180
2084
  * @returns Promise that resolves in queue when connections amount with 'CONNECTING' state is below the limit.
2181
2085
  */
2182
2086
  async connecting(sessionId) {
2183
2087
  invariant5(!this._waitingPromises.has(sessionId), "Peer is already waiting for connection", {
2184
- F: __dxlog_file7,
2088
+ F: __dxlog_file6,
2185
2089
  L: 48,
2186
2090
  S: this,
2187
2091
  A: [
@@ -2189,10 +2093,10 @@ var ConnectionLimiter = class {
2189
2093
  "'Peer is already waiting for connection'"
2190
2094
  ]
2191
2095
  });
2192
- log7("waiting", {
2096
+ log6("waiting", {
2193
2097
  sessionId
2194
2098
  }, {
2195
- F: __dxlog_file7,
2099
+ F: __dxlog_file6,
2196
2100
  L: 49,
2197
2101
  S: this,
2198
2102
  C: (f, a) => f(...a)
@@ -2204,10 +2108,10 @@ var ConnectionLimiter = class {
2204
2108
  });
2205
2109
  this.resolveWaitingPromises.schedule();
2206
2110
  });
2207
- log7("allow", {
2111
+ log6("allow", {
2208
2112
  sessionId
2209
2113
  }, {
2210
- F: __dxlog_file7,
2114
+ F: __dxlog_file6,
2211
2115
  L: 57,
2212
2116
  S: this,
2213
2117
  C: (f, a) => f(...a)
@@ -2217,10 +2121,10 @@ var ConnectionLimiter = class {
2217
2121
  * Rejects promise returned by `connecting` method.
2218
2122
  */
2219
2123
  doneConnecting(sessionId) {
2220
- log7("done", {
2124
+ log6("done", {
2221
2125
  sessionId
2222
2126
  }, {
2223
- F: __dxlog_file7,
2127
+ F: __dxlog_file6,
2224
2128
  L: 64,
2225
2129
  S: this,
2226
2130
  C: (f, a) => f(...a)
@@ -2232,20 +2136,6 @@ var ConnectionLimiter = class {
2232
2136
  this._waitingPromises.delete(sessionId);
2233
2137
  this.resolveWaitingPromises.schedule();
2234
2138
  }
2235
- constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
2236
- _define_property6(this, "_ctx", new Context5(void 0, {
2237
- F: __dxlog_file7,
2238
- L: 23
2239
- }));
2240
- _define_property6(this, "_maxConcurrentInitConnections", void 0);
2241
- _define_property6(this, "_waitingPromises", new ComplexMap4(PublicKey6.hash));
2242
- _define_property6(this, "resolveWaitingPromises", new DeferredTask2(this._ctx, async () => {
2243
- Array.from(this._waitingPromises.values()).slice(0, this._maxConcurrentInitConnections).forEach(({ resolve }) => {
2244
- resolve();
2245
- });
2246
- }));
2247
- this._maxConcurrentInitConnections = maxConcurrentInitConnections;
2248
- }
2249
2139
  };
2250
2140
 
2251
2141
  // src/connection-log.ts
@@ -2253,19 +2143,6 @@ import { Event as Event5 } from "@dxos/async";
2253
2143
  import { raise } from "@dxos/debug";
2254
2144
  import { PublicKey as PublicKey7 } from "@dxos/keys";
2255
2145
  import { ComplexMap as ComplexMap5 } from "@dxos/util";
2256
- function _define_property7(obj, key, value) {
2257
- if (key in obj) {
2258
- Object.defineProperty(obj, key, {
2259
- value,
2260
- enumerable: true,
2261
- configurable: true,
2262
- writable: true
2263
- });
2264
- } else {
2265
- obj[key] = value;
2266
- }
2267
- return obj;
2268
- }
2269
2146
  var CONNECTION_GC_THRESHOLD = 1e3 * 60 * 15;
2270
2147
  var EventType = /* @__PURE__ */ (function(EventType2) {
2271
2148
  EventType2["CONNECTION_STATE_CHANGED"] = "CONNECTION_STATE_CHANGED";
@@ -2276,6 +2153,11 @@ var EventType = /* @__PURE__ */ (function(EventType2) {
2276
2153
  return EventType2;
2277
2154
  })({});
2278
2155
  var ConnectionLog = class {
2156
+ /**
2157
+ * SwarmId => info
2158
+ */
2159
+ _swarms = new ComplexMap5(PublicKey7.hash);
2160
+ update = new Event5();
2279
2161
  getSwarmInfo(swarmId) {
2280
2162
  return this._swarms.get(swarmId) ?? raise(new Error(`Swarm not found: ${swarmId}`));
2281
2163
  }
@@ -2339,10 +2221,6 @@ var ConnectionLog = class {
2339
2221
  this.getSwarmInfo(PublicKey7.from(swarm._instanceId)).isActive = false;
2340
2222
  this.update.emit();
2341
2223
  }
2342
- constructor() {
2343
- _define_property7(this, "_swarms", new ComplexMap5(PublicKey7.hash));
2344
- _define_property7(this, "update", new Event5());
2345
- }
2346
2224
  };
2347
2225
  var gcSwarm = (swarm) => {
2348
2226
  swarm.connections = swarm.connections?.filter((connection) => {
@@ -2354,32 +2232,52 @@ var gcSwarm = (swarm) => {
2354
2232
  import { Event as Event6, synchronized as synchronized4 } from "@dxos/async";
2355
2233
  import { invariant as invariant6 } from "@dxos/invariant";
2356
2234
  import { PublicKey as PublicKey8 } from "@dxos/keys";
2357
- import { log as log8 } from "@dxos/log";
2235
+ import { log as log7 } from "@dxos/log";
2358
2236
  import { Messenger } from "@dxos/messaging";
2359
2237
  import { trace as trace3 } from "@dxos/protocols";
2360
2238
  import { ConnectionState as ConnectionState2 } from "@dxos/protocols/proto/dxos/client/services";
2361
2239
  import { ComplexMap as ComplexMap6 } from "@dxos/util";
2362
- function _define_property8(obj, key, value) {
2363
- if (key in obj) {
2364
- Object.defineProperty(obj, key, {
2365
- value,
2366
- enumerable: true,
2367
- configurable: true,
2368
- writable: true
2369
- });
2370
- } else {
2371
- obj[key] = value;
2372
- }
2373
- return obj;
2374
- }
2375
2240
  function _ts_decorate4(decorators, target, key, desc) {
2376
2241
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2377
2242
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
2378
2243
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2379
2244
  return c > 3 && r && Object.defineProperty(target, key, r), r;
2380
2245
  }
2381
- var __dxlog_file8 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts";
2246
+ var __dxlog_file7 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts";
2382
2247
  var SwarmNetworkManager = class {
2248
+ /**
2249
+ * @internal
2250
+ */
2251
+ _swarms = new ComplexMap6(PublicKey8.hash);
2252
+ _mappers = new ComplexMap6(PublicKey8.hash);
2253
+ _transportFactory;
2254
+ _signalManager;
2255
+ _messenger;
2256
+ _signalConnection;
2257
+ _connectionLimiter;
2258
+ _connectionLog;
2259
+ _instanceId = PublicKey8.random().toHex();
2260
+ _peerInfo = void 0;
2261
+ _connectionState = ConnectionState2.ONLINE;
2262
+ connectionStateChanged = new Event6();
2263
+ topicsUpdated = new Event6();
2264
+ constructor({ transportFactory, signalManager, enableDevtoolsLogging, peerInfo }) {
2265
+ this._transportFactory = transportFactory;
2266
+ this._signalManager = signalManager;
2267
+ this._signalManager.swarmEvent.on((event) => this._swarms.get(event.topic)?.onSwarmEvent(event));
2268
+ this._messenger = new Messenger({
2269
+ signalManager: this._signalManager
2270
+ });
2271
+ this._signalConnection = {
2272
+ join: (ctx, opts) => this._signalManager.join(ctx, opts),
2273
+ leave: (ctx, opts) => this._signalManager.leave(ctx, opts)
2274
+ };
2275
+ this._peerInfo = peerInfo;
2276
+ this._connectionLimiter = new ConnectionLimiter();
2277
+ if (enableDevtoolsLogging) {
2278
+ this._connectionLog = new ConnectionLog();
2279
+ }
2280
+ }
2383
2281
  // TODO(burdon): Remove access (Devtools only).
2384
2282
  get connectionLog() {
2385
2283
  return this._connectionLog;
@@ -2401,31 +2299,31 @@ var SwarmNetworkManager = class {
2401
2299
  this._peerInfo = peerInfo;
2402
2300
  }
2403
2301
  async open() {
2404
- log8.trace("dxos.mesh.network-manager.open", trace3.begin({
2302
+ log7.trace("dxos.mesh.network-manager.open", trace3.begin({
2405
2303
  id: this._instanceId
2406
2304
  }), {
2407
- F: __dxlog_file8,
2408
- L: 133,
2305
+ F: __dxlog_file7,
2306
+ L: 134,
2409
2307
  S: this,
2410
2308
  C: (f, a) => f(...a)
2411
2309
  });
2412
2310
  await this._messenger.open();
2413
2311
  await this._signalManager.open();
2414
- log8.trace("dxos.mesh.network-manager.open", trace3.end({
2312
+ log7.trace("dxos.mesh.network-manager.open", trace3.end({
2415
2313
  id: this._instanceId
2416
2314
  }), {
2417
- F: __dxlog_file8,
2418
- L: 136,
2315
+ F: __dxlog_file7,
2316
+ L: 137,
2419
2317
  S: this,
2420
2318
  C: (f, a) => f(...a)
2421
2319
  });
2422
2320
  }
2423
- async close() {
2321
+ async close(ctx) {
2424
2322
  for (const topic of this._swarms.keys()) {
2425
- await this.leaveSwarm(topic).catch((err) => {
2426
- log8(err, void 0, {
2427
- F: __dxlog_file8,
2428
- L: 142,
2323
+ await this.leaveSwarm(ctx, topic).catch((err) => {
2324
+ log7(err, void 0, {
2325
+ F: __dxlog_file7,
2326
+ L: 143,
2429
2327
  S: this,
2430
2328
  C: (f, a) => f(...a)
2431
2329
  });
@@ -2437,10 +2335,10 @@ var SwarmNetworkManager = class {
2437
2335
  /**
2438
2336
  * Join the swarm.
2439
2337
  */
2440
- async joinSwarm({ topic, topology, protocolProvider: protocol, label }) {
2338
+ async joinSwarm(ctx, { topic, topology, protocolProvider: protocol, label }) {
2441
2339
  invariant6(PublicKey8.isPublicKey(topic), void 0, {
2442
- F: __dxlog_file8,
2443
- L: 160,
2340
+ F: __dxlog_file7,
2341
+ L: 164,
2444
2342
  S: this,
2445
2343
  A: [
2446
2344
  "PublicKey.isPublicKey(topic)",
@@ -2448,8 +2346,8 @@ var SwarmNetworkManager = class {
2448
2346
  ]
2449
2347
  });
2450
2348
  invariant6(topology, void 0, {
2451
- F: __dxlog_file8,
2452
- L: 161,
2349
+ F: __dxlog_file7,
2350
+ L: 165,
2453
2351
  S: this,
2454
2352
  A: [
2455
2353
  "topology",
@@ -2457,8 +2355,8 @@ var SwarmNetworkManager = class {
2457
2355
  ]
2458
2356
  });
2459
2357
  invariant6(this._peerInfo, void 0, {
2460
- F: __dxlog_file8,
2461
- L: 162,
2358
+ F: __dxlog_file7,
2359
+ L: 166,
2462
2360
  S: this,
2463
2361
  A: [
2464
2362
  "this._peerInfo",
@@ -2466,8 +2364,8 @@ var SwarmNetworkManager = class {
2466
2364
  ]
2467
2365
  });
2468
2366
  invariant6(typeof protocol === "function", void 0, {
2469
- F: __dxlog_file8,
2470
- L: 163,
2367
+ F: __dxlog_file7,
2368
+ L: 167,
2471
2369
  S: this,
2472
2370
  A: [
2473
2371
  "typeof protocol === 'function'",
@@ -2477,23 +2375,23 @@ var SwarmNetworkManager = class {
2477
2375
  if (this._swarms.has(topic)) {
2478
2376
  throw new Error(`Already connected to swarm: ${PublicKey8.from(topic)}`);
2479
2377
  }
2480
- log8("joining", {
2378
+ log7("joining", {
2481
2379
  topic: PublicKey8.from(topic),
2482
2380
  peerInfo: this._peerInfo,
2483
2381
  topology: topology.toString()
2484
2382
  }, {
2485
- F: __dxlog_file8,
2486
- L: 168,
2383
+ F: __dxlog_file7,
2384
+ L: 172,
2487
2385
  S: this,
2488
2386
  C: (f, a) => f(...a)
2489
2387
  });
2490
2388
  const swarm = new Swarm(topic, this._peerInfo, topology, protocol, this._messenger, this._transportFactory, label, this._connectionLimiter);
2491
2389
  swarm.errors.handle((error) => {
2492
- log8("swarm error", {
2390
+ log7("swarm error", {
2493
2391
  error
2494
2392
  }, {
2495
- F: __dxlog_file8,
2496
- L: 181,
2393
+ F: __dxlog_file7,
2394
+ L: 185,
2497
2395
  S: this,
2498
2396
  C: (f, a) => f(...a)
2499
2397
  });
@@ -2501,47 +2399,47 @@ var SwarmNetworkManager = class {
2501
2399
  this._swarms.set(topic, swarm);
2502
2400
  this._mappers.set(topic, new SwarmMapper(swarm));
2503
2401
  await swarm.open();
2504
- this._signalConnection.join({
2402
+ this._signalConnection.join(ctx, {
2505
2403
  topic,
2506
2404
  peer: this._peerInfo
2507
- }).catch((error) => log8.catch(error, void 0, {
2508
- F: __dxlog_file8,
2509
- L: 190,
2405
+ }).catch((error) => log7.catch(error, void 0, {
2406
+ F: __dxlog_file7,
2407
+ L: 194,
2510
2408
  S: this,
2511
2409
  C: (f, a) => f(...a)
2512
2410
  }));
2513
2411
  this.topicsUpdated.emit();
2514
2412
  this._connectionLog?.joinedSwarm(swarm);
2515
- log8("joined", {
2413
+ log7("joined", {
2516
2414
  topic: PublicKey8.from(topic),
2517
2415
  count: this._swarms.size
2518
2416
  }, {
2519
- F: __dxlog_file8,
2520
- L: 194,
2417
+ F: __dxlog_file7,
2418
+ L: 198,
2521
2419
  S: this,
2522
2420
  C: (f, a) => f(...a)
2523
2421
  });
2524
2422
  return {
2525
- close: () => this.leaveSwarm(topic)
2423
+ close: (ctx2) => this.leaveSwarm(ctx2, topic)
2526
2424
  };
2527
2425
  }
2528
2426
  /**
2529
2427
  * Close the connection.
2530
2428
  */
2531
- async leaveSwarm(topic) {
2429
+ async leaveSwarm(ctx, topic) {
2532
2430
  if (!this._swarms.has(topic)) {
2533
2431
  return;
2534
2432
  }
2535
- log8("leaving", {
2433
+ log7("leaving", {
2536
2434
  topic: PublicKey8.from(topic)
2537
2435
  }, {
2538
- F: __dxlog_file8,
2539
- L: 211,
2436
+ F: __dxlog_file7,
2437
+ L: 215,
2540
2438
  S: this,
2541
2439
  C: (f, a) => f(...a)
2542
2440
  });
2543
2441
  const swarm = this._swarms.get(topic);
2544
- await this._signalConnection.leave({
2442
+ await this._signalConnection.leave(ctx, {
2545
2443
  topic,
2546
2444
  peer: swarm.ownPeer
2547
2445
  });
@@ -2552,12 +2450,12 @@ var SwarmNetworkManager = class {
2552
2450
  await swarm.destroy();
2553
2451
  this._swarms.delete(topic);
2554
2452
  this.topicsUpdated.emit();
2555
- log8("left", {
2453
+ log7("left", {
2556
2454
  topic: PublicKey8.from(topic),
2557
2455
  count: this._swarms.size
2558
2456
  }, {
2559
- F: __dxlog_file8,
2560
- L: 225,
2457
+ F: __dxlog_file7,
2458
+ L: 229,
2561
2459
  S: this,
2562
2460
  C: (f, a) => f(...a)
2563
2461
  });
@@ -2576,475 +2474,83 @@ var SwarmNetworkManager = class {
2576
2474
  await this._signalManager.close();
2577
2475
  break;
2578
2476
  }
2579
- case ConnectionState2.ONLINE: {
2580
- this._connectionState = state;
2581
- this._messenger.open();
2582
- await Promise.all([
2583
- ...this._swarms.values()
2584
- ].map((swarm) => swarm.goOnline()));
2585
- await this._signalManager.open();
2586
- break;
2587
- }
2588
- }
2589
- this.connectionStateChanged.emit(this._connectionState);
2590
- }
2591
- constructor({ transportFactory, signalManager, enableDevtoolsLogging, peerInfo }) {
2592
- _define_property8(this, "_swarms", new ComplexMap6(PublicKey8.hash));
2593
- _define_property8(this, "_mappers", new ComplexMap6(PublicKey8.hash));
2594
- _define_property8(this, "_transportFactory", void 0);
2595
- _define_property8(this, "_signalManager", void 0);
2596
- _define_property8(this, "_messenger", void 0);
2597
- _define_property8(this, "_signalConnection", void 0);
2598
- _define_property8(this, "_connectionLimiter", void 0);
2599
- _define_property8(this, "_connectionLog", void 0);
2600
- _define_property8(this, "_instanceId", PublicKey8.random().toHex());
2601
- _define_property8(this, "_peerInfo", void 0);
2602
- _define_property8(this, "_connectionState", ConnectionState2.ONLINE);
2603
- _define_property8(this, "connectionStateChanged", new Event6());
2604
- _define_property8(this, "topicsUpdated", new Event6());
2605
- this._transportFactory = transportFactory;
2606
- this._signalManager = signalManager;
2607
- this._signalManager.swarmEvent.on((event) => this._swarms.get(event.topic)?.onSwarmEvent(event));
2608
- this._messenger = new Messenger({
2609
- signalManager: this._signalManager
2610
- });
2611
- this._signalConnection = {
2612
- join: (opts) => this._signalManager.join(opts),
2613
- leave: (opts) => this._signalManager.leave(opts)
2614
- };
2615
- this._peerInfo = peerInfo;
2616
- this._connectionLimiter = new ConnectionLimiter();
2617
- if (enableDevtoolsLogging) {
2618
- this._connectionLog = new ConnectionLog();
2619
- }
2620
- }
2621
- };
2622
- _ts_decorate4([
2623
- synchronized4
2624
- ], SwarmNetworkManager.prototype, "joinSwarm", null);
2625
- _ts_decorate4([
2626
- synchronized4
2627
- ], SwarmNetworkManager.prototype, "leaveSwarm", null);
2628
-
2629
- // src/topology/fully-connected-topology.ts
2630
- import { invariant as invariant7 } from "@dxos/invariant";
2631
- function _define_property9(obj, key, value) {
2632
- if (key in obj) {
2633
- Object.defineProperty(obj, key, {
2634
- value,
2635
- enumerable: true,
2636
- configurable: true,
2637
- writable: true
2638
- });
2639
- } else {
2640
- obj[key] = value;
2641
- }
2642
- return obj;
2643
- }
2644
- var __dxlog_file9 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
2645
- var FullyConnectedTopology = class {
2646
- toString() {
2647
- return "FullyConnectedTopology";
2648
- }
2649
- init(controller) {
2650
- invariant7(!this._controller, "Already initialized", {
2651
- F: __dxlog_file9,
2652
- L: 18,
2653
- S: this,
2654
- A: [
2655
- "!this._controller",
2656
- "'Already initialized'"
2657
- ]
2658
- });
2659
- this._controller = controller;
2660
- }
2661
- update() {
2662
- invariant7(this._controller, "Not initialized", {
2663
- F: __dxlog_file9,
2664
- L: 23,
2665
- S: this,
2666
- A: [
2667
- "this._controller",
2668
- "'Not initialized'"
2669
- ]
2670
- });
2671
- const { candidates: discovered } = this._controller.getState();
2672
- for (const peer of discovered) {
2673
- this._controller.connect(peer);
2674
- }
2675
- }
2676
- async onOffer(peer) {
2677
- return true;
2678
- }
2679
- async destroy() {
2680
- }
2681
- constructor() {
2682
- _define_property9(this, "_controller", void 0);
2683
- }
2684
- };
2685
-
2686
- // src/topology/mmst-topology.ts
2687
- import { invariant as invariant8 } from "@dxos/invariant";
2688
- import { log as log9 } from "@dxos/log";
2689
- function _define_property10(obj, key, value) {
2690
- if (key in obj) {
2691
- Object.defineProperty(obj, key, {
2692
- value,
2693
- enumerable: true,
2694
- configurable: true,
2695
- writable: true
2696
- });
2697
- } else {
2698
- obj[key] = value;
2699
- }
2700
- return obj;
2701
- }
2702
- var __dxlog_file10 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/topology/mmst-topology.ts";
2703
- var MIN_UPDATE_INTERVAL = 1e3 * 10;
2704
- var MAX_CHANGES_PER_UPDATE = 1;
2705
- var MMSTTopology = class {
2706
- init(controller) {
2707
- invariant8(!this._controller, "Already initialized", {
2708
- F: __dxlog_file10,
2709
- L: 49,
2710
- S: this,
2711
- A: [
2712
- "!this._controller",
2713
- "'Already initialized'"
2714
- ]
2715
- });
2716
- this._controller = controller;
2717
- }
2718
- update() {
2719
- invariant8(this._controller, "Not initialized", {
2720
- F: __dxlog_file10,
2721
- L: 54,
2722
- S: this,
2723
- A: [
2724
- "this._controller",
2725
- "'Not initialized'"
2726
- ]
2727
- });
2728
- const { connected, candidates } = this._controller.getState();
2729
- if (this._sampleCollected || connected.length > this._maxPeers || candidates.length > 0) {
2730
- log9("Running the algorithm.", void 0, {
2731
- F: __dxlog_file10,
2732
- L: 58,
2733
- S: this,
2734
- C: (f, a) => f(...a)
2735
- });
2736
- this._sampleCollected = true;
2737
- this._runAlgorithm();
2738
- }
2739
- }
2740
- forceUpdate() {
2741
- this._lastAction = /* @__PURE__ */ new Date(0);
2742
- this.update();
2743
- }
2744
- async onOffer(peer) {
2745
- invariant8(this._controller, "Not initialized", {
2746
- F: __dxlog_file10,
2747
- L: 70,
2748
- S: this,
2749
- A: [
2750
- "this._controller",
2751
- "'Not initialized'"
2752
- ]
2753
- });
2754
- const { connected } = this._controller.getState();
2755
- const accept = connected.length < this._maxPeers;
2756
- log9(`Offer ${peer} accept=${accept}`, void 0, {
2757
- F: __dxlog_file10,
2758
- L: 73,
2759
- S: this,
2760
- C: (f, a) => f(...a)
2761
- });
2762
- return accept;
2763
- }
2764
- async destroy() {
2765
- }
2766
- _runAlgorithm() {
2767
- invariant8(this._controller, "Not initialized", {
2768
- F: __dxlog_file10,
2769
- L: 82,
2770
- S: this,
2771
- A: [
2772
- "this._controller",
2773
- "'Not initialized'"
2774
- ]
2775
- });
2776
- const { connected, candidates, ownPeerId } = this._controller.getState();
2777
- if (connected.length > this._maxPeers) {
2778
- log9(`disconnect ${connected.length - this._maxPeers} peers.`, void 0, {
2779
- F: __dxlog_file10,
2780
- L: 88,
2781
- S: this,
2782
- C: (f, a) => f(...a)
2783
- });
2784
- const sorted = sortByXorDistance(connected, ownPeerId).reverse().slice(0, this._maxPeers - connected.length);
2785
- invariant8(sorted.length === 0, void 0, {
2786
- F: __dxlog_file10,
2787
- L: 92,
2788
- S: this,
2789
- A: [
2790
- "sorted.length === 0",
2791
- ""
2792
- ]
2793
- });
2794
- if (sorted.length > MAX_CHANGES_PER_UPDATE) {
2795
- log9(`want to disconnect ${sorted.length} peers but limited to ${MAX_CHANGES_PER_UPDATE}`, void 0, {
2796
- F: __dxlog_file10,
2797
- L: 95,
2798
- S: this,
2799
- C: (f, a) => f(...a)
2800
- });
2801
- }
2802
- if (Date.now() - this._lastAction.getTime() > MIN_UPDATE_INTERVAL) {
2803
- for (const peer of sorted.slice(0, MAX_CHANGES_PER_UPDATE)) {
2804
- log9(`Disconnect ${peer}.`, void 0, {
2805
- F: __dxlog_file10,
2806
- L: 100,
2807
- S: this,
2808
- C: (f, a) => f(...a)
2809
- });
2810
- this._controller.disconnect(peer);
2811
- }
2812
- this._lastAction = /* @__PURE__ */ new Date();
2813
- } else {
2814
- log9("rate limited disconnect", void 0, {
2815
- F: __dxlog_file10,
2816
- L: 105,
2817
- S: this,
2818
- C: (f, a) => f(...a)
2819
- });
2820
- }
2821
- } else if (connected.length < this._originateConnections) {
2822
- log9(`connect ${this._originateConnections - connected.length} peers.`, void 0, {
2823
- F: __dxlog_file10,
2824
- L: 109,
2825
- S: this,
2826
- C: (f, a) => f(...a)
2827
- });
2828
- const sample = candidates.sort(() => Math.random() - 0.5).slice(0, this._sampleSize);
2829
- const sorted = sortByXorDistance(sample, ownPeerId).slice(0, this._originateConnections - connected.length);
2830
- if (sorted.length > MAX_CHANGES_PER_UPDATE) {
2831
- log9(`want to connect ${sorted.length} peers but limited to ${MAX_CHANGES_PER_UPDATE}`, void 0, {
2832
- F: __dxlog_file10,
2833
- L: 114,
2834
- S: this,
2835
- C: (f, a) => f(...a)
2836
- });
2837
- }
2838
- if (Date.now() - this._lastAction.getTime() > MIN_UPDATE_INTERVAL) {
2839
- for (const peer of sorted.slice(0, MAX_CHANGES_PER_UPDATE)) {
2840
- log9(`Connect ${peer}.`, void 0, {
2841
- F: __dxlog_file10,
2842
- L: 118,
2843
- S: this,
2844
- C: (f, a) => f(...a)
2845
- });
2846
- this._controller.connect(peer);
2847
- }
2848
- this._lastAction = /* @__PURE__ */ new Date();
2849
- } else {
2850
- log9("rate limited connect", void 0, {
2851
- F: __dxlog_file10,
2852
- L: 123,
2853
- S: this,
2854
- C: (f, a) => f(...a)
2855
- });
2856
- }
2857
- }
2858
- }
2859
- toString() {
2860
- return "MMSTTopology";
2861
- }
2862
- constructor({ originateConnections = 2, maxPeers = 4, sampleSize = 10 } = {}) {
2863
- _define_property10(this, "_originateConnections", void 0);
2864
- _define_property10(this, "_maxPeers", void 0);
2865
- _define_property10(this, "_sampleSize", void 0);
2866
- _define_property10(this, "_controller", void 0);
2867
- _define_property10(this, "_sampleCollected", false);
2868
- _define_property10(this, "_lastAction", /* @__PURE__ */ new Date(0));
2869
- this._originateConnections = originateConnections;
2870
- this._maxPeers = maxPeers;
2871
- this._sampleSize = sampleSize;
2872
- }
2873
- };
2874
- var sortByXorDistance = (keys, reference) => {
2875
- const sorted = keys.sort((a, b) => {
2876
- return compareXor(distXor(a.asBuffer(), reference.asBuffer()), distXor(b.asBuffer(), reference.asBuffer()));
2877
- });
2878
- log9("Sorted keys", {
2879
- keys,
2880
- reference,
2881
- sorted
2882
- }, {
2883
- F: __dxlog_file10,
2884
- L: 137,
2885
- S: void 0,
2886
- C: (f, a) => f(...a)
2887
- });
2888
- return sorted;
2889
- };
2890
- var distXor = (a, b) => {
2891
- const maxLength = Math.max(a.length, b.length);
2892
- const result = Buffer.allocUnsafe(maxLength);
2893
- for (let i = 0; i < maxLength; i++) {
2894
- result[i] = (a[i] || 0) ^ (b[i] || 0);
2895
- }
2896
- return result;
2897
- };
2898
- var compareXor = (a, b) => {
2899
- const maxLength = Math.max(a.length, b.length);
2900
- for (let i = 0; i < maxLength; i++) {
2901
- if ((a[i] || 0) === (b[i] || 0)) {
2902
- continue;
2477
+ case ConnectionState2.ONLINE: {
2478
+ this._connectionState = state;
2479
+ this._messenger.open();
2480
+ await Promise.all([
2481
+ ...this._swarms.values()
2482
+ ].map((swarm) => swarm.goOnline()));
2483
+ await this._signalManager.open();
2484
+ break;
2485
+ }
2903
2486
  }
2904
- return (a[i] || 0) < (b[i] || 0) ? -1 : 1;
2487
+ this.connectionStateChanged.emit(this._connectionState);
2905
2488
  }
2906
- return 0;
2907
2489
  };
2490
+ _ts_decorate4([
2491
+ synchronized4
2492
+ ], SwarmNetworkManager.prototype, "joinSwarm", null);
2493
+ _ts_decorate4([
2494
+ synchronized4
2495
+ ], SwarmNetworkManager.prototype, "leaveSwarm", null);
2908
2496
 
2909
- // src/topology/star-topology.ts
2910
- import { invariant as invariant9 } from "@dxos/invariant";
2911
- import { log as log10 } from "@dxos/log";
2912
- function _define_property11(obj, key, value) {
2913
- if (key in obj) {
2914
- Object.defineProperty(obj, key, {
2915
- value,
2916
- enumerable: true,
2917
- configurable: true,
2918
- writable: true
2919
- });
2920
- } else {
2921
- obj[key] = value;
2922
- }
2923
- return obj;
2924
- }
2925
- var __dxlog_file11 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
2926
- var StarTopology = class {
2497
+ // src/topology/fully-connected-topology.ts
2498
+ import { invariant as invariant7 } from "@dxos/invariant";
2499
+ var __dxlog_file8 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
2500
+ var FullyConnectedTopology = class {
2501
+ _controller;
2927
2502
  toString() {
2928
- return `StarTopology(${this._centralPeer.truncate()})`;
2503
+ return "FullyConnectedTopology";
2929
2504
  }
2930
2505
  init(controller) {
2931
- invariant9(!this._controller, "Already initialized.", {
2932
- F: __dxlog_file11,
2933
- L: 21,
2506
+ invariant7(!this._controller, "Already initialized", {
2507
+ F: __dxlog_file8,
2508
+ L: 18,
2934
2509
  S: this,
2935
2510
  A: [
2936
2511
  "!this._controller",
2937
- "'Already initialized.'"
2512
+ "'Already initialized'"
2938
2513
  ]
2939
2514
  });
2940
2515
  this._controller = controller;
2941
2516
  }
2942
2517
  update() {
2943
- invariant9(this._controller, "Not initialized.", {
2944
- F: __dxlog_file11,
2945
- L: 26,
2518
+ invariant7(this._controller, "Not initialized", {
2519
+ F: __dxlog_file8,
2520
+ L: 23,
2946
2521
  S: this,
2947
2522
  A: [
2948
2523
  "this._controller",
2949
- "'Not initialized.'"
2524
+ "'Not initialized'"
2950
2525
  ]
2951
2526
  });
2952
- const { candidates, connected, ownPeerId } = this._controller.getState();
2953
- if (!ownPeerId.equals(this._centralPeer)) {
2954
- log10("leaf peer dropping all connections apart from central peer.", void 0, {
2955
- F: __dxlog_file11,
2956
- L: 29,
2957
- S: this,
2958
- C: (f, a) => f(...a)
2959
- });
2960
- for (const peer of connected) {
2961
- if (!peer.equals(this._centralPeer)) {
2962
- log10("dropping connection", {
2963
- peer
2964
- }, {
2965
- F: __dxlog_file11,
2966
- L: 34,
2967
- S: this,
2968
- C: (f, a) => f(...a)
2969
- });
2970
- this._controller.disconnect(peer);
2971
- }
2972
- }
2973
- }
2974
- for (const peer of candidates) {
2975
- if (peer.equals(this._centralPeer) || ownPeerId.equals(this._centralPeer)) {
2976
- log10("connecting to peer", {
2977
- peer
2978
- }, {
2979
- F: __dxlog_file11,
2980
- L: 43,
2981
- S: this,
2982
- C: (f, a) => f(...a)
2983
- });
2984
- this._controller.connect(peer);
2985
- }
2527
+ const { candidates: discovered } = this._controller.getState();
2528
+ for (const peer of discovered) {
2529
+ this._controller.connect(peer);
2986
2530
  }
2987
2531
  }
2988
2532
  async onOffer(peer) {
2989
- invariant9(this._controller, "Not initialized.", {
2990
- F: __dxlog_file11,
2991
- L: 50,
2992
- S: this,
2993
- A: [
2994
- "this._controller",
2995
- "'Not initialized.'"
2996
- ]
2997
- });
2998
- const { ownPeerId } = this._controller.getState();
2999
- log10("offer", {
3000
- peer,
3001
- isCentral: peer.equals(this._centralPeer),
3002
- isSelfCentral: ownPeerId.equals(this._centralPeer)
3003
- }, {
3004
- F: __dxlog_file11,
3005
- L: 52,
3006
- S: this,
3007
- C: (f, a) => f(...a)
3008
- });
3009
- return ownPeerId.equals(this._centralPeer) || peer.equals(this._centralPeer);
2533
+ return true;
3010
2534
  }
3011
2535
  async destroy() {
3012
2536
  }
3013
- constructor(_centralPeer) {
3014
- _define_property11(this, "_centralPeer", void 0);
3015
- _define_property11(this, "_controller", void 0);
3016
- this._centralPeer = _centralPeer;
3017
- }
3018
2537
  };
3019
2538
 
3020
2539
  // src/transport/memory-transport.ts
3021
2540
  import { Transform } from "@dxos/node-std/stream";
3022
2541
  import { Event as Event7, Trigger as Trigger2 } from "@dxos/async";
3023
2542
  import { ErrorStream as ErrorStream3 } from "@dxos/debug";
3024
- import { invariant as invariant10 } from "@dxos/invariant";
2543
+ import { invariant as invariant8 } from "@dxos/invariant";
3025
2544
  import { PublicKey as PublicKey9 } from "@dxos/keys";
3026
- import { log as log11, logInfo as logInfo3 } from "@dxos/log";
2545
+ import { log as log8, logInfo as logInfo3 } from "@dxos/log";
3027
2546
  import { ComplexMap as ComplexMap7 } from "@dxos/util";
3028
- function _define_property12(obj, key, value) {
3029
- if (key in obj) {
3030
- Object.defineProperty(obj, key, {
3031
- value,
3032
- enumerable: true,
3033
- configurable: true,
3034
- writable: true
3035
- });
3036
- } else {
3037
- obj[key] = value;
3038
- }
3039
- return obj;
3040
- }
3041
2547
  function _ts_decorate5(decorators, target, key, desc) {
3042
2548
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3043
2549
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
3044
2550
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
3045
2551
  return c > 3 && r && Object.defineProperty(target, key, r), r;
3046
2552
  }
3047
- var __dxlog_file12 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
2553
+ var __dxlog_file9 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
3048
2554
  var MEMORY_TRANSPORT_DELAY = 1;
3049
2555
  var createStreamDelay = (delay) => {
3050
2556
  return new Transform({
@@ -3058,19 +2564,45 @@ var MemoryTransportFactory = {
3058
2564
  createTransport: (options) => new MemoryTransport(options)
3059
2565
  };
3060
2566
  var MemoryTransport = class _MemoryTransport {
2567
+ _options;
2568
+ // TODO(burdon): Remove static properties (inject context into constructor).
2569
+ static _connections = new ComplexMap7(PublicKey9.hash);
2570
+ _instanceId = PublicKey9.random();
2571
+ _remote = new Trigger2();
2572
+ _outgoingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
2573
+ _incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
2574
+ _closed = false;
2575
+ _remoteInstanceId;
2576
+ _remoteConnection;
2577
+ closed = new Event7();
2578
+ connected = new Event7();
2579
+ errors = new ErrorStream3();
2580
+ constructor(_options) {
2581
+ this._options = _options;
2582
+ invariant8(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection", {
2583
+ F: __dxlog_file9,
2584
+ L: 64,
2585
+ S: this,
2586
+ A: [
2587
+ "!MemoryTransport._connections.has(this._instanceId)",
2588
+ "'Duplicate memory connection'"
2589
+ ]
2590
+ });
2591
+ _MemoryTransport._connections.set(this._instanceId, this);
2592
+ }
3061
2593
  get isOpen() {
3062
2594
  return !this._closed;
3063
2595
  }
3064
2596
  async open() {
3065
- log11("opening...", void 0, {
3066
- F: __dxlog_file12,
2597
+ log8("opening...", void 0, {
2598
+ F: __dxlog_file9,
3067
2599
  L: 74,
3068
2600
  S: this,
3069
2601
  C: (f, a) => f(...a)
3070
2602
  });
3071
2603
  if (this._options.initiator) {
3072
- log11("sending signal", void 0, {
3073
- F: __dxlog_file12,
2604
+ log8("sending signal", void 0, {
2605
+ F: __dxlog_file9,
3074
2606
  L: 78,
3075
2607
  S: this,
3076
2608
  C: (f, a) => f(...a)
@@ -3100,8 +2632,8 @@ var MemoryTransport = class _MemoryTransport {
3100
2632
  this.closed.emit();
3101
2633
  return;
3102
2634
  }
3103
- invariant10(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`, {
3104
- F: __dxlog_file12,
2635
+ invariant8(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`, {
2636
+ F: __dxlog_file9,
3105
2637
  L: 104,
3106
2638
  S: this,
3107
2639
  A: [
@@ -3111,8 +2643,8 @@ var MemoryTransport = class _MemoryTransport {
3111
2643
  });
3112
2644
  this._remoteConnection._remoteConnection = this;
3113
2645
  this._remoteConnection._remoteInstanceId = this._instanceId;
3114
- log11("connected", void 0, {
3115
- F: __dxlog_file12,
2646
+ log8("connected", void 0, {
2647
+ F: __dxlog_file9,
3116
2648
  L: 108,
3117
2649
  S: this,
3118
2650
  C: (f, a) => f(...a)
@@ -3130,8 +2662,8 @@ var MemoryTransport = class _MemoryTransport {
3130
2662
  return this;
3131
2663
  }
3132
2664
  async close() {
3133
- log11("closing...", void 0, {
3134
- F: __dxlog_file12,
2665
+ log8("closing...", void 0, {
2666
+ F: __dxlog_file9,
3135
2667
  L: 130,
3136
2668
  S: this,
3137
2669
  C: (f, a) => f(...a)
@@ -3151,8 +2683,8 @@ var MemoryTransport = class _MemoryTransport {
3151
2683
  this._remoteConnection = void 0;
3152
2684
  }
3153
2685
  this.closed.emit();
3154
- log11("closed", void 0, {
3155
- F: __dxlog_file12,
2686
+ log8("closed", void 0, {
2687
+ F: __dxlog_file9,
3156
2688
  L: 158,
3157
2689
  S: this,
3158
2690
  C: (f, a) => f(...a)
@@ -3160,10 +2692,10 @@ var MemoryTransport = class _MemoryTransport {
3160
2692
  return this;
3161
2693
  }
3162
2694
  async onSignal({ payload }) {
3163
- log11("received signal", {
2695
+ log8("received signal", {
3164
2696
  payload
3165
2697
  }, {
3166
- F: __dxlog_file12,
2698
+ F: __dxlog_file9,
3167
2699
  L: 163,
3168
2700
  S: this,
3169
2701
  C: (f, a) => f(...a)
@@ -3188,40 +2720,7 @@ var MemoryTransport = class _MemoryTransport {
3188
2720
  packetsReceived: 0
3189
2721
  };
3190
2722
  }
3191
- constructor(_options) {
3192
- _define_property12(this, "_options", void 0);
3193
- _define_property12(this, "_instanceId", void 0);
3194
- _define_property12(this, "_remote", void 0);
3195
- _define_property12(this, "_outgoingDelay", void 0);
3196
- _define_property12(this, "_incomingDelay", void 0);
3197
- _define_property12(this, "_closed", void 0);
3198
- _define_property12(this, "_remoteInstanceId", void 0);
3199
- _define_property12(this, "_remoteConnection", void 0);
3200
- _define_property12(this, "closed", void 0);
3201
- _define_property12(this, "connected", void 0);
3202
- _define_property12(this, "errors", void 0);
3203
- this._options = _options;
3204
- this._instanceId = PublicKey9.random();
3205
- this._remote = new Trigger2();
3206
- this._outgoingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
3207
- this._incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
3208
- this._closed = false;
3209
- this.closed = new Event7();
3210
- this.connected = new Event7();
3211
- this.errors = new ErrorStream3();
3212
- invariant10(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection", {
3213
- F: __dxlog_file12,
3214
- L: 64,
3215
- S: this,
3216
- A: [
3217
- "!MemoryTransport._connections.has(this._instanceId)",
3218
- "'Duplicate memory connection'"
3219
- ]
3220
- });
3221
- _MemoryTransport._connections.set(this._instanceId, this);
3222
- }
3223
2723
  };
3224
- _define_property12(MemoryTransport, "_connections", new ComplexMap7(PublicKey9.hash));
3225
2724
  _ts_decorate5([
3226
2725
  logInfo3
3227
2726
  ], MemoryTransport.prototype, "_instanceId", void 0);
@@ -3241,19 +2740,6 @@ var TransportKind = /* @__PURE__ */ (function(TransportKind2) {
3241
2740
 
3242
2741
  // src/transport/webrtc/rtc-connection-factory.ts
3243
2742
  import { Mutex } from "@dxos/async";
3244
- function _define_property13(obj, key, value) {
3245
- if (key in obj) {
3246
- Object.defineProperty(obj, key, {
3247
- value,
3248
- enumerable: true,
3249
- configurable: true,
3250
- writable: true
3251
- });
3252
- } else {
3253
- obj[key] = value;
3254
- }
3255
- return obj;
3256
- }
3257
2743
  var BrowserRtcConnectionFactory = class {
3258
2744
  async initialize() {
3259
2745
  }
@@ -3266,6 +2752,8 @@ var BrowserRtcConnectionFactory = class {
3266
2752
  }
3267
2753
  };
3268
2754
  var NodeRtcConnectionFactory = class _NodeRtcConnectionFactory {
2755
+ static _createdConnections = 0;
2756
+ static _cleanupMutex = new Mutex();
3269
2757
  // This should be inside the function to avoid triggering `eval` in the global scope.
3270
2758
  // eslint-disable-next-line @typescript-eslint/no-implied-eval
3271
2759
  // TODO(burdon): Do imports here?
@@ -3291,16 +2779,14 @@ var NodeRtcConnectionFactory = class _NodeRtcConnectionFactory {
3291
2779
  }
3292
2780
  }
3293
2781
  };
3294
- _define_property13(NodeRtcConnectionFactory, "_createdConnections", 0);
3295
- _define_property13(NodeRtcConnectionFactory, "_cleanupMutex", new Mutex());
3296
2782
  var getRtcConnectionFactory = () => {
3297
2783
  return typeof globalThis.RTCPeerConnection === "undefined" ? new NodeRtcConnectionFactory() : new BrowserRtcConnectionFactory();
3298
2784
  };
3299
2785
 
3300
2786
  // src/transport/webrtc/rtc-peer-connection.ts
3301
2787
  import { Mutex as Mutex2, Trigger as Trigger3, synchronized as synchronized5 } from "@dxos/async";
3302
- import { invariant as invariant12 } from "@dxos/invariant";
3303
- import { log as log13, logInfo as logInfo4 } from "@dxos/log";
2788
+ import { invariant as invariant10 } from "@dxos/invariant";
2789
+ import { log as log10, logInfo as logInfo4 } from "@dxos/log";
3304
2790
  import { ConnectivityError as ConnectivityError3 } from "@dxos/protocols";
3305
2791
  import { trace as trace4 } from "@dxos/tracing";
3306
2792
 
@@ -3309,8 +2795,8 @@ import { Duplex } from "@dxos/node-std/stream";
3309
2795
  import { Event as AsyncEvent } from "@dxos/async";
3310
2796
  import { Resource } from "@dxos/context";
3311
2797
  import { ErrorStream as ErrorStream4 } from "@dxos/debug";
3312
- import { invariant as invariant11 } from "@dxos/invariant";
3313
- import { log as log12 } from "@dxos/log";
2798
+ import { invariant as invariant9 } from "@dxos/invariant";
2799
+ import { log as log9 } from "@dxos/log";
3314
2800
  import { ConnectivityError as ConnectivityError2 } from "@dxos/protocols";
3315
2801
 
3316
2802
  // src/transport/webrtc/rtc-transport-stats.ts
@@ -3361,23 +2847,22 @@ var getRtcConnectionStats = async (connection, channelTopic) => {
3361
2847
  };
3362
2848
 
3363
2849
  // src/transport/webrtc/rtc-transport-channel.ts
3364
- function _define_property14(obj, key, value) {
3365
- if (key in obj) {
3366
- Object.defineProperty(obj, key, {
3367
- value,
3368
- enumerable: true,
3369
- configurable: true,
3370
- writable: true
3371
- });
3372
- } else {
3373
- obj[key] = value;
3374
- }
3375
- return obj;
3376
- }
3377
- var __dxlog_file13 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc/rtc-transport-channel.ts";
2850
+ var __dxlog_file10 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc/rtc-transport-channel.ts";
3378
2851
  var MAX_MESSAGE_SIZE = 64 * 1024;
3379
2852
  var MAX_BUFFERED_AMOUNT = 64 * 1024;
3380
2853
  var RtcTransportChannel = class extends Resource {
2854
+ _connection;
2855
+ _options;
2856
+ closed = new AsyncEvent();
2857
+ connected = new AsyncEvent();
2858
+ errors = new ErrorStream4();
2859
+ _channel;
2860
+ _stream;
2861
+ _streamDataFlushedCallback = null;
2862
+ _isChannelCreationInProgress = false;
2863
+ constructor(_connection, _options) {
2864
+ super(), this._connection = _connection, this._options = _options;
2865
+ }
3381
2866
  get isRtcChannelCreationInProgress() {
3382
2867
  return this._isChannelCreationInProgress;
3383
2868
  }
@@ -3387,9 +2872,9 @@ var RtcTransportChannel = class extends Resource {
3387
2872
  }
3388
2873
  }
3389
2874
  async _open() {
3390
- invariant11(!this._isChannelCreationInProgress, void 0, {
3391
- F: __dxlog_file13,
3392
- L: 57,
2875
+ invariant9(!this._isChannelCreationInProgress, void 0, {
2876
+ F: __dxlog_file10,
2877
+ L: 56,
3393
2878
  S: this,
3394
2879
  A: [
3395
2880
  "!this._isChannelCreationInProgress",
@@ -3406,14 +2891,16 @@ var RtcTransportChannel = class extends Resource {
3406
2891
  }
3407
2892
  }).catch((err) => {
3408
2893
  if (this.isOpen) {
3409
- const error = err instanceof Error ? err : new ConnectivityError2(`Failed to create a channel: ${JSON.stringify(err?.message)}`);
2894
+ const error = err instanceof Error ? err : new ConnectivityError2({
2895
+ message: `Failed to create a channel: ${JSON.stringify(err?.message)}`
2896
+ });
3410
2897
  this.errors.raise(error);
3411
2898
  } else {
3412
- log12.verbose("connection establishment failed after transport was closed", {
2899
+ log9.verbose("connection establishment failed after transport was closed", {
3413
2900
  err
3414
2901
  }, {
3415
- F: __dxlog_file13,
3416
- L: 77,
2902
+ F: __dxlog_file10,
2903
+ L: 76,
3417
2904
  S: this,
3418
2905
  C: (f, a) => f(...a)
3419
2906
  });
@@ -3429,9 +2916,9 @@ var RtcTransportChannel = class extends Resource {
3429
2916
  this._stream = void 0;
3430
2917
  }
3431
2918
  this.closed.emit();
3432
- log12("closed", void 0, {
3433
- F: __dxlog_file13,
3434
- L: 93,
2919
+ log9("closed", void 0, {
2920
+ F: __dxlog_file10,
2921
+ L: 92,
3435
2922
  S: this,
3436
2923
  C: (f, a) => f(...a)
3437
2924
  });
@@ -3440,20 +2927,20 @@ var RtcTransportChannel = class extends Resource {
3440
2927
  Object.assign(channel, {
3441
2928
  onopen: () => {
3442
2929
  if (!this.isOpen) {
3443
- log12.warn("channel opened in a closed transport", {
2930
+ log9.warn("channel opened in a closed transport", {
3444
2931
  topic: this._options.topic
3445
2932
  }, {
3446
- F: __dxlog_file13,
3447
- L: 100,
2933
+ F: __dxlog_file10,
2934
+ L: 99,
3448
2935
  S: this,
3449
2936
  C: (f, a) => f(...a)
3450
2937
  });
3451
2938
  this._safeCloseChannel(channel);
3452
2939
  return;
3453
2940
  }
3454
- log12("onopen", void 0, {
3455
- F: __dxlog_file13,
3456
- L: 105,
2941
+ log9("onopen", void 0, {
2942
+ F: __dxlog_file10,
2943
+ L: 104,
3457
2944
  S: this,
3458
2945
  C: (f, a) => f(...a)
3459
2946
  });
@@ -3469,9 +2956,9 @@ var RtcTransportChannel = class extends Resource {
3469
2956
  this.connected.emit();
3470
2957
  },
3471
2958
  onclose: async () => {
3472
- log12("onclose", void 0, {
3473
- F: __dxlog_file13,
3474
- L: 118,
2959
+ log9("onclose", void 0, {
2960
+ F: __dxlog_file10,
2961
+ L: 117,
3475
2962
  S: this,
3476
2963
  C: (f, a) => f(...a)
3477
2964
  });
@@ -3479,9 +2966,9 @@ var RtcTransportChannel = class extends Resource {
3479
2966
  },
3480
2967
  onmessage: async (event) => {
3481
2968
  if (!this._stream) {
3482
- log12.warn("ignoring message on a closed channel", void 0, {
3483
- F: __dxlog_file13,
3484
- L: 124,
2969
+ log9.warn("ignoring message on a closed channel", void 0, {
2970
+ F: __dxlog_file10,
2971
+ L: 123,
3485
2972
  S: this,
3486
2973
  C: (f, a) => f(...a)
3487
2974
  });
@@ -3510,9 +2997,9 @@ var RtcTransportChannel = class extends Resource {
3510
2997
  }
3511
2998
  async _handleChannelWrite(chunk, callback) {
3512
2999
  if (!this._channel) {
3513
- log12.warn("writing to a channel after a connection was closed", void 0, {
3514
- F: __dxlog_file13,
3515
- L: 154,
3000
+ log9.warn("writing to a channel after a connection was closed", void 0, {
3001
+ F: __dxlog_file10,
3002
+ L: 153,
3516
3003
  S: this,
3517
3004
  C: (f, a) => f(...a)
3518
3005
  });
@@ -3533,9 +3020,9 @@ var RtcTransportChannel = class extends Resource {
3533
3020
  }
3534
3021
  if (this._channel.bufferedAmount > MAX_BUFFERED_AMOUNT) {
3535
3022
  if (this._streamDataFlushedCallback !== null) {
3536
- log12.error("consumer trying to write before we are ready for more data", void 0, {
3537
- F: __dxlog_file13,
3538
- L: 175,
3023
+ log9.error("consumer trying to write before we are ready for more data", void 0, {
3024
+ F: __dxlog_file10,
3025
+ L: 174,
3539
3026
  S: this,
3540
3027
  C: (f, a) => f(...a)
3541
3028
  });
@@ -3549,9 +3036,9 @@ var RtcTransportChannel = class extends Resource {
3549
3036
  try {
3550
3037
  channel.close();
3551
3038
  } catch (error) {
3552
- log12.catch(error, void 0, {
3553
- F: __dxlog_file13,
3554
- L: 187,
3039
+ log9.catch(error, void 0, {
3040
+ F: __dxlog_file10,
3041
+ L: 186,
3555
3042
  S: this,
3556
3043
  C: (f, a) => f(...a)
3557
3044
  });
@@ -3566,9 +3053,6 @@ var RtcTransportChannel = class extends Resource {
3566
3053
  async getStats() {
3567
3054
  return createRtcTransportStats(this._connection.currentConnection, this._options.topic);
3568
3055
  }
3569
- constructor(_connection, _options) {
3570
- super(), _define_property14(this, "_connection", void 0), _define_property14(this, "_options", void 0), _define_property14(this, "closed", void 0), _define_property14(this, "connected", void 0), _define_property14(this, "errors", void 0), _define_property14(this, "_channel", void 0), _define_property14(this, "_stream", void 0), _define_property14(this, "_streamDataFlushedCallback", void 0), _define_property14(this, "_isChannelCreationInProgress", void 0), this._connection = _connection, this._options = _options, this.closed = new AsyncEvent(), this.connected = new AsyncEvent(), this.errors = new ErrorStream4(), this._streamDataFlushedCallback = null, this._isChannelCreationInProgress = false;
3571
- }
3572
3056
  };
3573
3057
 
3574
3058
  // src/transport/webrtc/utils.ts
@@ -3598,27 +3082,37 @@ var deduplicatedSdpLines = (sdp) => {
3598
3082
  };
3599
3083
 
3600
3084
  // src/transport/webrtc/rtc-peer-connection.ts
3601
- function _define_property15(obj, key, value) {
3602
- if (key in obj) {
3603
- Object.defineProperty(obj, key, {
3604
- value,
3605
- enumerable: true,
3606
- configurable: true,
3607
- writable: true
3608
- });
3609
- } else {
3610
- obj[key] = value;
3611
- }
3612
- return obj;
3613
- }
3614
3085
  function _ts_decorate6(decorators, target, key, desc) {
3615
3086
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3616
3087
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
3617
3088
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
3618
3089
  return c > 3 && r && Object.defineProperty(target, key, r), r;
3619
3090
  }
3620
- var __dxlog_file14 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc/rtc-peer-connection.ts";
3091
+ var __dxlog_file11 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc/rtc-peer-connection.ts";
3621
3092
  var RtcPeerConnection = class {
3093
+ _factory;
3094
+ _options;
3095
+ // A peer who is not the initiator waits for another party to open a channel.
3096
+ _channelCreatedCallbacks = /* @__PURE__ */ new Map();
3097
+ // Channels indexed by topic.
3098
+ _transportChannels = /* @__PURE__ */ new Map();
3099
+ _dataChannels = /* @__PURE__ */ new Map();
3100
+ // A peer is ready to receive ICE candidates when local and remote description were set.
3101
+ _readyForCandidates = new Trigger3();
3102
+ _offerProcessingMutex = new Mutex2();
3103
+ /**
3104
+ * Can't use peer.connection.initiator, because if two connections to the same peer are created in
3105
+ * different swarms, we might be the initiator of the first one, but not of the other one.
3106
+ * Use a stable peer keypair property (key ordering) to decide who's acting as the initiator of
3107
+ * transport connection establishment and data channel creation.
3108
+ */
3109
+ _initiator;
3110
+ _connection;
3111
+ constructor(_factory, _options) {
3112
+ this._factory = _factory;
3113
+ this._options = _options;
3114
+ this._initiator = chooseInitiatorPeer(_options.ownPeerKey, _options.remotePeerKey) === _options.ownPeerKey;
3115
+ }
3622
3116
  get transportChannelCount() {
3623
3117
  return this._transportChannels.size;
3624
3118
  }
@@ -3642,9 +3136,9 @@ var RtcPeerConnection = class {
3642
3136
  if (existingChannel) {
3643
3137
  return existingChannel;
3644
3138
  }
3645
- log13("waiting for initiator-peer to open a data channel", void 0, {
3646
- F: __dxlog_file14,
3647
- L: 96,
3139
+ log10("waiting for initiator-peer to open a data channel", void 0, {
3140
+ F: __dxlog_file11,
3141
+ L: 95,
3648
3142
  S: this,
3649
3143
  C: (f, a) => f(...a)
3650
3144
  });
@@ -3671,11 +3165,11 @@ var RtcPeerConnection = class {
3671
3165
  if (this._connection) {
3672
3166
  return this._connection;
3673
3167
  }
3674
- log13("initializing connection...", () => ({
3168
+ log10("initializing connection...", () => ({
3675
3169
  remotePeer: this._options.remotePeerKey
3676
3170
  }), {
3677
- F: __dxlog_file14,
3678
- L: 121,
3171
+ F: __dxlog_file11,
3172
+ L: 120,
3679
3173
  S: this,
3680
3174
  C: (f, a) => f(...a)
3681
3175
  });
@@ -3684,9 +3178,9 @@ var RtcPeerConnection = class {
3684
3178
  const iceCandidateErrors = [];
3685
3179
  Object.assign(connection, {
3686
3180
  onnegotiationneeded: async () => {
3687
- invariant12(this._initiator, void 0, {
3688
- F: __dxlog_file14,
3689
- L: 136,
3181
+ invariant10(this._initiator, void 0, {
3182
+ F: __dxlog_file11,
3183
+ L: 135,
3690
3184
  S: this,
3691
3185
  A: [
3692
3186
  "this._initiator",
@@ -3697,9 +3191,9 @@ var RtcPeerConnection = class {
3697
3191
  this._onConnectionCallbackAfterClose("onnegotiationneeded", connection);
3698
3192
  return;
3699
3193
  }
3700
- log13("onnegotiationneeded", void 0, {
3701
- F: __dxlog_file14,
3702
- L: 143,
3194
+ log10("onnegotiationneeded", void 0, {
3195
+ F: __dxlog_file11,
3196
+ L: 142,
3703
3197
  S: this,
3704
3198
  C: (f, a) => f(...a)
3705
3199
  });
@@ -3719,19 +3213,19 @@ var RtcPeerConnection = class {
3719
3213
  return;
3720
3214
  }
3721
3215
  if (event.candidate) {
3722
- log13("onicecandidate", {
3216
+ log10("onicecandidate", {
3723
3217
  candidate: event.candidate.candidate
3724
3218
  }, {
3725
- F: __dxlog_file14,
3726
- L: 162,
3219
+ F: __dxlog_file11,
3220
+ L: 161,
3727
3221
  S: this,
3728
3222
  C: (f, a) => f(...a)
3729
3223
  });
3730
3224
  await this._sendIceCandidate(event.candidate);
3731
3225
  } else {
3732
- log13("onicecandidate gathering complete", void 0, {
3733
- F: __dxlog_file14,
3734
- L: 165,
3226
+ log10("onicecandidate gathering complete", void 0, {
3227
+ F: __dxlog_file11,
3228
+ L: 164,
3735
3229
  S: this,
3736
3230
  C: (f, a) => f(...a)
3737
3231
  });
@@ -3755,11 +3249,11 @@ var RtcPeerConnection = class {
3755
3249
  this._onConnectionCallbackAfterClose("oniceconnectionstatechange", connection);
3756
3250
  return;
3757
3251
  }
3758
- log13("oniceconnectionstatechange", {
3252
+ log10("oniceconnectionstatechange", {
3759
3253
  state: connection.iceConnectionState
3760
3254
  }, {
3761
- F: __dxlog_file14,
3762
- L: 185,
3255
+ F: __dxlog_file11,
3256
+ L: 184,
3763
3257
  S: this,
3764
3258
  C: (f, a) => f(...a)
3765
3259
  });
@@ -3777,11 +3271,11 @@ var RtcPeerConnection = class {
3777
3271
  }
3778
3272
  return;
3779
3273
  }
3780
- log13("onconnectionstatechange", {
3274
+ log10("onconnectionstatechange", {
3781
3275
  state: connection.connectionState
3782
3276
  }, {
3783
- F: __dxlog_file14,
3784
- L: 202,
3277
+ F: __dxlog_file11,
3278
+ L: 201,
3785
3279
  S: this,
3786
3280
  C: (f, a) => f(...a)
3787
3281
  });
@@ -3790,11 +3284,11 @@ var RtcPeerConnection = class {
3790
3284
  }
3791
3285
  },
3792
3286
  onsignalingstatechange: () => {
3793
- log13("onsignalingstatechange", {
3287
+ log10("onsignalingstatechange", {
3794
3288
  state: connection.signalingState
3795
3289
  }, {
3796
- F: __dxlog_file14,
3797
- L: 209,
3290
+ F: __dxlog_file11,
3291
+ L: 208,
3798
3292
  S: this,
3799
3293
  C: (f, a) => f(...a)
3800
3294
  });
@@ -3802,9 +3296,9 @@ var RtcPeerConnection = class {
3802
3296
  // When channel is added to connection.
3803
3297
  // https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/datachannel_event
3804
3298
  ondatachannel: (event) => {
3805
- invariant12(!this._initiator, "Initiator is expected to create data channels.", {
3806
- F: __dxlog_file14,
3807
- L: 215,
3299
+ invariant10(!this._initiator, "Initiator is expected to create data channels.", {
3300
+ F: __dxlog_file11,
3301
+ L: 214,
3808
3302
  S: this,
3809
3303
  A: [
3810
3304
  "!this._initiator",
@@ -3815,11 +3309,11 @@ var RtcPeerConnection = class {
3815
3309
  this._onConnectionCallbackAfterClose("ondatachannel", connection);
3816
3310
  return;
3817
3311
  }
3818
- log13("ondatachannel", {
3312
+ log10("ondatachannel", {
3819
3313
  label: event.channel.label
3820
3314
  }, {
3821
- F: __dxlog_file14,
3822
- L: 222,
3315
+ F: __dxlog_file11,
3316
+ L: 221,
3823
3317
  S: this,
3824
3318
  C: (f, a) => f(...a)
3825
3319
  });
@@ -3843,11 +3337,11 @@ var RtcPeerConnection = class {
3843
3337
  }
3844
3338
  _abortConnection(connection, error) {
3845
3339
  if (connection !== this._connection) {
3846
- log13.error("attempted to abort an inactive connection", {
3340
+ log10.error("attempted to abort an inactive connection", {
3847
3341
  error
3848
3342
  }, {
3849
- F: __dxlog_file14,
3850
- L: 247,
3343
+ F: __dxlog_file11,
3344
+ L: 246,
3851
3345
  S: this,
3852
3346
  C: (f, a) => f(...a)
3853
3347
  });
@@ -3864,19 +3358,19 @@ var RtcPeerConnection = class {
3864
3358
  }
3865
3359
  this._transportChannels.clear();
3866
3360
  this._safeCloseConnection();
3867
- log13("connection aborted", {
3361
+ log10("connection aborted", {
3868
3362
  reason: error.message
3869
3363
  }, {
3870
- F: __dxlog_file14,
3871
- L: 261,
3364
+ F: __dxlog_file11,
3365
+ L: 260,
3872
3366
  S: this,
3873
3367
  C: (f, a) => f(...a)
3874
3368
  });
3875
3369
  }
3876
3370
  async _lockAndCloseConnection() {
3877
- invariant12(this._transportChannels.size === 0, void 0, {
3878
- F: __dxlog_file14,
3879
- L: 266,
3371
+ invariant10(this._transportChannels.size === 0, void 0, {
3372
+ F: __dxlog_file11,
3373
+ L: 265,
3880
3374
  S: this,
3881
3375
  A: [
3882
3376
  "this._transportChannels.size === 0",
@@ -3885,9 +3379,9 @@ var RtcPeerConnection = class {
3885
3379
  });
3886
3380
  if (this._connection) {
3887
3381
  this._safeCloseConnection();
3888
- log13("connection closed", void 0, {
3889
- F: __dxlog_file14,
3890
- L: 269,
3382
+ log10("connection closed", void 0, {
3383
+ F: __dxlog_file11,
3384
+ L: 268,
3891
3385
  S: this,
3892
3386
  C: (f, a) => f(...a)
3893
3387
  });
@@ -3896,11 +3390,11 @@ var RtcPeerConnection = class {
3896
3390
  async onSignal(signal) {
3897
3391
  const connection = this._connection;
3898
3392
  if (!connection) {
3899
- log13.warn("a signal ignored because the connection was closed", {
3393
+ log10.warn("a signal ignored because the connection was closed", {
3900
3394
  type: signal.payload.data.type
3901
3395
  }, {
3902
- F: __dxlog_file14,
3903
- L: 277,
3396
+ F: __dxlog_file11,
3397
+ L: 276,
3904
3398
  S: this,
3905
3399
  C: (f, a) => f(...a)
3906
3400
  });
@@ -3963,11 +3457,11 @@ var RtcPeerConnection = class {
3963
3457
  this._abortConnection(connection, new Error(`Unknown signal type ${data.type}.`));
3964
3458
  break;
3965
3459
  }
3966
- log13("signal processed", {
3460
+ log10("signal processed", {
3967
3461
  type: data.type
3968
3462
  }, {
3969
- F: __dxlog_file14,
3970
- L: 336,
3463
+ F: __dxlog_file11,
3464
+ L: 335,
3971
3465
  S: this,
3972
3466
  C: (f, a) => f(...a)
3973
3467
  });
@@ -3976,20 +3470,20 @@ var RtcPeerConnection = class {
3976
3470
  try {
3977
3471
  await this._readyForCandidates.wait();
3978
3472
  if (connection === this._connection) {
3979
- log13("adding ice candidate", {
3473
+ log10("adding ice candidate", {
3980
3474
  candidate
3981
3475
  }, {
3982
- F: __dxlog_file14,
3983
- L: 344,
3476
+ F: __dxlog_file11,
3477
+ L: 343,
3984
3478
  S: this,
3985
3479
  C: (f, a) => f(...a)
3986
3480
  });
3987
3481
  await connection.addIceCandidate(candidate);
3988
3482
  }
3989
3483
  } catch (err) {
3990
- log13.catch(err, void 0, {
3991
- F: __dxlog_file14,
3992
- L: 348,
3484
+ log10.catch(err, void 0, {
3485
+ F: __dxlog_file11,
3486
+ L: 347,
3993
3487
  S: this,
3994
3488
  C: (f, a) => f(...a)
3995
3489
  });
@@ -3997,29 +3491,29 @@ var RtcPeerConnection = class {
3997
3491
  }
3998
3492
  _onSessionNegotiated(connection) {
3999
3493
  if (connection === this._connection) {
4000
- log13("ready to process ice candidates", void 0, {
4001
- F: __dxlog_file14,
4002
- L: 354,
3494
+ log10("ready to process ice candidates", void 0, {
3495
+ F: __dxlog_file11,
3496
+ L: 353,
4003
3497
  S: this,
4004
3498
  C: (f, a) => f(...a)
4005
3499
  });
4006
3500
  this._readyForCandidates.wake();
4007
3501
  } else {
4008
- log13.warn("session was negotiated after connection became inactive", void 0, {
4009
- F: __dxlog_file14,
4010
- L: 357,
3502
+ log10.warn("session was negotiated after connection became inactive", void 0, {
3503
+ F: __dxlog_file11,
3504
+ L: 356,
4011
3505
  S: this,
4012
3506
  C: (f, a) => f(...a)
4013
3507
  });
4014
3508
  }
4015
3509
  }
4016
3510
  _onConnectionCallbackAfterClose(callback, connection) {
4017
- log13.warn("callback invoked after a connection was destroyed, this is probably a bug", {
3511
+ log10.warn("callback invoked after a connection was destroyed, this is probably a bug", {
4018
3512
  callback,
4019
3513
  state: connection.connectionState
4020
3514
  }, {
4021
- F: __dxlog_file14,
4022
- L: 362,
3515
+ F: __dxlog_file11,
3516
+ L: 361,
4023
3517
  S: this,
4024
3518
  C: (f, a) => f(...a)
4025
3519
  });
@@ -4030,9 +3524,9 @@ var RtcPeerConnection = class {
4030
3524
  try {
4031
3525
  connection?.close();
4032
3526
  } catch (err) {
4033
- log13.catch(err, void 0, {
4034
- F: __dxlog_file14,
4035
- L: 374,
3527
+ log10.catch(err, void 0, {
3528
+ F: __dxlog_file11,
3529
+ L: 373,
4036
3530
  S: this,
4037
3531
  C: (f, a) => f(...a)
4038
3532
  });
@@ -4041,9 +3535,9 @@ var RtcPeerConnection = class {
4041
3535
  this._connection = void 0;
4042
3536
  this._dataChannels.clear();
4043
3537
  this._readyForCandidates.wake();
4044
- void this._factory.onConnectionDestroyed().catch((err) => log13.catch(err, void 0, {
4045
- F: __dxlog_file14,
4046
- L: 380,
3538
+ void this._factory.onConnectionDestroyed().catch((err) => log10.catch(err, void 0, {
3539
+ F: __dxlog_file11,
3540
+ L: 379,
4047
3541
  S: this,
4048
3542
  C: (f, a) => f(...a)
4049
3543
  }));
@@ -4066,9 +3560,9 @@ var RtcPeerConnection = class {
4066
3560
  ];
4067
3561
  }
4068
3562
  } catch (error) {
4069
- log13.catch(error, void 0, {
4070
- F: __dxlog_file14,
4071
- L: 396,
3563
+ log10.catch(error, void 0, {
3564
+ F: __dxlog_file11,
3565
+ L: 395,
4072
3566
  S: this,
4073
3567
  C: (f, a) => f(...a)
4074
3568
  });
@@ -4091,11 +3585,11 @@ var RtcPeerConnection = class {
4091
3585
  }
4092
3586
  });
4093
3587
  } catch (err) {
4094
- log13.warn("signaling error", {
3588
+ log10.warn("signaling error", {
4095
3589
  err
4096
3590
  }, {
4097
- F: __dxlog_file14,
4098
- L: 417,
3591
+ F: __dxlog_file11,
3592
+ L: 416,
4099
3593
  S: this,
4100
3594
  C: (f, a) => f(...a)
4101
3595
  });
@@ -4142,25 +3636,6 @@ var RtcPeerConnection = class {
4142
3636
  channels: this._transportChannels.size
4143
3637
  };
4144
3638
  }
4145
- constructor(_factory, _options) {
4146
- _define_property15(this, "_factory", void 0);
4147
- _define_property15(this, "_options", void 0);
4148
- _define_property15(this, "_channelCreatedCallbacks", void 0);
4149
- _define_property15(this, "_transportChannels", void 0);
4150
- _define_property15(this, "_dataChannels", void 0);
4151
- _define_property15(this, "_readyForCandidates", void 0);
4152
- _define_property15(this, "_offerProcessingMutex", void 0);
4153
- _define_property15(this, "_initiator", void 0);
4154
- _define_property15(this, "_connection", void 0);
4155
- this._factory = _factory;
4156
- this._options = _options;
4157
- this._channelCreatedCallbacks = /* @__PURE__ */ new Map();
4158
- this._transportChannels = /* @__PURE__ */ new Map();
4159
- this._dataChannels = /* @__PURE__ */ new Map();
4160
- this._readyForCandidates = new Trigger3();
4161
- this._offerProcessingMutex = new Mutex2();
4162
- this._initiator = chooseInitiatorPeer(_options.ownPeerKey, _options.remotePeerKey) === _options.ownPeerKey;
4163
- }
4164
3639
  };
4165
3640
  _ts_decorate6([
4166
3641
  synchronized5
@@ -4191,8 +3666,10 @@ var isRemoteDescriptionSet = (connection, data) => {
4191
3666
  };
4192
3667
  var createIceFailureError = (details) => {
4193
3668
  const candidateErrors = details.map(({ url, errorCode, errorText }) => `${errorCode} ${url}: ${errorText}`);
4194
- return new ConnectivityError3(`ICE failed:
4195
- ${candidateErrors.join("\n")}`);
3669
+ return new ConnectivityError3({
3670
+ message: `ICE failed:
3671
+ ${candidateErrors.join("\n")}`
3672
+ });
4196
3673
  };
4197
3674
 
4198
3675
  // src/transport/webrtc/rtc-transport-factory.ts
@@ -4218,30 +3695,26 @@ import { Writable } from "@dxos/node-std/stream";
4218
3695
  import { Event as Event8, scheduleTask as scheduleTask4 } from "@dxos/async";
4219
3696
  import { Resource as Resource2 } from "@dxos/context";
4220
3697
  import { ErrorStream as ErrorStream5 } from "@dxos/debug";
4221
- import { invariant as invariant13 } from "@dxos/invariant";
3698
+ import { invariant as invariant11 } from "@dxos/invariant";
4222
3699
  import { PublicKey as PublicKey10 } from "@dxos/keys";
4223
- import { log as log14 } from "@dxos/log";
3700
+ import { log as log11 } from "@dxos/log";
4224
3701
  import { ConnectionResetError as ConnectionResetError2, ConnectivityError as ConnectivityError4, TimeoutError as TimeoutError3 } from "@dxos/protocols";
4225
3702
  import { ConnectionState as ConnectionState3 } from "@dxos/protocols/proto/dxos/mesh/bridge";
4226
3703
  import { arrayToBuffer } from "@dxos/util";
4227
- function _define_property16(obj, key, value) {
4228
- if (key in obj) {
4229
- Object.defineProperty(obj, key, {
4230
- value,
4231
- enumerable: true,
4232
- configurable: true,
4233
- writable: true
4234
- });
4235
- } else {
4236
- obj[key] = value;
4237
- }
4238
- return obj;
4239
- }
4240
- var __dxlog_file15 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc/rtc-transport-proxy.ts";
3704
+ var __dxlog_file12 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc/rtc-transport-proxy.ts";
4241
3705
  var RPC_TIMEOUT = 1e4;
4242
3706
  var CLOSE_RPC_TIMEOUT = 3e3;
4243
3707
  var RESP_MIN_THRESHOLD = 500;
4244
3708
  var RtcTransportProxy = class extends Resource2 {
3709
+ _options;
3710
+ _proxyId = PublicKey10.random();
3711
+ closed = new Event8();
3712
+ connected = new Event8();
3713
+ errors = new ErrorStream5();
3714
+ _serviceStream;
3715
+ constructor(_options) {
3716
+ super(), this._options = _options;
3717
+ }
4245
3718
  async _open() {
4246
3719
  let stream;
4247
3720
  try {
@@ -4261,8 +3734,8 @@ var RtcTransportProxy = class extends Resource2 {
4261
3734
  this._serviceStream = stream;
4262
3735
  stream.waitUntilReady().then(() => {
4263
3736
  stream.subscribe(async (event) => {
4264
- log14("rtc transport proxy event", event, {
4265
- F: __dxlog_file15,
3737
+ log11("rtc transport proxy event", event, {
3738
+ F: __dxlog_file12,
4266
3739
  L: 66,
4267
3740
  S: this,
4268
3741
  C: (f, a) => f(...a)
@@ -4275,10 +3748,10 @@ var RtcTransportProxy = class extends Resource2 {
4275
3748
  await this._handleSignal(event.signal);
4276
3749
  }
4277
3750
  }, (err) => {
4278
- log14("rtc bridge stream closed", {
3751
+ log11("rtc bridge stream closed", {
4279
3752
  err
4280
3753
  }, {
4281
- F: __dxlog_file15,
3754
+ F: __dxlog_file12,
4282
3755
  L: 76,
4283
3756
  S: this,
4284
3757
  C: (f, a) => f(...a)
@@ -4299,8 +3772,8 @@ var RtcTransportProxy = class extends Resource2 {
4299
3772
  timeout: RPC_TIMEOUT
4300
3773
  }).then(() => {
4301
3774
  if (Date.now() - sendStartMs > RESP_MIN_THRESHOLD) {
4302
- log14("slow response, delaying callback", void 0, {
4303
- F: __dxlog_file15,
3775
+ log11("slow response, delaying callback", void 0, {
3776
+ F: __dxlog_file12,
4304
3777
  L: 93,
4305
3778
  S: this,
4306
3779
  C: (f, a) => f(...a)
@@ -4332,8 +3805,8 @@ var RtcTransportProxy = class extends Resource2 {
4332
3805
  await this._serviceStream?.close();
4333
3806
  this._serviceStream = void 0;
4334
3807
  } catch (err) {
4335
- log14.catch(err, void 0, {
4336
- F: __dxlog_file15,
3808
+ log11.catch(err, void 0, {
3809
+ F: __dxlog_file12,
4337
3810
  L: 128,
4338
3811
  S: this,
4339
3812
  C: (f, a) => f(...a)
@@ -4346,8 +3819,8 @@ var RtcTransportProxy = class extends Resource2 {
4346
3819
  timeout: CLOSE_RPC_TIMEOUT
4347
3820
  });
4348
3821
  } catch (err) {
4349
- log14.catch(err, void 0, {
4350
- F: __dxlog_file15,
3822
+ log11.catch(err, void 0, {
3823
+ F: __dxlog_file12,
4351
3824
  L: 134,
4352
3825
  S: this,
4353
3826
  C: (f, a) => f(...a)
@@ -4392,7 +3865,9 @@ var RtcTransportProxy = class extends Resource2 {
4392
3865
  } catch (error) {
4393
3866
  const type = signalEvent.payload.payload.data?.type;
4394
3867
  if (type === "offer" || type === "answer") {
4395
- this._raiseIfOpen(new ConnectivityError4(`Session establishment failed: ${type} couldn't be sent.`));
3868
+ this._raiseIfOpen(new ConnectivityError4({
3869
+ message: `Session establishment failed: ${type} couldn't be sent.`
3870
+ }));
4396
3871
  }
4397
3872
  }
4398
3873
  }
@@ -4430,11 +3905,11 @@ var RtcTransportProxy = class extends Resource2 {
4430
3905
  if (this.isOpen) {
4431
3906
  this.errors.raise(error);
4432
3907
  } else {
4433
- log14.info("error swallowed because transport was closed", {
3908
+ log11.info("error swallowed because transport was closed", {
4434
3909
  message: error.message
4435
3910
  }, {
4436
- F: __dxlog_file15,
4437
- L: 215,
3911
+ F: __dxlog_file12,
3912
+ L: 217,
4438
3913
  S: this,
4439
3914
  C: (f, a) => f(...a)
4440
3915
  });
@@ -4447,11 +3922,10 @@ var RtcTransportProxy = class extends Resource2 {
4447
3922
  void this._serviceStream?.close();
4448
3923
  this.closed.emit();
4449
3924
  }
4450
- constructor(_options) {
4451
- super(), _define_property16(this, "_options", void 0), _define_property16(this, "_proxyId", void 0), _define_property16(this, "closed", void 0), _define_property16(this, "connected", void 0), _define_property16(this, "errors", void 0), _define_property16(this, "_serviceStream", void 0), this._options = _options, this._proxyId = PublicKey10.random(), this.closed = new Event8(), this.connected = new Event8(), this.errors = new ErrorStream5();
4452
- }
4453
3925
  };
4454
3926
  var RtcTransportProxyFactory = class {
3927
+ _bridgeService;
3928
+ _connections = /* @__PURE__ */ new Set();
4455
3929
  /**
4456
3930
  * Sets the current BridgeService to be used to open connections.
4457
3931
  * Calling this method will close any existing connections.
@@ -4464,9 +3938,9 @@ var RtcTransportProxyFactory = class {
4464
3938
  return this;
4465
3939
  }
4466
3940
  createTransport(options) {
4467
- invariant13(this._bridgeService, "RtcTransportProxyFactory is not ready to open connections", {
4468
- F: __dxlog_file15,
4469
- L: 245,
3941
+ invariant11(this._bridgeService, "RtcTransportProxyFactory is not ready to open connections", {
3942
+ F: __dxlog_file12,
3943
+ L: 247,
4470
3944
  S: this,
4471
3945
  A: [
4472
3946
  "this._bridgeService",
@@ -4483,19 +3957,21 @@ var RtcTransportProxyFactory = class {
4483
3957
  });
4484
3958
  return transport;
4485
3959
  }
4486
- constructor() {
4487
- _define_property16(this, "_bridgeService", void 0);
4488
- _define_property16(this, "_connections", /* @__PURE__ */ new Set());
4489
- }
4490
3960
  };
4491
3961
  var decodeError = (err) => {
4492
3962
  const message = typeof err === "string" ? err : err.message;
4493
3963
  if (message.includes("CONNECTION_RESET")) {
4494
- return new ConnectionResetError2(message);
3964
+ return new ConnectionResetError2({
3965
+ message
3966
+ });
4495
3967
  } else if (message.includes("TIMEOUT")) {
4496
- return new TimeoutError3(message);
3968
+ return new TimeoutError3({
3969
+ message
3970
+ });
4497
3971
  } else if (message.includes("CONNECTIVITY_ERROR")) {
4498
- return new ConnectivityError4(message);
3972
+ return new ConnectivityError4({
3973
+ message
3974
+ });
4499
3975
  } else {
4500
3976
  return typeof err === "string" ? new Error(err) : err;
4501
3977
  }
@@ -4504,35 +3980,27 @@ var decodeError = (err) => {
4504
3980
  // src/transport/webrtc/rtc-transport-service.ts
4505
3981
  import { Duplex as Duplex2 } from "@dxos/node-std/stream";
4506
3982
  import { Stream } from "@dxos/codec-protobuf/stream";
4507
- import { invariant as invariant14 } from "@dxos/invariant";
3983
+ import { invariant as invariant12 } from "@dxos/invariant";
4508
3984
  import { PublicKey as PublicKey11 } from "@dxos/keys";
4509
- import { log as log15 } from "@dxos/log";
3985
+ import { log as log12 } from "@dxos/log";
4510
3986
  import { ConnectionState as ConnectionState4 } from "@dxos/protocols/proto/dxos/mesh/bridge";
4511
3987
  import { ComplexMap as ComplexMap8 } from "@dxos/util";
4512
- function _define_property17(obj, key, value) {
4513
- if (key in obj) {
4514
- Object.defineProperty(obj, key, {
4515
- value,
4516
- enumerable: true,
4517
- configurable: true,
4518
- writable: true
4519
- });
4520
- } else {
4521
- obj[key] = value;
4522
- }
4523
- return obj;
4524
- }
4525
- var __dxlog_file16 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc/rtc-transport-service.ts";
3988
+ var __dxlog_file13 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc/rtc-transport-service.ts";
4526
3989
  var RtcTransportService = class {
3990
+ _transportFactory;
3991
+ _openTransports = new ComplexMap8(PublicKey11.hash);
3992
+ constructor(webrtcConfig, iceProvider, _transportFactory = createRtcTransportFactory(webrtcConfig, iceProvider)) {
3993
+ this._transportFactory = _transportFactory;
3994
+ }
4527
3995
  hasOpenTransports() {
4528
3996
  return this._openTransports.size > 0;
4529
3997
  }
4530
3998
  open(request) {
4531
3999
  const existingTransport = this._openTransports.get(request.proxyId);
4532
4000
  if (existingTransport) {
4533
- log15.error("requesting a new transport bridge for an existing proxy", void 0, {
4534
- F: __dxlog_file16,
4535
- L: 54,
4001
+ log12.error("requesting a new transport bridge for an existing proxy", void 0, {
4002
+ F: __dxlog_file13,
4003
+ L: 53,
4536
4004
  S: this,
4537
4005
  C: (f, a) => f(...a)
4538
4006
  });
@@ -4596,9 +4064,9 @@ var RtcTransportService = class {
4596
4064
  close(err);
4597
4065
  });
4598
4066
  ready();
4599
- log15("stream ready", void 0, {
4600
- F: __dxlog_file16,
4601
- L: 116,
4067
+ log12("stream ready", void 0, {
4068
+ F: __dxlog_file13,
4069
+ L: 115,
4602
4070
  S: this,
4603
4071
  C: (f, a) => f(...a)
4604
4072
  });
@@ -4607,9 +4075,9 @@ var RtcTransportService = class {
4607
4075
  }
4608
4076
  async sendSignal({ proxyId, signal }) {
4609
4077
  const transport = this._openTransports.get(proxyId);
4610
- invariant14(transport, void 0, {
4611
- F: __dxlog_file16,
4612
- L: 124,
4078
+ invariant12(transport, void 0, {
4079
+ F: __dxlog_file13,
4080
+ L: 123,
4613
4081
  S: this,
4614
4082
  A: [
4615
4083
  "transport",
@@ -4620,9 +4088,9 @@ var RtcTransportService = class {
4620
4088
  }
4621
4089
  async getDetails({ proxyId }) {
4622
4090
  const transport = this._openTransports.get(proxyId);
4623
- invariant14(transport, void 0, {
4624
- F: __dxlog_file16,
4625
- L: 131,
4091
+ invariant12(transport, void 0, {
4092
+ F: __dxlog_file13,
4093
+ L: 130,
4626
4094
  S: this,
4627
4095
  A: [
4628
4096
  "transport",
@@ -4635,9 +4103,9 @@ var RtcTransportService = class {
4635
4103
  }
4636
4104
  async getStats({ proxyId }) {
4637
4105
  const transport = this._openTransports.get(proxyId);
4638
- invariant14(transport, void 0, {
4639
- F: __dxlog_file16,
4640
- L: 138,
4106
+ invariant12(transport, void 0, {
4107
+ F: __dxlog_file13,
4108
+ L: 137,
4641
4109
  S: this,
4642
4110
  A: [
4643
4111
  "transport",
@@ -4650,9 +4118,9 @@ var RtcTransportService = class {
4650
4118
  }
4651
4119
  async sendData({ proxyId, payload }) {
4652
4120
  const transport = this._openTransports.get(proxyId);
4653
- invariant14(transport, void 0, {
4654
- F: __dxlog_file16,
4655
- L: 145,
4121
+ invariant12(transport, void 0, {
4122
+ F: __dxlog_file13,
4123
+ L: 144,
4656
4124
  S: this,
4657
4125
  A: [
4658
4126
  "transport",
@@ -4682,11 +4150,11 @@ var RtcTransportService = class {
4682
4150
  try {
4683
4151
  await transport.transport.close();
4684
4152
  } catch (error) {
4685
- log15.warn("transport close error", {
4153
+ log12.warn("transport close error", {
4686
4154
  message: error?.message
4687
4155
  }, {
4688
- F: __dxlog_file16,
4689
- L: 175,
4156
+ F: __dxlog_file13,
4157
+ L: 174,
4690
4158
  S: this,
4691
4159
  C: (f, a) => f(...a)
4692
4160
  });
@@ -4694,28 +4162,22 @@ var RtcTransportService = class {
4694
4162
  try {
4695
4163
  transport.connectorStream.end();
4696
4164
  } catch (error) {
4697
- log15.warn("connectorStream close error", {
4165
+ log12.warn("connectorStream close error", {
4698
4166
  message: error?.message
4699
4167
  }, {
4700
- F: __dxlog_file16,
4701
- L: 180,
4168
+ F: __dxlog_file13,
4169
+ L: 179,
4702
4170
  S: this,
4703
4171
  C: (f, a) => f(...a)
4704
4172
  });
4705
4173
  }
4706
- log15("closed", void 0, {
4707
- F: __dxlog_file16,
4708
- L: 182,
4174
+ log12("closed", void 0, {
4175
+ F: __dxlog_file13,
4176
+ L: 181,
4709
4177
  S: this,
4710
4178
  C: (f, a) => f(...a)
4711
4179
  });
4712
4180
  }
4713
- constructor(webrtcConfig, iceProvider, _transportFactory = createRtcTransportFactory(webrtcConfig, iceProvider)) {
4714
- _define_property17(this, "_transportFactory", void 0);
4715
- _define_property17(this, "_openTransports", void 0);
4716
- this._transportFactory = _transportFactory;
4717
- this._openTransports = new ComplexMap8(PublicKey11.hash);
4718
- }
4719
4181
  };
4720
4182
  var createStateUpdater = (next) => {
4721
4183
  return (state, err) => {
@@ -4732,10 +4194,10 @@ var createStateUpdater = (next) => {
4732
4194
 
4733
4195
  // src/wire-protocol.ts
4734
4196
  import { Teleport } from "@dxos/teleport";
4735
- var createTeleportProtocolFactory = (onConnection, defaultParams) => {
4197
+ var createTeleportProtocolFactory = (onConnection, defaultProps) => {
4736
4198
  return (params) => {
4737
4199
  const teleport = new Teleport({
4738
- ...defaultParams,
4200
+ ...defaultProps,
4739
4201
  ...params
4740
4202
  });
4741
4203
  return {
@@ -4757,7 +4219,6 @@ var createTeleportProtocolFactory = (onConnection, defaultParams) => {
4757
4219
  export {
4758
4220
  ConnectionState,
4759
4221
  Connection,
4760
- createIceProvider,
4761
4222
  SwarmMessenger,
4762
4223
  Swarm,
4763
4224
  SwarmMapper,
@@ -4767,8 +4228,6 @@ export {
4767
4228
  ConnectionLog,
4768
4229
  SwarmNetworkManager,
4769
4230
  FullyConnectedTopology,
4770
- MMSTTopology,
4771
- StarTopology,
4772
4231
  MemoryTransportFactory,
4773
4232
  MemoryTransport,
4774
4233
  TransportKind,
@@ -4778,4 +4237,4 @@ export {
4778
4237
  RtcTransportService,
4779
4238
  createTeleportProtocolFactory
4780
4239
  };
4781
- //# sourceMappingURL=chunk-NRRQ4H6U.mjs.map
4240
+ //# sourceMappingURL=chunk-Y2FDDNGM.mjs.map