@dxos/network-manager 0.8.4-main.937b3ca → 0.8.4-main.9be5663bfe

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 (61) hide show
  1. package/dist/lib/browser/{chunk-AQSYW43M.mjs → chunk-Y2FDDNGM.mjs} +185 -190
  2. package/dist/lib/browser/chunk-Y2FDDNGM.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +1 -1
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/browser/testing/index.mjs +14 -4
  6. package/dist/lib/browser/testing/index.mjs.map +3 -3
  7. package/dist/lib/node-esm/{chunk-SCX3PZRE.mjs → chunk-OU3FYVBA.mjs} +185 -190
  8. package/dist/lib/node-esm/chunk-OU3FYVBA.mjs.map +7 -0
  9. package/dist/lib/node-esm/index.mjs +1 -1
  10. package/dist/lib/node-esm/meta.json +1 -1
  11. package/dist/lib/node-esm/testing/index.mjs +14 -4
  12. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  13. package/dist/types/src/network-manager.d.ts +5 -4
  14. package/dist/types/src/network-manager.d.ts.map +1 -1
  15. package/dist/types/src/signal/signal-connection.d.ts +3 -2
  16. package/dist/types/src/signal/signal-connection.d.ts.map +1 -1
  17. package/dist/types/src/signal/signal-messenger.d.ts +3 -2
  18. package/dist/types/src/signal/signal-messenger.d.ts.map +1 -1
  19. package/dist/types/src/signal/swarm-messenger.d.ts +7 -7
  20. package/dist/types/src/signal/swarm-messenger.d.ts.map +1 -1
  21. package/dist/types/src/swarm/connection.d.ts +2 -1
  22. package/dist/types/src/swarm/connection.d.ts.map +1 -1
  23. package/dist/types/src/swarm/peer.d.ts +4 -3
  24. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  25. package/dist/types/src/swarm/swarm.d.ts +3 -2
  26. package/dist/types/src/swarm/swarm.d.ts.map +1 -1
  27. package/dist/types/src/testing/test-builder.d.ts.map +1 -1
  28. package/dist/types/src/tests/basic-test-suite.d.ts.map +1 -1
  29. package/dist/types/src/tests/property-test-suite.d.ts.map +1 -1
  30. package/dist/types/src/transport/webrtc/rtc-peer-connection.d.ts.map +1 -1
  31. package/dist/types/src/transport/webrtc/rtc-transport-channel.d.ts.map +1 -1
  32. package/dist/types/src/transport/webrtc/rtc-transport-factory.d.ts.map +1 -1
  33. package/dist/types/src/transport/webrtc/rtc-transport-service.d.ts.map +1 -1
  34. package/dist/types/tsconfig.tsbuildinfo +1 -1
  35. package/package.json +18 -18
  36. package/src/network-manager.ts +19 -15
  37. package/src/signal/integration.node.test.ts +11 -10
  38. package/src/signal/signal-connection.ts +3 -2
  39. package/src/signal/signal-messenger.ts +3 -2
  40. package/src/signal/swarm-messenger.node.test.ts +25 -24
  41. package/src/signal/swarm-messenger.ts +65 -55
  42. package/src/swarm/connection.test.ts +6 -7
  43. package/src/swarm/connection.ts +5 -5
  44. package/src/swarm/peer.ts +5 -6
  45. package/src/swarm/swarm.test.ts +2 -3
  46. package/src/swarm/swarm.ts +9 -10
  47. package/src/testing/test-builder.ts +4 -4
  48. package/src/tests/basic-test-suite.ts +0 -1
  49. package/src/tests/memory-transport.test.ts +0 -2
  50. package/src/tests/property-test-suite.ts +4 -3
  51. package/src/tests/tcp-transport.node.test.ts +0 -2
  52. package/src/tests/webrtc-transport.test.ts +0 -1
  53. package/src/transport/webrtc/rtc-peer-connection.ts +0 -1
  54. package/src/transport/webrtc/rtc-transport-channel.test.ts +0 -2
  55. package/src/transport/webrtc/rtc-transport-channel.ts +0 -1
  56. package/src/transport/webrtc/rtc-transport-factory.ts +0 -1
  57. package/src/transport/webrtc/rtc-transport-proxy.test.ts +0 -2
  58. package/src/transport/webrtc/rtc-transport-service.ts +0 -1
  59. package/src/transport/webrtc/rtc-transport.test.ts +0 -1
  60. package/dist/lib/browser/chunk-AQSYW43M.mjs.map +0 -7
  61. package/dist/lib/node-esm/chunk-SCX3PZRE.mjs.map +0 -7
