@dxos/network-manager 0.5.1-main.f02b2c7 → 0.5.1-main.f0ff320

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.
@@ -15,7 +15,7 @@ import {
15
15
  } from "@dxos/node-std/inject-globals";
16
16
 
17
17
  // packages/core/mesh/network-manager/src/swarm/connection.ts
18
- import { DeferredTask, Event, sleep, scheduleTask, scheduleTaskInterval, synchronized } from "@dxos/async";
18
+ import { DeferredTask, Event, sleep, scheduleTask, scheduleTaskInterval, synchronized, Trigger } from "@dxos/async";
19
19
  import { Context, cancelWithContext } from "@dxos/context";
20
20
  import { ErrorStream } from "@dxos/debug";
21
21
  import { invariant } from "@dxos/invariant";
@@ -61,6 +61,8 @@ var Connection = class {
61
61
  this._callbacks = _callbacks;
62
62
  this._ctx = new Context();
63
63
  this.connectedTimeoutContext = new Context();
64
+ this._protocolClosed = new Trigger();
65
+ this._transportClosed = new Trigger();
64
66
  this._state = "CREATED";
65
67
  this._incomingSignalBuffer = [];
66
68
  this._outgoingSignalBuffer = [];
@@ -80,7 +82,7 @@ var Connection = class {
80
82
  initiator: this.initiator
81
83
  }, {
82
84
  F: __dxlog_file,
83
- L: 134,
85
+ L: 137,
84
86
  S: this,
85
87
  C: (f, a) => f(...a)
86
88
  });
