@dxos/network-manager 0.8.4-main.f9ba587 → 0.8.4-main.fcfe5033a5

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 (105) hide show
  1. package/dist/lib/browser/{chunk-HQNQOWFA.mjs → chunk-Y2FDDNGM.mjs} +687 -916
  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 +78 -49
  7. package/dist/lib/browser/testing/index.mjs.map +3 -3
  8. package/dist/lib/browser/transport/tcp/index.mjs +2 -35
  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-JPPMRZTH.mjs → chunk-OU3FYVBA.mjs} +687 -916
  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 +78 -49
  20. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  21. package/dist/lib/node-esm/transport/tcp/index.mjs +2 -156
  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/connection-log.d.ts.map +1 -1
  28. package/dist/types/src/index.d.ts +1 -1
  29. package/dist/types/src/index.d.ts.map +1 -1
  30. package/dist/types/src/network-manager.d.ts +5 -4
  31. package/dist/types/src/network-manager.d.ts.map +1 -1
  32. package/dist/types/src/signal/index.d.ts +2 -2
  33. package/dist/types/src/signal/index.d.ts.map +1 -1
  34. package/dist/types/src/signal/signal-connection.d.ts +3 -2
  35. package/dist/types/src/signal/signal-connection.d.ts.map +1 -1
  36. package/dist/types/src/signal/signal-messenger.d.ts +3 -2
  37. package/dist/types/src/signal/signal-messenger.d.ts.map +1 -1
  38. package/dist/types/src/signal/swarm-messenger.d.ts +8 -8
  39. package/dist/types/src/signal/swarm-messenger.d.ts.map +1 -1
  40. package/dist/types/src/swarm/connection.d.ts +2 -1
  41. package/dist/types/src/swarm/connection.d.ts.map +1 -1
  42. package/dist/types/src/swarm/peer.d.ts +6 -5
  43. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  44. package/dist/types/src/swarm/swarm.d.ts +6 -5
  45. package/dist/types/src/swarm/swarm.d.ts.map +1 -1
  46. package/dist/types/src/testing/test-builder.d.ts +1 -1
  47. package/dist/types/src/testing/test-builder.d.ts.map +1 -1
  48. package/dist/types/src/testing/test-wire-protocol.d.ts +1 -1
  49. package/dist/types/src/testing/test-wire-protocol.d.ts.map +1 -1
  50. package/dist/types/src/tests/basic-test-suite.d.ts.map +1 -1
  51. package/dist/types/src/tests/property-test-suite.d.ts.map +1 -1
  52. package/dist/types/src/topology/index.d.ts +1 -1
  53. package/dist/types/src/topology/index.d.ts.map +1 -1
  54. package/dist/types/src/transport/tcp/index.d.ts +1 -1
  55. package/dist/types/src/transport/tcp/index.d.ts.map +1 -1
  56. package/dist/types/src/transport/webrtc/rtc-peer-connection.d.ts +2 -2
  57. package/dist/types/src/transport/webrtc/rtc-peer-connection.d.ts.map +1 -1
  58. package/dist/types/src/transport/webrtc/rtc-transport-channel.d.ts +1 -1
  59. package/dist/types/src/transport/webrtc/rtc-transport-channel.d.ts.map +1 -1
  60. package/dist/types/src/transport/webrtc/rtc-transport-factory.d.ts.map +1 -1
  61. package/dist/types/src/transport/webrtc/rtc-transport-proxy.d.ts.map +1 -1
  62. package/dist/types/src/transport/webrtc/rtc-transport-service.d.ts +1 -1
  63. package/dist/types/src/transport/webrtc/rtc-transport-service.d.ts.map +1 -1
  64. package/dist/types/src/wire-protocol.d.ts +5 -5
  65. package/dist/types/src/wire-protocol.d.ts.map +1 -1
  66. package/dist/types/tsconfig.tsbuildinfo +1 -1
  67. package/package.json +37 -33
  68. package/src/connection-log.ts +1 -1
  69. package/src/index.ts +1 -1
  70. package/src/network-manager.ts +20 -16
  71. package/src/signal/ice.test.ts +1 -1
  72. package/src/signal/index.ts +2 -2
  73. package/src/signal/integration.node.test.ts +13 -12
  74. package/src/signal/signal-connection.ts +3 -2
  75. package/src/signal/signal-messenger.ts +3 -2
  76. package/src/signal/swarm-messenger.node.test.ts +26 -25
  77. package/src/signal/swarm-messenger.ts +66 -56
  78. package/src/swarm/connection.test.ts +9 -8
  79. package/src/swarm/connection.ts +16 -14
  80. package/src/swarm/peer.ts +9 -8
  81. package/src/swarm/swarm-mapper.ts +1 -1
  82. package/src/swarm/swarm.test.ts +8 -7
  83. package/src/swarm/swarm.ts +13 -13
  84. package/src/testing/test-builder.ts +15 -7
  85. package/src/testing/test-wire-protocol.ts +2 -2
  86. package/src/tests/basic-test-suite.ts +2 -2
  87. package/src/tests/memory-transport.test.ts +2 -2
  88. package/src/tests/property-test-suite.ts +4 -3
  89. package/src/tests/tcp-transport.node.test.ts +2 -2
  90. package/src/tests/webrtc-transport.test.ts +5 -3
  91. package/src/topology/index.ts +1 -1
  92. package/src/transport/tcp/index.ts +1 -1
  93. package/src/transport/tcp/tcp-transport.ts +1 -1
  94. package/src/transport/webrtc/rtc-connection-factory.ts +1 -1
  95. package/src/transport/webrtc/rtc-peer-connection.ts +4 -4
  96. package/src/transport/webrtc/rtc-transport-channel.test.ts +1 -1
  97. package/src/transport/webrtc/rtc-transport-channel.ts +2 -2
  98. package/src/transport/webrtc/rtc-transport-factory.ts +2 -2
  99. package/src/transport/webrtc/rtc-transport-proxy.test.ts +5 -4
  100. package/src/transport/webrtc/rtc-transport-proxy.ts +6 -4
  101. package/src/transport/webrtc/rtc-transport-service.ts +5 -5
  102. package/src/transport/webrtc/rtc-transport.test.ts +5 -4
  103. package/src/wire-protocol.ts +6 -6
  104. package/dist/lib/browser/chunk-HQNQOWFA.mjs.map +0 -7
  105. package/dist/lib/node-esm/chunk-JPPMRZTH.mjs.map +0 -7
@@ -1,13 +1,13 @@
1
1
  import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
2
 
3
3
  // src/swarm/connection.ts
4
- import { DeferredTask, Event, sleep, scheduleTask, scheduleTaskInterval, synchronized, Trigger } from "@dxos/async";
5
- import { Context, cancelWithContext, ContextDisposedError } from "@dxos/context";
4
+ import { DeferredTask, Event, Trigger, scheduleTask, scheduleTaskInterval, sleep, synchronized } from "@dxos/async";
5
+ import { Context, ContextDisposedError, cancelWithContext } from "@dxos/context";
6
6
  import { ErrorStream } from "@dxos/debug";
7
7
  import { invariant } from "@dxos/invariant";
8
8
  import { PublicKey } from "@dxos/keys";
9
9
  import { log, logInfo } from "@dxos/log";
