@dxos/network-manager 0.8.4-main.c4373fc → 0.8.4-main.cb12b3f963

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