@dxos/network-manager 0.1.53 → 0.1.54-main.270ec56
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-6CP5XM3F.mjs → chunk-HDCPX2MD.mjs} +512 -499
- package/dist/lib/browser/chunk-HDCPX2MD.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +2 -2
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +34 -11
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node/index.cjs +510 -497
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +488 -452
- package/dist/lib/node/testing/index.cjs.map +3 -3
- package/dist/types/src/signal/swarm-messenger.d.ts.map +1 -1
- package/dist/types/src/testing/test-wire-protocol.d.ts +4 -1
- package/dist/types/src/testing/test-wire-protocol.d.ts.map +1 -1
- package/dist/types/src/transport/memory-transport.d.ts.map +1 -1
- package/package.json +19 -18
- package/src/network-manager.ts +5 -5
- package/src/signal/swarm-messenger.ts +11 -9
- package/src/swarm/connection.ts +9 -9
- package/src/swarm/peer.ts +6 -6
- package/src/swarm/swarm.ts +8 -8
- package/src/testing/test-wire-protocol.ts +32 -1
- package/src/topology/fully-connected-topology.ts +3 -3
- package/src/topology/mmst-topology.ts +5 -5
- package/src/topology/star-topology.ts +4 -4
- package/src/transport/memory-transport.ts +3 -6
- package/src/transport/webrtc-transport-proxy.ts +2 -2
- package/src/transport/webrtc-transport-service.ts +3 -3
- package/src/transport/webrtc-transport.ts +2 -2
- package/dist/lib/browser/chunk-6CP5XM3F.mjs.map +0 -7
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"swarm-messenger.d.ts","sourceRoot":"","sources":["../../../../src/signal/swarm-messenger.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,GAAG,EAAE,MAAM,sBAAsB,CAAC;AAE3C,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CAAC,IAAI,CAAC,CAAC;IACpD,KAAK,EAAE,SAAS,CAAC;CAClB,CAAC;AAIF;;GAEG;AACH,qBAAa,cAAe,YAAW,eAAe;IACpD,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAiB;IACtC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA4C;IACtE,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAoF;IAEjH,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA6C;IACtE,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAY;IAEnC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAA4E;gBAE9F,EAAE,WAAW,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,qBAAqB;IAOtE,cAAc,CAAC,EACnB,MAAM,EACN,SAAS,EACT,OAAO,GACR,EAAE;QACD,MAAM,EAAE,SAAS,CAAC;QAClB,SAAS,EAAE,SAAS,CAAC;QACrB,OAAO,EAAE,GAAG,CAAC;KACd,GAAG,OAAO,CAAC,IAAI,CAAC;IA2BX,MAAM,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAS7C,KAAK,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC;YAerC,oBAAoB;YA0BpB,eAAe;YAWf,YAAY;YAiCZ,aAAa;CAuB5B"}
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import { Event } from '@dxos/async';
|
|
2
2
|
import { PublicKey } from '@dxos/keys';
|
|
3
|
-
import { TestExtension } from '@dxos/teleport';
|
|
3
|
+
import { TestExtension, TestExtensionWithStreams, type TestStreamStats } from '@dxos/teleport';
|
|
4
4
|
import { ComplexMap } from '@dxos/util';
|
|
5
5
|
export declare class TestWireProtocol {
|
|
6
6
|
readonly peerId: PublicKey;
|
|
7
7
|
readonly connections: ComplexMap<PublicKey, TestExtension>;
|
|
8
|
+
readonly streamConnections: ComplexMap<PublicKey, TestExtensionWithStreams>;
|
|
8
9
|
readonly connected: Event<PublicKey>;
|
|
9
10
|
readonly disconnected: Event<PublicKey>;
|
|
10
11
|
constructor(peerId: PublicKey);
|
|
11
12
|
readonly factory: import("../wire-protocol").WireProtocolProvider;
|
|
12
13
|
waitForConnection(peerId: PublicKey): Promise<TestExtension>;
|
|
13
14
|
testConnection(peerId: PublicKey, message?: string): Promise<void>;
|
|
15
|
+
startStream(peerId: PublicKey, streamTag: string, streamLoadInterval: number, streamLoadChunkSize: number): Promise<string>;
|
|
16
|
+
closeStream(peerId: PublicKey, streamTag: string): Promise<TestStreamStats>;
|
|
14
17
|
}
|
|
15
18
|
//# sourceMappingURL=test-wire-protocol.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"test-wire-protocol.d.ts","sourceRoot":"","sources":["../../../../src/testing/test-wire-protocol.ts"],"names":[],"mappings":"AAIA,OAAO,EAAgB,KAAK,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC/
|
|
1
|
+
{"version":3,"file":"test-wire-protocol.d.ts","sourceRoot":"","sources":["../../../../src/testing/test-wire-protocol.ts"],"names":[],"mappings":"AAIA,OAAO,EAAgB,KAAK,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAAE,aAAa,EAAE,wBAAwB,EAAE,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAC/F,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAIxC,qBAAa,gBAAgB;aAOC,MAAM,EAAE,SAAS;IAN7C,SAAgB,WAAW,uCAA4D;IACvF,SAAgB,iBAAiB,kDAAuE;IAExG,SAAgB,SAAS,mBAA0B;IACnD,SAAgB,YAAY,mBAA0B;gBAE1B,MAAM,EAAE,SAAS;IAE7C,QAAQ,CAAC,OAA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,CAAC,iBAAiB,CAAC,CAAkB;gBAEf,OAAO,EAAE,gBAAgB;
|
|
1
|
+
{"version":3,"file":"memory-transport.d.ts","sourceRoot":"","sources":["../../../../src/transport/memory-transport.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,KAAK,EAAW,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAG1C,OAAO,EAAE,MAAM,EAAE,MAAM,uCAAuC,CAAC;AAG/D,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAkB5E,eAAO,MAAM,sBAAsB,EAAE,gBAEpC,CAAC;AAEF;;GAEG;AACH,qBAAa,eAAgB,YAAW,SAAS;IAuBnC,OAAO,CAAC,QAAQ,CAAC,OAAO;IArBpC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,YAAY,CAA8D;IAElG,SAAgB,MAAM,cAAqB;IAC3C,SAAgB,SAAS,cAAqB;IAC9C,SAAgB,MAAM,cAAqB;IAG3C,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAsB;IAElD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA4B;IAEpD,OAAO,CAAC,QAAQ,CAAC,cAAc,CAA6C;IAC5E,OAAO,CAAC,QAAQ,CAAC,cAAc,CAA6C;IAE5E,OAAO,CAAC,UAAU,CAAS;IAG3B,OAAO,CAAC,iBAAiB,CAAa;IAEtC,OAAO,CAAC,iBAAiB,CAAC,CAAkB;gBAEf,OAAO,EAAE,gBAAgB;IA4DhD,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IA+B9B,MAAM,CAAC,EAAE,OAAO,EAAE,EAAE,MAAM;CAY3B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/network-manager",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.54-main.270ec56",
|
|
4
4
|
"description": "Network Manager",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -23,33 +23,34 @@
|
|
|
23
23
|
"isomorphic-ws": "^4.0.1",
|
|
24
24
|
"nanomessage-rpc": "^3.0.0",
|
|
25
25
|
"simple-peer": "9.11.1",
|
|
26
|
+
"tiny-invariant": "^1.3.1",
|
|
26
27
|
"ws": "^7.4.4",
|
|
27
28
|
"xor-distance": "^2.0.0",
|
|
28
|
-
"@dxos/
|
|
29
|
-
"@dxos/
|
|
30
|
-
"@dxos/context": "0.1.
|
|
31
|
-
"@dxos/credentials": "0.1.
|
|
32
|
-
"@dxos/crypto": "0.1.
|
|
33
|
-
"@dxos/debug": "0.1.
|
|
34
|
-
"@dxos/errors": "0.1.
|
|
35
|
-
"@dxos/keys": "0.1.
|
|
36
|
-
"@dxos/
|
|
37
|
-
"@dxos/
|
|
38
|
-
"@dxos/node-std": "0.1.
|
|
39
|
-
"@dxos/protocols": "0.1.
|
|
40
|
-
"@dxos/rpc": "0.1.
|
|
41
|
-
"@dxos/teleport": "0.1.
|
|
42
|
-
"@dxos/util": "0.1.
|
|
29
|
+
"@dxos/codec-protobuf": "0.1.54-main.270ec56",
|
|
30
|
+
"@dxos/async": "0.1.54-main.270ec56",
|
|
31
|
+
"@dxos/context": "0.1.54-main.270ec56",
|
|
32
|
+
"@dxos/credentials": "0.1.54-main.270ec56",
|
|
33
|
+
"@dxos/crypto": "0.1.54-main.270ec56",
|
|
34
|
+
"@dxos/debug": "0.1.54-main.270ec56",
|
|
35
|
+
"@dxos/errors": "0.1.54-main.270ec56",
|
|
36
|
+
"@dxos/keys": "0.1.54-main.270ec56",
|
|
37
|
+
"@dxos/messaging": "0.1.54-main.270ec56",
|
|
38
|
+
"@dxos/log": "0.1.54-main.270ec56",
|
|
39
|
+
"@dxos/node-std": "0.1.54-main.270ec56",
|
|
40
|
+
"@dxos/protocols": "0.1.54-main.270ec56",
|
|
41
|
+
"@dxos/rpc": "0.1.54-main.270ec56",
|
|
42
|
+
"@dxos/teleport": "0.1.54-main.270ec56",
|
|
43
|
+
"@dxos/util": "0.1.54-main.270ec56"
|
|
43
44
|
},
|
|
44
45
|
"devDependencies": {
|
|
45
46
|
"@swc-node/register": "1.5.4",
|
|
46
|
-
"@swc/core": "1.3.
|
|
47
|
+
"@swc/core": "1.3.70",
|
|
47
48
|
"@types/simple-peer": "9.11.3",
|
|
48
49
|
"@types/ws": "^7.4.0",
|
|
49
50
|
"earljs": "~0.1.10",
|
|
50
51
|
"fast-check": "~3.3.0",
|
|
51
52
|
"typescript": "^5.0.4",
|
|
52
|
-
"@dxos/signal": "0.1.
|
|
53
|
+
"@dxos/signal": "0.1.54-main.270ec56"
|
|
53
54
|
},
|
|
54
55
|
"publishConfig": {
|
|
55
56
|
"access": "public"
|
package/src/network-manager.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import invariant from 'tiny-invariant';
|
|
6
6
|
|
|
7
7
|
import { Event } from '@dxos/async';
|
|
8
8
|
import { PublicKey } from '@dxos/keys';
|
|
@@ -151,10 +151,10 @@ export class NetworkManager {
|
|
|
151
151
|
protocolProvider: protocol,
|
|
152
152
|
label,
|
|
153
153
|
}: SwarmOptions): Promise<SwarmConnection> {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
154
|
+
invariant(PublicKey.isPublicKey(topic));
|
|
155
|
+
invariant(PublicKey.isPublicKey(peerId));
|
|
156
|
+
invariant(topology);
|
|
157
|
+
invariant(typeof protocol === 'function');
|
|
158
158
|
if (this._swarms.has(topic)) {
|
|
159
159
|
throw new Error(`Already connected to swarm: ${PublicKey.from(topic)}`);
|
|
160
160
|
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import invariant from 'tiny-invariant';
|
|
6
6
|
|
|
7
7
|
import { Any } from '@dxos/codec-protobuf';
|
|
8
8
|
import { Context } from '@dxos/context';
|
|
@@ -25,6 +25,8 @@ export type SwarmMessengerOptions = {
|
|
|
25
25
|
topic: PublicKey;
|
|
26
26
|
};
|
|
27
27
|
|
|
28
|
+
const SwarmMessage = schema.getCodecForType('dxos.mesh.swarm.SwarmMessage');
|
|
29
|
+
|
|
28
30
|
/**
|
|
29
31
|
* Adds offer/answer and signal interfaces.
|
|
30
32
|
*/
|
|
@@ -58,7 +60,7 @@ export class SwarmMessenger implements SignalMessenger {
|
|
|
58
60
|
// Ignore not swarm messages.
|
|
59
61
|
return;
|
|
60
62
|
}
|
|
61
|
-
const message: SwarmMessage =
|
|
63
|
+
const message: SwarmMessage = SwarmMessage.decode(payload.value);
|
|
62
64
|
|
|
63
65
|
if (!this._topic.equals(message.topic)) {
|
|
64
66
|
// Ignore messages from wrong topics.
|
|
@@ -81,7 +83,7 @@ export class SwarmMessenger implements SignalMessenger {
|
|
|
81
83
|
}
|
|
82
84
|
|
|
83
85
|
async signal(message: SignalMessage): Promise<void> {
|
|
84
|
-
|
|
86
|
+
invariant(message.data?.signal || message.data?.signalBatch, 'Invalid message');
|
|
85
87
|
await this._sendReliableMessage({
|
|
86
88
|
author: message.author,
|
|
87
89
|
recipient: message.recipient,
|
|
@@ -125,17 +127,17 @@ export class SwarmMessenger implements SignalMessenger {
|
|
|
125
127
|
recipient,
|
|
126
128
|
payload: {
|
|
127
129
|
type_url: 'dxos.mesh.swarm.SwarmMessage',
|
|
128
|
-
value:
|
|
130
|
+
value: SwarmMessage.encode(networkMessage),
|
|
129
131
|
},
|
|
130
132
|
});
|
|
131
133
|
}
|
|
132
134
|
|
|
133
135
|
private async _resolveAnswers(message: SwarmMessage): Promise<void> {
|
|
134
|
-
|
|
136
|
+
invariant(message.data?.answer?.offerMessageId, 'No offerMessageId');
|
|
135
137
|
const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
|
|
136
138
|
if (offerRecord) {
|
|
137
139
|
this._offerRecords.delete(message.data.answer.offerMessageId);
|
|
138
|
-
|
|
140
|
+
invariant(message.data?.answer, 'No answer');
|
|
139
141
|
log('resolving', { answer: message.data.answer });
|
|
140
142
|
offerRecord.resolve(message.data.answer);
|
|
141
143
|
}
|
|
@@ -150,7 +152,7 @@ export class SwarmMessenger implements SignalMessenger {
|
|
|
150
152
|
recipient: PublicKey;
|
|
151
153
|
message: SwarmMessage;
|
|
152
154
|
}): Promise<void> {
|
|
153
|
-
|
|
155
|
+
invariant(message.data.offer, 'No offer');
|
|
154
156
|
const offerMessage: OfferMessage = {
|
|
155
157
|
author,
|
|
156
158
|
recipient,
|
|
@@ -183,8 +185,8 @@ export class SwarmMessenger implements SignalMessenger {
|
|
|
183
185
|
recipient: PublicKey;
|
|
184
186
|
message: SwarmMessage;
|
|
185
187
|
}): Promise<void> {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
+
invariant(message.messageId);
|
|
189
|
+
invariant(message.data.signal || message.data.signalBatch, 'Invalid message');
|
|
188
190
|
const signalMessage: SignalMessage = {
|
|
189
191
|
author,
|
|
190
192
|
recipient,
|
package/src/swarm/connection.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2021 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import invariant from 'tiny-invariant';
|
|
6
6
|
|
|
7
7
|
import { DeferredTask, Event, sleep, synchronized } from '@dxos/async';
|
|
8
8
|
import { Context, cancelWithContext } from '@dxos/context';
|
|
@@ -111,7 +111,7 @@ export class Connection {
|
|
|
111
111
|
*/
|
|
112
112
|
// TODO(burdon): Make async?
|
|
113
113
|
openConnection() {
|
|
114
|
-
|
|
114
|
+
invariant(this._state === ConnectionState.INITIAL, 'Invalid state.');
|
|
115
115
|
log.trace('dxos.mesh.connection.open-connection', trace.begin({ id: this._instanceId }));
|
|
116
116
|
|
|
117
117
|
this._changeState(ConnectionState.CONNECTING);
|
|
@@ -127,7 +127,7 @@ export class Connection {
|
|
|
127
127
|
this.close().catch((err) => this.errors.raise(err));
|
|
128
128
|
});
|
|
129
129
|
|
|
130
|
-
|
|
130
|
+
invariant(!this._transport);
|
|
131
131
|
this._transport = this._transportFactory.createTransport({
|
|
132
132
|
initiator: this.initiator,
|
|
133
133
|
stream: this._protocol.stream,
|
|
@@ -227,14 +227,14 @@ export class Connection {
|
|
|
227
227
|
* Receive a signal from the remote peer.
|
|
228
228
|
*/
|
|
229
229
|
async signal(msg: SignalMessage) {
|
|
230
|
-
|
|
230
|
+
invariant(msg.sessionId);
|
|
231
231
|
if (!msg.sessionId.equals(this.sessionId)) {
|
|
232
232
|
log('dropping signal for incorrect session id');
|
|
233
233
|
return;
|
|
234
234
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
235
|
+
invariant(msg.data.signal || msg.data.signalBatch);
|
|
236
|
+
invariant(msg.author?.equals(this.remoteId));
|
|
237
|
+
invariant(msg.recipient?.equals(this.ownId));
|
|
238
238
|
|
|
239
239
|
const signals = msg.data.signalBatch ? msg.data.signalBatch.signals ?? [] : [msg.data.signal];
|
|
240
240
|
for (const signal of signals) {
|
|
@@ -246,7 +246,7 @@ export class Connection {
|
|
|
246
246
|
log('buffered signal', { peerId: this.ownId, remoteId: this.remoteId, msg: msg.data });
|
|
247
247
|
this._incomingSignalBuffer.push(signal);
|
|
248
248
|
} else {
|
|
249
|
-
|
|
249
|
+
invariant(this._transport, 'Connection not ready to accept signals.');
|
|
250
250
|
log('received signal', { peerId: this.ownId, remoteId: this.remoteId, msg: msg.data });
|
|
251
251
|
await this._transport.signal(signal);
|
|
252
252
|
}
|
|
@@ -255,7 +255,7 @@ export class Connection {
|
|
|
255
255
|
|
|
256
256
|
private _changeState(state: ConnectionState): void {
|
|
257
257
|
log('stateChanged', { from: this._state, too: state, peerId: this.ownId });
|
|
258
|
-
|
|
258
|
+
invariant(state !== this._state, 'Already in this state.');
|
|
259
259
|
this._state = state;
|
|
260
260
|
this.stateChanged.emit(state);
|
|
261
261
|
}
|
package/src/swarm/peer.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import invariant from 'tiny-invariant';
|
|
6
6
|
|
|
7
7
|
import { scheduleTask, synchronized } from '@dxos/async';
|
|
8
8
|
import { Context } from '@dxos/context';
|
|
@@ -121,7 +121,7 @@ export class Peer {
|
|
|
121
121
|
if (await this._callbacks.onOffer(remoteId)) {
|
|
122
122
|
if (!this.connection) {
|
|
123
123
|
// Connection might have been already established.
|
|
124
|
-
|
|
124
|
+
invariant(message.sessionId);
|
|
125
125
|
const connection = this._createConnection(false, message.sessionId);
|
|
126
126
|
try {
|
|
127
127
|
connection.openConnection();
|
|
@@ -141,8 +141,8 @@ export class Peer {
|
|
|
141
141
|
* Initiate a connection to the remote peer.
|
|
142
142
|
*/
|
|
143
143
|
async initiateConnection() {
|
|
144
|
-
|
|
145
|
-
|
|
144
|
+
invariant(!this.initiating, 'Initiation in progress.');
|
|
145
|
+
invariant(!this.connection, 'Already connected.');
|
|
146
146
|
const sessionId = PublicKey.random();
|
|
147
147
|
log('initiating...', { id: this.id, topic: this.topic, peerId: this.id, sessionId });
|
|
148
148
|
const connection = this._createConnection(true, sessionId);
|
|
@@ -190,7 +190,7 @@ export class Peer {
|
|
|
190
190
|
initiator,
|
|
191
191
|
sessionId,
|
|
192
192
|
});
|
|
193
|
-
|
|
193
|
+
invariant(!this.connection, 'Already connected.');
|
|
194
194
|
|
|
195
195
|
const connection = new Connection(
|
|
196
196
|
this.topic,
|
|
@@ -219,7 +219,7 @@ export class Peer {
|
|
|
219
219
|
|
|
220
220
|
case ConnectionState.CLOSED: {
|
|
221
221
|
log('connection closed', { topic: this.topic, peerId: this.localPeerId, remoteId: this.id, initiator });
|
|
222
|
-
|
|
222
|
+
invariant(this.connection === connection, 'Connection mismatch (race condition).');
|
|
223
223
|
|
|
224
224
|
if (this._lastConnectionTime && this._lastConnectionTime + CONNECTION_COUNTS_STABLE_AFTER < Date.now()) {
|
|
225
225
|
// If we're closing the connection, and it has been connected for a while, reset the backoff.
|
package/src/swarm/swarm.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import invariant from 'tiny-invariant';
|
|
6
6
|
|
|
7
7
|
import { Event, scheduleTask, sleep, synchronized } from '@dxos/async';
|
|
8
8
|
import { Context } from '@dxos/context';
|
|
@@ -123,7 +123,7 @@ export class Swarm {
|
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
async open() {
|
|
126
|
-
|
|
126
|
+
invariant(!this._listeningHandle);
|
|
127
127
|
this._listeningHandle = await this._messenger.listen({
|
|
128
128
|
peerId: this._ownPeerId,
|
|
129
129
|
payloadType: 'dxos.mesh.swarm.SwarmMessage',
|
|
@@ -147,7 +147,7 @@ export class Swarm {
|
|
|
147
147
|
}
|
|
148
148
|
|
|
149
149
|
async setTopology(topology: Topology) {
|
|
150
|
-
|
|
150
|
+
invariant(!this._ctx.disposed, 'Swarm is offline');
|
|
151
151
|
if (topology === this._topology) {
|
|
152
152
|
return;
|
|
153
153
|
}
|
|
@@ -201,7 +201,7 @@ export class Swarm {
|
|
|
201
201
|
}
|
|
202
202
|
|
|
203
203
|
// Id of the peer offering us the connection.
|
|
204
|
-
|
|
204
|
+
invariant(message.author);
|
|
205
205
|
if (!message.recipient?.equals(this._ownPeerId)) {
|
|
206
206
|
log('rejecting offer with incorrect peerId', { message });
|
|
207
207
|
return { accept: false };
|
|
@@ -224,12 +224,12 @@ export class Swarm {
|
|
|
224
224
|
log.info('ignored for offline swarm');
|
|
225
225
|
return;
|
|
226
226
|
}
|
|
227
|
-
|
|
227
|
+
invariant(
|
|
228
228
|
message.recipient?.equals(this._ownPeerId),
|
|
229
229
|
`Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`,
|
|
230
230
|
);
|
|
231
|
-
|
|
232
|
-
|
|
231
|
+
invariant(message.topic?.equals(this._topic));
|
|
232
|
+
invariant(message.author);
|
|
233
233
|
|
|
234
234
|
const peer = this._getOrCreatePeer(message.author);
|
|
235
235
|
await peer.onSignal(message);
|
|
@@ -298,7 +298,7 @@ export class Swarm {
|
|
|
298
298
|
}
|
|
299
299
|
|
|
300
300
|
private async _destroyPeer(peerId: PublicKey) {
|
|
301
|
-
|
|
301
|
+
invariant(this._peers.has(peerId));
|
|
302
302
|
await this._peers.get(peerId)!.destroy();
|
|
303
303
|
this._peers.delete(peerId);
|
|
304
304
|
}
|
|
@@ -5,13 +5,15 @@
|
|
|
5
5
|
import { asyncTimeout, Event } from '@dxos/async';
|
|
6
6
|
import { PublicKey } from '@dxos/keys';
|
|
7
7
|
import { log } from '@dxos/log';
|
|
8
|
-
import { TestExtension } from '@dxos/teleport';
|
|
8
|
+
import { TestExtension, TestExtensionWithStreams, type TestStreamStats } from '@dxos/teleport';
|
|
9
9
|
import { ComplexMap } from '@dxos/util';
|
|
10
10
|
|
|
11
11
|
import { createTeleportProtocolFactory } from '../wire-protocol';
|
|
12
12
|
|
|
13
13
|
export class TestWireProtocol {
|
|
14
14
|
public readonly connections = new ComplexMap<PublicKey, TestExtension>(PublicKey.hash);
|
|
15
|
+
public readonly streamConnections = new ComplexMap<PublicKey, TestExtensionWithStreams>(PublicKey.hash);
|
|
16
|
+
|
|
15
17
|
public readonly connected = new Event<PublicKey>();
|
|
16
18
|
public readonly disconnected = new Event<PublicKey>();
|
|
17
19
|
|
|
@@ -28,6 +30,14 @@ export class TestWireProtocol {
|
|
|
28
30
|
this.connections.set(teleport.remotePeerId, extension);
|
|
29
31
|
teleport.addExtension('test', extension);
|
|
30
32
|
this.connected.emit(teleport.remotePeerId);
|
|
33
|
+
|
|
34
|
+
const streamExtension = new TestExtensionWithStreams({
|
|
35
|
+
onClose: async () => {
|
|
36
|
+
this.streamConnections.delete(teleport.remotePeerId);
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
this.streamConnections.set(teleport.remotePeerId, streamExtension);
|
|
40
|
+
teleport.addExtension('test-stream', streamExtension);
|
|
31
41
|
});
|
|
32
42
|
|
|
33
43
|
async waitForConnection(peerId: PublicKey) {
|
|
@@ -47,4 +57,25 @@ export class TestWireProtocol {
|
|
|
47
57
|
const connection = await this.waitForConnection(peerId);
|
|
48
58
|
await connection.test(message);
|
|
49
59
|
}
|
|
60
|
+
|
|
61
|
+
async startStream(
|
|
62
|
+
peerId: PublicKey,
|
|
63
|
+
streamTag: string,
|
|
64
|
+
streamLoadInterval: number,
|
|
65
|
+
streamLoadChunkSize: number,
|
|
66
|
+
): Promise<string> {
|
|
67
|
+
if (!this.streamConnections.has(peerId)) {
|
|
68
|
+
throw new Error('Connection does not exist.');
|
|
69
|
+
}
|
|
70
|
+
const connection = this.streamConnections.get(peerId)!;
|
|
71
|
+
return connection.addNewStream(streamLoadInterval, streamLoadChunkSize, streamTag);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async closeStream(peerId: PublicKey, streamTag: string): Promise<TestStreamStats> {
|
|
75
|
+
if (!this.streamConnections.has(peerId)) {
|
|
76
|
+
throw new Error('Connection does not exist.');
|
|
77
|
+
}
|
|
78
|
+
const connection = this.streamConnections.get(peerId)!;
|
|
79
|
+
return connection.closeStream(streamTag);
|
|
80
|
+
}
|
|
50
81
|
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import invariant from 'tiny-invariant';
|
|
6
6
|
|
|
7
7
|
import { PublicKey } from '@dxos/keys';
|
|
8
8
|
|
|
@@ -16,12 +16,12 @@ export class FullyConnectedTopology implements Topology {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
init(controller: SwarmController): void {
|
|
19
|
-
|
|
19
|
+
invariant(!this._controller, 'Already initialized');
|
|
20
20
|
this._controller = controller;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
update(): void {
|
|
24
|
-
|
|
24
|
+
invariant(this._controller, 'Not initialized');
|
|
25
25
|
const { candidates: discovered } = this._controller.getState();
|
|
26
26
|
for (const peer of discovered) {
|
|
27
27
|
// TODO(burdon): Back-off.
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import invariant from 'tiny-invariant';
|
|
6
6
|
import distance from 'xor-distance';
|
|
7
7
|
|
|
8
8
|
import { PublicKey } from '@dxos/keys';
|
|
@@ -43,12 +43,12 @@ export class MMSTTopology implements Topology {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
init(controller: SwarmController): void {
|
|
46
|
-
|
|
46
|
+
invariant(!this._controller, 'Already initialized');
|
|
47
47
|
this._controller = controller;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
update(): void {
|
|
51
|
-
|
|
51
|
+
invariant(this._controller, 'Not initialized');
|
|
52
52
|
const { connected, candidates } = this._controller.getState();
|
|
53
53
|
// Run the algorithms if we have first candidates, ran it before, or have more connections than needed.
|
|
54
54
|
if (this._sampleCollected || connected.length > this._maxPeers || candidates.length > 0) {
|
|
@@ -59,7 +59,7 @@ export class MMSTTopology implements Topology {
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
async onOffer(peer: PublicKey): Promise<boolean> {
|
|
62
|
-
|
|
62
|
+
invariant(this._controller, 'Not initialized');
|
|
63
63
|
const { connected } = this._controller.getState();
|
|
64
64
|
const accept = connected.length < this._maxPeers;
|
|
65
65
|
log(`Offer ${peer} accept=${accept}`);
|
|
@@ -71,7 +71,7 @@ export class MMSTTopology implements Topology {
|
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
private _runAlgorithm() {
|
|
74
|
-
|
|
74
|
+
invariant(this._controller, 'Not initialized');
|
|
75
75
|
const { connected, candidates, ownPeerId } = this._controller.getState();
|
|
76
76
|
|
|
77
77
|
if (connected.length > this._maxPeers) {
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import invariant from 'tiny-invariant';
|
|
6
6
|
|
|
7
7
|
import { PublicKey } from '@dxos/keys';
|
|
8
8
|
import { log } from '@dxos/log';
|
|
@@ -22,12 +22,12 @@ export class StarTopology implements Topology {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
init(controller: SwarmController): void {
|
|
25
|
-
|
|
25
|
+
invariant(!this._controller, 'Already initialized.');
|
|
26
26
|
this._controller = controller;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
update(): void {
|
|
30
|
-
|
|
30
|
+
invariant(this._controller, 'Not initialized.');
|
|
31
31
|
const { candidates, connected, ownPeerId } = this._controller.getState();
|
|
32
32
|
if (!ownPeerId.equals(this._centralPeer)) {
|
|
33
33
|
log('leaf peer dropping all connections apart from central peer.');
|
|
@@ -51,7 +51,7 @@ export class StarTopology implements Topology {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
async onOffer(peer: PublicKey): Promise<boolean> {
|
|
54
|
-
|
|
54
|
+
invariant(this._controller, 'Not initialized.');
|
|
55
55
|
const { ownPeerId } = this._controller.getState();
|
|
56
56
|
log('offer', {
|
|
57
57
|
peer,
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import assert from 'node:assert';
|
|
6
5
|
import { Transform } from 'node:stream';
|
|
6
|
+
import invariant from 'tiny-invariant';
|
|
7
7
|
|
|
8
8
|
import { Event, Trigger } from '@dxos/async';
|
|
9
9
|
import { ErrorStream } from '@dxos/debug';
|
|
@@ -63,7 +63,7 @@ export class MemoryTransport implements Transport {
|
|
|
63
63
|
constructor(private readonly options: TransportOptions) {
|
|
64
64
|
log('creating');
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
invariant(!MemoryTransport._connections.has(this._instanceId), 'Duplicate memory connection');
|
|
67
67
|
MemoryTransport._connections.set(this._instanceId, this);
|
|
68
68
|
|
|
69
69
|
// Initiator will send a signal, the receiver will receive the unique ID and connect the streams.
|
|
@@ -96,10 +96,7 @@ export class MemoryTransport implements Transport {
|
|
|
96
96
|
return;
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
|
|
100
|
-
!this._remoteConnection._remoteConnection,
|
|
101
|
-
new Error(`Remote already connected: ${this._remoteInstanceId}`),
|
|
102
|
-
);
|
|
99
|
+
invariant(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`);
|
|
103
100
|
this._remoteConnection._remoteConnection = this;
|
|
104
101
|
this._remoteConnection._remoteInstanceId = this._instanceId;
|
|
105
102
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import invariant from 'tiny-invariant';
|
|
6
6
|
|
|
7
7
|
import { Event } from '@dxos/async';
|
|
8
8
|
import { Stream } from '@dxos/codec-protobuf';
|
|
@@ -158,7 +158,7 @@ export class WebRTCTransportProxyFactory implements TransportFactory {
|
|
|
158
158
|
}
|
|
159
159
|
|
|
160
160
|
createTransport(options: TransportOptions): Transport {
|
|
161
|
-
|
|
161
|
+
invariant(this._bridgeService, 'WebRTCTransportProxyFactory is not ready to open connections');
|
|
162
162
|
|
|
163
163
|
const transport = new WebRTCTransportProxy({
|
|
164
164
|
...options,
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import assert from 'node:assert';
|
|
6
5
|
import { Duplex } from 'node:stream';
|
|
6
|
+
import invariant from 'tiny-invariant';
|
|
7
7
|
|
|
8
8
|
import { Stream } from '@dxos/codec-protobuf';
|
|
9
9
|
import { PublicKey } from '@dxos/keys';
|
|
@@ -93,12 +93,12 @@ export class WebRTCTransportService implements BridgeService {
|
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
async sendSignal({ proxyId, signal }: SignalRequest): Promise<void> {
|
|
96
|
-
|
|
96
|
+
invariant(this.transports.has(proxyId));
|
|
97
97
|
await this.transports.get(proxyId)!.transport.signal(signal);
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
async sendData({ proxyId, payload }: DataRequest): Promise<void> {
|
|
101
|
-
|
|
101
|
+
invariant(this.transports.has(proxyId));
|
|
102
102
|
await this.transports.get(proxyId)!.stream.push(payload);
|
|
103
103
|
}
|
|
104
104
|
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import assert from 'node:assert';
|
|
6
5
|
import SimplePeerConstructor, { Instance as SimplePeer } from 'simple-peer';
|
|
6
|
+
import invariant from 'tiny-invariant';
|
|
7
7
|
|
|
8
8
|
import { Event } from '@dxos/async';
|
|
9
9
|
import { ErrorStream, raise } from '@dxos/debug';
|
|
@@ -95,7 +95,7 @@ export class WebRTCTransport implements Transport {
|
|
|
95
95
|
return; // Ignore signals after close.
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
|
|
98
|
+
invariant(signal.payload.data, 'Signal message must contain signal data.');
|
|
99
99
|
this._peer.signal(signal.payload.data);
|
|
100
100
|
}
|
|
101
101
|
|