10
- import { CancelledError, ProtocolError, ConnectionResetError, ConnectivityError, TimeoutError, trace } from "@dxos/protocols";
10
+ import { CancelledError, ConnectionResetError, ConnectivityError, ProtocolError, TimeoutError, trace } from "@dxos/protocols";
11
11
  function _ts_decorate(decorators, target, key, desc) {
12
12
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
13
13
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -19,7 +19,7 @@ var STARTING_SIGNALLING_DELAY = 10;
19
19
  var TRANSPORT_CONNECTION_TIMEOUT = 1e4;
20
20
  var TRANSPORT_STATS_INTERVAL = 5e3;
21
21
  var MAX_SIGNALLING_DELAY = 300;
22
- var ConnectionState = /* @__PURE__ */ function(ConnectionState5) {
22
+ var ConnectionState = /* @__PURE__ */ (function(ConnectionState5) {
23
23
  ConnectionState5["CREATED"] = "CREATED";
24
24
  ConnectionState5["INITIAL"] = "INITIAL";
25
25
  ConnectionState5["CONNECTING"] = "CONNECTING";
@@ -29,8 +29,40 @@ var ConnectionState = /* @__PURE__ */ function(ConnectionState5) {
29
29
  ConnectionState5["ABORTING"] = "ABORTING";
30
30
  ConnectionState5["ABORTED"] = "ABORTED";
31
31
  return ConnectionState5;
32
- }({});
32
+ })({});
33
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;
34
66
  constructor(topic, localInfo, remoteInfo, sessionId, initiator, _signalMessaging, _protocol, _transportFactory, _callbacks) {
35
67
  this.topic = topic;
36
68
  this.localInfo = localInfo;
@@ -41,27 +73,6 @@ var Connection = class {
41
73
  this._protocol = _protocol;
42
74
  this._transportFactory = _transportFactory;
43
75
  this._callbacks = _callbacks;
44
- this._ctx = new Context(void 0, {
45
- F: __dxlog_file,
46
- L: 100
47
- });
48
- this.connectedTimeoutContext = new Context(void 0, {
49
- F: __dxlog_file,
50
- L: 101
51
- });
52
- this._protocolClosed = new Trigger();
53
- this._transportClosed = new Trigger();
54
- this._state = "CREATED";
55
- this._incomingSignalBuffer = [];
56
- this._outgoingSignalBuffer = [];
57
- this.stateChanged = new Event();
58
- this.errors = new ErrorStream();
59
- this._instanceId = PublicKey.random().toHex();
60
- this.transportStats = new Event();
61
- this._signalSendTask = new DeferredTask(this._ctx, async () => {
62
- await this._flushSignalBuffer();
63
- });
64
- this._signallingDelay = STARTING_SIGNALLING_DELAY;
65
76
  log.trace("dxos.mesh.connection.construct", {
66
77
  sessionId: this.sessionId,
67
78
  topic: this.topic,
@@ -133,21 +144,25 @@ var Connection = class {
133
144
  });
134
145
  this._protocolClosed.wake();
135
146
  this.close({
136
- error: new ProtocolError("protocol stream closed")
147
+ error: new ProtocolError({
148
+ message: "protocol stream closed"
149
+ })
137
150
  }).catch((err) => this.errors.raise(err));
138
151
  });
139
152
  scheduleTask(this.connectedTimeoutContext, async () => {
140
153
  log.info(`timeout waiting ${TRANSPORT_CONNECTION_TIMEOUT / 1e3}s for transport to connect, aborting`, void 0, {
141
154
  F: __dxlog_file,
142
- L: 194,
155
+ L: 196,
143
156
  S: this,
144
157
  C: (f, a) => f(...a)
145
158
  });
146
- 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));
147
162
  }, TRANSPORT_CONNECTION_TIMEOUT);
148
163
  invariant(!this._transport, void 0, {
149
164
  F: __dxlog_file,
150
- L: 202,
165
+ L: 204,
151
166
  S: this,
152
167
  A: [
153
168
  "!this._transport",
@@ -174,7 +189,7 @@ var Connection = class {
174
189
  this._transportClosed.wake();
175
190
  log("abort triggered by transport close", void 0, {
176
191
  F: __dxlog_file,
177
- L: 224,
192
+ L: 226,
178
193
  S: this,
179
194
  C: (f, a) => f(...a)
180
195
  });
@@ -185,7 +200,7 @@ var Connection = class {
185
200
  err
186
201
  }, {
187
202
  F: __dxlog_file,
188
- L: 229,
203
+ L: 231,
189
204
  S: this,
190
205
  C: (f, a) => f(...a)
191
206
  });
@@ -195,7 +210,7 @@ var Connection = class {
195
210
  if (err instanceof ConnectionResetError) {
196
211
  log.info("aborting due to transport ConnectionResetError", void 0, {
197
212
  F: __dxlog_file,
198
- L: 236,
213
+ L: 238,
199
214
  S: this,
200
215
  C: (f, a) => f(...a)
201
216
  });
@@ -203,7 +218,7 @@ var Connection = class {
203
218
  } else if (err instanceof ConnectivityError) {
204
219
  log.info("aborting due to transport ConnectivityError", void 0, {
205
220
  F: __dxlog_file,
206
- L: 239,
221
+ L: 241,
207
222
  S: this,
208
223
  C: (f, a) => f(...a)
209
224
  });
@@ -223,7 +238,7 @@ var Connection = class {
223
238
  id: this._instanceId
224
239
  }), {
225
240
  F: __dxlog_file,
226
- L: 258,
241
+ L: 260,
227
242
  S: this,
228
243
  C: (f, a) => f(...a)
229
244
  });
@@ -233,14 +248,14 @@ var Connection = class {
233
248
  err
234
249
  }, {
235
250
  F: __dxlog_file,
236
- L: 265,
251
+ L: 267,
237
252
  S: this,
238
253
  C: (f, a) => f(...a)
239
254
  });
240
255
  if (this._state === "CLOSED" || this._state === "ABORTED") {
241
256
  log(`abort ignored: already ${this._state}`, this.closeReason, {
242
257
  F: __dxlog_file,
243
- L: 267,
258
+ L: 269,
244
259
  S: this,
245
260
  C: (f, a) => f(...a)
246
261
  });
@@ -257,7 +272,7 @@ var Connection = class {
257
272
  err
258
273
  }, {
259
274
  F: __dxlog_file,
260
- L: 279,
275
+ L: 281,
261
276
  S: this,
262
277
  C: (f, a) => f(...a)
263
278
  });
@@ -268,7 +283,7 @@ var Connection = class {
268
283
  } catch (err2) {
269
284
  log.catch(err2, void 0, {
270
285
  F: __dxlog_file,
271
- L: 285,
286
+ L: 287,
272
287
  S: this,
273
288
  C: (f, a) => f(...a)
274
289
  });
@@ -278,7 +293,7 @@ var Connection = class {
278
293
  } catch (err2) {
279
294
  log.catch(err2, void 0, {
280
295
  F: __dxlog_file,
281
- L: 292,
296
+ L: 294,
282
297
  S: this,
283
298
  C: (f, a) => f(...a)
284
299
  });
@@ -288,7 +303,7 @@ var Connection = class {
288
303
  } catch (err2) {
289
304
  log.catch(err2, void 0, {
290
305
  F: __dxlog_file,
291
- L: 298,
306
+ L: 300,
292
307
  S: this,
293
308
  C: (f, a) => f(...a)
294
309
  });
@@ -300,7 +315,7 @@ var Connection = class {
300
315
  error
301
316
  }, {
302
317
  F: __dxlog_file,
303
- L: 305,
318
+ L: 307,
304
319
  S: this,
305
320
  C: (f, a) => f(...a)
306
321
  });
@@ -315,7 +330,7 @@ var Connection = class {
315
330
  error
316
331
  }, {
317
332
  F: __dxlog_file,
318
- L: 316,
333
+ L: 318,
319
334
  S: this,
320
335
  C: (f, a) => f(...a)
321
336
  });
@@ -329,7 +344,7 @@ var Connection = class {
329
344
  if (lastState !== "CONNECTED" || error != null) {
330
345
  log(`graceful close requested when we were in ${lastState} state? aborting`, void 0, {
331
346
  F: __dxlog_file,
332
- L: 327,
347
+ L: 329,
333
348
  S: this,
334
349
  C: (f, a) => f(...a)
335
350
  });
@@ -341,7 +356,7 @@ var Connection = class {
341
356
  error
342
357
  }, {
343
358
  F: __dxlog_file,
344
- L: 331,
359
+ L: 333,
345
360
  S: this,
346
361
  C: (f, a) => f(...a)
347
362
  });
@@ -352,7 +367,7 @@ var Connection = class {
352
367
  } catch (err) {
353
368
  log.catch(err, void 0, {
354
369
  F: __dxlog_file,
355
- L: 336,
370
+ L: 338,
356
371
  S: this,
357
372
  C: (f, a) => f(...a)
358
373
  });
@@ -362,7 +377,7 @@ var Connection = class {
362
377
  } catch (err) {
363
378
  log.catch(err, void 0, {
364
379
  F: __dxlog_file,
365
- L: 342,
380
+ L: 344,
366
381
  S: this,
367
382
  C: (f, a) => f(...a)
368
383
  });
@@ -371,7 +386,7 @@ var Connection = class {
371
386
  peerId: this.localInfo
372
387
  }, {
373
388
  F: __dxlog_file,
374
- L: 345,
389
+ L: 347,
375
390
  S: this,
376
391
  C: (f, a) => f(...a)
377
392
  });
@@ -381,7 +396,7 @@ var Connection = class {
381
396
  async _closeProtocol(options) {
382
397
  log("closing protocol", options, {
383
398
  F: __dxlog_file,
384
- L: 351,
399
+ L: 353,
385
400
  S: this,
386
401
  C: (f, a) => f(...a)
387
402
  });
@@ -391,7 +406,7 @@ var Connection = class {
391
406
  ]);
392
407
  log("protocol closed", options, {
393
408
  F: __dxlog_file,
394
- L: 353,
409
+ L: 355,
395
410
  S: this,
396
411
  C: (f, a) => f(...a)
397
412
  });
@@ -399,7 +414,7 @@ var Connection = class {
399
414
  async _closeTransport() {
400
415
  log("closing transport", void 0, {
401
416
  F: __dxlog_file,
402
- L: 357,
417
+ L: 359,
403
418
  S: this,
404
419
  C: (f, a) => f(...a)
405
420
  });
@@ -409,7 +424,7 @@ var Connection = class {
409
424
  ]);
410
425
  log("transport closed", void 0, {
411
426
  F: __dxlog_file,
412
- L: 359,
427
+ L: 361,
413
428
  S: this,
414
429
  C: (f, a) => f(...a)
415
430
  });
@@ -418,20 +433,20 @@ var Connection = class {
418
433
  this._outgoingSignalBuffer.push(signal);
419
434
  this._signalSendTask.schedule();
420
435
  }
421
- async _flushSignalBuffer() {
436
+ async _flushSignalBuffer(ctx) {
422
437
  if (this._outgoingSignalBuffer.length === 0) {
423
438
  return;
424
439
  }
425
440
  try {
426
441
  if (process.env.NODE_ENV !== "test") {
427
- await cancelWithContext(this._ctx, sleep(this._signallingDelay));
442
+ await cancelWithContext(ctx, sleep(this._signallingDelay));
428
443
  this._signallingDelay = Math.min(this._signallingDelay * 2, MAX_SIGNALLING_DELAY);
429
444
  }
430
445
  const signals = [
431
446
  ...this._outgoingSignalBuffer
432
447
  ];
433
448
  this._outgoingSignalBuffer.length = 0;
434
- await this._signalMessaging.signal({
449
+ await this._signalMessaging.signal(ctx, {
435
450
  author: this.localInfo,
436
451
  recipient: this.remoteInfo,
437
452
  sessionId: this.sessionId,
@@ -450,22 +465,25 @@ var Connection = class {
450
465
  err
451
466
  }, {
452
467
  F: __dxlog_file,
453
- L: 399,
468
+ L: 401,
454
469
  S: this,
455
470
  C: (f, a) => f(...a)
456
471
  });
457
472
  await this.close({
458
- error: new ConnectivityError("signal message failed to deliver", err)
473
+ error: new ConnectivityError({
474
+ message: "signal message failed to deliver",
475
+ cause: err
476
+ })
459
477
  });
460
478
  }
461
479
  }
462
480
  /**
463
481
  * Receive a signal from the remote peer.
464
482
  */
465
- async signal(msg) {
483
+ async signal(_ctx, msg) {
466
484
  invariant(msg.sessionId, void 0, {
467
485
  F: __dxlog_file,
468
- L: 408,
486
+ L: 410,
469
487
  S: this,
470
488
  A: [
471
489
  "msg.sessionId",
@@ -475,7 +493,7 @@ var Connection = class {
475
493
  if (!msg.sessionId.equals(this.sessionId)) {
476
494
  log("dropping signal for incorrect session id", void 0, {
477
495
  F: __dxlog_file,
478
- L: 410,
496
+ L: 412,
479
497
  S: this,
480
498
  C: (f, a) => f(...a)
481
499
  });
@@ -483,7 +501,7 @@ var Connection = class {
483
501
  }
484
502
  invariant(msg.data.signal || msg.data.signalBatch, void 0, {
485
503
  F: __dxlog_file,
486
- L: 413,
504
+ L: 415,
487
505
  S: this,
488
506
  A: [
489
507
  "msg.data.signal || msg.data.signalBatch",
@@ -492,7 +510,7 @@ var Connection = class {
492
510
  });
493
511
  invariant(msg.author.peerKey === this.remoteInfo.peerKey, void 0, {
494
512
  F: __dxlog_file,
495
- L: 414,
513
+ L: 416,
496
514
  S: this,
497
515
  A: [
498
516
  "msg.author.peerKey === this.remoteInfo.peerKey",
@@ -501,7 +519,7 @@ var Connection = class {
501
519
  });
502
520
  invariant(msg.recipient.peerKey === this.localInfo.peerKey, void 0, {
503
521
  F: __dxlog_file,
504
- L: 415,
522
+ L: 417,
505
523
  S: this,
506
524
  A: [
507
525
  "msg.recipient.peerKey === this.localInfo.peerKey",
@@ -525,7 +543,7 @@ var Connection = class {
525
543
  msg: msg.data
526
544
  }, {
527
545
  F: __dxlog_file,
528
- L: 424,
546
+ L: 426,
529
547
  S: this,
530
548
  C: (f, a) => f(...a)
531
549
  });
@@ -533,7 +551,7 @@ var Connection = class {
533
551
  } else {
534
552
  invariant(this._transport, "Connection not ready to accept signals.", {
535
553
  F: __dxlog_file,
536
- L: 427,
554
+ L: 429,
537
555
  S: this,
538
556
  A: [
539
557
  "this._transport",
@@ -546,7 +564,7 @@ var Connection = class {
546
564
  msg: msg.data
547
565
  }, {
548
566
  F: __dxlog_file,
549
- L: 428,
567
+ L: 430,
550
568
  S: this,
551
569
  C: (f, a) => f(...a)
552
570
  });
@@ -564,13 +582,13 @@ var Connection = class {
564
582
  peerId: this.localInfo
565
583
  }, {
566
584
  F: __dxlog_file,
567
- L: 439,
585
+ L: 441,
568
586
  S: this,
569
587
  C: (f, a) => f(...a)
570
588
  });
571
589
  invariant(state !== this._state, "Already in this state.", {
572
590
  F: __dxlog_file,
573
- L: 440,
591
+ L: 442,
574
592
  S: this,
575
593
  A: [
576
594
  "state !== this._state",
@@ -597,62 +615,28 @@ _ts_decorate([
597
615
  synchronized
598
616
  ], Connection.prototype, "close", null);
599
617
 
600
- // src/signal/ice.ts
601
- import { asyncTimeout } from "@dxos/async";
602
- import { log as log2 } from "@dxos/log";
603
- import { isNonNullable } from "@dxos/util";
604
- var __dxlog_file2 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/signal/ice.ts";
605
- var createIceProvider = (iceProviders) => {
606
- let cachedIceServers;
607
- return {
608
- getIceServers: async () => {
609
- if (cachedIceServers) {
610
- return cachedIceServers;
611
- }
612
- cachedIceServers = (await Promise.all(iceProviders.map(({ urls }) => asyncTimeout(fetch(urls, {
613
- method: "GET"
614
- }), 1e4).then((response) => response.json()).catch((err) => {
615
- const isDev = typeof window !== "undefined" && window.location.href.includes("localhost");
616
- if (!isDev) {
617
- log2.error("Failed to fetch ICE servers from provider", {
618
- urls,
619
- err
620
- }, {
621
- F: __dxlog_file2,
622
- L: 30,
623
- S: void 0,
624
- C: (f, a) => f(...a)
625
- });
626
- }
627
- })))).filter(isNonNullable).map(({ iceServers }) => iceServers).flat();
628
- return cachedIceServers;
629
- }
630
- };
631
- };
632
-
633
618
  // src/signal/swarm-messenger.ts
634
- import { Context as Context2 } from "@dxos/context";
635
619
  import { invariant as invariant2 } from "@dxos/invariant";
636
620
  import { PublicKey as PublicKey2 } from "@dxos/keys";
637
- import { log as log3 } from "@dxos/log";
621
+ import { log as log2 } from "@dxos/log";
638
622
  import { TimeoutError as TimeoutError2 } from "@dxos/protocols";
639
623
  import { schema } from "@dxos/protocols/proto";
640
624
  import { ComplexMap } from "@dxos/util";
641
- 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";
642
626
  var SwarmMessage = schema.getCodecForType("dxos.mesh.swarm.SwarmMessage");
643
627
  var SwarmMessenger = class {
628
+ _sendMessage;
629
+ _onSignal;
630
+ _onOffer;
631
+ _topic;
632
+ _offerRecords = new ComplexMap((key) => key.toHex());
644
633
  constructor({ sendMessage, onSignal, onOffer, topic }) {
645
- this._ctx = new Context2(void 0, {
646
- F: __dxlog_file3,
647
- L: 35
648
- });
649
- this._offerRecords = new ComplexMap((key) => key.toHex());
650
634
  this._sendMessage = sendMessage;
651
635
  this._onSignal = onSignal;
652
636
  this._onOffer = onOffer;
653
637
  this._topic = topic;
654
638
  }
655
- async receiveMessage({ author, recipient, payload }) {
639
+ async receiveMessage(ctx, { author, recipient, payload }) {
656
640
  if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
657
641
  return;
658
642
  }
@@ -660,18 +644,18 @@ var SwarmMessenger = class {
660
644
  if (!this._topic.equals(message.topic)) {
661
645
  return;
662
646
  }
663
- log3("received", {
647
+ log2("received", {
664
648
  from: author,
665
649
  to: recipient,
666
650
  msg: message
667
651
  }, {
668
- F: __dxlog_file3,
669
- L: 71,
652
+ F: __dxlog_file2,
653
+ L: 72,
670
654
  S: this,
671
655
  C: (f, a) => f(...a)
672
656
  });
673
657
  if (message.data?.offer) {
674
- await this._handleOffer({
658
+ await this._handleOffer(ctx, {
675
659
  author,
676
660
  recipient,
677
661
  message
@@ -679,45 +663,45 @@ var SwarmMessenger = class {
679
663
  } else if (message.data?.answer) {
680
664
  await this._resolveAnswers(message);
681
665
  } else if (message.data?.signal) {
682
- await this._handleSignal({
666
+ await this._handleSignal(ctx, {
683
667
  author,
684
668
  recipient,
685
669
  message
686
670
  });
687
671
  } else if (message.data?.signalBatch) {
688
- await this._handleSignal({
672
+ await this._handleSignal(ctx, {
689
673
  author,
690
674
  recipient,
691
675
  message
692
676
  });
693
677
  } else {
694
- log3.warn("unknown message", {
678
+ log2.warn("unknown message", {
695
679
  message
696
680
  }, {
697
- F: __dxlog_file3,
698
- L: 82,
681
+ F: __dxlog_file2,
682
+ L: 83,
699
683
  S: this,
700
684
  C: (f, a) => f(...a)
701
685
  });
702
686
  }
703
687
  }
704
- async signal(message) {
688
+ async signal(ctx, message) {
705
689
  invariant2(message.data?.signal || message.data?.signalBatch, "Invalid message", {
706
- F: __dxlog_file3,
707
- L: 87,
690
+ F: __dxlog_file2,
691
+ L: 88,
708
692
  S: this,
709
693
  A: [
710
694
  "message.data?.signal || message.data?.signalBatch",
711
695
  "'Invalid message'"
712
696
  ]
713
697
  });
714
- await this._sendReliableMessage({
698
+ await this._sendReliableMessage(ctx, {
715
699
  author: message.author,
716
700
  recipient: message.recipient,
717
701
  message
718
702
  });
719
703
  }
720
- async offer(message) {
704
+ async offer(ctx, message) {
721
705
  const networkMessage = {
722
706
  ...message,
723
707
  messageId: PublicKey2.random()
@@ -726,30 +710,30 @@ var SwarmMessenger = class {
726
710
  this._offerRecords.set(networkMessage.messageId, {
727
711
  resolve
728
712
  });
729
- this._sendReliableMessage({
713
+ this._sendReliableMessage(ctx, {
730
714
  author: message.author,
731
715
  recipient: message.recipient,
732
716
  message: networkMessage
733
717
  }).catch((err) => reject(err));
734
718
  });
735
719
  }
736
- async _sendReliableMessage({ author, recipient, message }) {
720
+ async _sendReliableMessage(ctx, { author, recipient, message }) {
737
721
  const networkMessage = {
738
722
  ...message,
739
723
  // Setting unique message_id if it not specified yet.
740
724
  messageId: message.messageId ?? PublicKey2.random()
741
725
  };
742
- log3("sending", {
726
+ log2("sending", {
743
727
  from: author,
744
728
  to: recipient,
745
729
  msg: networkMessage
746
730
  }, {
747
- F: __dxlog_file3,
748
- L: 125,
731
+ F: __dxlog_file2,
732
+ L: 129,
749
733
  S: this,
750
734
  C: (f, a) => f(...a)
751
735
  });
752
- await this._sendMessage({
736
+ await this._sendMessage(ctx, {
753
737
  author,
754
738
  recipient,
755
739
  payload: {
@@ -760,8 +744,8 @@ var SwarmMessenger = class {
760
744
  }
761
745
  async _resolveAnswers(message) {
762
746
  invariant2(message.data?.answer?.offerMessageId, "No offerMessageId", {
763
- F: __dxlog_file3,
764
- L: 137,
747
+ F: __dxlog_file2,
748
+ L: 141,
765
749
  S: this,
766
750
  A: [
767
751
  "message.data?.answer?.offerMessageId",
@@ -772,29 +756,29 @@ var SwarmMessenger = class {
772
756
  if (offerRecord) {
773
757
  this._offerRecords.delete(message.data.answer.offerMessageId);
774
758
  invariant2(message.data?.answer, "No answer", {
775
- F: __dxlog_file3,
776
- L: 141,
759
+ F: __dxlog_file2,
760
+ L: 145,
777
761
  S: this,
778
762
  A: [
779
763
  "message.data?.answer",
780
764
  "'No answer'"
781
765
  ]
782
766
  });
783
- log3("resolving", {
767
+ log2("resolving", {
784
768
  answer: message.data.answer
785
769
  }, {
786
- F: __dxlog_file3,
787
- L: 142,
770
+ F: __dxlog_file2,
771
+ L: 146,
788
772
  S: this,
789
773
  C: (f, a) => f(...a)
790
774
  });
791
775
  offerRecord.resolve(message.data.answer);
792
776
  }
793
777
  }
794
- async _handleOffer({ author, recipient, message }) {
778
+ async _handleOffer(ctx, { author, recipient, message }) {
795
779
  invariant2(message.data.offer, "No offer", {
796
- F: __dxlog_file3,
797
- L: 156,
780
+ F: __dxlog_file2,
781
+ L: 163,
798
782
  S: this,
799
783
  A: [
800
784
  "message.data.offer",
@@ -809,10 +793,10 @@ var SwarmMessenger = class {
809
793
  offer: message.data.offer
810
794
  }
811
795
  };
812
- const answer = await this._onOffer(offerMessage);
796
+ const answer = await this._onOffer(ctx, offerMessage);
813
797
  answer.offerMessageId = message.messageId;
814
798
  try {
815
- await this._sendReliableMessage({
799
+ await this._sendReliableMessage(ctx, {
816
800
  author: recipient,
817
801
  recipient: author,
818
802
  message: {
@@ -825,30 +809,30 @@ var SwarmMessenger = class {
825
809
  });
826
810
  } catch (err) {
827
811
  if (err instanceof TimeoutError2) {
828
- log3.info("timeout sending answer to offer", {
812
+ log2.info("timeout sending answer to offer", {
829
813
  err
830
814
  }, {
831
- F: __dxlog_file3,
832
- L: 177,
815
+ F: __dxlog_file2,
816
+ L: 184,
833
817
  S: this,
834
818
  C: (f, a) => f(...a)
835
819
  });
836
820
  } else {
837
- log3.info("error sending answer to offer", {
821
+ log2.info("error sending answer to offer", {
838
822
  err
839
823
  }, {
840
- F: __dxlog_file3,
841
- L: 179,
824
+ F: __dxlog_file2,
825
+ L: 186,
842
826
  S: this,
843
827
  C: (f, a) => f(...a)
844
828
  });
845
829
  }
846
830
  }
847
831
  }
848
- async _handleSignal({ author, recipient, message }) {
832
+ async _handleSignal(ctx, { author, recipient, message }) {
849
833
  invariant2(message.messageId, void 0, {
850
- F: __dxlog_file3,
851
- L: 193,
834
+ F: __dxlog_file2,
835
+ L: 203,
852
836
  S: this,
853
837
  A: [
854
838
  "message.messageId",
@@ -856,8 +840,8 @@ var SwarmMessenger = class {
856
840
  ]
857
841
  });
858
842
  invariant2(message.data.signal || message.data.signalBatch, "Invalid message", {
859
- F: __dxlog_file3,
860
- L: 194,
843
+ F: __dxlog_file2,
844
+ L: 204,
861
845
  S: this,
862
846
  A: [
863
847
  "message.data.signal || message.data.signalBatch",
@@ -873,27 +857,27 @@ var SwarmMessenger = class {
873
857
  signalBatch: message.data.signalBatch
874
858
  }
875
859
  };
876
- await this._onSignal(signalMessage);
860
+ await this._onSignal(ctx, signalMessage);
877
861
  }
878
862
  };
879
863
 
880
864
  // src/swarm/swarm.ts
881
865
  import { Event as Event3, scheduleTask as scheduleTask3, sleep as sleep2, synchronized as synchronized3 } from "@dxos/async";
882
- import { Context as Context4 } from "@dxos/context";
866
+ import { Context as Context3 } from "@dxos/context";
883
867
  import { ErrorStream as ErrorStream2 } from "@dxos/debug";
884
868
  import { invariant as invariant4 } from "@dxos/invariant";
885
869
  import { PublicKey as PublicKey4 } from "@dxos/keys";
886
- import { log as log5, logInfo as logInfo2 } from "@dxos/log";
870
+ import { log as log4, logInfo as logInfo2 } from "@dxos/log";
887
871
  import { PeerInfoHash } from "@dxos/messaging";
888
872
  import { trace as trace2 } from "@dxos/protocols";
889
- import { ComplexMap as ComplexMap2, isNonNullable as isNonNullable2 } from "@dxos/util";
873
+ import { ComplexMap as ComplexMap2, isNonNullable } from "@dxos/util";
890
874
 
891
875
  // src/swarm/peer.ts
892
876
  import { Event as Event2, scheduleTask as scheduleTask2, synchronized as synchronized2 } from "@dxos/async";
893
- import { Context as Context3 } from "@dxos/context";
877
+ import { Context as Context2 } from "@dxos/context";
894
878
  import { invariant as invariant3 } from "@dxos/invariant";
895
879
  import { PublicKey as PublicKey3 } from "@dxos/keys";
896
- import { log as log4 } from "@dxos/log";
880
+ import { log as log3 } from "@dxos/log";
897
881
  import { CancelledError as CancelledError2, SystemError } from "@dxos/protocols";
898
882
  function _ts_decorate2(decorators, target, key, desc) {
899
883
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -901,14 +885,42 @@ function _ts_decorate2(decorators, target, key, desc) {
901
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;
902
886
  return c > 3 && r && Object.defineProperty(target, key, r), r;
903
887
  }
904
- 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";
905
889
  var ConnectionDisplacedError = class extends SystemError {
906
890
  constructor() {
907
- super("Connection displaced by remote initiator.");
891
+ super({
892
+ message: "Connection displaced by remote initiator."
893
+ });
908
894
  }
909
895
  };
910
896
  var CONNECTION_COUNTS_STABLE_AFTER = 5e3;
911
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();
912
924
  constructor(remoteInfo, topic, localInfo, _signalMessaging, _protocolProvider, _transportFactory, _connectionLimiter, _callbacks) {
913
925
  this.remoteInfo = remoteInfo;
914
926
  this.topic = topic;
@@ -918,28 +930,19 @@ var Peer = class {
918
930
  this._transportFactory = _transportFactory;
919
931
  this._connectionLimiter = _connectionLimiter;
920
932
  this._callbacks = _callbacks;
921
- this._availableAfter = 0;
922
- this.availableToConnect = true;
923
- this._ctx = new Context3(void 0, {
924
- F: __dxlog_file4,
925
- L: 80
926
- });
927
- this.advertizing = false;
928
- this.initiating = false;
929
- this.connectionDisplaced = new Event2();
930
933
  }
931
934
  /**
932
935
  * Respond to remote offer.
933
936
  */
934
- async onOffer(message) {
937
+ async onOffer(_ctx, message) {
935
938
  const remote = message.author;
936
939
  if (this.connection && ![
937
940
  ConnectionState.CREATED,
938
941
  ConnectionState.INITIAL,
939
942
  ConnectionState.CONNECTING
940
943
  ].includes(this.connection.state)) {
941
- log4.info(`received offer when connection already in ${this.connection.state} state`, void 0, {
942
- F: __dxlog_file4,
944
+ log3.info(`received offer when connection already in ${this.connection.state} state`, void 0, {
945
+ F: __dxlog_file3,
943
946
  L: 115,
944
947
  S: this,
945
948
  C: (f, a) => f(...a)
@@ -950,13 +953,13 @@ var Peer = class {
950
953
  }
951
954
  if (this.connection || this.initiating) {
952
955
  if (remote.peerKey < this.localInfo.peerKey) {
953
- log4("close local connection", {
956
+ log3("close local connection", {
954
957
  localPeer: this.localInfo,
955
958
  topic: this.topic,
956
959
  remotePeer: this.remoteInfo,
957
960
  sessionId: this.connection?.sessionId
958
961
  }, {
959
- F: __dxlog_file4,
962
+ F: __dxlog_file3,
960
963
  L: 124,
961
964
  S: this,
962
965
  C: (f, a) => f(...a)
@@ -973,7 +976,7 @@ var Peer = class {
973
976
  if (await this._callbacks.onOffer(remote)) {
974
977
  if (!this.connection) {
975
978
  invariant3(message.sessionId, void 0, {
976
- F: __dxlog_file4,
979
+ F: __dxlog_file3,
977
980
  L: 144,
978
981
  S: this,
979
982
  A: [
@@ -988,13 +991,13 @@ var Peer = class {
988
991
  await connection.openConnection();
989
992
  } catch (err) {
990
993
  if (!(err instanceof CancelledError2)) {
991
- log4.info("connection error", {
994
+ log3.info("connection error", {
992
995
  topic: this.topic,
993
996
  peerId: this.localInfo,
994
997
  remoteId: this.remoteInfo,
995
998
  err
996
999
  }, {
997
- F: __dxlog_file4,
1000
+ F: __dxlog_file3,
998
1001
  L: 154,
999
1002
  S: this,
1000
1003
  C: (f, a) => f(...a)
@@ -1014,10 +1017,10 @@ var Peer = class {
1014
1017
  /**
1015
1018
  * Initiate a connection to the remote peer.
1016
1019
  */
1017
- async initiateConnection() {
1020
+ async initiateConnection(ctx) {
1018
1021
  invariant3(!this.initiating, "Initiation in progress.", {
1019
- F: __dxlog_file4,
1020
- L: 171,
1022
+ F: __dxlog_file3,
1023
+ L: 172,
1021
1024
  S: this,
1022
1025
  A: [
1023
1026
  "!this.initiating",
@@ -1025,8 +1028,8 @@ var Peer = class {
1025
1028
  ]
1026
1029
  });
1027
1030
  invariant3(!this.connection, "Already connected.", {
1028
- F: __dxlog_file4,
1029
- L: 172,
1031
+ F: __dxlog_file3,
1032
+ L: 173,
1030
1033
  S: this,
1031
1034
  A: [
1032
1035
  "!this.connection",
@@ -1034,14 +1037,14 @@ var Peer = class {
1034
1037
  ]
1035
1038
  });
1036
1039
  const sessionId = PublicKey3.random();
1037
- log4("initiating...", {
1040
+ log3("initiating...", {
1038
1041
  local: this.localInfo,
1039
1042
  topic: this.topic,
1040
1043
  remote: this.remoteInfo,
1041
1044
  sessionId
1042
1045
  }, {
1043
- F: __dxlog_file4,
1044
- L: 174,
1046
+ F: __dxlog_file3,
1047
+ L: 175,
1045
1048
  S: this,
1046
1049
  C: (f, a) => f(...a)
1047
1050
  });
@@ -1051,7 +1054,7 @@ var Peer = class {
1051
1054
  try {
1052
1055
  await this._connectionLimiter.connecting(sessionId);
1053
1056
  connection.initiate();
1054
- answer = await this._signalMessaging.offer({
1057
+ answer = await this._signalMessaging.offer(ctx, {
1055
1058
  author: this.localInfo,
1056
1059
  recipient: this.remoteInfo,
1057
1060
  sessionId,
@@ -1060,35 +1063,35 @@ var Peer = class {
1060
1063
  offer: {}
1061
1064
  }
1062
1065
  });
1063
- log4("received", {
1066
+ log3("received", {
1064
1067
  answer,
1065
1068
  topic: this.topic,
1066
1069
  local: this.localInfo,
1067
1070
  remote: this.remoteInfo
1068
1071
  }, {
1069
- F: __dxlog_file4,
1070
- L: 191,
1072
+ F: __dxlog_file3,
1073
+ L: 192,
1071
1074
  S: this,
1072
1075
  C: (f, a) => f(...a)
1073
1076
  });
1074
1077
  if (connection.state !== ConnectionState.INITIAL) {
1075
- log4("ignoring response", void 0, {
1076
- F: __dxlog_file4,
1077
- L: 193,
1078
+ log3("ignoring response", void 0, {
1079
+ F: __dxlog_file3,
1080
+ L: 194,
1078
1081
  S: this,
1079
1082
  C: (f, a) => f(...a)
1080
1083
  });
1081
1084
  return;
1082
1085
  }
1083
1086
  } catch (err) {
1084
- log4("initiation error: send offer", {
1087
+ log3("initiation error: send offer", {
1085
1088
  err,
1086
1089
  topic: this.topic,
1087
1090
  local: this.localInfo,
1088
1091
  remote: this.remoteInfo
1089
1092
  }, {
1090
- F: __dxlog_file4,
1091
- L: 197,
1093
+ F: __dxlog_file3,
1094
+ L: 198,
1092
1095
  S: this,
1093
1096
  C: (f, a) => f(...a)
1094
1097
  });
@@ -1103,14 +1106,14 @@ var Peer = class {
1103
1106
  return;
1104
1107
  }
1105
1108
  } catch (err) {
1106
- log4("initiation error: accept answer", {
1109
+ log3("initiation error: accept answer", {
1107
1110
  err,
1108
1111
  topic: this.topic,
1109
1112
  local: this.localInfo,
1110
1113
  remote: this.remoteInfo
1111
1114
  }, {
1112
- F: __dxlog_file4,
1113
- L: 210,
1115
+ F: __dxlog_file3,
1116
+ L: 211,
1114
1117
  S: this,
1115
1118
  C: (f, a) => f(...a)
1116
1119
  });
@@ -1120,31 +1123,31 @@ var Peer = class {
1120
1123
  this.initiating = false;
1121
1124
  }
1122
1125
  try {
1123
- log4("opening connection as initiator", void 0, {
1124
- F: __dxlog_file4,
1125
- L: 223,
1126
+ log3("opening connection as initiator", void 0, {
1127
+ F: __dxlog_file3,
1128
+ L: 224,
1126
1129
  S: this,
1127
1130
  C: (f, a) => f(...a)
1128
1131
  });
1129
1132
  await connection.openConnection();
1130
1133
  this._callbacks.onAccepted();
1131
1134
  } catch (err) {
1132
- log4("initiation error: open connection", {
1135
+ log3("initiation error: open connection", {
1133
1136
  err,
1134
1137
  topic: this.topic,
1135
1138
  local: this.localInfo,
1136
1139
  remote: this.remoteInfo
1137
1140
  }, {
1138
- F: __dxlog_file4,
1139
- L: 227,
1141
+ F: __dxlog_file3,
1142
+ L: 228,
1140
1143
  S: this,
1141
1144
  C: (f, a) => f(...a)
1142
1145
  });
1143
- log4.warn("closing connection due to unhandled error on openConnection", {
1146
+ log3.warn("closing connection due to unhandled error on openConnection", {
1144
1147
  err
1145
1148
  }, {
1146
- F: __dxlog_file4,
1147
- L: 234,
1149
+ F: __dxlog_file3,
1150
+ L: 235,
1148
1151
  S: this,
1149
1152
  C: (f, a) => f(...a)
1150
1153
  });
@@ -1159,21 +1162,21 @@ var Peer = class {
1159
1162
  * Either we're initiating a connection or creating one in response to an offer from the other peer.
1160
1163
  */
1161
1164
  _createConnection(initiator, sessionId) {
1162
- log4("creating connection", {
1165
+ log3("creating connection", {
1163
1166
  topic: this.topic,
1164
1167
  peerId: this.localInfo,
1165
1168
  remoteId: this.remoteInfo,
1166
1169
  initiator,
1167
1170
  sessionId
1168
1171
  }, {
1169
- F: __dxlog_file4,
1170
- L: 248,
1172
+ F: __dxlog_file3,
1173
+ L: 249,
1171
1174
  S: this,
1172
1175
  C: (f, a) => f(...a)
1173
1176
  });
1174
1177
  invariant3(!this.connection, "Already connected.", {
1175
- F: __dxlog_file4,
1176
- L: 255,
1178
+ F: __dxlog_file3,
1179
+ L: 256,
1177
1180
  S: this,
1178
1181
  A: [
1179
1182
  "!this.connection",
@@ -1201,15 +1204,15 @@ var Peer = class {
1201
1204
  this._lastConnectionTime = Date.now();
1202
1205
  this._callbacks.onConnected();
1203
1206
  this._connectionLimiter.doneConnecting(sessionId);
1204
- log4.trace("dxos.mesh.connection.connected", {
1207
+ log3.trace("dxos.mesh.connection.connected", {
1205
1208
  topic: this.topic,
1206
1209
  localPeerId: this.localInfo,
1207
1210
  remotePeerId: this.remoteInfo,
1208
1211
  sessionId,
1209
1212
  initiator
1210
1213
  }, {
1211
- F: __dxlog_file4,
1212
- L: 279,
1214
+ F: __dxlog_file3,
1215
+ L: 280,
1213
1216
  S: this,
1214
1217
  C: (f, a) => f(...a)
1215
1218
  });
@@ -1221,31 +1224,31 @@ var Peer = class {
1221
1224
  remoteId: this.remoteInfo,
1222
1225
  initiator
1223
1226
  };
1224
- log4("connection closed", logMeta, {
1225
- F: __dxlog_file4,
1226
- L: 289,
1227
+ log3("connection closed", logMeta, {
1228
+ F: __dxlog_file3,
1229
+ L: 290,
1227
1230
  S: this,
1228
1231
  C: (f, a) => f(...a)
1229
1232
  });
1230
1233
  this._connectionLimiter.doneConnecting(sessionId);
1231
1234
  invariant3(this.connection === connection, "Connection mismatch (race condition).", {
1232
- F: __dxlog_file4,
1233
- L: 294,
1235
+ F: __dxlog_file3,
1236
+ L: 295,
1234
1237
  S: this,
1235
1238
  A: [
1236
1239
  "this.connection === connection",
1237
1240
  "'Connection mismatch (race condition).'"
1238
1241
  ]
1239
1242
  });
1240
- log4.trace("dxos.mesh.connection.closed", {
1243
+ log3.trace("dxos.mesh.connection.closed", {
1241
1244
  topic: this.topic,
1242
1245
  localPeerId: this.localInfo,
1243
1246
  remotePeerId: this.remoteInfo,
1244
1247
  sessionId,
1245
1248
  initiator
1246
1249
  }, {
1247
- F: __dxlog_file4,
1248
- L: 296,
1250
+ F: __dxlog_file3,
1251
+ L: 297,
1249
1252
  S: this,
1250
1253
  C: (f, a) => f(...a)
1251
1254
  });
@@ -1260,9 +1263,9 @@ var Peer = class {
1260
1263
  }
1261
1264
  this._callbacks.onDisconnected();
1262
1265
  scheduleTask2(this._connectionCtx, () => {
1263
- log4("peer became available", logMeta, {
1264
- F: __dxlog_file4,
1265
- L: 320,
1266
+ log3("peer became available", logMeta, {
1267
+ F: __dxlog_file3,
1268
+ L: 321,
1266
1269
  S: this,
1267
1270
  C: (f, a) => f(...a)
1268
1271
  });
@@ -1278,19 +1281,19 @@ var Peer = class {
1278
1281
  void this._connectionCtx?.dispose();
1279
1282
  this._connectionCtx = this._ctx.derive();
1280
1283
  connection.errors.handle((err) => {
1281
- log4.info("connection error, closing", {
1284
+ log3.info("connection error, closing", {
1282
1285
  topic: this.topic,
1283
1286
  peerId: this.localInfo,
1284
1287
  remoteId: this.remoteInfo,
1285
1288
  initiator,
1286
1289
  err
1287
1290
  }, {
1288
- F: __dxlog_file4,
1289
- L: 338,
1291
+ F: __dxlog_file3,
1292
+ L: 339,
1290
1293
  S: this,
1291
1294
  C: (f, a) => f(...a)
1292
1295
  });
1293
- log4.trace("dxos.mesh.connection.error", {
1296
+ log3.trace("dxos.mesh.connection.error", {
1294
1297
  topic: this.topic,
1295
1298
  localPeerId: this.localInfo,
1296
1299
  remotePeerId: this.remoteInfo,
@@ -1298,8 +1301,8 @@ var Peer = class {
1298
1301
  initiator,
1299
1302
  err
1300
1303
  }, {
1301
- F: __dxlog_file4,
1302
- L: 345,
1304
+ F: __dxlog_file3,
1305
+ L: 346,
1303
1306
  S: this,
1304
1307
  C: (f, a) => f(...a)
1305
1308
  });
@@ -1313,50 +1316,50 @@ var Peer = class {
1313
1316
  return;
1314
1317
  }
1315
1318
  const connection = this.connection;
1316
- log4("closing...", {
1319
+ log3("closing...", {
1317
1320
  peerId: this.remoteInfo,
1318
1321
  sessionId: connection.sessionId
1319
1322
  }, {
1320
- F: __dxlog_file4,
1321
- L: 370,
1323
+ F: __dxlog_file3,
1324
+ L: 371,
1322
1325
  S: this,
1323
1326
  C: (f, a) => f(...a)
1324
1327
  });
1325
1328
  await connection.close({
1326
1329
  error: err
1327
1330
  });
1328
- log4("closed", {
1331
+ log3("closed", {
1329
1332
  peerId: this.remoteInfo,
1330
1333
  sessionId: connection.sessionId
1331
1334
  }, {
1332
- F: __dxlog_file4,
1333
- L: 376,
1335
+ F: __dxlog_file3,
1336
+ L: 377,
1334
1337
  S: this,
1335
1338
  C: (f, a) => f(...a)
1336
1339
  });
1337
1340
  }
1338
- async onSignal(message) {
1341
+ async onSignal(ctx, message) {
1339
1342
  if (!this.connection) {
1340
- log4("dropping signal message for non-existent connection", {
1343
+ log3("dropping signal message for non-existent connection", {
1341
1344
  message
1342
1345
  }, {
1343
- F: __dxlog_file4,
1344
- L: 381,
1346
+ F: __dxlog_file3,
1347
+ L: 382,
1345
1348
  S: this,
1346
1349
  C: (f, a) => f(...a)
1347
1350
  });
1348
1351
  return;
1349
1352
  }
1350
- await this.connection.signal(message);
1353
+ await this.connection.signal(ctx, message);
1351
1354
  }
1352
1355
  async safeDestroy(reason) {
1353
1356
  await this._ctx.dispose();
1354
- log4("Destroying peer", {
1357
+ log3("Destroying peer", {
1355
1358
  peerId: this.remoteInfo,
1356
1359
  topic: this.topic
1357
1360
  }, {
1358
- F: __dxlog_file4,
1359
- L: 391,
1361
+ F: __dxlog_file3,
1362
+ L: 392,
1360
1363
  S: this,
1361
1364
  C: (f, a) => f(...a)
1362
1365
  });
@@ -1388,10 +1391,50 @@ function _ts_decorate3(decorators, target, key, desc) {
1388
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;
1389
1392
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1390
1393
  }
1391
- 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";
1392
1395
  var INITIATION_DELAY = 100;
1393
1396
  var getClassName = (obj) => Object.getPrototypeOf(obj).constructor.name;
1394
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();
1395
1438
  // TODO(burdon): Swarm => Peer.create/destroy =< Connection.open/close
1396
1439
  // TODO(burdon): Pass in object.
1397
1440
  constructor(_topic, _ownPeer, _topology, _protocolProvider, _messenger, _transportFactory, _label, _connectionLimiter, _initiationDelay = INITIATION_DELAY) {
@@ -1404,55 +1447,44 @@ var Swarm = class {
1404
1447
  this._label = _label;
1405
1448
  this._connectionLimiter = _connectionLimiter;
1406
1449
  this._initiationDelay = _initiationDelay;
1407
- this._ctx = new Context4(void 0, {
1408
- F: __dxlog_file5,
1409
- L: 38
1410
- });
1411
- this._listeningHandle = void 0;
1412
- this._peers = new ComplexMap2(PeerInfoHash);
1413
- this._instanceId = PublicKey4.random().toHex();
1414
- this.connectionAdded = new Event3();
1415
- this.disconnected = new Event3();
1416
- this.connected = new Event3();
1417
- this.errors = new ErrorStream2();
1418
- log5.trace("dxos.mesh.swarm.constructor", trace2.begin({
1450
+ log4.trace("dxos.mesh.swarm.constructor", trace2.begin({
1419
1451
  id: this._instanceId,
1420
1452
  data: {
1421
1453
  topic: this._topic.toHex(),
1422
1454
  peer: this._ownPeer
1423
1455
  }
1424
1456
  }), {
1425
- F: __dxlog_file5,
1457
+ F: __dxlog_file4,
1426
1458
  L: 88,
1427
1459
  S: this,
1428
1460
  C: (f, a) => f(...a)
1429
1461
  });
1430
- log5("creating swarm", {
1462
+ log4("creating swarm", {
1431
1463
  peerId: _ownPeer
1432
1464
  }, {
1433
- F: __dxlog_file5,
1465
+ F: __dxlog_file4,
1434
1466
  L: 92,
1435
1467
  S: this,
1436
1468
  C: (f, a) => f(...a)
1437
1469
  });
1438
1470
  _topology.init(this._getSwarmController());
1439
1471
  this._swarmMessenger = new SwarmMessenger({
1440
- sendMessage: async (msg) => await this._messenger.sendMessage(msg),
1441
- onSignal: async (msg) => await this.onSignal(msg),
1442
- onOffer: async (msg) => await this.onOffer(msg),
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),
1443
1475
  topic: this._topic
1444
1476
  });
1445
- log5.trace("dxos.mesh.swarm.constructor", trace2.end({
1477
+ log4.trace("dxos.mesh.swarm.constructor", trace2.end({
1446
1478
  id: this._instanceId
1447
1479
  }), {
1448
- F: __dxlog_file5,
1480
+ F: __dxlog_file4,
1449
1481
  L: 101,
1450
1482
  S: this,
1451
1483
  C: (f, a) => f(...a)
1452
1484
  });
1453
1485
  }
1454
1486
  get connections() {
1455
- 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);
1456
1488
  }
1457
1489
  get ownPeerId() {
1458
1490
  return PublicKey4.from(this._ownPeer.peerKey);
@@ -1471,7 +1503,7 @@ var Swarm = class {
1471
1503
  }
1472
1504
  async open() {
1473
1505
  invariant4(!this._listeningHandle, void 0, {
1474
- F: __dxlog_file5,
1506
+ F: __dxlog_file4,
1475
1507
  L: 132,
1476
1508
  S: this,
1477
1509
  A: [
@@ -1483,10 +1515,10 @@ var Swarm = class {
1483
1515
  peer: this._ownPeer,
1484
1516
  payloadType: "dxos.mesh.swarm.SwarmMessage",
1485
1517
  onMessage: async (message) => {
1486
- 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", {
1487
1519
  err
1488
1520
  }, {
1489
- F: __dxlog_file5,
1521
+ F: __dxlog_file4,
1490
1522
  L: 140,
1491
1523
  S: this,
1492
1524
  C: (f, a) => f(...a)
@@ -1495,8 +1527,8 @@ var Swarm = class {
1495
1527
  });
1496
1528
  }
1497
1529
  async destroy() {
1498
- log5("destroying...", void 0, {
1499
- F: __dxlog_file5,
1530
+ log4("destroying...", void 0, {
1531
+ F: __dxlog_file4,
1500
1532
  L: 146,
1501
1533
  S: this,
1502
1534
  C: (f, a) => f(...a)
@@ -1506,8 +1538,8 @@ var Swarm = class {
1506
1538
  await this._ctx.dispose();
1507
1539
  await this._topology.destroy();
1508
1540
  await Promise.all(Array.from(this._peers.keys()).map((key) => this._destroyPeer(key, "swarm destroyed")));
1509
- log5("destroyed", void 0, {
1510
- F: __dxlog_file5,
1541
+ log4("destroyed", void 0, {
1542
+ F: __dxlog_file4,
1511
1543
  L: 153,
1512
1544
  S: this,
1513
1545
  C: (f, a) => f(...a)
@@ -1515,7 +1547,7 @@ var Swarm = class {
1515
1547
  }
1516
1548
  async setTopology(topology) {
1517
1549
  invariant4(!this._ctx.disposed, "Swarm is offline", {
1518
- F: __dxlog_file5,
1550
+ F: __dxlog_file4,
1519
1551
  L: 157,
1520
1552
  S: this,
1521
1553
  A: [
@@ -1526,11 +1558,11 @@ var Swarm = class {
1526
1558
  if (topology === this._topology) {
1527
1559
  return;
1528
1560
  }
1529
- log5("setting topology", {
1561
+ log4("setting topology", {
1530
1562
  previous: getClassName(this._topology),
1531
1563
  topology: getClassName(topology)
1532
1564
  }, {
1533
- F: __dxlog_file5,
1565
+ F: __dxlog_file4,
1534
1566
  L: 161,
1535
1567
  S: this,
1536
1568
  C: (f, a) => f(...a)
@@ -1541,17 +1573,17 @@ var Swarm = class {
1541
1573
  this._topology.update();
1542
1574
  }
1543
1575
  async onSwarmEvent(swarmEvent) {
1544
- log5("swarm event", {
1576
+ log4("swarm event", {
1545
1577
  swarmEvent
1546
1578
  }, {
1547
- F: __dxlog_file5,
1579
+ F: __dxlog_file4,
1548
1580
  L: 174,
1549
1581
  S: this,
1550
1582
  C: (f, a) => f(...a)
1551
1583
  });
1552
1584
  if (this._ctx.disposed) {
1553
- log5("swarm event ignored for disposed swarm", void 0, {
1554
- F: __dxlog_file5,
1585
+ log4("swarm event ignored for disposed swarm", void 0, {
1586
+ F: __dxlog_file4,
1555
1587
  L: 177,
1556
1588
  S: this,
1557
1589
  C: (f, a) => f(...a)
@@ -1561,10 +1593,10 @@ var Swarm = class {
1561
1593
  if (swarmEvent.peerAvailable) {
1562
1594
  const peerId = swarmEvent.peerAvailable.peer.peerKey;
1563
1595
  if (peerId !== this._ownPeer.peerKey) {
1564
- log5("new peer", {
1596
+ log4("new peer", {
1565
1597
  peerId
1566
1598
  }, {
1567
- F: __dxlog_file5,
1599
+ F: __dxlog_file4,
1568
1600
  L: 184,
1569
1601
  S: this,
1570
1602
  C: (f, a) => f(...a)
@@ -1577,24 +1609,24 @@ var Swarm = class {
1577
1609
  if (peer) {
1578
1610
  peer.advertizing = false;
1579
1611
  if (this._isConnectionEstablishmentInProgress(peer)) {
1580
- log5(`destroying peer, state: ${peer.connection?.state}`, void 0, {
1581
- F: __dxlog_file5,
1612
+ log4(`destroying peer, state: ${peer.connection?.state}`, void 0, {
1613
+ F: __dxlog_file4,
1582
1614
  L: 195,
1583
1615
  S: this,
1584
1616
  C: (f, a) => f(...a)
1585
1617
  });
1586
- void this._destroyPeer(swarmEvent.peerLeft.peer, "peer left").catch((err) => log5.catch(err, void 0, {
1587
- F: __dxlog_file5,
1618
+ void this._destroyPeer(swarmEvent.peerLeft.peer, "peer left").catch((err) => log4.catch(err, void 0, {
1619
+ F: __dxlog_file4,
1588
1620
  L: 196,
1589
1621
  S: this,
1590
1622
  C: (f, a) => f(...a)
1591
1623
  }));
1592
1624
  }
1593
1625
  } else {
1594
- log5("received peerLeft but no peer found", {
1626
+ log4("received peerLeft but no peer found", {
1595
1627
  peer: swarmEvent.peerLeft.peer.peerKey
1596
1628
  }, {
1597
- F: __dxlog_file5,
1629
+ F: __dxlog_file4,
1598
1630
  L: 199,
1599
1631
  S: this,
1600
1632
  C: (f, a) => f(...a)
@@ -1603,18 +1635,18 @@ var Swarm = class {
1603
1635
  }
1604
1636
  this._topology.update();
1605
1637
  }
1606
- async onOffer(message) {
1607
- log5("offer", {
1638
+ async onOffer(ctx, message) {
1639
+ log4("offer", {
1608
1640
  message
1609
1641
  }, {
1610
- F: __dxlog_file5,
1642
+ F: __dxlog_file4,
1611
1643
  L: 208,
1612
1644
  S: this,
1613
1645
  C: (f, a) => f(...a)
1614
1646
  });
1615
1647
  if (this._ctx.disposed) {
1616
- log5("ignored for disposed swarm", void 0, {
1617
- F: __dxlog_file5,
1648
+ log4("ignored for disposed swarm", void 0, {
1649
+ F: __dxlog_file4,
1618
1650
  L: 210,
1619
1651
  S: this,
1620
1652
  C: (f, a) => f(...a)
@@ -1624,7 +1656,7 @@ var Swarm = class {
1624
1656
  };
1625
1657
  }
1626
1658
  invariant4(message.author, void 0, {
1627
- F: __dxlog_file5,
1659
+ F: __dxlog_file4,
1628
1660
  L: 215,
1629
1661
  S: this,
1630
1662
  A: [
@@ -1633,10 +1665,10 @@ var Swarm = class {
1633
1665
  ]
1634
1666
  });
1635
1667
  if (message.recipient.peerKey !== this._ownPeer.peerKey) {
1636
- log5("rejecting offer with incorrect peerId", {
1668
+ log4("rejecting offer with incorrect peerId", {
1637
1669
  message
1638
1670
  }, {
1639
- F: __dxlog_file5,
1671
+ F: __dxlog_file4,
1640
1672
  L: 217,
1641
1673
  S: this,
1642
1674
  C: (f, a) => f(...a)
@@ -1646,10 +1678,10 @@ var Swarm = class {
1646
1678
  };
1647
1679
  }
1648
1680
  if (!message.topic?.equals(this._topic)) {
1649
- log5("rejecting offer with incorrect topic", {
1681
+ log4("rejecting offer with incorrect topic", {
1650
1682
  message
1651
1683
  }, {
1652
- F: __dxlog_file5,
1684
+ F: __dxlog_file4,
1653
1685
  L: 221,
1654
1686
  S: this,
1655
1687
  C: (f, a) => f(...a)
@@ -1659,7 +1691,7 @@ var Swarm = class {
1659
1691
  };
1660
1692
  }
1661
1693
  const peer = this._getOfferSenderPeer(message.author);
1662
- const answer = await peer.onOffer(message);
1694
+ const answer = await peer.onOffer(ctx, message);
1663
1695
  this._topology.update();
1664
1696
  return answer;
1665
1697
  }
@@ -1673,18 +1705,18 @@ var Swarm = class {
1673
1705
  }
1674
1706
  return peer;
1675
1707
  }
1676
- async onSignal(message) {
1677
- log5("signal", {
1708
+ async onSignal(ctx, message) {
1709
+ log4("signal", {
1678
1710
  message
1679
1711
  }, {
1680
- F: __dxlog_file5,
1712
+ F: __dxlog_file4,
1681
1713
  L: 246,
1682
1714
  S: this,
1683
1715
  C: (f, a) => f(...a)
1684
1716
  });
1685
1717
  if (this._ctx.disposed) {
1686
- log5.info("ignored for offline swarm", void 0, {
1687
- F: __dxlog_file5,
1718
+ log4.info("ignored for offline swarm", void 0, {
1719
+ F: __dxlog_file4,
1688
1720
  L: 248,
1689
1721
  S: this,
1690
1722
  C: (f, a) => f(...a)
@@ -1692,7 +1724,7 @@ var Swarm = class {
1692
1724
  return;
1693
1725
  }
1694
1726
  invariant4(message.recipient.peerKey === this._ownPeer.peerKey, `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1695
- F: __dxlog_file5,
1727
+ F: __dxlog_file4,
1696
1728
  L: 251,
1697
1729
  S: this,
1698
1730
  A: [
@@ -1701,7 +1733,7 @@ var Swarm = class {
1701
1733
  ]
1702
1734
  });
1703
1735
  invariant4(message.topic?.equals(this._topic), void 0, {
1704
- F: __dxlog_file5,
1736
+ F: __dxlog_file4,
1705
1737
  L: 255,
1706
1738
  S: this,
1707
1739
  A: [
@@ -1710,7 +1742,7 @@ var Swarm = class {
1710
1742
  ]
1711
1743
  });
1712
1744
  invariant4(message.author, void 0, {
1713
- F: __dxlog_file5,
1745
+ F: __dxlog_file4,
1714
1746
  L: 256,
1715
1747
  S: this,
1716
1748
  A: [
@@ -1719,7 +1751,7 @@ var Swarm = class {
1719
1751
  ]
1720
1752
  });
1721
1753
  const peer = this._getOrCreatePeer(message.author);
1722
- await peer.onSignal(message);
1754
+ await peer.onSignal(ctx, message);
1723
1755
  }
1724
1756
  // For debug purposes
1725
1757
  async goOffline() {
@@ -1730,14 +1762,14 @@ var Swarm = class {
1730
1762
  }
1731
1763
  // For debug purposes
1732
1764
  async goOnline() {
1733
- this._ctx = new Context4(void 0, {
1734
- F: __dxlog_file5,
1765
+ this._ctx = new Context3(void 0, {
1766
+ F: __dxlog_file4,
1735
1767
  L: 272
1736
1768
  });
1737
1769
  }
1738
1770
  _getOrCreatePeer(peerInfo) {
1739
1771
  invariant4(peerInfo.peerKey, "PeerInfo.peerKey is required", {
1740
- F: __dxlog_file5,
1772
+ F: __dxlog_file4,
1741
1773
  L: 276,
1742
1774
  S: this,
1743
1775
  A: [
@@ -1756,8 +1788,8 @@ var Swarm = class {
1756
1788
  },
1757
1789
  onDisconnected: async () => {
1758
1790
  if (this._isUnregistered(peer)) {
1759
- log5.verbose("ignored onDisconnected for unregistered peer", void 0, {
1760
- F: __dxlog_file5,
1791
+ log4.verbose("ignored onDisconnected for unregistered peer", void 0, {
1792
+ F: __dxlog_file4,
1761
1793
  L: 296,
1762
1794
  S: this,
1763
1795
  C: (f, a) => f(...a)
@@ -1772,10 +1804,10 @@ var Swarm = class {
1772
1804
  },
1773
1805
  onRejected: () => {
1774
1806
  if (!this._isUnregistered(peer)) {
1775
- log5("peer rejected connection", {
1807
+ log4("peer rejected connection", {
1776
1808
  peerInfo
1777
1809
  }, {
1778
- F: __dxlog_file5,
1810
+ F: __dxlog_file4,
1779
1811
  L: 310,
1780
1812
  S: this,
1781
1813
  C: (f, a) => f(...a)
@@ -1798,18 +1830,18 @@ var Swarm = class {
1798
1830
  return peer;
1799
1831
  }
1800
1832
  async _destroyPeer(peerInfo, reason) {
1801
- log5("destroy peer", {
1833
+ log4("destroy peer", {
1802
1834
  peerKey: peerInfo.peerKey,
1803
1835
  reason
1804
1836
  }, {
1805
- F: __dxlog_file5,
1837
+ F: __dxlog_file4,
1806
1838
  L: 332,
1807
1839
  S: this,
1808
1840
  C: (f, a) => f(...a)
1809
1841
  });
1810
1842
  const peer = this._peers.get(peerInfo);
1811
1843
  invariant4(peer, void 0, {
1812
- F: __dxlog_file5,
1844
+ F: __dxlog_file4,
1813
1845
  L: 334,
1814
1846
  S: this,
1815
1847
  A: [
@@ -1838,8 +1870,8 @@ var Swarm = class {
1838
1870
  peerKey: peer.toHex()
1839
1871
  });
1840
1872
  } catch (err) {
1841
- log5("initiation error", err, {
1842
- F: __dxlog_file5,
1873
+ log4("initiation error", err, {
1874
+ F: __dxlog_file4,
1843
1875
  L: 361,
1844
1876
  S: this,
1845
1877
  C: (f, a) => f(...a)
@@ -1867,10 +1899,10 @@ var Swarm = class {
1867
1899
  const ctx = this._ctx;
1868
1900
  const peer = this._getOrCreatePeer(remotePeer);
1869
1901
  if (remotePeer.peerKey < this._ownPeer.peerKey) {
1870
- log5("initiation delay", {
1902
+ log4("initiation delay", {
1871
1903
  remotePeer
1872
1904
  }, {
1873
- F: __dxlog_file5,
1905
+ F: __dxlog_file4,
1874
1906
  L: 389,
1875
1907
  S: this,
1876
1908
  C: (f, a) => f(...a)
@@ -1886,20 +1918,20 @@ var Swarm = class {
1886
1918
  if (peer.connection) {
1887
1919
  return;
1888
1920
  }
1889
- log5("initiating connection...", {
1921
+ log4("initiating connection...", {
1890
1922
  remotePeer
1891
1923
  }, {
1892
- F: __dxlog_file5,
1924
+ F: __dxlog_file4,
1893
1925
  L: 405,
1894
1926
  S: this,
1895
1927
  C: (f, a) => f(...a)
1896
1928
  });
1897
- await peer.initiateConnection();
1929
+ await peer.initiateConnection(ctx);
1898
1930
  this._topology.update();
1899
- log5("initiated", {
1931
+ log4("initiated", {
1900
1932
  remotePeer
1901
1933
  }, {
1902
- F: __dxlog_file5,
1934
+ F: __dxlog_file4,
1903
1935
  L: 408,
1904
1936
  S: this,
1905
1937
  C: (f, a) => f(...a)
@@ -1951,20 +1983,21 @@ _ts_decorate3([
1951
1983
  // src/swarm/swarm-mapper.ts
1952
1984
  import { Event as Event4, SubscriptionList } from "@dxos/async";
1953
1985
  import { PublicKey as PublicKey5 } from "@dxos/keys";
1954
- import { log as log6 } from "@dxos/log";
1986
+ import { log as log5 } from "@dxos/log";
1955
1987
  import { PeerInfoHash as PeerInfoHash2 } from "@dxos/messaging";
1956
1988
  import { ComplexMap as ComplexMap3 } from "@dxos/util";
1957
- 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";
1958
1990
  var SwarmMapper = class {
1991
+ _swarm;
1992
+ _subscriptions = new SubscriptionList();
1993
+ _connectionSubscriptions = new ComplexMap3(PeerInfoHash2);
1994
+ _peers = new ComplexMap3(PeerInfoHash2);
1995
+ mapUpdated = new Event4();
1959
1996
  get peers() {
1960
1997
  return Array.from(this._peers.values());
1961
1998
  }
1962
1999
  constructor(_swarm) {
1963
2000
  this._swarm = _swarm;
1964
- this._subscriptions = new SubscriptionList();
1965
- this._connectionSubscriptions = new ComplexMap3(PeerInfoHash2);
1966
- this._peers = new ComplexMap3(PeerInfoHash2);
1967
- this.mapUpdated = new Event4();
1968
2001
  this._subscriptions.add(_swarm.connectionAdded.on((connection) => {
1969
2002
  this._update();
1970
2003
  this._connectionSubscriptions.set(connection.remoteInfo, connection.stateChanged.on(() => {
@@ -1979,8 +2012,8 @@ var SwarmMapper = class {
1979
2012
  this._update();
1980
2013
  }
1981
2014
  _update() {
1982
- log6("updating swarm", void 0, {
1983
- F: __dxlog_file6,
2015
+ log5("updating swarm", void 0, {
2016
+ F: __dxlog_file5,
1984
2017
  L: 71,
1985
2018
  S: this,
1986
2019
  C: (f, a) => f(...a)
@@ -2000,11 +2033,11 @@ var SwarmMapper = class {
2000
2033
  ]
2001
2034
  });
2002
2035
  }
2003
- log6("graph changed", {
2036
+ log5("graph changed", {
2004
2037
  directConnections: this._swarm.connections.length,
2005
2038
  totalPeersInSwarm: this._peers.size
2006
2039
  }, {
2007
- F: __dxlog_file6,
2040
+ F: __dxlog_file5,
2008
2041
  L: 112,
2009
2042
  S: this,
2010
2043
  C: (f, a) => f(...a)
@@ -2021,29 +2054,30 @@ var SwarmMapper = class {
2021
2054
 
2022
2055
  // src/swarm/connection-limiter.ts
2023
2056
  import { DeferredTask as DeferredTask2 } from "@dxos/async";
2024
- import { Context as Context5 } from "@dxos/context";
2057
+ import { Context as Context4 } from "@dxos/context";
2025
2058
  import { invariant as invariant5 } from "@dxos/invariant";
2026
2059
  import { PublicKey as PublicKey6 } from "@dxos/keys";
2027
- import { log as log7 } from "@dxos/log";
2060
+ import { log as log6 } from "@dxos/log";
2028
2061
  import { CancelledError as CancelledError3 } from "@dxos/protocols";
2029
2062
  import { ComplexMap as ComplexMap4 } from "@dxos/util";
2030
- 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";
2031
2064
  var MAX_CONCURRENT_INITIATING_CONNECTIONS = 50;
2032
2065
  var ConnectionLimiter = class {
2033
- constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
2034
- this._ctx = new Context5(void 0, {
2035
- F: __dxlog_file7,
2036
- L: 23
2037
- });
2038
- /**
2039
- * Queue of promises to resolve when initiating connections amount is below the limit.
2040
- */
2041
- this._waitingPromises = new ComplexMap4(PublicKey6.hash);
2042
- this.resolveWaitingPromises = new DeferredTask2(this._ctx, async () => {
2043
- Array.from(this._waitingPromises.values()).slice(0, this._maxConcurrentInitConnections).forEach(({ resolve }) => {
2044
- resolve();
2045
- });
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();
2046
2078
  });
2079
+ });
2080
+ constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
2047
2081
  this._maxConcurrentInitConnections = maxConcurrentInitConnections;
2048
2082
  }
2049
2083
  /**
@@ -2051,7 +2085,7 @@ var ConnectionLimiter = class {
2051
2085
  */
2052
2086
  async connecting(sessionId) {
2053
2087
  invariant5(!this._waitingPromises.has(sessionId), "Peer is already waiting for connection", {
2054
- F: __dxlog_file7,
2088
+ F: __dxlog_file6,
2055
2089
  L: 48,
2056
2090
  S: this,
2057
2091
  A: [
@@ -2059,10 +2093,10 @@ var ConnectionLimiter = class {
2059
2093
  "'Peer is already waiting for connection'"
2060
2094
  ]
2061
2095
  });
2062
- log7("waiting", {
2096
+ log6("waiting", {
2063
2097
  sessionId
2064
2098
  }, {
2065
- F: __dxlog_file7,
2099
+ F: __dxlog_file6,
2066
2100
  L: 49,
2067
2101
  S: this,
2068
2102
  C: (f, a) => f(...a)
@@ -2074,10 +2108,10 @@ var ConnectionLimiter = class {
2074
2108
  });
2075
2109
  this.resolveWaitingPromises.schedule();
2076
2110
  });
2077
- log7("allow", {
2111
+ log6("allow", {
2078
2112
  sessionId
2079
2113
  }, {
2080
- F: __dxlog_file7,
2114
+ F: __dxlog_file6,
2081
2115
  L: 57,
2082
2116
  S: this,
2083
2117
  C: (f, a) => f(...a)
@@ -2087,10 +2121,10 @@ var ConnectionLimiter = class {
2087
2121
  * Rejects promise returned by `connecting` method.
2088
2122
  */
2089
2123
  doneConnecting(sessionId) {
2090
- log7("done", {
2124
+ log6("done", {
2091
2125
  sessionId
2092
2126
  }, {
2093
- F: __dxlog_file7,
2127
+ F: __dxlog_file6,
2094
2128
  L: 64,
2095
2129
  S: this,
2096
2130
  C: (f, a) => f(...a)
@@ -2110,22 +2144,20 @@ import { raise } from "@dxos/debug";
2110
2144
  import { PublicKey as PublicKey7 } from "@dxos/keys";
2111
2145
  import { ComplexMap as ComplexMap5 } from "@dxos/util";
2112
2146
  var CONNECTION_GC_THRESHOLD = 1e3 * 60 * 15;
2113
- var EventType = /* @__PURE__ */ function(EventType2) {
2147
+ var EventType = /* @__PURE__ */ (function(EventType2) {
2114
2148
  EventType2["CONNECTION_STATE_CHANGED"] = "CONNECTION_STATE_CHANGED";
2115
2149
  EventType2["PROTOCOL_ERROR"] = "PROTOCOL_ERROR";
2116
2150
  EventType2["PROTOCOL_EXTENSIONS_INITIALIZED"] = "PROTOCOL_EXTENSIONS_INITIALIZED";
2117
2151
  EventType2["PROTOCOL_EXTENSIONS_HANDSHAKE"] = "PROTOCOL_EXTENSIONS_HANDSHAKE";
2118
2152
  EventType2["PROTOCOL_HANDSHAKE"] = "PROTOCOL_HANDSHAKE";
2119
2153
  return EventType2;
2120
- }({});
2154
+ })({});
2121
2155
  var ConnectionLog = class {
2122
- constructor() {
2123
- /**
2124
- * SwarmId => info
2125
- */
2126
- this._swarms = new ComplexMap5(PublicKey7.hash);
2127
- this.update = new Event5();
2128
- }
2156
+ /**
2157
+ * SwarmId => info
2158
+ */
2159
+ _swarms = new ComplexMap5(PublicKey7.hash);
2160
+ update = new Event5();
2129
2161
  getSwarmInfo(swarmId) {
2130
2162
  return this._swarms.get(swarmId) ?? raise(new Error(`Swarm not found: ${swarmId}`));
2131
2163
  }
@@ -2200,7 +2232,7 @@ var gcSwarm = (swarm) => {
2200
2232
  import { Event as Event6, synchronized as synchronized4 } from "@dxos/async";
2201
2233
  import { invariant as invariant6 } from "@dxos/invariant";
2202
2234
  import { PublicKey as PublicKey8 } from "@dxos/keys";
2203
- import { log as log8 } from "@dxos/log";
2235
+ import { log as log7 } from "@dxos/log";
2204
2236
  import { Messenger } from "@dxos/messaging";
2205
2237
  import { trace as trace3 } from "@dxos/protocols";
2206
2238
  import { ConnectionState as ConnectionState2 } from "@dxos/protocols/proto/dxos/client/services";
@@ -2211,19 +2243,25 @@ function _ts_decorate4(decorators, target, key, desc) {
2211
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;
2212
2244
  return c > 3 && r && Object.defineProperty(target, key, r), r;
2213
2245
  }
2214
- 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";
2215
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();
2216
2264
  constructor({ transportFactory, signalManager, enableDevtoolsLogging, peerInfo }) {
2217
- /**
2218
- * @internal
2219
- */
2220
- this._swarms = new ComplexMap6(PublicKey8.hash);
2221
- this._mappers = new ComplexMap6(PublicKey8.hash);
2222
- this._instanceId = PublicKey8.random().toHex();
2223
- this._peerInfo = void 0;
2224
- this._connectionState = ConnectionState2.ONLINE;
2225
- this.connectionStateChanged = new Event6();
2226
- this.topicsUpdated = new Event6();
2227
2265
  this._transportFactory = transportFactory;
2228
2266
  this._signalManager = signalManager;
2229
2267
  this._signalManager.swarmEvent.on((event) => this._swarms.get(event.topic)?.onSwarmEvent(event));
@@ -2231,8 +2269,8 @@ var SwarmNetworkManager = class {
2231
2269
  signalManager: this._signalManager
2232
2270
  });
2233
2271
  this._signalConnection = {
2234
- join: (opts) => this._signalManager.join(opts),
2235
- leave: (opts) => this._signalManager.leave(opts)
2272
+ join: (ctx, opts) => this._signalManager.join(ctx, opts),
2273
+ leave: (ctx, opts) => this._signalManager.leave(ctx, opts)
2236
2274
  };
2237
2275
  this._peerInfo = peerInfo;
2238
2276
  this._connectionLimiter = new ConnectionLimiter();
@@ -2261,31 +2299,31 @@ var SwarmNetworkManager = class {
2261
2299
  this._peerInfo = peerInfo;
2262
2300
  }
2263
2301
  async open() {
2264
- log8.trace("dxos.mesh.network-manager.open", trace3.begin({
2302
+ log7.trace("dxos.mesh.network-manager.open", trace3.begin({
2265
2303
  id: this._instanceId
2266
2304
  }), {
2267
- F: __dxlog_file8,
2268
- L: 133,
2305
+ F: __dxlog_file7,
2306
+ L: 134,
2269
2307
  S: this,
2270
2308
  C: (f, a) => f(...a)
2271
2309
  });
2272
2310
  await this._messenger.open();
2273
2311
  await this._signalManager.open();
2274
- log8.trace("dxos.mesh.network-manager.open", trace3.end({
2312
+ log7.trace("dxos.mesh.network-manager.open", trace3.end({
2275
2313
  id: this._instanceId
2276
2314
  }), {
2277
- F: __dxlog_file8,
2278
- L: 136,
2315
+ F: __dxlog_file7,
2316
+ L: 137,
2279
2317
  S: this,
2280
2318
  C: (f, a) => f(...a)
2281
2319
  });
2282
2320
  }
2283
- async close() {
2321
+ async close(ctx) {
2284
2322
  for (const topic of this._swarms.keys()) {
2285
- await this.leaveSwarm(topic).catch((err) => {
2286
- log8(err, void 0, {
2287
- F: __dxlog_file8,
2288
- L: 142,
2323
+ await this.leaveSwarm(ctx, topic).catch((err) => {
2324
+ log7(err, void 0, {
2325
+ F: __dxlog_file7,
2326
+ L: 143,
2289
2327
  S: this,
2290
2328
  C: (f, a) => f(...a)
2291
2329
  });
@@ -2297,10 +2335,10 @@ var SwarmNetworkManager = class {
2297
2335
  /**
2298
2336
  * Join the swarm.
2299
2337
  */
2300
- async joinSwarm({ topic, topology, protocolProvider: protocol, label }) {
2338
+ async joinSwarm(ctx, { topic, topology, protocolProvider: protocol, label }) {
2301
2339
  invariant6(PublicKey8.isPublicKey(topic), void 0, {
2302
- F: __dxlog_file8,
2303
- L: 160,
2340
+ F: __dxlog_file7,
2341
+ L: 164,
2304
2342
  S: this,
2305
2343
  A: [
2306
2344
  "PublicKey.isPublicKey(topic)",
@@ -2308,8 +2346,8 @@ var SwarmNetworkManager = class {
2308
2346
  ]
2309
2347
  });
2310
2348
  invariant6(topology, void 0, {
2311
- F: __dxlog_file8,
2312
- L: 161,
2349
+ F: __dxlog_file7,
2350
+ L: 165,
2313
2351
  S: this,
2314
2352
  A: [
2315
2353
  "topology",
@@ -2317,8 +2355,8 @@ var SwarmNetworkManager = class {
2317
2355
  ]
2318
2356
  });
2319
2357
  invariant6(this._peerInfo, void 0, {
2320
- F: __dxlog_file8,
2321
- L: 162,
2358
+ F: __dxlog_file7,
2359
+ L: 166,
2322
2360
  S: this,
2323
2361
  A: [
2324
2362
  "this._peerInfo",
@@ -2326,8 +2364,8 @@ var SwarmNetworkManager = class {
2326
2364
  ]
2327
2365
  });
2328
2366
  invariant6(typeof protocol === "function", void 0, {
2329
- F: __dxlog_file8,
2330
- L: 163,
2367
+ F: __dxlog_file7,
2368
+ L: 167,
2331
2369
  S: this,
2332
2370
  A: [
2333
2371
  "typeof protocol === 'function'",
@@ -2337,23 +2375,23 @@ var SwarmNetworkManager = class {
2337
2375
  if (this._swarms.has(topic)) {
2338
2376
  throw new Error(`Already connected to swarm: ${PublicKey8.from(topic)}`);
2339
2377
  }
2340
- log8("joining", {
2378
+ log7("joining", {
2341
2379
  topic: PublicKey8.from(topic),
2342
2380
  peerInfo: this._peerInfo,
2343
2381
  topology: topology.toString()
2344
2382
  }, {
2345
- F: __dxlog_file8,
2346
- L: 168,
2383
+ F: __dxlog_file7,
2384
+ L: 172,
2347
2385
  S: this,
2348
2386
  C: (f, a) => f(...a)
2349
2387
  });
2350
2388
  const swarm = new Swarm(topic, this._peerInfo, topology, protocol, this._messenger, this._transportFactory, label, this._connectionLimiter);
2351
2389
  swarm.errors.handle((error) => {
2352
- log8("swarm error", {
2390
+ log7("swarm error", {
2353
2391
  error
2354
2392
  }, {
2355
- F: __dxlog_file8,
2356
- L: 181,
2393
+ F: __dxlog_file7,
2394
+ L: 185,
2357
2395
  S: this,
2358
2396
  C: (f, a) => f(...a)
2359
2397
  });
@@ -2361,47 +2399,47 @@ var SwarmNetworkManager = class {
2361
2399
  this._swarms.set(topic, swarm);
2362
2400
  this._mappers.set(topic, new SwarmMapper(swarm));
2363
2401
  await swarm.open();
2364
- this._signalConnection.join({
2402
+ this._signalConnection.join(ctx, {
2365
2403
  topic,
2366
2404
  peer: this._peerInfo
2367
- }).catch((error) => log8.catch(error, void 0, {
2368
- F: __dxlog_file8,
2369
- L: 190,
2405
+ }).catch((error) => log7.catch(error, void 0, {
2406
+ F: __dxlog_file7,
2407
+ L: 194,
2370
2408
  S: this,
2371
2409
  C: (f, a) => f(...a)
2372
2410
  }));
2373
2411
  this.topicsUpdated.emit();
2374
2412
  this._connectionLog?.joinedSwarm(swarm);
2375
- log8("joined", {
2413
+ log7("joined", {
2376
2414
  topic: PublicKey8.from(topic),
2377
2415
  count: this._swarms.size
2378
2416
  }, {
2379
- F: __dxlog_file8,
2380
- L: 194,
2417
+ F: __dxlog_file7,
2418
+ L: 198,
2381
2419
  S: this,
2382
2420
  C: (f, a) => f(...a)
2383
2421
  });
2384
2422
  return {
2385
- close: () => this.leaveSwarm(topic)
2423
+ close: (ctx2) => this.leaveSwarm(ctx2, topic)
2386
2424
  };
2387
2425
  }
2388
2426
  /**
2389
2427
  * Close the connection.
2390
2428
  */
2391
- async leaveSwarm(topic) {
2429
+ async leaveSwarm(ctx, topic) {
2392
2430
  if (!this._swarms.has(topic)) {
2393
2431
  return;
2394
2432
  }
2395
- log8("leaving", {
2433
+ log7("leaving", {
2396
2434
  topic: PublicKey8.from(topic)
2397
2435
  }, {
2398
- F: __dxlog_file8,
2399
- L: 211,
2436
+ F: __dxlog_file7,
2437
+ L: 215,
2400
2438
  S: this,
2401
2439
  C: (f, a) => f(...a)
2402
2440
  });
2403
2441
  const swarm = this._swarms.get(topic);
2404
- await this._signalConnection.leave({
2442
+ await this._signalConnection.leave(ctx, {
2405
2443
  topic,
2406
2444
  peer: swarm.ownPeer
2407
2445
  });
@@ -2412,12 +2450,12 @@ var SwarmNetworkManager = class {
2412
2450
  await swarm.destroy();
2413
2451
  this._swarms.delete(topic);
2414
2452
  this.topicsUpdated.emit();
2415
- log8("left", {
2453
+ log7("left", {
2416
2454
  topic: PublicKey8.from(topic),
2417
2455
  count: this._swarms.size
2418
2456
  }, {
2419
- F: __dxlog_file8,
2420
- L: 225,
2457
+ F: __dxlog_file7,
2458
+ L: 229,
2421
2459
  S: this,
2422
2460
  C: (f, a) => f(...a)
2423
2461
  });
@@ -2458,14 +2496,15 @@ _ts_decorate4([
2458
2496
 
2459
2497
  // src/topology/fully-connected-topology.ts
2460
2498
  import { invariant as invariant7 } from "@dxos/invariant";
2461
- var __dxlog_file9 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
2499
+ var __dxlog_file8 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
2462
2500
  var FullyConnectedTopology = class {
2501
+ _controller;
2463
2502
  toString() {
2464
2503
  return "FullyConnectedTopology";
2465
2504
  }
2466
2505
  init(controller) {
2467
2506
  invariant7(!this._controller, "Already initialized", {
2468
- F: __dxlog_file9,
2507
+ F: __dxlog_file8,
2469
2508
  L: 18,
2470
2509
  S: this,
2471
2510
  A: [
@@ -2477,7 +2516,7 @@ var FullyConnectedTopology = class {
2477
2516
  }
2478
2517
  update() {
2479
2518
  invariant7(this._controller, "Not initialized", {
2480
- F: __dxlog_file9,
2519
+ F: __dxlog_file8,
2481
2520
  L: 23,
2482
2521
  S: this,
2483
2522
  A: [
@@ -2497,315 +2536,13 @@ var FullyConnectedTopology = class {
2497
2536
  }
2498
2537
  };
2499
2538
 
2500
- // src/topology/mmst-topology.ts
2501
- import { invariant as invariant8 } from "@dxos/invariant";
2502
- import { log as log9 } from "@dxos/log";
2503
- var __dxlog_file10 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/topology/mmst-topology.ts";
2504
- var MIN_UPDATE_INTERVAL = 1e3 * 10;
2505
- var MAX_CHANGES_PER_UPDATE = 1;
2506
- var MMSTTopology = class {
2507
- constructor({ originateConnections = 2, maxPeers = 4, sampleSize = 10 } = {}) {
2508
- this._sampleCollected = false;
2509
- this._lastAction = /* @__PURE__ */ new Date(0);
2510
- this._originateConnections = originateConnections;
2511
- this._maxPeers = maxPeers;
2512
- this._sampleSize = sampleSize;
2513
- }
2514
- init(controller) {
2515
- invariant8(!this._controller, "Already initialized", {
2516
- F: __dxlog_file10,
2517
- L: 49,
2518
- S: this,
2519
- A: [
2520
- "!this._controller",
2521
- "'Already initialized'"
2522
- ]
2523
- });
2524
- this._controller = controller;
2525
- }
2526
- update() {
2527
- invariant8(this._controller, "Not initialized", {
2528
- F: __dxlog_file10,
2529
- L: 54,
2530
- S: this,
2531
- A: [
2532
- "this._controller",
2533
- "'Not initialized'"
2534
- ]
2535
- });
2536
- const { connected, candidates } = this._controller.getState();
2537
- if (this._sampleCollected || connected.length > this._maxPeers || candidates.length > 0) {
2538
- log9("Running the algorithm.", void 0, {
2539
- F: __dxlog_file10,
2540
- L: 58,
2541
- S: this,
2542
- C: (f, a) => f(...a)
2543
- });
2544
- this._sampleCollected = true;
2545
- this._runAlgorithm();
2546
- }
2547
- }
2548
- forceUpdate() {
2549
- this._lastAction = /* @__PURE__ */ new Date(0);
2550
- this.update();
2551
- }
2552
- async onOffer(peer) {
2553
- invariant8(this._controller, "Not initialized", {
2554
- F: __dxlog_file10,
2555
- L: 70,
2556
- S: this,
2557
- A: [
2558
- "this._controller",
2559
- "'Not initialized'"
2560
- ]
2561
- });
2562
- const { connected } = this._controller.getState();
2563
- const accept = connected.length < this._maxPeers;
2564
- log9(`Offer ${peer} accept=${accept}`, void 0, {
2565
- F: __dxlog_file10,
2566
- L: 73,
2567
- S: this,
2568
- C: (f, a) => f(...a)
2569
- });
2570
- return accept;
2571
- }
2572
- async destroy() {
2573
- }
2574
- _runAlgorithm() {
2575
- invariant8(this._controller, "Not initialized", {
2576
- F: __dxlog_file10,
2577
- L: 82,
2578
- S: this,
2579
- A: [
2580
- "this._controller",
2581
- "'Not initialized'"
2582
- ]
2583
- });
2584
- const { connected, candidates, ownPeerId } = this._controller.getState();
2585
- if (connected.length > this._maxPeers) {
2586
- log9(`disconnect ${connected.length - this._maxPeers} peers.`, void 0, {
2587
- F: __dxlog_file10,
2588
- L: 88,
2589
- S: this,
2590
- C: (f, a) => f(...a)
2591
- });
2592
- const sorted = sortByXorDistance(connected, ownPeerId).reverse().slice(0, this._maxPeers - connected.length);
2593
- invariant8(sorted.length === 0, void 0, {
2594
- F: __dxlog_file10,
2595
- L: 92,
2596
- S: this,
2597
- A: [
2598
- "sorted.length === 0",
2599
- ""
2600
- ]
2601
- });
2602
- if (sorted.length > MAX_CHANGES_PER_UPDATE) {
2603
- log9(`want to disconnect ${sorted.length} peers but limited to ${MAX_CHANGES_PER_UPDATE}`, void 0, {
2604
- F: __dxlog_file10,
2605
- L: 95,
2606
- S: this,
2607
- C: (f, a) => f(...a)
2608
- });
2609
- }
2610
- if (Date.now() - this._lastAction.getTime() > MIN_UPDATE_INTERVAL) {
2611
- for (const peer of sorted.slice(0, MAX_CHANGES_PER_UPDATE)) {
2612
- log9(`Disconnect ${peer}.`, void 0, {
2613
- F: __dxlog_file10,
2614
- L: 100,
2615
- S: this,
2616
- C: (f, a) => f(...a)
2617
- });
2618
- this._controller.disconnect(peer);
2619
- }
2620
- this._lastAction = /* @__PURE__ */ new Date();
2621
- } else {
2622
- log9("rate limited disconnect", void 0, {
2623
- F: __dxlog_file10,
2624
- L: 105,
2625
- S: this,
2626
- C: (f, a) => f(...a)
2627
- });
2628
- }
2629
- } else if (connected.length < this._originateConnections) {
2630
- log9(`connect ${this._originateConnections - connected.length} peers.`, void 0, {
2631
- F: __dxlog_file10,
2632
- L: 109,
2633
- S: this,
2634
- C: (f, a) => f(...a)
2635
- });
2636
- const sample = candidates.sort(() => Math.random() - 0.5).slice(0, this._sampleSize);
2637
- const sorted = sortByXorDistance(sample, ownPeerId).slice(0, this._originateConnections - connected.length);
2638
- if (sorted.length > MAX_CHANGES_PER_UPDATE) {
2639
- log9(`want to connect ${sorted.length} peers but limited to ${MAX_CHANGES_PER_UPDATE}`, void 0, {
2640
- F: __dxlog_file10,
2641
- L: 114,
2642
- S: this,
2643
- C: (f, a) => f(...a)
2644
- });
2645
- }
2646
- if (Date.now() - this._lastAction.getTime() > MIN_UPDATE_INTERVAL) {
2647
- for (const peer of sorted.slice(0, MAX_CHANGES_PER_UPDATE)) {
2648
- log9(`Connect ${peer}.`, void 0, {
2649
- F: __dxlog_file10,
2650
- L: 118,
2651
- S: this,
2652
- C: (f, a) => f(...a)
2653
- });
2654
- this._controller.connect(peer);
2655
- }
2656
- this._lastAction = /* @__PURE__ */ new Date();
2657
- } else {
2658
- log9("rate limited connect", void 0, {
2659
- F: __dxlog_file10,
2660
- L: 123,
2661
- S: this,
2662
- C: (f, a) => f(...a)
2663
- });
2664
- }
2665
- }
2666
- }
2667
- toString() {
2668
- return "MMSTTopology";
2669
- }
2670
- };
2671
- var sortByXorDistance = (keys, reference) => {
2672
- const sorted = keys.sort((a, b) => {
2673
- return compareXor(distXor(a.asBuffer(), reference.asBuffer()), distXor(b.asBuffer(), reference.asBuffer()));
2674
- });
2675
- log9("Sorted keys", {
2676
- keys,
2677
- reference,
2678
- sorted
2679
- }, {
2680
- F: __dxlog_file10,
2681
- L: 137,
2682
- S: void 0,
2683
- C: (f, a) => f(...a)
2684
- });
2685
- return sorted;
2686
- };
2687
- var distXor = (a, b) => {
2688
- const maxLength = Math.max(a.length, b.length);
2689
- const result = Buffer.allocUnsafe(maxLength);
2690
- for (let i = 0; i < maxLength; i++) {
2691
- result[i] = (a[i] || 0) ^ (b[i] || 0);
2692
- }
2693
- return result;
2694
- };
2695
- var compareXor = (a, b) => {
2696
- const maxLength = Math.max(a.length, b.length);
2697
- for (let i = 0; i < maxLength; i++) {
2698
- if ((a[i] || 0) === (b[i] || 0)) {
2699
- continue;
2700
- }
2701
- return (a[i] || 0) < (b[i] || 0) ? -1 : 1;
2702
- }
2703
- return 0;
2704
- };
2705
-
2706
- // src/topology/star-topology.ts
2707
- import { invariant as invariant9 } from "@dxos/invariant";
2708
- import { log as log10 } from "@dxos/log";
2709
- var __dxlog_file11 = "/__w/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
2710
- var StarTopology = class {
2711
- constructor(_centralPeer) {
2712
- this._centralPeer = _centralPeer;
2713
- }
2714
- toString() {
2715
- return `StarTopology(${this._centralPeer.truncate()})`;
2716
- }
2717
- init(controller) {
2718
- invariant9(!this._controller, "Already initialized.", {
2719
- F: __dxlog_file11,
2720
- L: 21,
2721
- S: this,
2722
- A: [
2723
- "!this._controller",
2724
- "'Already initialized.'"
2725
- ]
2726
- });
2727
- this._controller = controller;
2728
- }
2729
- update() {
2730
- invariant9(this._controller, "Not initialized.", {
2731
- F: __dxlog_file11,
2732
- L: 26,
2733
- S: this,
2734
- A: [
2735
- "this._controller",
2736
- "'Not initialized.'"
2737
- ]
2738
- });
2739
- const { candidates, connected, ownPeerId } = this._controller.getState();
2740
- if (!ownPeerId.equals(this._centralPeer)) {
2741
- log10("leaf peer dropping all connections apart from central peer.", void 0, {
2742
- F: __dxlog_file11,
2743
- L: 29,
2744
- S: this,
2745
- C: (f, a) => f(...a)
2746
- });
2747
- for (const peer of connected) {
2748
- if (!peer.equals(this._centralPeer)) {
2749
- log10("dropping connection", {
2750
- peer
2751
- }, {
2752
- F: __dxlog_file11,
2753
- L: 34,
2754
- S: this,
2755
- C: (f, a) => f(...a)
2756
- });
2757
- this._controller.disconnect(peer);
2758
- }
2759
- }
2760
- }
2761
- for (const peer of candidates) {
2762
- if (peer.equals(this._centralPeer) || ownPeerId.equals(this._centralPeer)) {
2763
- log10("connecting to peer", {
2764
- peer
2765
- }, {
2766
- F: __dxlog_file11,
2767
- L: 43,
2768
- S: this,
2769
- C: (f, a) => f(...a)
2770
- });
2771
- this._controller.connect(peer);
2772
- }
2773
- }
2774
- }
2775
- async onOffer(peer) {
2776
- invariant9(this._controller, "Not initialized.", {
2777
- F: __dxlog_file11,
2778
- L: 50,
2779
- S: this,
2780
- A: [
2781
- "this._controller",
2782
- "'Not initialized.'"
2783
- ]
2784
- });
2785
- const { ownPeerId } = this._controller.getState();
2786
- log10("offer", {
2787
- peer,
2788
- isCentral: peer.equals(this._centralPeer),
2789
- isSelfCentral: ownPeerId.equals(this._centralPeer)
2790
- }, {
2791
- F: __dxlog_file11,
2792
- L: 52,
2793
- S: this,
2794
- C: (f, a) => f(...a)
2795
- });
2796
- return ownPeerId.equals(this._centralPeer) || peer.equals(this._centralPeer);
2797
- }
2798
- async destroy() {
2799
- }
2800
- };
2801
-
2802
2539
  // src/transport/memory-transport.ts
2803
2540
  import { Transform } from "node:stream";
2804
2541
  import { Event as Event7, Trigger as Trigger2 } from "@dxos/async";
2805
2542
  import { ErrorStream as ErrorStream3 } from "@dxos/debug";
2806
- import { invariant as invariant10 } from "@dxos/invariant";
2543
+ import { invariant as invariant8 } from "@dxos/invariant";
2807
2544
  import { PublicKey as PublicKey9 } from "@dxos/keys";
2808
- import { log as log11, logInfo as logInfo3 } from "@dxos/log";
2545
+ import { log as log8, logInfo as logInfo3 } from "@dxos/log";
2809
2546
  import { ComplexMap as ComplexMap7 } from "@dxos/util";
2810
2547
  function _ts_decorate5(decorators, target, key, desc) {
2811
2548
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -2813,7 +2550,7 @@ function _ts_decorate5(decorators, target, key, desc) {
2813
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;
2814
2551
  return c > 3 && r && Object.defineProperty(target, key, r), r;
2815
2552
  }
2816
- 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";
2817
2554
  var MEMORY_TRANSPORT_DELAY = 1;
2818
2555
  var createStreamDelay = (delay) => {
2819
2556
  return new Transform({
@@ -2827,22 +2564,23 @@ var MemoryTransportFactory = {
2827
2564
  createTransport: (options) => new MemoryTransport(options)
2828
2565
  };
2829
2566
  var MemoryTransport = class _MemoryTransport {
2830
- static {
2831
- // TODO(burdon): Remove static properties (inject context into constructor).
2832
- this._connections = new ComplexMap7(PublicKey9.hash);
2833
- }
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();
2834
2580
  constructor(_options) {
2835
2581
  this._options = _options;
2836
- this._instanceId = PublicKey9.random();
2837
- this._remote = new Trigger2();
2838
- this._outgoingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
2839
- this._incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
2840
- this._closed = false;
2841
- this.closed = new Event7();
2842
- this.connected = new Event7();
2843
- this.errors = new ErrorStream3();
2844
- invariant10(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection", {
2845
- F: __dxlog_file12,
2582
+ invariant8(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection", {
2583
+ F: __dxlog_file9,
2846
2584
  L: 64,
2847
2585
  S: this,
2848
2586
  A: [
@@ -2856,15 +2594,15 @@ var MemoryTransport = class _MemoryTransport {
2856
2594
  return !this._closed;
2857
2595
  }
2858
2596
  async open() {
2859
- log11("opening...", void 0, {
2860
- F: __dxlog_file12,
2597
+ log8("opening...", void 0, {
2598
+ F: __dxlog_file9,
2861
2599
  L: 74,
2862
2600
  S: this,
2863
2601
  C: (f, a) => f(...a)
2864
2602
  });
2865
2603
  if (this._options.initiator) {
2866
- log11("sending signal", void 0, {
2867
- F: __dxlog_file12,
2604
+ log8("sending signal", void 0, {
2605
+ F: __dxlog_file9,
2868
2606
  L: 78,
2869
2607
  S: this,
2870
2608
  C: (f, a) => f(...a)
@@ -2894,8 +2632,8 @@ var MemoryTransport = class _MemoryTransport {
2894
2632
  this.closed.emit();
2895
2633
  return;
2896
2634
  }
2897
- invariant10(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`, {
2898
- F: __dxlog_file12,
2635
+ invariant8(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`, {
2636
+ F: __dxlog_file9,
2899
2637
  L: 104,
2900
2638
  S: this,
2901
2639
  A: [
@@ -2905,8 +2643,8 @@ var MemoryTransport = class _MemoryTransport {
2905
2643
  });
2906
2644
  this._remoteConnection._remoteConnection = this;
2907
2645
  this._remoteConnection._remoteInstanceId = this._instanceId;
2908
- log11("connected", void 0, {
2909
- F: __dxlog_file12,
2646
+ log8("connected", void 0, {
2647
+ F: __dxlog_file9,
2910
2648
  L: 108,
2911
2649
  S: this,
2912
2650
  C: (f, a) => f(...a)
@@ -2924,8 +2662,8 @@ var MemoryTransport = class _MemoryTransport {
2924
2662
  return this;
2925
2663
  }
2926
2664
  async close() {
2927
- log11("closing...", void 0, {
2928
- F: __dxlog_file12,
2665
+ log8("closing...", void 0, {
2666
+ F: __dxlog_file9,
2929
2667
  L: 130,
2930
2668
  S: this,
2931
2669
  C: (f, a) => f(...a)
@@ -2945,8 +2683,8 @@ var MemoryTransport = class _MemoryTransport {
2945
2683
  this._remoteConnection = void 0;
2946
2684
  }
2947
2685
  this.closed.emit();
2948
- log11("closed", void 0, {
2949
- F: __dxlog_file12,
2686
+ log8("closed", void 0, {
2687
+ F: __dxlog_file9,
2950
2688
  L: 158,
2951
2689
  S: this,
2952
2690
  C: (f, a) => f(...a)
@@ -2954,10 +2692,10 @@ var MemoryTransport = class _MemoryTransport {
2954
2692
  return this;
2955
2693
  }
2956
2694
  async onSignal({ payload }) {
2957
- log11("received signal", {
2695
+ log8("received signal", {
2958
2696
  payload
2959
2697
  }, {
2960
- F: __dxlog_file12,
2698
+ F: __dxlog_file9,
2961
2699
  L: 163,
2962
2700
  S: this,
2963
2701
  C: (f, a) => f(...a)
@@ -2992,13 +2730,13 @@ _ts_decorate5([
2992
2730
  var toError = (err) => err instanceof Error ? err : new Error(String(err));
2993
2731
 
2994
2732
  // src/transport/transport.ts
2995
- var TransportKind = /* @__PURE__ */ function(TransportKind2) {
2733
+ var TransportKind = /* @__PURE__ */ (function(TransportKind2) {
2996
2734
  TransportKind2["WEB_RTC"] = "WEB-RTC";
2997
2735
  TransportKind2["WEB_RTC_PROXY"] = "WEB-RTC_PROXY";
2998
2736
  TransportKind2["MEMORY"] = "MEMORY";
2999
2737
  TransportKind2["TCP"] = "TCP";
3000
2738
  return TransportKind2;
3001
- }({});
2739
+ })({});
3002
2740
 
3003
2741
  // src/transport/webrtc/rtc-connection-factory.ts
3004
2742
  import { Mutex } from "@dxos/async";
@@ -3014,14 +2752,10 @@ var BrowserRtcConnectionFactory = class {
3014
2752
  }
3015
2753
  };
3016
2754
  var NodeRtcConnectionFactory = class _NodeRtcConnectionFactory {
3017
- static {
3018
- this._createdConnections = 0;
3019
- }
3020
- static {
3021
- this._cleanupMutex = new Mutex();
3022
- }
2755
+ static _createdConnections = 0;
2756
+ static _cleanupMutex = new Mutex();
3023
2757
  // This should be inside the function to avoid triggering `eval` in the global scope.
3024
- // eslint-disable-next-line no-new-func
2758
+ // eslint-disable-next-line @typescript-eslint/no-implied-eval
3025
2759
  // TODO(burdon): Do imports here?
3026
2760
  async initialize() {
3027
2761
  }
@@ -3050,9 +2784,9 @@ var getRtcConnectionFactory = () => {
3050
2784
  };
3051
2785
 
3052
2786
  // src/transport/webrtc/rtc-peer-connection.ts
3053
- import { synchronized as synchronized5, Trigger as Trigger3, Mutex as Mutex2 } from "@dxos/async";
3054
- import { invariant as invariant12 } from "@dxos/invariant";
3055
- import { log as log13, logInfo as logInfo4 } from "@dxos/log";
2787
+ import { Mutex as Mutex2, Trigger as Trigger3, synchronized as synchronized5 } from "@dxos/async";
2788
+ import { invariant as invariant10 } from "@dxos/invariant";
2789
+ import { log as log10, logInfo as logInfo4 } from "@dxos/log";
3056
2790
  import { ConnectivityError as ConnectivityError3 } from "@dxos/protocols";
3057
2791
  import { trace as trace4 } from "@dxos/tracing";
3058
2792
 
@@ -3061,8 +2795,8 @@ import { Duplex } from "node:stream";
3061
2795
  import { Event as AsyncEvent } from "@dxos/async";
3062
2796
  import { Resource } from "@dxos/context";
3063
2797
  import { ErrorStream as ErrorStream4 } from "@dxos/debug";
3064
- import { invariant as invariant11 } from "@dxos/invariant";
3065
- import { log as log12 } from "@dxos/log";
2798
+ import { invariant as invariant9 } from "@dxos/invariant";
2799
+ import { log as log9 } from "@dxos/log";
3066
2800
  import { ConnectivityError as ConnectivityError2 } from "@dxos/protocols";
3067
2801
 
3068
2802
  // src/transport/webrtc/rtc-transport-stats.ts
@@ -3113,12 +2847,21 @@ var getRtcConnectionStats = async (connection, channelTopic) => {
3113
2847
  };
3114
2848
 
3115
2849
  // src/transport/webrtc/rtc-transport-channel.ts
3116
- 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";
3117
2851
  var MAX_MESSAGE_SIZE = 64 * 1024;
3118
2852
  var MAX_BUFFERED_AMOUNT = 64 * 1024;
3119
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;
3120
2863
  constructor(_connection, _options) {
3121
- super(), this._connection = _connection, this._options = _options, this.closed = new AsyncEvent(), this.connected = new AsyncEvent(), this.errors = new ErrorStream4(), this._streamDataFlushedCallback = null, this._isChannelCreationInProgress = false;
2864
+ super(), this._connection = _connection, this._options = _options;
3122
2865
  }
3123
2866
  get isRtcChannelCreationInProgress() {
3124
2867
  return this._isChannelCreationInProgress;
@@ -3129,8 +2872,8 @@ var RtcTransportChannel = class extends Resource {
3129
2872
  }
3130
2873
  }
3131
2874
  async _open() {
3132
- invariant11(!this._isChannelCreationInProgress, void 0, {
3133
- F: __dxlog_file13,
2875
+ invariant9(!this._isChannelCreationInProgress, void 0, {
2876
+ F: __dxlog_file10,
3134
2877
  L: 56,
3135
2878
  S: this,
3136
2879
  A: [
@@ -3148,13 +2891,15 @@ var RtcTransportChannel = class extends Resource {
3148
2891
  }
3149
2892
  }).catch((err) => {
3150
2893
  if (this.isOpen) {
3151
- 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
+ });
3152
2897
  this.errors.raise(error);
3153
2898
  } else {
3154
- log12.verbose("connection establishment failed after transport was closed", {
2899
+ log9.verbose("connection establishment failed after transport was closed", {
3155
2900
  err
3156
2901
  }, {
3157
- F: __dxlog_file13,
2902
+ F: __dxlog_file10,
3158
2903
  L: 76,
3159
2904
  S: this,
3160
2905
  C: (f, a) => f(...a)
@@ -3171,8 +2916,8 @@ var RtcTransportChannel = class extends Resource {
3171
2916
  this._stream = void 0;
3172
2917
  }
3173
2918
  this.closed.emit();
3174
- log12("closed", void 0, {
3175
- F: __dxlog_file13,
2919
+ log9("closed", void 0, {
2920
+ F: __dxlog_file10,
3176
2921
  L: 92,
3177
2922
  S: this,
3178
2923
  C: (f, a) => f(...a)
@@ -3182,10 +2927,10 @@ var RtcTransportChannel = class extends Resource {
3182
2927
  Object.assign(channel, {
3183
2928
  onopen: () => {
3184
2929
  if (!this.isOpen) {
3185
- log12.warn("channel opened in a closed transport", {
2930
+ log9.warn("channel opened in a closed transport", {
3186
2931
  topic: this._options.topic
3187
2932
  }, {
3188
- F: __dxlog_file13,
2933
+ F: __dxlog_file10,
3189
2934
  L: 99,
3190
2935
  S: this,
3191
2936
  C: (f, a) => f(...a)
@@ -3193,8 +2938,8 @@ var RtcTransportChannel = class extends Resource {
3193
2938
  this._safeCloseChannel(channel);
3194
2939
  return;
3195
2940
  }
3196
- log12("onopen", void 0, {
3197
- F: __dxlog_file13,
2941
+ log9("onopen", void 0, {
2942
+ F: __dxlog_file10,
3198
2943
  L: 104,
3199
2944
  S: this,
3200
2945
  C: (f, a) => f(...a)
@@ -3211,8 +2956,8 @@ var RtcTransportChannel = class extends Resource {
3211
2956
  this.connected.emit();
3212
2957
  },
3213
2958
  onclose: async () => {
3214
- log12("onclose", void 0, {
3215
- F: __dxlog_file13,
2959
+ log9("onclose", void 0, {
2960
+ F: __dxlog_file10,
3216
2961
  L: 117,
3217
2962
  S: this,
3218
2963
  C: (f, a) => f(...a)
@@ -3221,8 +2966,8 @@ var RtcTransportChannel = class extends Resource {
3221
2966
  },
3222
2967
  onmessage: async (event) => {
3223
2968
  if (!this._stream) {
3224
- log12.warn("ignoring message on a closed channel", void 0, {
3225
- F: __dxlog_file13,
2969
+ log9.warn("ignoring message on a closed channel", void 0, {
2970
+ F: __dxlog_file10,
3226
2971
  L: 123,
3227
2972
  S: this,
3228
2973
  C: (f, a) => f(...a)
@@ -3252,8 +2997,8 @@ var RtcTransportChannel = class extends Resource {
3252
2997
  }
3253
2998
  async _handleChannelWrite(chunk, callback) {
3254
2999
  if (!this._channel) {
3255
- log12.warn("writing to a channel after a connection was closed", void 0, {
3256
- F: __dxlog_file13,
3000
+ log9.warn("writing to a channel after a connection was closed", void 0, {
3001
+ F: __dxlog_file10,
3257
3002
  L: 153,
3258
3003
  S: this,
3259
3004
  C: (f, a) => f(...a)
@@ -3275,8 +3020,8 @@ var RtcTransportChannel = class extends Resource {
3275
3020
  }
3276
3021
  if (this._channel.bufferedAmount > MAX_BUFFERED_AMOUNT) {
3277
3022
  if (this._streamDataFlushedCallback !== null) {
3278
- log12.error("consumer trying to write before we are ready for more data", void 0, {
3279
- F: __dxlog_file13,
3023
+ log9.error("consumer trying to write before we are ready for more data", void 0, {
3024
+ F: __dxlog_file10,
3280
3025
  L: 174,
3281
3026
  S: this,
3282
3027
  C: (f, a) => f(...a)
@@ -3291,8 +3036,8 @@ var RtcTransportChannel = class extends Resource {
3291
3036
  try {
3292
3037
  channel.close();
3293
3038
  } catch (error) {
3294
- log12.catch(error, void 0, {
3295
- F: __dxlog_file13,
3039
+ log9.catch(error, void 0, {
3040
+ F: __dxlog_file10,
3296
3041
  L: 186,
3297
3042
  S: this,
3298
3043
  C: (f, a) => f(...a)
@@ -3343,16 +3088,29 @@ function _ts_decorate6(decorators, target, key, desc) {
3343
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;
3344
3089
  return c > 3 && r && Object.defineProperty(target, key, r), r;
3345
3090
  }
3346
- 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";
3347
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;
3348
3111
  constructor(_factory, _options) {
3349
3112
  this._factory = _factory;
3350
3113
  this._options = _options;
3351
- this._channelCreatedCallbacks = /* @__PURE__ */ new Map();
3352
- this._transportChannels = /* @__PURE__ */ new Map();
3353
- this._dataChannels = /* @__PURE__ */ new Map();
3354
- this._readyForCandidates = new Trigger3();
3355
- this._offerProcessingMutex = new Mutex2();
3356
3114
  this._initiator = chooseInitiatorPeer(_options.ownPeerKey, _options.remotePeerKey) === _options.ownPeerKey;
3357
3115
  }
3358
3116
  get transportChannelCount() {
@@ -3378,8 +3136,8 @@ var RtcPeerConnection = class {
3378
3136
  if (existingChannel) {
3379
3137
  return existingChannel;
3380
3138
  }
3381
- log13("waiting for initiator-peer to open a data channel", void 0, {
3382
- F: __dxlog_file14,
3139
+ log10("waiting for initiator-peer to open a data channel", void 0, {
3140
+ F: __dxlog_file11,
3383
3141
  L: 95,
3384
3142
  S: this,
3385
3143
  C: (f, a) => f(...a)
@@ -3407,10 +3165,10 @@ var RtcPeerConnection = class {
3407
3165
  if (this._connection) {
3408
3166
  return this._connection;
3409
3167
  }
3410
- log13("initializing connection...", () => ({
3168
+ log10("initializing connection...", () => ({
3411
3169
  remotePeer: this._options.remotePeerKey
3412
3170
  }), {
3413
- F: __dxlog_file14,
3171
+ F: __dxlog_file11,
3414
3172
  L: 120,
3415
3173
  S: this,
3416
3174
  C: (f, a) => f(...a)
@@ -3420,8 +3178,8 @@ var RtcPeerConnection = class {
3420
3178
  const iceCandidateErrors = [];
3421
3179
  Object.assign(connection, {
3422
3180
  onnegotiationneeded: async () => {
3423
- invariant12(this._initiator, void 0, {
3424
- F: __dxlog_file14,
3181
+ invariant10(this._initiator, void 0, {
3182
+ F: __dxlog_file11,
3425
3183
  L: 135,
3426
3184
  S: this,
3427
3185
  A: [
@@ -3433,8 +3191,8 @@ var RtcPeerConnection = class {
3433
3191
  this._onConnectionCallbackAfterClose("onnegotiationneeded", connection);
3434
3192
  return;
3435
3193
  }
3436
- log13("onnegotiationneeded", void 0, {
3437
- F: __dxlog_file14,
3194
+ log10("onnegotiationneeded", void 0, {
3195
+ F: __dxlog_file11,
3438
3196
  L: 142,
3439
3197
  S: this,
3440
3198
  C: (f, a) => f(...a)
@@ -3455,18 +3213,18 @@ var RtcPeerConnection = class {
3455
3213
  return;
3456
3214
  }
3457
3215
  if (event.candidate) {
3458
- log13("onicecandidate", {
3216
+ log10("onicecandidate", {
3459
3217
  candidate: event.candidate.candidate
3460
3218
  }, {
3461
- F: __dxlog_file14,
3219
+ F: __dxlog_file11,
3462
3220
  L: 161,
3463
3221
  S: this,
3464
3222
  C: (f, a) => f(...a)
3465
3223
  });
3466
3224
  await this._sendIceCandidate(event.candidate);
3467
3225
  } else {
3468
- log13("onicecandidate gathering complete", void 0, {
3469
- F: __dxlog_file14,
3226
+ log10("onicecandidate gathering complete", void 0, {
3227
+ F: __dxlog_file11,
3470
3228
  L: 164,
3471
3229
  S: this,
3472
3230
  C: (f, a) => f(...a)
@@ -3491,10 +3249,10 @@ var RtcPeerConnection = class {
3491
3249
  this._onConnectionCallbackAfterClose("oniceconnectionstatechange", connection);
3492
3250
  return;
3493
3251
  }
3494
- log13("oniceconnectionstatechange", {
3252
+ log10("oniceconnectionstatechange", {
3495
3253
  state: connection.iceConnectionState
3496
3254
  }, {
3497
- F: __dxlog_file14,
3255
+ F: __dxlog_file11,
3498
3256
  L: 184,
3499
3257
  S: this,
3500
3258
  C: (f, a) => f(...a)
@@ -3513,10 +3271,10 @@ var RtcPeerConnection = class {
3513
3271
  }
3514
3272
  return;
3515
3273
  }
3516
- log13("onconnectionstatechange", {
3274
+ log10("onconnectionstatechange", {
3517
3275
  state: connection.connectionState
3518
3276
  }, {
3519
- F: __dxlog_file14,
3277
+ F: __dxlog_file11,
3520
3278
  L: 201,
3521
3279
  S: this,
3522
3280
  C: (f, a) => f(...a)
@@ -3526,10 +3284,10 @@ var RtcPeerConnection = class {
3526
3284
  }
3527
3285
  },
3528
3286
  onsignalingstatechange: () => {
3529
- log13("onsignalingstatechange", {
3287
+ log10("onsignalingstatechange", {
3530
3288
  state: connection.signalingState
3531
3289
  }, {
3532
- F: __dxlog_file14,
3290
+ F: __dxlog_file11,
3533
3291
  L: 208,
3534
3292
  S: this,
3535
3293
  C: (f, a) => f(...a)
@@ -3538,8 +3296,8 @@ var RtcPeerConnection = class {
3538
3296
  // When channel is added to connection.
3539
3297
  // https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/datachannel_event
3540
3298
  ondatachannel: (event) => {
3541
- invariant12(!this._initiator, "Initiator is expected to create data channels.", {
3542
- F: __dxlog_file14,
3299
+ invariant10(!this._initiator, "Initiator is expected to create data channels.", {
3300
+ F: __dxlog_file11,
3543
3301
  L: 214,
3544
3302
  S: this,
3545
3303
  A: [
@@ -3551,10 +3309,10 @@ var RtcPeerConnection = class {
3551
3309
  this._onConnectionCallbackAfterClose("ondatachannel", connection);
3552
3310
  return;
3553
3311
  }
3554
- log13("ondatachannel", {
3312
+ log10("ondatachannel", {
3555
3313
  label: event.channel.label
3556
3314
  }, {
3557
- F: __dxlog_file14,
3315
+ F: __dxlog_file11,
3558
3316
  L: 221,
3559
3317
  S: this,
3560
3318
  C: (f, a) => f(...a)
@@ -3579,10 +3337,10 @@ var RtcPeerConnection = class {
3579
3337
  }
3580
3338
  _abortConnection(connection, error) {
3581
3339
  if (connection !== this._connection) {
3582
- log13.error("attempted to abort an inactive connection", {
3340
+ log10.error("attempted to abort an inactive connection", {
3583
3341
  error
3584
3342
  }, {
3585
- F: __dxlog_file14,
3343
+ F: __dxlog_file11,
3586
3344
  L: 246,
3587
3345
  S: this,
3588
3346
  C: (f, a) => f(...a)
@@ -3600,18 +3358,18 @@ var RtcPeerConnection = class {
3600
3358
  }
3601
3359
  this._transportChannels.clear();
3602
3360
  this._safeCloseConnection();
3603
- log13("connection aborted", {
3361
+ log10("connection aborted", {
3604
3362
  reason: error.message
3605
3363
  }, {
3606
- F: __dxlog_file14,
3364
+ F: __dxlog_file11,
3607
3365
  L: 260,
3608
3366
  S: this,
3609
3367
  C: (f, a) => f(...a)
3610
3368
  });
3611
3369
  }
3612
3370
  async _lockAndCloseConnection() {
3613
- invariant12(this._transportChannels.size === 0, void 0, {
3614
- F: __dxlog_file14,
3371
+ invariant10(this._transportChannels.size === 0, void 0, {
3372
+ F: __dxlog_file11,
3615
3373
  L: 265,
3616
3374
  S: this,
3617
3375
  A: [
@@ -3621,8 +3379,8 @@ var RtcPeerConnection = class {
3621
3379
  });
3622
3380
  if (this._connection) {
3623
3381
  this._safeCloseConnection();
3624
- log13("connection closed", void 0, {
3625
- F: __dxlog_file14,
3382
+ log10("connection closed", void 0, {
3383
+ F: __dxlog_file11,
3626
3384
  L: 268,
3627
3385
  S: this,
3628
3386
  C: (f, a) => f(...a)
@@ -3632,10 +3390,10 @@ var RtcPeerConnection = class {
3632
3390
  async onSignal(signal) {
3633
3391
  const connection = this._connection;
3634
3392
  if (!connection) {
3635
- log13.warn("a signal ignored because the connection was closed", {
3393
+ log10.warn("a signal ignored because the connection was closed", {
3636
3394
  type: signal.payload.data.type
3637
3395
  }, {
3638
- F: __dxlog_file14,
3396
+ F: __dxlog_file11,
3639
3397
  L: 276,
3640
3398
  S: this,
3641
3399
  C: (f, a) => f(...a)
@@ -3699,10 +3457,10 @@ var RtcPeerConnection = class {
3699
3457
  this._abortConnection(connection, new Error(`Unknown signal type ${data.type}.`));
3700
3458
  break;
3701
3459
  }
3702
- log13("signal processed", {
3460
+ log10("signal processed", {
3703
3461
  type: data.type
3704
3462
  }, {
3705
- F: __dxlog_file14,
3463
+ F: __dxlog_file11,
3706
3464
  L: 335,
3707
3465
  S: this,
3708
3466
  C: (f, a) => f(...a)
@@ -3712,10 +3470,10 @@ var RtcPeerConnection = class {
3712
3470
  try {
3713
3471
  await this._readyForCandidates.wait();
3714
3472
  if (connection === this._connection) {
3715
- log13("adding ice candidate", {
3473
+ log10("adding ice candidate", {
3716
3474
  candidate
3717
3475
  }, {
3718
- F: __dxlog_file14,
3476
+ F: __dxlog_file11,
3719
3477
  L: 343,
3720
3478
  S: this,
3721
3479
  C: (f, a) => f(...a)
@@ -3723,8 +3481,8 @@ var RtcPeerConnection = class {
3723
3481
  await connection.addIceCandidate(candidate);
3724
3482
  }
3725
3483
  } catch (err) {
3726
- log13.catch(err, void 0, {
3727
- F: __dxlog_file14,
3484
+ log10.catch(err, void 0, {
3485
+ F: __dxlog_file11,
3728
3486
  L: 347,
3729
3487
  S: this,
3730
3488
  C: (f, a) => f(...a)
@@ -3733,16 +3491,16 @@ var RtcPeerConnection = class {
3733
3491
  }
3734
3492
  _onSessionNegotiated(connection) {
3735
3493
  if (connection === this._connection) {
3736
- log13("ready to process ice candidates", void 0, {
3737
- F: __dxlog_file14,
3494
+ log10("ready to process ice candidates", void 0, {
3495
+ F: __dxlog_file11,
3738
3496
  L: 353,
3739
3497
  S: this,
3740
3498
  C: (f, a) => f(...a)
3741
3499
  });
3742
3500
  this._readyForCandidates.wake();
3743
3501
  } else {
3744
- log13.warn("session was negotiated after connection became inactive", void 0, {
3745
- F: __dxlog_file14,
3502
+ log10.warn("session was negotiated after connection became inactive", void 0, {
3503
+ F: __dxlog_file11,
3746
3504
  L: 356,
3747
3505
  S: this,
3748
3506
  C: (f, a) => f(...a)
@@ -3750,11 +3508,11 @@ var RtcPeerConnection = class {
3750
3508
  }
3751
3509
  }
3752
3510
  _onConnectionCallbackAfterClose(callback, connection) {
3753
- 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", {
3754
3512
  callback,
3755
3513
  state: connection.connectionState
3756
3514
  }, {
3757
- F: __dxlog_file14,
3515
+ F: __dxlog_file11,
3758
3516
  L: 361,
3759
3517
  S: this,
3760
3518
  C: (f, a) => f(...a)
@@ -3766,8 +3524,8 @@ var RtcPeerConnection = class {
3766
3524
  try {
3767
3525
  connection?.close();
3768
3526
  } catch (err) {
3769
- log13.catch(err, void 0, {
3770
- F: __dxlog_file14,
3527
+ log10.catch(err, void 0, {
3528
+ F: __dxlog_file11,
3771
3529
  L: 373,
3772
3530
  S: this,
3773
3531
  C: (f, a) => f(...a)
@@ -3777,8 +3535,8 @@ var RtcPeerConnection = class {
3777
3535
  this._connection = void 0;
3778
3536
  this._dataChannels.clear();
3779
3537
  this._readyForCandidates.wake();
3780
- void this._factory.onConnectionDestroyed().catch((err) => log13.catch(err, void 0, {
3781
- F: __dxlog_file14,
3538
+ void this._factory.onConnectionDestroyed().catch((err) => log10.catch(err, void 0, {
3539
+ F: __dxlog_file11,
3782
3540
  L: 379,
3783
3541
  S: this,
3784
3542
  C: (f, a) => f(...a)
@@ -3802,8 +3560,8 @@ var RtcPeerConnection = class {
3802
3560
  ];
3803
3561
  }
3804
3562
  } catch (error) {
3805
- log13.catch(error, void 0, {
3806
- F: __dxlog_file14,
3563
+ log10.catch(error, void 0, {
3564
+ F: __dxlog_file11,
3807
3565
  L: 395,
3808
3566
  S: this,
3809
3567
  C: (f, a) => f(...a)
@@ -3827,10 +3585,10 @@ var RtcPeerConnection = class {
3827
3585
  }
3828
3586
  });
3829
3587
  } catch (err) {
3830
- log13.warn("signaling error", {
3588
+ log10.warn("signaling error", {
3831
3589
  err
3832
3590
  }, {
3833
- F: __dxlog_file14,
3591
+ F: __dxlog_file11,
3834
3592
  L: 416,
3835
3593
  S: this,
3836
3594
  C: (f, a) => f(...a)
@@ -3908,8 +3666,10 @@ var isRemoteDescriptionSet = (connection, data) => {
3908
3666
  };
3909
3667
  var createIceFailureError = (details) => {
3910
3668
  const candidateErrors = details.map(({ url, errorCode, errorText }) => `${errorCode} ${url}: ${errorText}`);
3911
- return new ConnectivityError3(`ICE failed:
3912
- ${candidateErrors.join("\n")}`);
3669
+ return new ConnectivityError3({
3670
+ message: `ICE failed:
3671
+ ${candidateErrors.join("\n")}`
3672
+ });
3913
3673
  };
3914
3674
 
3915
3675
  // src/transport/webrtc/rtc-transport-factory.ts
@@ -3935,19 +3695,25 @@ import { Writable } from "node:stream";
3935
3695
  import { Event as Event8, scheduleTask as scheduleTask4 } from "@dxos/async";
3936
3696
  import { Resource as Resource2 } from "@dxos/context";
3937
3697
  import { ErrorStream as ErrorStream5 } from "@dxos/debug";
3938
- import { invariant as invariant13 } from "@dxos/invariant";
3698
+ import { invariant as invariant11 } from "@dxos/invariant";
3939
3699
  import { PublicKey as PublicKey10 } from "@dxos/keys";
3940
- import { log as log14 } from "@dxos/log";
3700
+ import { log as log11 } from "@dxos/log";
3941
3701
  import { ConnectionResetError as ConnectionResetError2, ConnectivityError as ConnectivityError4, TimeoutError as TimeoutError3 } from "@dxos/protocols";
3942
3702
  import { ConnectionState as ConnectionState3 } from "@dxos/protocols/proto/dxos/mesh/bridge";
3943
3703
  import { arrayToBuffer } from "@dxos/util";
3944
- 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";
3945
3705
  var RPC_TIMEOUT = 1e4;
3946
3706
  var CLOSE_RPC_TIMEOUT = 3e3;
3947
3707
  var RESP_MIN_THRESHOLD = 500;
3948
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;
3949
3715
  constructor(_options) {
3950
- super(), this._options = _options, this._proxyId = PublicKey10.random(), this.closed = new Event8(), this.connected = new Event8(), this.errors = new ErrorStream5();
3716
+ super(), this._options = _options;
3951
3717
  }
3952
3718
  async _open() {
3953
3719
  let stream;
@@ -3968,8 +3734,8 @@ var RtcTransportProxy = class extends Resource2 {
3968
3734
  this._serviceStream = stream;
3969
3735
  stream.waitUntilReady().then(() => {
3970
3736
  stream.subscribe(async (event) => {
3971
- log14("rtc transport proxy event", event, {
3972
- F: __dxlog_file15,
3737
+ log11("rtc transport proxy event", event, {
3738
+ F: __dxlog_file12,
3973
3739
  L: 66,
3974
3740
  S: this,
3975
3741
  C: (f, a) => f(...a)
@@ -3982,10 +3748,10 @@ var RtcTransportProxy = class extends Resource2 {
3982
3748
  await this._handleSignal(event.signal);
3983
3749
  }
3984
3750
  }, (err) => {
3985
- log14("rtc bridge stream closed", {
3751
+ log11("rtc bridge stream closed", {
3986
3752
  err
3987
3753
  }, {
3988
- F: __dxlog_file15,
3754
+ F: __dxlog_file12,
3989
3755
  L: 76,
3990
3756
  S: this,
3991
3757
  C: (f, a) => f(...a)
@@ -4006,8 +3772,8 @@ var RtcTransportProxy = class extends Resource2 {
4006
3772
  timeout: RPC_TIMEOUT
4007
3773
  }).then(() => {
4008
3774
  if (Date.now() - sendStartMs > RESP_MIN_THRESHOLD) {
4009
- log14("slow response, delaying callback", void 0, {
4010
- F: __dxlog_file15,
3775
+ log11("slow response, delaying callback", void 0, {
3776
+ F: __dxlog_file12,
4011
3777
  L: 93,
4012
3778
  S: this,
4013
3779
  C: (f, a) => f(...a)
@@ -4039,8 +3805,8 @@ var RtcTransportProxy = class extends Resource2 {
4039
3805
  await this._serviceStream?.close();
4040
3806
  this._serviceStream = void 0;
4041
3807
  } catch (err) {
4042
- log14.catch(err, void 0, {
4043
- F: __dxlog_file15,
3808
+ log11.catch(err, void 0, {
3809
+ F: __dxlog_file12,
4044
3810
  L: 128,
4045
3811
  S: this,
4046
3812
  C: (f, a) => f(...a)
@@ -4053,8 +3819,8 @@ var RtcTransportProxy = class extends Resource2 {
4053
3819
  timeout: CLOSE_RPC_TIMEOUT
4054
3820
  });
4055
3821
  } catch (err) {
4056
- log14.catch(err, void 0, {
4057
- F: __dxlog_file15,
3822
+ log11.catch(err, void 0, {
3823
+ F: __dxlog_file12,
4058
3824
  L: 134,
4059
3825
  S: this,
4060
3826
  C: (f, a) => f(...a)
@@ -4099,7 +3865,9 @@ var RtcTransportProxy = class extends Resource2 {
4099
3865
  } catch (error) {
4100
3866
  const type = signalEvent.payload.payload.data?.type;
4101
3867
  if (type === "offer" || type === "answer") {
4102
- 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
+ }));
4103
3871
  }
4104
3872
  }
4105
3873
  }
@@ -4137,11 +3905,11 @@ var RtcTransportProxy = class extends Resource2 {
4137
3905
  if (this.isOpen) {
4138
3906
  this.errors.raise(error);
4139
3907
  } else {
4140
- log14.info("error swallowed because transport was closed", {
3908
+ log11.info("error swallowed because transport was closed", {
4141
3909
  message: error.message
4142
3910
  }, {
4143
- F: __dxlog_file15,
4144
- L: 215,
3911
+ F: __dxlog_file12,
3912
+ L: 217,
4145
3913
  S: this,
4146
3914
  C: (f, a) => f(...a)
4147
3915
  });
@@ -4156,9 +3924,8 @@ var RtcTransportProxy = class extends Resource2 {
4156
3924
  }
4157
3925
  };
4158
3926
  var RtcTransportProxyFactory = class {
4159
- constructor() {
4160
- this._connections = /* @__PURE__ */ new Set();
4161
- }
3927
+ _bridgeService;
3928
+ _connections = /* @__PURE__ */ new Set();
4162
3929
  /**
4163
3930
  * Sets the current BridgeService to be used to open connections.
4164
3931
  * Calling this method will close any existing connections.
@@ -4171,9 +3938,9 @@ var RtcTransportProxyFactory = class {
4171
3938
  return this;
4172
3939
  }
4173
3940
  createTransport(options) {
4174
- invariant13(this._bridgeService, "RtcTransportProxyFactory is not ready to open connections", {
4175
- F: __dxlog_file15,
4176
- L: 245,
3941
+ invariant11(this._bridgeService, "RtcTransportProxyFactory is not ready to open connections", {
3942
+ F: __dxlog_file12,
3943
+ L: 247,
4177
3944
  S: this,
4178
3945
  A: [
4179
3946
  "this._bridgeService",
@@ -4194,11 +3961,17 @@ var RtcTransportProxyFactory = class {
4194
3961
  var decodeError = (err) => {
4195
3962
  const message = typeof err === "string" ? err : err.message;
4196
3963
  if (message.includes("CONNECTION_RESET")) {
4197
- return new ConnectionResetError2(message);
3964
+ return new ConnectionResetError2({
3965
+ message
3966
+ });
4198
3967
  } else if (message.includes("TIMEOUT")) {
4199
- return new TimeoutError3(message);
3968
+ return new TimeoutError3({
3969
+ message
3970
+ });
4200
3971
  } else if (message.includes("CONNECTIVITY_ERROR")) {
4201
- return new ConnectivityError4(message);
3972
+ return new ConnectivityError4({
3973
+ message
3974
+ });
4202
3975
  } else {
4203
3976
  return typeof err === "string" ? new Error(err) : err;
4204
3977
  }
@@ -4207,16 +3980,17 @@ var decodeError = (err) => {
4207
3980
  // src/transport/webrtc/rtc-transport-service.ts
4208
3981
  import { Duplex as Duplex2 } from "node:stream";
4209
3982
  import { Stream } from "@dxos/codec-protobuf/stream";
4210
- import { invariant as invariant14 } from "@dxos/invariant";
3983
+ import { invariant as invariant12 } from "@dxos/invariant";
4211
3984
  import { PublicKey as PublicKey11 } from "@dxos/keys";
4212
- import { log as log15 } from "@dxos/log";
3985
+ import { log as log12 } from "@dxos/log";
4213
3986
  import { ConnectionState as ConnectionState4 } from "@dxos/protocols/proto/dxos/mesh/bridge";
4214
3987
  import { ComplexMap as ComplexMap8 } from "@dxos/util";
4215
- 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";
4216
3989
  var RtcTransportService = class {
3990
+ _transportFactory;
3991
+ _openTransports = new ComplexMap8(PublicKey11.hash);
4217
3992
  constructor(webrtcConfig, iceProvider, _transportFactory = createRtcTransportFactory(webrtcConfig, iceProvider)) {
4218
3993
  this._transportFactory = _transportFactory;
4219
- this._openTransports = new ComplexMap8(PublicKey11.hash);
4220
3994
  }
4221
3995
  hasOpenTransports() {
4222
3996
  return this._openTransports.size > 0;
@@ -4224,8 +3998,8 @@ var RtcTransportService = class {
4224
3998
  open(request) {
4225
3999
  const existingTransport = this._openTransports.get(request.proxyId);
4226
4000
  if (existingTransport) {
4227
- log15.error("requesting a new transport bridge for an existing proxy", void 0, {
4228
- F: __dxlog_file16,
4001
+ log12.error("requesting a new transport bridge for an existing proxy", void 0, {
4002
+ F: __dxlog_file13,
4229
4003
  L: 53,
4230
4004
  S: this,
4231
4005
  C: (f, a) => f(...a)
@@ -4290,8 +4064,8 @@ var RtcTransportService = class {
4290
4064
  close(err);
4291
4065
  });
4292
4066
  ready();
4293
- log15("stream ready", void 0, {
4294
- F: __dxlog_file16,
4067
+ log12("stream ready", void 0, {
4068
+ F: __dxlog_file13,
4295
4069
  L: 115,
4296
4070
  S: this,
4297
4071
  C: (f, a) => f(...a)
@@ -4301,8 +4075,8 @@ var RtcTransportService = class {
4301
4075
  }
4302
4076
  async sendSignal({ proxyId, signal }) {
4303
4077
  const transport = this._openTransports.get(proxyId);
4304
- invariant14(transport, void 0, {
4305
- F: __dxlog_file16,
4078
+ invariant12(transport, void 0, {
4079
+ F: __dxlog_file13,
4306
4080
  L: 123,
4307
4081
  S: this,
4308
4082
  A: [
@@ -4314,8 +4088,8 @@ var RtcTransportService = class {
4314
4088
  }
4315
4089
  async getDetails({ proxyId }) {
4316
4090
  const transport = this._openTransports.get(proxyId);
4317
- invariant14(transport, void 0, {
4318
- F: __dxlog_file16,
4091
+ invariant12(transport, void 0, {
4092
+ F: __dxlog_file13,
4319
4093
  L: 130,
4320
4094
  S: this,
4321
4095
  A: [
@@ -4329,8 +4103,8 @@ var RtcTransportService = class {
4329
4103
  }
4330
4104
  async getStats({ proxyId }) {
4331
4105
  const transport = this._openTransports.get(proxyId);
4332
- invariant14(transport, void 0, {
4333
- F: __dxlog_file16,
4106
+ invariant12(transport, void 0, {
4107
+ F: __dxlog_file13,
4334
4108
  L: 137,
4335
4109
  S: this,
4336
4110
  A: [
@@ -4344,8 +4118,8 @@ var RtcTransportService = class {
4344
4118
  }
4345
4119
  async sendData({ proxyId, payload }) {
4346
4120
  const transport = this._openTransports.get(proxyId);
4347
- invariant14(transport, void 0, {
4348
- F: __dxlog_file16,
4121
+ invariant12(transport, void 0, {
4122
+ F: __dxlog_file13,
4349
4123
  L: 144,
4350
4124
  S: this,
4351
4125
  A: [
@@ -4376,10 +4150,10 @@ var RtcTransportService = class {
4376
4150
  try {
4377
4151
  await transport.transport.close();
4378
4152
  } catch (error) {
4379
- log15.warn("transport close error", {
4153
+ log12.warn("transport close error", {
4380
4154
  message: error?.message
4381
4155
  }, {
4382
- F: __dxlog_file16,
4156
+ F: __dxlog_file13,
4383
4157
  L: 174,
4384
4158
  S: this,
4385
4159
  C: (f, a) => f(...a)
@@ -4388,17 +4162,17 @@ var RtcTransportService = class {
4388
4162
  try {
4389
4163
  transport.connectorStream.end();
4390
4164
  } catch (error) {
4391
- log15.warn("connectorStream close error", {
4165
+ log12.warn("connectorStream close error", {
4392
4166
  message: error?.message
4393
4167
  }, {
4394
- F: __dxlog_file16,
4168
+ F: __dxlog_file13,
4395
4169
  L: 179,
4396
4170
  S: this,
4397
4171
  C: (f, a) => f(...a)
4398
4172
  });
4399
4173
  }
4400
- log15("closed", void 0, {
4401
- F: __dxlog_file16,
4174
+ log12("closed", void 0, {
4175
+ F: __dxlog_file13,
4402
4176
  L: 181,
4403
4177
  S: this,
4404
4178
  C: (f, a) => f(...a)
@@ -4420,10 +4194,10 @@ var createStateUpdater = (next) => {
4420
4194
 
4421
4195
  // src/wire-protocol.ts
4422
4196
  import { Teleport } from "@dxos/teleport";
4423
- var createTeleportProtocolFactory = (onConnection, defaultParams) => {
4197
+ var createTeleportProtocolFactory = (onConnection, defaultProps) => {
4424
4198
  return (params) => {
4425
4199
  const teleport = new Teleport({
4426
- ...defaultParams,
4200
+ ...defaultProps,
4427
4201
  ...params
4428
4202
  });
4429
4203
  return {
@@ -4445,7 +4219,6 @@ var createTeleportProtocolFactory = (onConnection, defaultParams) => {
4445
4219
  export {
4446
4220
  ConnectionState,
4447
4221
  Connection,
4448
- createIceProvider,
4449
4222
  SwarmMessenger,
4450
4223
  Swarm,
4451
4224
  SwarmMapper,
@@ -4455,8 +4228,6 @@ export {
4455
4228
  ConnectionLog,
4456
4229
  SwarmNetworkManager,
4457
4230
  FullyConnectedTopology,
4458
- MMSTTopology,
4459
- StarTopology,
4460
4231
  MemoryTransportFactory,
4461
4232
  MemoryTransport,
4462
4233
  TransportKind,
@@ -4466,4 +4237,4 @@ export {
4466
4237
  RtcTransportService,
4467
4238
  createTeleportProtocolFactory
4468
4239
  };
4469
- //# sourceMappingURL=chunk-JPPMRZTH.mjs.map
4240
+ //# sourceMappingURL=chunk-OU3FYVBA.mjs.map