@dxos/network-manager 0.1.58-main.75cbd70 → 0.1.58-main.7858df5
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.
- package/dist/lib/browser/{chunk-VP6DWC3F.mjs → chunk-HKHUCTRB.mjs} +205 -114
- package/dist/lib/browser/chunk-HKHUCTRB.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +1 -1
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +146 -13
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/node/index.cjs +493 -402
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +1085 -860
- package/dist/lib/node/testing/index.cjs.map +4 -4
- package/dist/types/src/swarm/connection-limiter.d.ts +1 -1
- package/dist/types/src/swarm/connection-limiter.d.ts.map +1 -1
- package/dist/types/src/swarm/connection.d.ts +3 -1
- package/dist/types/src/swarm/connection.d.ts.map +1 -1
- package/dist/types/src/swarm/peer.d.ts +3 -3
- package/dist/types/src/swarm/peer.d.ts.map +1 -1
- package/dist/types/src/swarm/swarm-mapper.d.ts.map +1 -1
- package/dist/types/src/swarm/swarm.d.ts +2 -2
- package/dist/types/src/swarm/swarm.d.ts.map +1 -1
- package/dist/types/src/testing/test-builder.d.ts +1 -1
- package/dist/types/src/testing/test-builder.d.ts.map +1 -1
- package/dist/types/src/tests/basic-test-suite.d.ts.map +1 -1
- package/dist/types/src/tests/tcp-transport.test.d.ts +2 -0
- package/dist/types/src/tests/tcp-transport.test.d.ts.map +1 -0
- package/dist/types/src/topology/star-topology.d.ts.map +1 -1
- package/dist/types/src/transport/simplepeer-transport-proxy.d.ts.map +1 -1
- package/dist/types/src/transport/simplepeer-transport-service.d.ts.map +1 -1
- package/dist/types/src/transport/simplepeer-transport.d.ts.map +1 -1
- package/dist/types/src/transport/tcp-transport.d.ts +23 -0
- package/dist/types/src/transport/tcp-transport.d.ts.map +1 -0
- package/dist/types/src/transport/transport.d.ts +2 -1
- package/dist/types/src/transport/transport.d.ts.map +1 -1
- package/package.json +17 -18
- package/src/swarm/connection-limiter.ts +2 -2
- package/src/swarm/connection.test.ts +1 -1
- package/src/swarm/connection.ts +53 -27
- package/src/swarm/peer.ts +6 -3
- package/src/swarm/swarm-mapper.ts +4 -7
- package/src/swarm/swarm.test.ts +3 -3
- package/src/swarm/swarm.ts +3 -3
- package/src/testing/test-builder.ts +6 -2
- package/src/tests/basic-test-suite.ts +1 -1
- package/src/tests/memory-transport.test.ts +2 -2
- package/src/tests/tcp-transport.test.ts +67 -0
- package/src/tests/webrtc-transport.test.ts +1 -1
- package/src/topology/star-topology.ts +1 -4
- package/src/transport/simplepeer-transport-proxy.ts +48 -21
- package/src/transport/simplepeer-transport-service.ts +1 -4
- package/src/transport/simplepeer-transport.ts +38 -4
- package/src/transport/tcp-transport.ts +107 -0
- package/src/transport/transport.ts +1 -0
- package/dist/lib/browser/chunk-VP6DWC3F.mjs.map +0 -7
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2021 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { randomBytes } from 'crypto';
|
|
6
|
+
|
|
7
|
+
import { PublicKey } from '@dxos/keys';
|
|
8
|
+
import { test } from '@dxos/test';
|
|
9
|
+
import { range } from '@dxos/util';
|
|
10
|
+
|
|
11
|
+
import { basicTestSuite } from './basic-test-suite';
|
|
12
|
+
import { exchangeMessages, joinSwarm, leaveSwarm, openAndCloseAfterTest } from './utils';
|
|
13
|
+
import { TestBuilder } from '../testing';
|
|
14
|
+
import { FullyConnectedTopology } from '../topology';
|
|
15
|
+
import { TransportKind } from '../transport';
|
|
16
|
+
|
|
17
|
+
describe('Tcp transport', () => {
|
|
18
|
+
const testBuilder = new TestBuilder({
|
|
19
|
+
transport: TransportKind.TCP,
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
basicTestSuite(testBuilder);
|
|
23
|
+
|
|
24
|
+
test
|
|
25
|
+
.skip('load', async () => {
|
|
26
|
+
const NUM_PAIRS = 100;
|
|
27
|
+
const NUM_ROUNDS = 10_000;
|
|
28
|
+
const PACKET_SIZE = 1_000;
|
|
29
|
+
|
|
30
|
+
const pairs = await Promise.all(
|
|
31
|
+
range(NUM_PAIRS).map(async () => {
|
|
32
|
+
const peer1 = testBuilder.createPeer();
|
|
33
|
+
const peer2 = testBuilder.createPeer();
|
|
34
|
+
await openAndCloseAfterTest([peer1, peer2]);
|
|
35
|
+
|
|
36
|
+
const topic = PublicKey.random();
|
|
37
|
+
const [swarm1, swarm2] = await joinSwarm([peer1, peer2], topic, () => new FullyConnectedTopology());
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
peer1,
|
|
41
|
+
peer2,
|
|
42
|
+
topic,
|
|
43
|
+
swarm1,
|
|
44
|
+
swarm2,
|
|
45
|
+
};
|
|
46
|
+
}),
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const message = randomBytes(PACKET_SIZE / 2).toString('hex');
|
|
50
|
+
|
|
51
|
+
for (let i = 0; i < NUM_ROUNDS; i++) {
|
|
52
|
+
// console.log(`Round ${i}/${NUM_ROUNDS}`);
|
|
53
|
+
await Promise.all(
|
|
54
|
+
pairs.map(async ({ swarm1, swarm2 }) => {
|
|
55
|
+
await exchangeMessages(swarm1, swarm2, message);
|
|
56
|
+
}),
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
await Promise.all(
|
|
61
|
+
pairs.map(async ({ peer1, peer2, topic }) => {
|
|
62
|
+
await leaveSwarm([peer1, peer2], topic);
|
|
63
|
+
}),
|
|
64
|
+
);
|
|
65
|
+
})
|
|
66
|
+
.timeout(1_000_000);
|
|
67
|
+
});
|
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
import { describe } from '@dxos/test';
|
|
6
6
|
|
|
7
|
-
import { TEST_SIGNAL_HOSTS, TestBuilder } from '../testing';
|
|
8
7
|
import { basicTestSuite } from './basic-test-suite';
|
|
8
|
+
import { TEST_SIGNAL_HOSTS, TestBuilder } from '../testing';
|
|
9
9
|
|
|
10
10
|
describe('WebRTC transport', () => {
|
|
11
11
|
const testBuilder = new TestBuilder();
|
|
@@ -11,10 +11,7 @@ import { SwarmController, Topology } from './topology';
|
|
|
11
11
|
export class StarTopology implements Topology {
|
|
12
12
|
private _controller?: SwarmController;
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
constructor(
|
|
16
|
-
private readonly _centralPeer: PublicKey
|
|
17
|
-
) {}
|
|
14
|
+
constructor(private readonly _centralPeer: PublicKey) {}
|
|
18
15
|
|
|
19
16
|
toString() {
|
|
20
17
|
return `StarTopology(${this._centralPeer.truncate()})`;
|
|
@@ -11,6 +11,13 @@ import { ErrorStream } from '@dxos/debug';
|
|
|
11
11
|
import { invariant } from '@dxos/invariant';
|
|
12
12
|
import { PublicKey } from '@dxos/keys';
|
|
13
13
|
import { log } from '@dxos/log';
|
|
14
|
+
import {
|
|
15
|
+
ConnectionResetError,
|
|
16
|
+
TimeoutError,
|
|
17
|
+
ProtocolError,
|
|
18
|
+
ConnectivityError,
|
|
19
|
+
UnknownProtocolError,
|
|
20
|
+
} from '@dxos/protocols';
|
|
14
21
|
import { ConnectionState, BridgeEvent, BridgeService } from '@dxos/protocols/proto/dxos/mesh/bridge';
|
|
15
22
|
import { Signal } from '@dxos/protocols/proto/dxos/mesh/swarm';
|
|
16
23
|
import { arrayToBuffer } from '@dxos/util';
|
|
@@ -36,13 +43,10 @@ export class SimplePeerTransportProxy implements Transport {
|
|
|
36
43
|
private _closed = false;
|
|
37
44
|
private _serviceStream!: Stream<BridgeEvent>;
|
|
38
45
|
|
|
39
|
-
|
|
40
|
-
constructor(
|
|
41
|
-
private readonly _params: SimplePeerTransportProxyParams
|
|
42
|
-
) {
|
|
46
|
+
constructor(private readonly _params: SimplePeerTransportProxyParams) {
|
|
43
47
|
this._serviceStream = this._params.bridgeService.open({
|
|
44
48
|
proxyId: this._proxyId,
|
|
45
|
-
initiator: this._params.initiator
|
|
49
|
+
initiator: this._params.initiator,
|
|
46
50
|
});
|
|
47
51
|
|
|
48
52
|
this._serviceStream.waitUntilReady().then(
|
|
@@ -58,27 +62,31 @@ export class SimplePeerTransportProxy implements Transport {
|
|
|
58
62
|
}
|
|
59
63
|
});
|
|
60
64
|
|
|
61
|
-
this._params.stream.pipe(
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
65
|
+
this._params.stream.pipe(
|
|
66
|
+
new Writable({
|
|
67
|
+
write: (chunk, _, callback) => {
|
|
68
|
+
this._params.bridgeService
|
|
69
|
+
.sendData({
|
|
70
|
+
proxyId: this._proxyId,
|
|
71
|
+
payload: chunk,
|
|
72
|
+
})
|
|
73
|
+
.then(
|
|
74
|
+
() => callback(),
|
|
75
|
+
(err: any) => {
|
|
76
|
+
log.catch(err);
|
|
77
|
+
},
|
|
78
|
+
);
|
|
79
|
+
},
|
|
80
|
+
}),
|
|
81
|
+
);
|
|
74
82
|
},
|
|
75
|
-
(error) => log.catch(error)
|
|
83
|
+
(error) => log.catch(error),
|
|
76
84
|
);
|
|
77
85
|
}
|
|
78
86
|
|
|
79
87
|
private async _handleConnection(connectionEvent: BridgeEvent.ConnectionEvent): Promise<void> {
|
|
80
88
|
if (connectionEvent.error) {
|
|
81
|
-
this.errors.raise(
|
|
89
|
+
this.errors.raise(decodeError(connectionEvent.error));
|
|
82
90
|
}
|
|
83
91
|
|
|
84
92
|
switch (connectionEvent.state) {
|
|
@@ -108,7 +116,7 @@ export class SimplePeerTransportProxy implements Transport {
|
|
|
108
116
|
proxyId: this._proxyId,
|
|
109
117
|
signal,
|
|
110
118
|
})
|
|
111
|
-
.catch((err) => this.errors.raise(err));
|
|
119
|
+
.catch((err) => this.errors.raise(decodeError(err)));
|
|
112
120
|
}
|
|
113
121
|
|
|
114
122
|
// TODO(burdon): Move open from constructor.
|
|
@@ -173,3 +181,22 @@ export class SimplePeerTransportProxyFactory implements TransportFactory {
|
|
|
173
181
|
return transport;
|
|
174
182
|
}
|
|
175
183
|
}
|
|
184
|
+
|
|
185
|
+
// TODO(nf): fix so Errors crossing RPC boundary preserve class
|
|
186
|
+
const decodeError = (err: Error | string) => {
|
|
187
|
+
const message = typeof err === 'string' ? err : err.message;
|
|
188
|
+
|
|
189
|
+
if (message.includes('CONNECTION_RESET')) {
|
|
190
|
+
return new ConnectionResetError(message);
|
|
191
|
+
} else if (message.includes('TIMEOUT')) {
|
|
192
|
+
return new TimeoutError(message);
|
|
193
|
+
} else if (message.includes('PROTOCOL_ERROR')) {
|
|
194
|
+
return new ProtocolError(message);
|
|
195
|
+
} else if (message.includes('CONNECTIVITY_ERROR')) {
|
|
196
|
+
return new ConnectivityError(message);
|
|
197
|
+
} else if (message.includes('UNKNOWN_PROTOCOL_ERROR')) {
|
|
198
|
+
return new UnknownProtocolError(message);
|
|
199
|
+
} else {
|
|
200
|
+
return typeof err === 'string' ? new Error(err) : err;
|
|
201
|
+
}
|
|
202
|
+
};
|
|
@@ -30,10 +30,7 @@ type TransportState = {
|
|
|
30
30
|
export class SimplePeerTransportService implements BridgeService {
|
|
31
31
|
private readonly transports = new ComplexMap<PublicKey, TransportState>(PublicKey.hash);
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
constructor(
|
|
35
|
-
private readonly _webrtcConfig?: any
|
|
36
|
-
) { }
|
|
33
|
+
constructor(private readonly _webrtcConfig?: any) {}
|
|
37
34
|
|
|
38
35
|
open(request: ConnectionRequest): Stream<BridgeEvent> {
|
|
39
36
|
const rpcStream: Stream<BridgeEvent> = new Stream(({ ready, next, close }) => {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable no-fallthrough */
|
|
1
2
|
//
|
|
2
3
|
// Copyright 2020 DXOS.org
|
|
3
4
|
//
|
|
@@ -9,7 +10,7 @@ import { Event } from '@dxos/async';
|
|
|
9
10
|
import { ErrorStream, raise } from '@dxos/debug';
|
|
10
11
|
import { PublicKey } from '@dxos/keys';
|
|
11
12
|
import { log } from '@dxos/log';
|
|
12
|
-
import { trace } from '@dxos/protocols';
|
|
13
|
+
import { ConnectionResetError, ConnectivityError, ProtocolError, UnknownProtocolError, trace } from '@dxos/protocols';
|
|
13
14
|
import { Signal } from '@dxos/protocols/proto/dxos/mesh/swarm';
|
|
14
15
|
|
|
15
16
|
import { Transport, TransportFactory } from './transport';
|
|
@@ -63,11 +64,42 @@ export class SimplePeerTransport implements Transport {
|
|
|
63
64
|
});
|
|
64
65
|
|
|
65
66
|
this._peer.on('error', async (err) => {
|
|
66
|
-
|
|
67
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/RTCError
|
|
68
|
+
if (typeof RTCError !== 'undefined' && err instanceof RTCError) {
|
|
69
|
+
// Sent when connection is unexpectedly severed
|
|
70
|
+
if (err.errorDetail === 'sctp-failure') {
|
|
71
|
+
this.errors.raise(new ConnectionResetError('sctp-failure from RTCError', err));
|
|
72
|
+
} else {
|
|
73
|
+
this.errors.raise(new UnknownProtocolError('unknown RTCError', err));
|
|
74
|
+
}
|
|
75
|
+
// catch more generic simple-peer errors: https://github.com/feross/simple-peer/blob/master/README.md#error-codes
|
|
76
|
+
} else if ('code' in err) {
|
|
77
|
+
log.info('simple-peer error', err);
|
|
78
|
+
switch (err.code) {
|
|
79
|
+
case 'ERR_WEBRTC_SUPPORT':
|
|
80
|
+
this.errors.raise(new ProtocolError('WebRTC not supported', err));
|
|
81
|
+
case 'ERR_ICE_CONNECTION_FAILURE':
|
|
82
|
+
case 'ERR_DATA_CHANNEL':
|
|
83
|
+
case 'ERR_CONNECTION_FAILURE':
|
|
84
|
+
case 'ERR_SIGNALING':
|
|
85
|
+
this.errors.raise(new ConnectivityError('unknown communication failure', err));
|
|
86
|
+
case 'ERR_CREATE_OFFER':
|
|
87
|
+
case 'ERR_CREATE_ANSWER':
|
|
88
|
+
case 'ERR_SET_LOCAL_DESCRIPTION':
|
|
89
|
+
case 'ERR_SET_REMOTE_DESCRIPTION':
|
|
90
|
+
case 'ERR_ADD_ICE_CANDIDATE':
|
|
91
|
+
this.errors.raise(new UnknownProtocolError('unknown simple-peer library failure', err));
|
|
92
|
+
default:
|
|
93
|
+
this.errors.raise(new Error('unknown simple-peer error'));
|
|
94
|
+
}
|
|
95
|
+
} else {
|
|
96
|
+
log.info('unknown peer connection error', err);
|
|
97
|
+
this.errors.raise(err);
|
|
98
|
+
}
|
|
67
99
|
|
|
68
100
|
// Try to gather additional information about the connection.
|
|
69
101
|
try {
|
|
70
|
-
if (typeof (this._peer as any)?._pc
|
|
102
|
+
if (typeof (this._peer as any)?._pc?.getStats === 'function') {
|
|
71
103
|
(this._peer as any)._pc.getStats().then((stats: any) => {
|
|
72
104
|
log.warn('report after webrtc error', {
|
|
73
105
|
config: this.params.webrtcConfig,
|
|
@@ -75,7 +107,9 @@ export class SimplePeerTransport implements Transport {
|
|
|
75
107
|
});
|
|
76
108
|
});
|
|
77
109
|
}
|
|
78
|
-
} catch
|
|
110
|
+
} catch (err) {
|
|
111
|
+
log.catch(err);
|
|
112
|
+
}
|
|
79
113
|
|
|
80
114
|
await this.destroy();
|
|
81
115
|
});
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2020 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { AddressInfo, Socket, type Server } from 'node:net';
|
|
6
|
+
|
|
7
|
+
import { Event } from '@dxos/async';
|
|
8
|
+
import { ErrorStream } from '@dxos/debug';
|
|
9
|
+
import { log } from '@dxos/log';
|
|
10
|
+
import { Signal } from '@dxos/protocols/proto/dxos/mesh/swarm';
|
|
11
|
+
|
|
12
|
+
import { Transport, TransportFactory, TransportOptions } from './transport';
|
|
13
|
+
|
|
14
|
+
export const TcpTransportFactory: TransportFactory = {
|
|
15
|
+
createTransport: (params) => new TcpTransport(params),
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Fake transport.
|
|
20
|
+
*/
|
|
21
|
+
export class TcpTransport implements Transport {
|
|
22
|
+
private _server?: Server = undefined;
|
|
23
|
+
private _socket?: Socket = undefined;
|
|
24
|
+
|
|
25
|
+
public readonly closed = new Event<void>();
|
|
26
|
+
public readonly connected = new Event<void>();
|
|
27
|
+
public readonly errors = new ErrorStream();
|
|
28
|
+
|
|
29
|
+
private _destroyed = false;
|
|
30
|
+
|
|
31
|
+
private _connected = false;
|
|
32
|
+
|
|
33
|
+
constructor(private readonly options: TransportOptions) {
|
|
34
|
+
log('creating');
|
|
35
|
+
|
|
36
|
+
// Initiator will send a signal, the receiver will receive the unique ID and connect the streams.
|
|
37
|
+
if (this.options.initiator) {
|
|
38
|
+
// prettier-ignore
|
|
39
|
+
setTimeout(async () => {
|
|
40
|
+
const { Server } = await import('node:net');
|
|
41
|
+
this._server = new Server(socket => {
|
|
42
|
+
log('new connection');
|
|
43
|
+
if (this._connected) {
|
|
44
|
+
socket.destroy();
|
|
45
|
+
}
|
|
46
|
+
this._handleSocket(socket);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
this._server.on('listening', () => {
|
|
50
|
+
const { port } = this._server!.address() as AddressInfo;
|
|
51
|
+
log('listening', { port });
|
|
52
|
+
void this.options.sendSignal({
|
|
53
|
+
payload: { port }
|
|
54
|
+
}).catch(err => {
|
|
55
|
+
if (!this._destroyed) {
|
|
56
|
+
this.errors.raise(err);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
this._server.on('error', err => {
|
|
63
|
+
this.errors.raise(err);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
this._server.listen(0);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async destroy(): Promise<void> {
|
|
72
|
+
log('closing');
|
|
73
|
+
this._socket?.destroy();
|
|
74
|
+
this._server?.close();
|
|
75
|
+
|
|
76
|
+
this._destroyed = true;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
signal({ payload }: Signal) {
|
|
80
|
+
log('received signal', { payload });
|
|
81
|
+
if (this.options.initiator || this._connected) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const socket = new Socket();
|
|
86
|
+
this._handleSocket(socket);
|
|
87
|
+
socket.connect({ port: payload.port, host: 'localhost' });
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private _handleSocket(socket: Socket) {
|
|
91
|
+
log('handling socket', { remotePort: socket.remotePort, localPort: socket.localPort });
|
|
92
|
+
this._socket = socket;
|
|
93
|
+
this.connected.emit();
|
|
94
|
+
this.options.stream.pipe(this._socket!).pipe(this.options.stream);
|
|
95
|
+
|
|
96
|
+
this._socket.on('connect', () => {
|
|
97
|
+
log('connected to', { port: this._socket?.remotePort });
|
|
98
|
+
this._connected = true;
|
|
99
|
+
});
|
|
100
|
+
this._socket.on('error', (err) => {
|
|
101
|
+
this.errors.raise(err);
|
|
102
|
+
});
|
|
103
|
+
this._socket.on('close', () => {
|
|
104
|
+
this.closed.emit();
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|