@@ -103,7 +105,7 @@ var Connection = class {
103
105
  async openConnection() {
104
106
  invariant(this._state === "INITIAL", "Invalid state.", {
105
107
  F: __dxlog_file,
106
- L: 164,
108
+ L: 167,
107
109
  S: this,
108
110
  A: [
109
111
  "this._state === ConnectionState.INITIAL",
@@ -114,7 +116,7 @@ var Connection = class {
114
116
  id: this._instanceId
115
117
  }), {
116
118
  F: __dxlog_file,
117
- L: 165,
119
+ L: 168,
118
120
  S: this,
119
121
  C: (f, a) => f(...a)
120
122
  });
@@ -126,7 +128,7 @@ var Connection = class {
126
128
  initiator: this.initiator
127
129
  }, {
128
130
  F: __dxlog_file,
129
- L: 166,
131
+ L: 169,
130
132
  S: this,
131
133
  C: (f, a) => f(...a)
132
134
  });
@@ -137,16 +139,17 @@ var Connection = class {
137
139
  this._protocol.stream.on("close", () => {
138
140
  log("protocol stream closed", void 0, {
139
141
  F: __dxlog_file,
140
- L: 183,
142
+ L: 186,
141
143
  S: this,
142
144
  C: (f, a) => f(...a)
143
145
  });
146
+ this._protocolClosed.wake();
144
147
  this.close(new ProtocolError("protocol stream closed")).catch((err) => this.errors.raise(err));
145
148
  });
146
149
  scheduleTask(this.connectedTimeoutContext, async () => {
147
150
  log.info(`timeout waiting ${TRANSPORT_CONNECTION_TIMEOUT / 1e3}s for transport to connect, aborting`, void 0, {
148
151
  F: __dxlog_file,
149
- L: 190,
152
+ L: 194,
150
153
  S: this,
151
154
  C: (f, a) => f(...a)
152
155
  });
@@ -154,7 +157,7 @@ var Connection = class {
154
157
  }, TRANSPORT_CONNECTION_TIMEOUT);
155
158
  invariant(!this._transport, void 0, {
156
159
  F: __dxlog_file,
157
- L: 198,
160
+ L: 202,
158
161
  S: this,
159
162
  A: [
160
163
  "!this._transport",
@@ -176,9 +179,10 @@ var Connection = class {
176
179
  });
177
180
  this._transport.closed.once(() => {
178
181
  this._transport = void 0;
182
+ this._transportClosed.wake();
179
183
  log("abort triggered by transport close", void 0, {
180
184
  F: __dxlog_file,
181
- L: 218,
185
+ L: 223,
182
186
  S: this,
183
187
  C: (f, a) => f(...a)
184
188
  });
@@ -189,7 +193,7 @@ var Connection = class {
189
193
  err
190
194
  }, {
191
195
  F: __dxlog_file,
192
- L: 223,
196
+ L: 228,
193
197
  S: this,
194
198
  C: (f, a) => f(...a)
195
199
  });
@@ -199,7 +203,7 @@ var Connection = class {
199
203
  if (err instanceof ConnectionResetError) {
200
204
  log.info("aborting due to transport ConnectionResetError", void 0, {
201
205
  F: __dxlog_file,
202
- L: 230,
206
+ L: 235,
203
207
  S: this,
204
208
  C: (f, a) => f(...a)
205
209
  });
@@ -207,7 +211,7 @@ var Connection = class {
207
211
  } else if (err instanceof ConnectivityError) {
208
212
  log.info("aborting due to transport ConnectivityError", void 0, {
209
213
  F: __dxlog_file,
210
- L: 233,
214
+ L: 238,
211
215
  S: this,
212
216
  C: (f, a) => f(...a)
213
217
  });
@@ -217,7 +221,7 @@ var Connection = class {
217
221
  err
218
222
  }, {
219
223
  F: __dxlog_file,
220
- L: 236,
224
+ L: 241,
221
225
  S: this,
222
226
  C: (f, a) => f(...a)
223
227
  });
@@ -235,7 +239,7 @@ var Connection = class {
235
239
  id: this._instanceId
236
240
  }), {
237
241
  F: __dxlog_file,
238
- L: 252,
242
+ L: 257,
239
243
  S: this,
240
244
  C: (f, a) => f(...a)
241
245
  });
@@ -245,14 +249,14 @@ var Connection = class {
245
249
  err
246
250
  }, {
247
251
  F: __dxlog_file,
248
- L: 259,
252
+ L: 264,
249
253
  S: this,
250
254
  C: (f, a) => f(...a)
251
255
  });
252
256
  if (this._state === "CLOSED" || this._state === "ABORTED") {
253
257
  log(`abort ignored: already ${this._state}`, this.closeReason, {
254
258
  F: __dxlog_file,
255
- L: 261,
259
+ L: 266,
256
260
  S: this,
257
261
  C: (f, a) => f(...a)
258
262
  });
@@ -265,27 +269,21 @@ var Connection = class {
265
269
  }
266
270
  await this._ctx.dispose();
267
271
  try {
268
- log("aborting protocol... ", void 0, {
269
- F: __dxlog_file,
270
- L: 275,
271
- S: this,
272
- C: (f, a) => f(...a)
273
- });
274
- await this._protocol.abort();
272
+ await this._closeProtocol();
275
273
  } catch (err2) {
276
274
  log.catch(err2, void 0, {
277
275
  F: __dxlog_file,
278
- L: 278,
276
+ L: 282,
279
277
  S: this,
280
278
  C: (f, a) => f(...a)
281
279
  });
282
280
  }
283
281
  try {
284
- await this._transport?.close();
282
+ await this._closeTransport();
285
283
  } catch (err2) {
286
284
  log.catch(err2, void 0, {
287
285
  F: __dxlog_file,
288
- L: 285,
286
+ L: 289,
289
287
  S: this,
290
288
  C: (f, a) => f(...a)
291
289
  });
@@ -295,7 +293,7 @@ var Connection = class {
295
293
  } catch (err2) {
296
294
  log.catch(err2, void 0, {
297
295
  F: __dxlog_file,
298
- L: 291,
296
+ L: 295,
299
297
  S: this,
300
298
  C: (f, a) => f(...a)
301
299
  });
@@ -319,27 +317,27 @@ var Connection = class {
319
317
  peerId: this.ownId
320
318
  }, {
321
319
  F: __dxlog_file,
322
- L: 316,
320
+ L: 320,
323
321
  S: this,
324
322
  C: (f, a) => f(...a)
325
323
  });
326
324
  if (lastState === "CONNECTED") {
327
325
  try {
328
- await this._protocol.close();
326
+ await this._closeProtocol();
329
327
  } catch (err2) {
330
328
  log.catch(err2, void 0, {
331
329
  F: __dxlog_file,
332
- L: 323,
330
+ L: 327,
333
331
  S: this,
334
332
  C: (f, a) => f(...a)
335
333
  });
336
334
  }
337
335
  try {
338
- await this._transport?.close();
336
+ await this._closeTransport();
339
337
  } catch (err2) {
340
338
  log.catch(err2, void 0, {
341
339
  F: __dxlog_file,
342
- L: 330,
340
+ L: 334,
343
341
  S: this,
344
342
  C: (f, a) => f(...a)
345
343
  });
@@ -347,26 +345,26 @@ var Connection = class {
347
345
  } else {
348
346
  log(`graceful close requested when we were in ${lastState} state? aborting`, void 0, {
349
347
  F: __dxlog_file,
350
- L: 333,
348
+ L: 337,
351
349
  S: this,
352
350
  C: (f, a) => f(...a)
353
351
  });
354
352
  try {
355
- await this._protocol.abort();
353
+ await this._closeProtocol();
356
354
  } catch (err2) {
357
355
  log.catch(err2, void 0, {
358
356
  F: __dxlog_file,
359
- L: 337,
357
+ L: 341,
360
358
  S: this,
361
359
  C: (f, a) => f(...a)
362
360
  });
363
361
  }
364
362
  try {
365
- await this._transport?.close();
363
+ await this._closeTransport();
366
364
  } catch (err2) {
367
365
  log.catch(err2, void 0, {
368
366
  F: __dxlog_file,
369
- L: 342,
367
+ L: 346,
370
368
  S: this,
371
369
  C: (f, a) => f(...a)
372
370
  });
@@ -376,13 +374,49 @@ var Connection = class {
376
374
  peerId: this.ownId
377
375
  }, {
378
376
  F: __dxlog_file,
379
- L: 346,
377
+ L: 350,
380
378
  S: this,
381
379
  C: (f, a) => f(...a)
382
380
  });
383
381
  this._changeState("CLOSED");
384
382
  this._callbacks?.onClosed?.(err);
385
383
  }
384
+ async _closeProtocol() {
385
+ log("closing protocol", void 0, {
386
+ F: __dxlog_file,
387
+ L: 356,
388
+ S: this,
389
+ C: (f, a) => f(...a)
390
+ });
391
+ await Promise.race([
392
+ this._protocol.close(),
393
+ this._protocolClosed.wait()
394
+ ]);
395
+ log("protocol closed", void 0, {
396
+ F: __dxlog_file,
397
+ L: 358,
398
+ S: this,
399
+ C: (f, a) => f(...a)
400
+ });
401
+ }
402
+ async _closeTransport() {
403
+ log("closing transport", void 0, {
404
+ F: __dxlog_file,
405
+ L: 362,
406
+ S: this,
407
+ C: (f, a) => f(...a)
408
+ });
409
+ await Promise.race([
410
+ this._transport?.close(),
411
+ this._transportClosed.wait()
412
+ ]);
413
+ log("transport closed", void 0, {
414
+ F: __dxlog_file,
415
+ L: 364,
416
+ S: this,
417
+ C: (f, a) => f(...a)
418
+ });
419
+ }
386
420
  _sendSignal(signal) {
387
421
  this._outgoingSignalBuffer.push(signal);
388
422
  this._signalSendTask.schedule();
@@ -419,7 +453,7 @@ var Connection = class {
419
453
  err
420
454
  }, {
421
455
  F: __dxlog_file,
422
- L: 384,
456
+ L: 400,
423
457
  S: this,
424
458
  C: (f, a) => f(...a)
425
459
  });
@@ -432,7 +466,7 @@ var Connection = class {
432
466
  async signal(msg) {
433
467
  invariant(msg.sessionId, void 0, {
434
468
  F: __dxlog_file,
435
- L: 393,
469
+ L: 409,
436
470
  S: this,
437
471
  A: [
438
472
  "msg.sessionId",
@@ -442,7 +476,7 @@ var Connection = class {
442
476
  if (!msg.sessionId.equals(this.sessionId)) {
443
477
  log("dropping signal for incorrect session id", void 0, {
444
478
  F: __dxlog_file,
445
- L: 395,
479
+ L: 411,
446
480
  S: this,
447
481
  C: (f, a) => f(...a)
448
482
  });
@@ -450,7 +484,7 @@ var Connection = class {
450
484
  }
451
485
  invariant(msg.data.signal || msg.data.signalBatch, void 0, {
452
486
  F: __dxlog_file,
453
- L: 398,
487
+ L: 414,
454
488
  S: this,
455
489
  A: [
456
490
  "msg.data.signal || msg.data.signalBatch",
@@ -459,7 +493,7 @@ var Connection = class {
459
493
  });
460
494
  invariant(msg.author?.equals(this.remoteId), void 0, {
461
495
  F: __dxlog_file,
462
- L: 399,
496
+ L: 415,
463
497
  S: this,
464
498
  A: [
465
499
  "msg.author?.equals(this.remoteId)",
@@ -468,7 +502,7 @@ var Connection = class {
468
502
  });
469
503
  invariant(msg.recipient?.equals(this.ownId), void 0, {
470
504
  F: __dxlog_file,
471
- L: 400,
505
+ L: 416,
472
506
  S: this,
473
507
  A: [
474
508
  "msg.recipient?.equals(this.ownId)",
@@ -492,7 +526,7 @@ var Connection = class {
492
526
  msg: msg.data
493
527
  }, {
494
528
  F: __dxlog_file,
495
- L: 409,
529
+ L: 425,
496
530
  S: this,
497
531
  C: (f, a) => f(...a)
498
532
  });
@@ -500,7 +534,7 @@ var Connection = class {
500
534
  } else {
501
535
  invariant(this._transport, "Connection not ready to accept signals.", {
502
536
  F: __dxlog_file,
503
- L: 412,
537
+ L: 428,
504
538
  S: this,
505
539
  A: [
506
540
  "this._transport",
@@ -513,7 +547,7 @@ var Connection = class {
513
547
  msg: msg.data
514
548
  }, {
515
549
  F: __dxlog_file,
516
- L: 413,
550
+ L: 429,
517
551
  S: this,
518
552
  C: (f, a) => f(...a)
519
553
  });
@@ -531,13 +565,13 @@ var Connection = class {
531
565
  peerId: this.ownId
532
566
  }, {
533
567
  F: __dxlog_file,
534
- L: 424,
568
+ L: 440,
535
569
  S: this,
536
570
  C: (f, a) => f(...a)
537
571
  });
538
572
  invariant(state !== this._state, "Already in this state.", {
539
573
  F: __dxlog_file,
540
- L: 425,
574
+ L: 441,
541
575
  S: this,
542
576
  A: [
543
577
  "state !== this._state",
@@ -965,9 +999,9 @@ var Peer = class {
965
999
  });
966
1000
  const sessionId = PublicKey3.random();
967
1001
  log3("initiating...", {
968
- id: this.id,
1002
+ ownPeerId: this.localPeerId,
969
1003
  topic: this.topic,
970
- peerId: this.id,
1004
+ remotePeerId: this.id,
971
1005
  sessionId
972
1006
  }, {
973
1007
  F: __dxlog_file3,
@@ -1695,7 +1729,8 @@ var Swarm = class {
1695
1729
  getState: () => ({
1696
1730
  ownPeerId: this._ownPeerId,
1697
1731
  connected: Array.from(this._peers.values()).filter((peer) => peer.connection).map((peer) => peer.id),
1698
- candidates: Array.from(this._peers.values()).filter((peer) => !peer.connection && peer.advertizing && peer.availableToConnect).map((peer) => peer.id)
1732
+ candidates: Array.from(this._peers.values()).filter((peer) => !peer.connection && peer.advertizing && peer.availableToConnect).map((peer) => peer.id),
1733
+ allPeers: Array.from(this._peers.values()).map((peer) => peer.id)
1699
1734
  }),
1700
1735
  connect: (peer) => {
1701
1736
  if (this._ctx.disposed) {
@@ -1707,7 +1742,7 @@ var Swarm = class {
1707
1742
  } catch (err) {
1708
1743
  log4("initiation error", err, {
1709
1744
  F: __dxlog_file4,
1710
- L: 334,
1745
+ L: 335,
1711
1746
  S: this,
1712
1747
  C: (f, a) => f(...a)
1713
1748
  });
@@ -1730,12 +1765,13 @@ var Swarm = class {
1730
1765
  */
1731
1766
  async _initiateConnection(remoteId) {
1732
1767
  const ctx = this._ctx;
1768
+ const peer = this._getOrCreatePeer(remoteId);
1733
1769
  if (remoteId.toHex() < this._ownPeerId.toHex()) {
1734
1770
  log4("initiation delay", {
1735
1771
  remoteId
1736
1772
  }, {
1737
1773
  F: __dxlog_file4,
1738
- L: 361,
1774
+ L: 363,
1739
1775
  S: this,
1740
1776
  C: (f, a) => f(...a)
1741
1777
  });
@@ -1744,7 +1780,9 @@ var Swarm = class {
1744
1780
  if (ctx.disposed) {
1745
1781
  return;
1746
1782
  }
1747
- const peer = this._getOrCreatePeer(remoteId);
1783
+ if (this._peers.get(remoteId) == null) {
1784
+ throw new Error("Peer left during initiation delay");
1785
+ }
1748
1786
  if (peer.connection) {
1749
1787
  return;
1750
1788
  }
@@ -1752,7 +1790,7 @@ var Swarm = class {
1752
1790
  remoteId
1753
1791
  }, {
1754
1792
  F: __dxlog_file4,
1755
- L: 375,
1793
+ L: 379,
1756
1794
  S: this,
1757
1795
  C: (f, a) => f(...a)
1758
1796
  });
@@ -1762,7 +1800,7 @@ var Swarm = class {
1762
1800
  remoteId
1763
1801
  }, {
1764
1802
  F: __dxlog_file4,
1765
- L: 378,
1803
+ L: 382,
1766
1804
  S: this,
1767
1805
  C: (f, a) => f(...a)
1768
1806
  });
@@ -2592,7 +2630,7 @@ var StarTopology = class {
2592
2630
 
2593
2631
  // packages/core/mesh/network-manager/src/transport/memory-transport.ts
2594
2632
  import { Transform } from "@dxos/node-std/stream";
2595
- import { Event as Event7, Trigger } from "@dxos/async";
2633
+ import { Event as Event7, Trigger as Trigger2 } from "@dxos/async";
2596
2634
  import { ErrorStream as ErrorStream3 } from "@dxos/debug";
2597
2635
  import { invariant as invariant10 } from "@dxos/invariant";
2598
2636
  import { PublicKey as PublicKey9 } from "@dxos/keys";
@@ -2629,7 +2667,7 @@ var MemoryTransport = class _MemoryTransport {
2629
2667
  constructor(_options) {
2630
2668
  this._options = _options;
2631
2669
  this._instanceId = PublicKey9.random();
2632
- this._remote = new Trigger();
2670
+ this._remote = new Trigger2();
2633
2671
  this._outgoingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
2634
2672
  this._incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
2635
2673
  this._closed = false;
@@ -3476,7 +3514,7 @@ var decodeError = (err) => {
3476
3514
 
3477
3515
  // packages/core/mesh/network-manager/src/transport/libdatachannel-transport.ts
3478
3516
  import { Duplex as Duplex2 } from "stream";
3479
- import { Event as Event10, Trigger as Trigger2, synchronized as synchronized4 } from "@dxos/async";
3517
+ import { Event as Event10, Trigger as Trigger3, synchronized as synchronized4 } from "@dxos/async";
3480
3518
  import { ErrorStream as ErrorStream6 } from "@dxos/debug";
3481
3519
  import { invariant as invariant14 } from "@dxos/invariant";
3482
3520
  import { log as log14 } from "@dxos/log";
@@ -3509,7 +3547,7 @@ var LibDataChannelTransport = class _LibDataChannelTransport {
3509
3547
  this._closed = false;
3510
3548
  this._connected = false;
3511
3549
  this._writeCallback = null;
3512
- this._readyForCandidates = new Trigger2();
3550
+ this._readyForCandidates = new Trigger3();
3513
3551
  this.closed = new Event10();
3514
3552
  this.connected = new Event10();
3515
3553
  this.errors = new ErrorStream6();
@@ -3954,9 +3992,12 @@ var TcpTransport = class {
3954
3992
 
3955
3993
  // packages/core/mesh/network-manager/src/wire-protocol.ts
3956
3994
  import { Teleport } from "@dxos/teleport";
3957
- var createTeleportProtocolFactory = (onConnection) => {
3995
+ var createTeleportProtocolFactory = (onConnection, defaultParams) => {
3958
3996
  return (params) => {
3959
- const teleport = new Teleport(params);
3997
+ const teleport = new Teleport({
3998
+ ...defaultParams,
3999
+ ...params
4000
+ });
3960
4001
  return {
3961
4002
  stream: teleport.stream,
3962
4003
  open: async (sessionId) => {
@@ -4002,4 +4043,4 @@ export {
4002
4043
  TcpTransport,
4003
4044
  createTeleportProtocolFactory
4004
4045
  };
4005
- //# sourceMappingURL=chunk-TVNVOM33.mjs.map
4046
+ //# sourceMappingURL=chunk-66SM4LDV.mjs.map