@dxos/network-manager 0.1.58-main.ffbb8a1 → 0.1.58-next.6d0af18
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-3KLH55NB.mjs} +273 -125
- package/dist/lib/browser/chunk-3KLH55NB.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +5 -1
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +9 -5
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node/index.cjs +824 -564
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +1124 -864
- 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/index.d.ts +1 -0
- package/dist/types/src/transport/index.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.browser.d.ts +12 -0
- package/dist/types/src/transport/tcp-transport.browser.d.ts.map +1 -0
- 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 +18 -18
- package/src/swarm/connection-limiter.ts +2 -2
- package/src/swarm/connection.test.ts +1 -1
- package/src/swarm/connection.ts +54 -27
- package/src/swarm/peer.ts +11 -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/index.ts +1 -0
- package/src/transport/simplepeer-transport-proxy.ts +48 -21
- package/src/transport/simplepeer-transport-service.ts +9 -5
- package/src/transport/simplepeer-transport.ts +43 -4
- package/src/transport/tcp-transport.browser.ts +28 -0
- 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
|
@@ -16,6 +16,7 @@ import { Runtime } from '@dxos/protocols/proto/dxos/config';
|
|
|
16
16
|
import { createLinkedPorts, createProtoRpcPeer, ProtoRpcPeer } from '@dxos/rpc';
|
|
17
17
|
import { ComplexMap } from '@dxos/util';
|
|
18
18
|
|
|
19
|
+
import { TestWireProtocol, type TestTeleportExtensionFactory } from './test-wire-protocol';
|
|
19
20
|
import { NetworkManager } from '../network-manager';
|
|
20
21
|
import { FullyConnectedTopology } from '../topology';
|
|
21
22
|
import {
|
|
@@ -27,7 +28,7 @@ import {
|
|
|
27
28
|
createLibDataChannelTransportFactory,
|
|
28
29
|
TransportKind,
|
|
29
30
|
} from '../transport';
|
|
30
|
-
import {
|
|
31
|
+
import { TcpTransportFactory } from '../transport/tcp-transport';
|
|
31
32
|
|
|
32
33
|
// Signal server will be started by the setup script.
|
|
33
34
|
const port = process.env.SIGNAL_PORT ?? 4000;
|
|
@@ -105,6 +106,9 @@ export class TestPeer {
|
|
|
105
106
|
switch (transport) {
|
|
106
107
|
case TransportKind.MEMORY:
|
|
107
108
|
throw new Error('Memory transport not supported with signal server.');
|
|
109
|
+
case TransportKind.TCP:
|
|
110
|
+
transportFactory = TcpTransportFactory;
|
|
111
|
+
break;
|
|
108
112
|
case TransportKind.SIMPLE_PEER:
|
|
109
113
|
transportFactory = createSimplePeerTransportFactory();
|
|
110
114
|
break;
|
|
@@ -146,7 +150,7 @@ export class TestPeer {
|
|
|
146
150
|
throw new Error(`Unsupported transport: ${transport}`);
|
|
147
151
|
}
|
|
148
152
|
} else {
|
|
149
|
-
if (transport !== TransportKind.MEMORY) {
|
|
153
|
+
if (transport !== TransportKind.MEMORY && transport !== TransportKind.TCP) {
|
|
150
154
|
log.warn(`specified transport ${transport} but no signalling configured, using memory transport instead`);
|
|
151
155
|
}
|
|
152
156
|
log.info(`using ${transport} transport without signal server.`);
|
|
@@ -11,9 +11,9 @@ import { log } from '@dxos/log';
|
|
|
11
11
|
import { afterTest, test } from '@dxos/test';
|
|
12
12
|
import { range } from '@dxos/util';
|
|
13
13
|
|
|
14
|
+
import { exchangeMessages, joinSwarm, leaveSwarm, openAndCloseAfterTest } from './utils';
|
|
14
15
|
import { TestBuilder } from '../testing';
|
|
15
16
|
import { FullyConnectedTopology, StarTopology } from '../topology';
|
|
16
|
-
import { exchangeMessages, joinSwarm, leaveSwarm, openAndCloseAfterTest } from './utils';
|
|
17
17
|
|
|
18
18
|
// TODO(burdon): Use PublicKey throughout (remove conversion to strings, from buffers, etc.)
|
|
19
19
|
|
|
@@ -8,10 +8,10 @@ import { PublicKey } from '@dxos/keys';
|
|
|
8
8
|
import { test } from '@dxos/test';
|
|
9
9
|
import { range } from '@dxos/util';
|
|
10
10
|
|
|
11
|
-
import { TestBuilder } from '../testing';
|
|
12
|
-
import { FullyConnectedTopology } from '../topology';
|
|
13
11
|
import { basicTestSuite } from './basic-test-suite';
|
|
14
12
|
import { exchangeMessages, joinSwarm, leaveSwarm, openAndCloseAfterTest } from './utils';
|
|
13
|
+
import { TestBuilder } from '../testing';
|
|
14
|
+
import { FullyConnectedTopology } from '../topology';
|
|
15
15
|
|
|
16
16
|
describe('Memory transport', () => {
|
|
17
17
|
const testBuilder = new TestBuilder();
|
|
@@ -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()})`;
|
package/src/transport/index.ts
CHANGED
|
@@ -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
|
+
};
|
|
@@ -25,15 +25,13 @@ type TransportState = {
|
|
|
25
25
|
transport: SimplePeerTransport;
|
|
26
26
|
stream: Duplex;
|
|
27
27
|
writeCallbacks: (() => void)[];
|
|
28
|
+
state: 'OPEN' | 'CLOSED';
|
|
28
29
|
};
|
|
29
30
|
|
|
30
31
|
export class SimplePeerTransportService implements BridgeService {
|
|
31
32
|
private readonly transports = new ComplexMap<PublicKey, TransportState>(PublicKey.hash);
|
|
32
33
|
|
|
33
|
-
|
|
34
|
-
constructor(
|
|
35
|
-
private readonly _webrtcConfig?: any
|
|
36
|
-
) { }
|
|
34
|
+
constructor(private readonly _webrtcConfig?: any) {}
|
|
37
35
|
|
|
38
36
|
open(request: ConnectionRequest): Stream<BridgeEvent> {
|
|
39
37
|
const rpcStream: Stream<BridgeEvent> = new Stream(({ ready, next, close }) => {
|
|
@@ -99,6 +97,7 @@ export class SimplePeerTransportService implements BridgeService {
|
|
|
99
97
|
transport,
|
|
100
98
|
stream: duplex,
|
|
101
99
|
writeCallbacks: [],
|
|
100
|
+
state: 'OPEN',
|
|
102
101
|
};
|
|
103
102
|
|
|
104
103
|
ready();
|
|
@@ -115,6 +114,9 @@ export class SimplePeerTransportService implements BridgeService {
|
|
|
115
114
|
}
|
|
116
115
|
|
|
117
116
|
async sendData({ proxyId, payload }: DataRequest): Promise<void> {
|
|
117
|
+
if (this.transports.get(proxyId)?.state !== 'OPEN') {
|
|
118
|
+
log.debug('transport is closed');
|
|
119
|
+
}
|
|
118
120
|
invariant(this.transports.has(proxyId));
|
|
119
121
|
const state = this.transports.get(proxyId)!;
|
|
120
122
|
const bufferHasSpace = state.stream.push(payload);
|
|
@@ -128,7 +130,9 @@ export class SimplePeerTransportService implements BridgeService {
|
|
|
128
130
|
async close({ proxyId }: CloseRequest) {
|
|
129
131
|
await this.transports.get(proxyId)?.transport.destroy();
|
|
130
132
|
await this.transports.get(proxyId)?.stream.end();
|
|
131
|
-
this.transports.
|
|
133
|
+
if (this.transports.get(proxyId)) {
|
|
134
|
+
this.transports.get(proxyId)!.state = 'CLOSED';
|
|
135
|
+
}
|
|
132
136
|
log('Closed.');
|
|
133
137
|
}
|
|
134
138
|
}
|
|
@@ -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,47 @@ 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
|
+
break;
|
|
82
|
+
case 'ERR_ICE_CONNECTION_FAILURE':
|
|
83
|
+
case 'ERR_DATA_CHANNEL':
|
|
84
|
+
case 'ERR_CONNECTION_FAILURE':
|
|
85
|
+
case 'ERR_SIGNALING':
|
|
86
|
+
this.errors.raise(new ConnectivityError('unknown communication failure', err));
|
|
87
|
+
break;
|
|
88
|
+
// errors due to library issues or improper API usage
|
|
89
|
+
case 'ERR_CREATE_OFFER':
|
|
90
|
+
case 'ERR_CREATE_ANSWER':
|
|
91
|
+
case 'ERR_SET_LOCAL_DESCRIPTION':
|
|
92
|
+
case 'ERR_SET_REMOTE_DESCRIPTION':
|
|
93
|
+
case 'ERR_ADD_ICE_CANDIDATE':
|
|
94
|
+
this.errors.raise(new UnknownProtocolError('unknown simple-peer library failure', err));
|
|
95
|
+
break;
|
|
96
|
+
default:
|
|
97
|
+
this.errors.raise(new Error('unknown simple-peer error'));
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
} else {
|
|
101
|
+
log.info('unknown peer connection error', err);
|
|
102
|
+
this.errors.raise(err);
|
|
103
|
+
}
|
|
67
104
|
|
|
68
105
|
// Try to gather additional information about the connection.
|
|
69
106
|
try {
|
|
70
|
-
if (typeof (this._peer as any)?._pc
|
|
107
|
+
if (typeof (this._peer as any)?._pc?.getStats === 'function') {
|
|
71
108
|
(this._peer as any)._pc.getStats().then((stats: any) => {
|
|
72
109
|
log.warn('report after webrtc error', {
|
|
73
110
|
config: this.params.webrtcConfig,
|
|
@@ -75,7 +112,9 @@ export class SimplePeerTransport implements Transport {
|
|
|
75
112
|
});
|
|
76
113
|
});
|
|
77
114
|
}
|
|
78
|
-
} catch
|
|
115
|
+
} catch (err) {
|
|
116
|
+
log.catch(err);
|
|
117
|
+
}
|
|
79
118
|
|
|
80
119
|
await this.destroy();
|
|
81
120
|
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2020 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Event } from '@dxos/async';
|
|
6
|
+
import { ErrorStream } from '@dxos/debug';
|
|
7
|
+
|
|
8
|
+
import { Transport, TransportFactory } from './transport';
|
|
9
|
+
|
|
10
|
+
// NOTE: Browser stub.
|
|
11
|
+
|
|
12
|
+
export const TcpTransportFactory: TransportFactory = {
|
|
13
|
+
createTransport: () => new TcpTransport(),
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export class TcpTransport implements Transport {
|
|
17
|
+
public readonly closed = new Event<void>();
|
|
18
|
+
public readonly connected = new Event<void>();
|
|
19
|
+
public readonly errors = new ErrorStream();
|
|
20
|
+
|
|
21
|
+
async destroy() {
|
|
22
|
+
throw new Error('Method not implemented.');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
signal() {
|
|
26
|
+
throw new Error('Method not implemented.');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -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
|
+
}
|