@@ -118,7 +118,7 @@ export class Connection {
118
118
  public readonly transportStats = new Event<TransportStats>();
119
119
 
120
120
  private readonly _signalSendTask = new DeferredTask(this._ctx, async () => {
121
- await this._flushSignalBuffer();
121
+ await this._flushSignalBuffer(this._ctx);
122
122
  });
123
123
 
124
124
  private _signallingDelay = STARTING_SIGNALLING_DELAY;
@@ -366,21 +366,21 @@ export class Connection {
366
366
  this._signalSendTask.schedule();
367
367
  }
368
368
 
369
- private async _flushSignalBuffer(): Promise<void> {
369
+ private async _flushSignalBuffer(ctx: Context): Promise<void> {
370
370
  if (this._outgoingSignalBuffer.length === 0) {
371
371
  return;
372
372
  }
373
373
 
374
374
  try {
375
375
  if (process.env.NODE_ENV !== 'test') {
376
- await cancelWithContext(this._ctx, sleep(this._signallingDelay));
376
+ await cancelWithContext(ctx, sleep(this._signallingDelay));
377
377
  this._signallingDelay = Math.min(this._signallingDelay * 2, MAX_SIGNALLING_DELAY);
378
378
  }
379
379
 
380
380
  const signals = [...this._outgoingSignalBuffer];
381
381
  this._outgoingSignalBuffer.length = 0;
382
382
 
383
- await this._signalMessaging.signal({
383
+ await this._signalMessaging.signal(ctx, {
384
384
  author: this.localInfo,
385
385
  recipient: this.remoteInfo,
386
386
  sessionId: this.sessionId,
@@ -406,7 +406,7 @@ export class Connection {
406
406
  /**
407
407
  * Receive a signal from the remote peer.
408
408
  */
409
- async signal(msg: SignalMessage): Promise<void> {
409
+ async signal(_ctx: Context, msg: SignalMessage): Promise<void> {
410
410
  invariant(msg.sessionId);
411
411
  if (!msg.sessionId.equals(this.sessionId)) {
412
412
  log('dropping signal for incorrect session id');
package/src/swarm/peer.ts CHANGED
@@ -14,7 +14,6 @@ import { type Answer } from '@dxos/protocols/proto/dxos/mesh/swarm';
14
14
  import { type OfferMessage, type SignalMessage, type SignalMessenger } from '../signal';
15
15
  import { type TransportFactory } from '../transport';
16
16
  import { type WireProtocolProvider } from '../wire-protocol';
17
-
18
17
  import { Connection, ConnectionState } from './connection';
19
18
  import { type ConnectionLimiter } from './connection-limiter';
20
19
 
@@ -106,7 +105,7 @@ export class Peer {
106
105
  /**
107
106
  * Respond to remote offer.
108
107
  */
109
- async onOffer(message: OfferMessage): Promise<Answer> {
108
+ async onOffer(_ctx: Context, message: OfferMessage): Promise<Answer> {
110
109
  const remote = message.author;
111
110
 
112
111
  if (
@@ -169,7 +168,7 @@ export class Peer {
169
168
  /**
170
169
  * Initiate a connection to the remote peer.
171
170
  */
172
- async initiateConnection(): Promise<void> {
171
+ async initiateConnection(ctx: Context): Promise<void> {
173
172
  invariant(!this.initiating, 'Initiation in progress.');
174
173
  invariant(!this.connection, 'Already connected.');
175
174
  const sessionId = PublicKey.random();
@@ -183,7 +182,7 @@ export class Peer {
183
182
  await this._connectionLimiter.connecting(sessionId);
184
183
  connection.initiate();
185
184
 
186
- answer = await this._signalMessaging.offer({
185
+ answer = await this._signalMessaging.offer(ctx, {
187
186
  author: this.localInfo,
188
187
  recipient: this.remoteInfo,
189
188
  sessionId,
@@ -378,13 +377,13 @@ export class Peer {
378
377
  log('closed', { peerId: this.remoteInfo, sessionId: connection.sessionId });
379
378
  }
380
379
 
381
- async onSignal(message: SignalMessage): Promise<void> {
380
+ async onSignal(ctx: Context, message: SignalMessage): Promise<void> {
382
381
  if (!this.connection) {
383
382
  log('dropping signal message for non-existent connection', { message });
384
383
  return;
385
384
  }
386
385
 
387
- await this.connection.signal(message);
386
+ await this.connection.signal(ctx, message);
388
387
  }
389
388
 
390
389
  @synchronized
@@ -18,7 +18,6 @@ import { ComplexSet } from '@dxos/util';
18
18
  import { TestWireProtocol } from '../testing/test-wire-protocol';
19
19
  import { FullyConnectedTopology } from '../topology';
20
20
  import { createRtcTransportFactory } from '../transport';
21
-
22
21
  import { ConnectionState } from './connection';
23
22
  import { ConnectionLimiter } from './connection-limiter';
24
23
  import { Swarm } from './swarm';
@@ -159,9 +158,9 @@ describe.skip('Swarm', () => {
159
158
  const messages = new ComplexSet<{ author: PeerInfo; recipient: PeerInfo }>(
160
159
  ({ author, recipient }) => author.peerKey + recipient.peerKey,
161
160
  );
162
- signalManager.sendMessage = async (message) => {
161
+ signalManager.sendMessage = async (ctx, message) => {
163
162
  messages.add({ author: message.author, recipient: message.recipient });
164
- return sendOriginal(message);
163
+ return sendOriginal(ctx, message);
165
164
  };
166
165
  // Stop signaling to stop connection in initiation state.
167
166
  signalManager.freeze();
@@ -18,7 +18,6 @@ import { type SwarmController, type Topology } from '../topology';
18
18
  import { type TransportFactory } from '../transport';
19
19
  import { type Topic } from '../types';
20
20
  import { type WireProtocolProvider } from '../wire-protocol';
21
-
22
21
  import { type Connection, ConnectionState } from './connection';
23
22
  import { type ConnectionLimiter } from './connection-limiter';
24
23
  import { Peer } from './peer';
@@ -94,9 +93,9 @@ export class Swarm {
94
93
  _topology.init(this._getSwarmController());
95
94
 
96
95
  this._swarmMessenger = new SwarmMessenger({
97
- sendMessage: async (msg) => await this._messenger.sendMessage(msg),
98
- onSignal: async (msg) => await this.onSignal(msg),
99
- onOffer: async (msg) => await this.onOffer(msg),
96
+ sendMessage: async (ctx, msg) => await this._messenger.sendMessage(ctx, msg),
97
+ onSignal: async (ctx, msg) => await this.onSignal(ctx, msg),
98
+ onOffer: async (ctx, msg) => await this.onOffer(ctx, msg),
100
99
  topic: this._topic,
101
100
  });
102
101
  log.trace('dxos.mesh.swarm.constructor', trace.end({ id: this._instanceId }));
@@ -136,7 +135,7 @@ export class Swarm {
136
135
  payloadType: 'dxos.mesh.swarm.SwarmMessage',
137
136
  onMessage: async (message) => {
138
137
  await this._swarmMessenger
139
- .receiveMessage(message)
138
+ .receiveMessage(this._ctx, message)
140
139
  // TODO(nf): discriminate between errors
141
140
  .catch((err) => log.info('Error while receiving message', { err }));
142
141
  },
@@ -205,7 +204,7 @@ export class Swarm {
205
204
  }
206
205
 
207
206
  @synchronized
208
- async onOffer(message: OfferMessage): Promise<Answer> {
207
+ async onOffer(ctx: Context, message: OfferMessage): Promise<Answer> {
209
208
  log('offer', { message });
210
209
  if (this._ctx.disposed) {
211
210
  log('ignored for disposed swarm');
@@ -224,7 +223,7 @@ export class Swarm {
224
223
  }
225
224
 
226
225
  const peer = this._getOfferSenderPeer(message.author);
227
- const answer = await peer.onOffer(message);
226
+ const answer = await peer.onOffer(ctx, message);
228
227
  this._topology.update();
229
228
  return answer;
230
229
  }
@@ -243,7 +242,7 @@ export class Swarm {
243
242
  return peer;
244
243
  }
245
244
 
246
- async onSignal(message: SignalMessage): Promise<void> {
245
+ async onSignal(ctx: Context, message: SignalMessage): Promise<void> {
247
246
  log('signal', { message });
248
247
  if (this._ctx.disposed) {
249
248
  log.info('ignored for offline swarm');
@@ -257,7 +256,7 @@ export class Swarm {
257
256
  invariant(message.author);
258
257
 
259
258
  const peer = this._getOrCreatePeer(message.author);
260
- await peer.onSignal(message);
259
+ await peer.onSignal(ctx, message);
261
260
  }
262
261
 
263
262
  // For debug purposes
@@ -404,7 +403,7 @@ export class Swarm {
404
403
  }
405
404
 
406
405
  log('initiating connection...', { remotePeer });
407
- await peer.initiateConnection();
406
+ await peer.initiateConnection(ctx);
408
407
  this._topology.update();
409
408
  log('initiated', { remotePeer });
410
409
  }
@@ -2,6 +2,7 @@
2
2
  // Copyright 2022 DXOS.org
3
3
  //
4
4
 
5
+ import { Context } from '@dxos/context';
5
6
  import { PublicKey } from '@dxos/keys';
6
7
  import { log } from '@dxos/log';
7
8
  import {
@@ -28,7 +29,6 @@ import {
28
29
  TransportKind,
29
30
  createRtcTransportFactory,
30
31
  } from '../transport';
31
-
32
32
  import { type TestTeleportExtensionFactory, TestWireProtocol } from './test-wire-protocol';
33
33
 
34
34
  // Signal server will be started by the setup script.
@@ -169,7 +169,7 @@ export class TestPeer {
169
169
 
170
170
  await this._proxy?.close();
171
171
  await this._service?.close();
172
- await this._networkManager.close();
172
+ await this._networkManager.close(Context.default());
173
173
  }
174
174
 
175
175
  getSwarm(topic: PublicKey): TestSwarmConnection {
@@ -218,7 +218,7 @@ export class TestSwarmConnection {
218
218
  // TODO(burdon): Need to create new plugin instance per swarm?
219
219
  // If so, then perhaps joinSwarm should return swarm object with access to plugins.
220
220
  async join(topology = new FullyConnectedTopology()): Promise<this> {
221
- await this.peer._networkManager.joinSwarm({
221
+ await this.peer._networkManager.joinSwarm(Context.default(), {
222
222
  topic: this.topic,
223
223
  peerInfo: { peerKey: this.peer.peerId.toHex(), identityKey: this.peer.peerId.toHex() },
224
224
  protocolProvider: this.protocol.factory,
@@ -229,7 +229,7 @@ export class TestSwarmConnection {
229
229
  }
230
230
 
231
231
  async leave(): Promise<this> {
232
- await this.peer._networkManager.leaveSwarm(this.topic);
232
+ await this.peer._networkManager.leaveSwarm(Context.default(), this.topic);
233
233
  return this;
234
234
  }
235
235
  }
@@ -11,7 +11,6 @@ import { range } from '@dxos/util';
11
11
 
12
12
  import { type TestBuilder } from '../testing';
13
13
  import { FullyConnectedTopology, StarTopology } from '../topology';
14
-
15
14
  import { exchangeMessages, joinSwarm, leaveSwarm, openAndCloseAfterTest } from './utils';
16
15
 
17
16
  // TODO(burdon): Use PublicKey throughout (remove conversion to strings, from buffers, etc.)
@@ -3,7 +3,6 @@
3
3
  //
4
4
 
5
5
  import { randomBytes } from 'node:crypto';
6
-
7
6
  import { describe, test } from 'vitest';
8
7
 
9
8
  import { PublicKey } from '@dxos/keys';
@@ -11,7 +10,6 @@ import { range } from '@dxos/util';
11
10
 
12
11
  import { TestBuilder } from '../testing';
13
12
  import { FullyConnectedTopology } from '../topology';
14
-
15
13
  import { basicTestSuite } from './basic-test-suite';
16
14
  import { exchangeMessages, joinSwarm, leaveSwarm, openAndCloseAfterTest } from './utils';
17
15
 
@@ -6,6 +6,7 @@ import * as fc from 'fast-check';
6
6
  import { type ModelRunSetup } from 'fast-check';
7
7
  import { test } from 'vitest';
8
8
 
9
+ import { Context } from '@dxos/context';
9
10
  import { todo } from '@dxos/debug';
10
11
  import { PublicKey } from '@dxos/keys';
11
12
  import { ComplexMap, ComplexSet, range } from '@dxos/util';
@@ -97,7 +98,7 @@ export const propertyTestSuite = () => {
97
98
  model.joinedPeers.delete(this.peerId);
98
99
 
99
100
  const peer = real.peers.get(this.peerId);
100
- await peer!.networkManager.close();
101
+ await peer!.networkManager.close(Context.default());
101
102
  real.peers.delete(this.peerId);
102
103
 
103
104
  await assertState(model, real);
@@ -119,7 +120,7 @@ export const propertyTestSuite = () => {
119
120
  // afterTest(() => presence.stop());
120
121
  // const protocol = createProtocolFactory(model.topic, this.peerId, [presence]);
121
122
 
122
- await peer.networkManager.joinSwarm({
123
+ await peer.networkManager.joinSwarm(Context.default(), {
123
124
  peerInfo: {
124
125
  peerKey: this.peerId.toHex(),
125
126
  identityKey: this.peerId.toHex(),
@@ -146,7 +147,7 @@ export const propertyTestSuite = () => {
146
147
  model.joinedPeers.delete(this.peerId);
147
148
 
148
149
  const peer = real.peers.get(this.peerId)!;
149
- await peer.networkManager.leaveSwarm(model.topic);
150
+ await peer.networkManager.leaveSwarm(Context.default(), model.topic);
150
151
  peer.presence = undefined;
151
152
 
152
153
  await assertState(model, real);
@@ -3,7 +3,6 @@
3
3
  //
4
4
 
5
5
  import { randomBytes } from 'node:crypto';
6
-
7
6
  import { describe, test } from 'vitest';
8
7
 
9
8
  import { PublicKey } from '@dxos/keys';
@@ -12,7 +11,6 @@ import { range } from '@dxos/util';
12
11
  import { TestBuilder } from '../testing';
13
12
  import { FullyConnectedTopology } from '../topology';
14
13
  import { TransportKind } from '../transport';
15
-
16
14
  import { basicTestSuite } from './basic-test-suite';
17
15
  import { exchangeMessages, joinSwarm, leaveSwarm, openAndCloseAfterTest } from './utils';
18
16
 
@@ -5,7 +5,6 @@
5
5
  import { describe } from 'vitest';
6
6
 
7
7
  import { TEST_SIGNAL_HOSTS, TestBuilder } from '../testing';
8
-
9
8
  import { basicTestSuite } from './basic-test-suite';
10
9
 
11
10
  // Segfault in node-datachannel.
@@ -11,7 +11,6 @@ import { trace } from '@dxos/tracing';
11
11
 
12
12
  import type { IceProvider } from '../../signal';
13
13
  import { type TransportOptions } from '../transport';
14
-
15
14
  import { type RtcConnectionFactory } from './rtc-connection-factory';
16
15
  import { RtcTransportChannel } from './rtc-transport-channel';
17
16
  import { areSdpEqual, chooseInitiatorPeer } from './utils';
@@ -3,13 +3,11 @@
3
3
  //
4
4
 
5
5
  import { Duplex } from 'node:stream';
6
-
7
6
  import { describe, expect, test } from 'vitest';
8
7
 
9
8
  import { sleep } from '@dxos/async';
10
9
 
11
10
  import { type TransportOptions } from '../transport';
12
-
13
11
  import { type RtcPeerConnection } from './rtc-peer-connection';
14
12
  import { RtcTransportChannel } from './rtc-transport-channel';
15
13
  import { handleChannelErrors } from './test-utils';
@@ -13,7 +13,6 @@ import { ConnectivityError } from '@dxos/protocols';
13
13
  import { type Signal } from '@dxos/protocols/proto/dxos/mesh/swarm';
14
14
 
15
15
  import { type Transport, type TransportOptions, type TransportStats } from '../transport';
16
-
17
16
  import { type RtcPeerConnection } from './rtc-peer-connection';
18
17
  import { createRtcTransportStats, describeSelectedRemoteCandidate } from './rtc-transport-stats';
19
18
 
@@ -4,7 +4,6 @@
4
4
 
5
5
  import type { IceProvider } from '../../signal';
6
6
  import type { TransportFactory } from '../transport';
7
-
8
7
  import { getRtcConnectionFactory } from './rtc-connection-factory';
9
8
  import { RtcPeerConnection } from './rtc-peer-connection';
10
9
 
@@ -3,7 +3,6 @@
3
3
  //
4
4
 
5
5
  import { Duplex } from 'stream';
6
-
7
6
  import { describe, expect, onTestFinished, test } from 'vitest';
8
7
 
9
8
  import { Event as AsyncEvent, TestStream, Trigger, sleep } from '@dxos/async';
@@ -14,7 +13,6 @@ import { type BridgeService } from '@dxos/protocols/proto/dxos/mesh/bridge';
14
13
  import { type RpcPort, createLinkedPorts, createProtoRpcPeer } from '@dxos/rpc';
15
14
 
16
15
  import { type Transport, type TransportFactory, type TransportOptions, type TransportStats } from '../transport';
17
-
18
16
  import { RtcTransportProxy } from './rtc-transport-proxy';
19
17
  import { RtcTransportService } from './rtc-transport-service';
20
18
  import { handleChannelErrors } from './test-utils';
@@ -25,7 +25,6 @@ import { ComplexMap } from '@dxos/util';
25
25
 
26
26
  import { type IceProvider } from '../../signal';
27
27
  import { type Transport, type TransportFactory } from '../transport';
28
-
29
28
  import { createRtcTransportFactory } from './rtc-transport-factory';
30
29
 
31
30
  type TransportState = {
@@ -8,7 +8,6 @@ import { TestStream, sleep } from '@dxos/async';
8
8
  import { PublicKey } from '@dxos/keys';
9
9
 
10
10
  import { type TransportOptions } from '../transport';
11
-
12
11
  import { getRtcConnectionFactory } from './rtc-connection-factory';
13
12
  import { RtcPeerConnection } from './rtc-peer-connection';
14
13
  import { type RtcTransportChannel } from './rtc-transport-channel';