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

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