@dxos/network-manager 0.1.17 → 0.1.19
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/index.mjs +221 -352
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +222 -410
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/node/index.cjs +222 -357
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +222 -413
- package/dist/lib/node/testing/index.cjs.map +4 -4
- package/dist/types/src/index.d.ts +0 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/network-manager.d.ts +1 -5
- package/dist/types/src/network-manager.d.ts.map +1 -1
- package/dist/types/src/swarm/connection.d.ts +4 -0
- package/dist/types/src/swarm/connection.d.ts.map +1 -1
- package/dist/types/src/swarm/peer.d.ts.map +1 -1
- package/dist/types/src/swarm/swarm-mapper.d.ts +1 -3
- package/dist/types/src/swarm/swarm-mapper.d.ts.map +1 -1
- package/dist/types/src/testing/index.d.ts +0 -1
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/test-builder.d.ts +2 -2
- package/dist/types/src/testing/test-builder.d.ts.map +1 -1
- package/dist/types/src/testing/test-wire-protocol.d.ts +14 -0
- package/dist/types/src/testing/test-wire-protocol.d.ts.map +1 -0
- package/dist/types/src/tests/basic-test-suite.d.ts.map +1 -1
- package/dist/types/src/tests/property-test-suite.d.ts.map +1 -1
- package/dist/types/src/tests/utils.d.ts.map +1 -1
- package/dist/types/src/transport/memory-transport.d.ts +3 -3
- package/dist/types/src/transport/memory-transport.d.ts.map +1 -1
- package/dist/types/src/wire-protocol.d.ts +0 -10
- package/dist/types/src/wire-protocol.d.ts.map +1 -1
- package/package.json +16 -18
- package/src/connection-log.ts +26 -26
- package/src/index.ts +0 -1
- package/src/network-manager.ts +1 -8
- package/src/swarm/connection.ts +12 -1
- package/src/swarm/peer.ts +1 -3
- package/src/swarm/swarm-mapper.ts +29 -31
- package/src/swarm/swarm.test.ts +12 -34
- package/src/testing/index.ts +0 -1
- package/src/testing/test-builder.ts +4 -7
- package/src/testing/test-wire-protocol.ts +44 -0
- package/src/tests/basic-test-suite.ts +13 -53
- package/src/tests/property-test-suite.ts +13 -15
- package/src/tests/utils.ts +10 -37
- package/src/transport/memory-transport.test.ts +13 -52
- package/src/transport/memory-transport.ts +37 -29
- package/src/transport/webrtc-transport-proxy.test.ts +13 -73
- package/src/transport/webrtc-transport.test.ts +7 -38
- package/src/wire-protocol.ts +0 -24
- package/dist/types/src/protocol-factory.d.ts +0 -36
- package/dist/types/src/protocol-factory.d.ts.map +0 -1
- package/dist/types/src/testing/test-protocol.d.ts +0 -52
- package/dist/types/src/testing/test-protocol.d.ts.map +0 -1
- package/dist/types/src/tests/presence.test.d.ts +0 -2
- package/dist/types/src/tests/presence.test.d.ts.map +0 -1
- package/src/protocol-factory.ts +0 -88
- package/src/testing/test-protocol.ts +0 -166
- package/src/tests/presence.test.ts +0 -80
|
@@ -8,7 +8,7 @@ import { Transform } from 'node:stream';
|
|
|
8
8
|
import { Event, Trigger } from '@dxos/async';
|
|
9
9
|
import { ErrorStream } from '@dxos/debug';
|
|
10
10
|
import { PublicKey } from '@dxos/keys';
|
|
11
|
-
import { log } from '@dxos/log';
|
|
11
|
+
import { log, logInfo } from '@dxos/log';
|
|
12
12
|
import { Signal } from '@dxos/protocols/proto/dxos/mesh/swarm';
|
|
13
13
|
import { ComplexMap } from '@dxos/util';
|
|
14
14
|
|
|
@@ -45,29 +45,34 @@ export class MemoryTransport implements Transport {
|
|
|
45
45
|
public readonly connected = new Event<void>();
|
|
46
46
|
public readonly errors = new ErrorStream();
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
@logInfo
|
|
49
|
+
private readonly _instanceId = PublicKey.random(); // TODO(burdon): Rename peerId? (Use local/remote labels in logs).
|
|
50
|
+
|
|
49
51
|
private readonly _remote = new Trigger<PublicKey>();
|
|
50
52
|
|
|
51
53
|
private readonly _outgoingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
|
|
52
54
|
private readonly _incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
|
|
53
55
|
|
|
54
56
|
private _destroyed = false;
|
|
55
|
-
|
|
57
|
+
|
|
58
|
+
@logInfo
|
|
59
|
+
private _remoteInstanceId!: PublicKey;
|
|
60
|
+
|
|
56
61
|
private _remoteConnection?: MemoryTransport;
|
|
57
62
|
|
|
58
63
|
constructor(private readonly options: TransportOptions) {
|
|
59
|
-
log('creating'
|
|
64
|
+
log('creating');
|
|
60
65
|
|
|
61
|
-
assert(!MemoryTransport._connections.has(this.
|
|
62
|
-
MemoryTransport._connections.set(this.
|
|
66
|
+
assert(!MemoryTransport._connections.has(this._instanceId), 'Duplicate memory connection');
|
|
67
|
+
MemoryTransport._connections.set(this._instanceId, this);
|
|
63
68
|
|
|
64
69
|
// Initiator will send a signal, the receiver will receive the unique ID and connect the streams.
|
|
65
70
|
if (this.options.initiator) {
|
|
66
71
|
// prettier-ignore
|
|
67
72
|
setTimeout(async () => {
|
|
68
|
-
log('sending signal'
|
|
73
|
+
log('sending signal');
|
|
69
74
|
void this.options.sendSignal({
|
|
70
|
-
payload: { transportId: this.
|
|
75
|
+
payload: { transportId: this._instanceId.toHex() }
|
|
71
76
|
});
|
|
72
77
|
});
|
|
73
78
|
} else {
|
|
@@ -78,8 +83,8 @@ export class MemoryTransport implements Transport {
|
|
|
78
83
|
return;
|
|
79
84
|
}
|
|
80
85
|
|
|
81
|
-
this.
|
|
82
|
-
this._remoteConnection = MemoryTransport._connections.get(this.
|
|
86
|
+
this._remoteInstanceId = remoteId;
|
|
87
|
+
this._remoteConnection = MemoryTransport._connections.get(this._remoteInstanceId);
|
|
83
88
|
if (!this._remoteConnection) {
|
|
84
89
|
// Remote connection was destroyed before we could connect.
|
|
85
90
|
this._destroyed = true;
|
|
@@ -87,11 +92,14 @@ export class MemoryTransport implements Transport {
|
|
|
87
92
|
return;
|
|
88
93
|
}
|
|
89
94
|
|
|
90
|
-
assert(
|
|
95
|
+
assert(
|
|
96
|
+
!this._remoteConnection._remoteConnection,
|
|
97
|
+
new Error(`Remote already connected: ${this._remoteInstanceId}`)
|
|
98
|
+
);
|
|
91
99
|
this._remoteConnection._remoteConnection = this;
|
|
92
|
-
this._remoteConnection.
|
|
100
|
+
this._remoteConnection._remoteInstanceId = this._instanceId;
|
|
93
101
|
|
|
94
|
-
log('connected'
|
|
102
|
+
log('connected');
|
|
95
103
|
this.options.stream
|
|
96
104
|
.pipe(this._outgoingDelay)
|
|
97
105
|
.pipe(this._remoteConnection.options.stream)
|
|
@@ -112,14 +120,14 @@ export class MemoryTransport implements Transport {
|
|
|
112
120
|
}
|
|
113
121
|
|
|
114
122
|
async destroy(): Promise<void> {
|
|
115
|
-
log('closing'
|
|
123
|
+
log('closing');
|
|
116
124
|
this._destroyed = true;
|
|
117
125
|
|
|
118
|
-
MemoryTransport._connections.delete(this.
|
|
126
|
+
MemoryTransport._connections.delete(this._instanceId);
|
|
119
127
|
if (this._remoteConnection) {
|
|
120
|
-
log('closing'
|
|
128
|
+
log('closing');
|
|
121
129
|
this._remoteConnection._destroyed = true;
|
|
122
|
-
MemoryTransport._connections.delete(this.
|
|
130
|
+
MemoryTransport._connections.delete(this._remoteInstanceId);
|
|
123
131
|
|
|
124
132
|
// TODO(dmaretskyi): Hypercore streams do not seem to have the unpipe method.
|
|
125
133
|
// NOTE(burdon): Using readable-stream.wrap() might help (see feed-store).
|
|
@@ -135,23 +143,23 @@ export class MemoryTransport implements Transport {
|
|
|
135
143
|
this._remoteConnection.closed.emit();
|
|
136
144
|
this._remoteConnection._remoteConnection = undefined;
|
|
137
145
|
this._remoteConnection = undefined;
|
|
138
|
-
log('closed'
|
|
146
|
+
log('closed');
|
|
139
147
|
}
|
|
140
148
|
|
|
141
149
|
this.closed.emit();
|
|
142
|
-
log('closed'
|
|
150
|
+
log('closed');
|
|
143
151
|
}
|
|
144
152
|
|
|
145
|
-
signal(
|
|
146
|
-
|
|
147
|
-
if (
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
153
|
+
signal({ payload }: Signal) {
|
|
154
|
+
log('received signal', { payload });
|
|
155
|
+
if (!payload?.transportId) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
// TODO(burdon): Check open?
|
|
159
|
+
const transportId = payload.transportId as string;
|
|
160
|
+
if (transportId) {
|
|
161
|
+
const remoteId = PublicKey.fromHex(transportId);
|
|
162
|
+
this._remote.wake(remoteId);
|
|
155
163
|
}
|
|
156
164
|
}
|
|
157
165
|
}
|
|
@@ -5,27 +5,21 @@
|
|
|
5
5
|
// @dxos/test platform=nodejs
|
|
6
6
|
|
|
7
7
|
import expect from 'expect';
|
|
8
|
-
import { Duplex } from 'stream';
|
|
9
|
-
import waitForExpect from 'wait-for-expect';
|
|
10
8
|
|
|
11
|
-
import { sleep } from '@dxos/async';
|
|
12
|
-
import { discoveryKey } from '@dxos/crypto';
|
|
13
|
-
import { PublicKey } from '@dxos/keys';
|
|
14
|
-
import { Protocol } from '@dxos/mesh-protocol';
|
|
9
|
+
import { sleep, TestStream } from '@dxos/async';
|
|
15
10
|
import { schema } from '@dxos/protocols';
|
|
16
11
|
import { BridgeService } from '@dxos/protocols/proto/dxos/mesh/bridge';
|
|
17
12
|
import { Signal } from '@dxos/protocols/proto/dxos/mesh/swarm';
|
|
18
13
|
import { createLinkedPorts, createProtoRpcPeer, ProtoRpcPeer } from '@dxos/rpc';
|
|
19
|
-
import { afterAll, beforeAll, describe, test
|
|
14
|
+
import { afterAll, afterTest, beforeAll, describe, test } from '@dxos/test';
|
|
20
15
|
|
|
21
|
-
import { TestProtocolPlugin, testProtocolProvider } from '../testing';
|
|
22
16
|
import { WebRTCTransportProxy } from './webrtc-transport-proxy';
|
|
23
17
|
import { WebRTCTransportService } from './webrtc-transport-service';
|
|
24
18
|
|
|
25
19
|
describe('WebRTCTransportProxy', () => {
|
|
26
20
|
const setupProxy = async ({
|
|
27
21
|
initiator = true,
|
|
28
|
-
stream = new
|
|
22
|
+
stream = new TestStream(),
|
|
29
23
|
sendSignal = async () => {}
|
|
30
24
|
}: {
|
|
31
25
|
initiator?: boolean;
|
|
@@ -99,18 +93,10 @@ describe('WebRTCTransportProxy', () => {
|
|
|
99
93
|
.retries(3);
|
|
100
94
|
|
|
101
95
|
test('establish connection and send data through with protocol', async () => {
|
|
102
|
-
const
|
|
103
|
-
const peer1Id = PublicKey.random();
|
|
104
|
-
const peer2Id = PublicKey.random();
|
|
105
|
-
|
|
106
|
-
const plugin1 = new TestProtocolPlugin(peer1Id.asBuffer());
|
|
107
|
-
const protocolProvider1 = testProtocolProvider(topic.asBuffer(), peer1Id, plugin1);
|
|
96
|
+
const stream1 = new TestStream();
|
|
108
97
|
const { webRTCTransportProxy: connection1 } = await setupProxy({
|
|
109
98
|
initiator: true,
|
|
110
|
-
stream:
|
|
111
|
-
channel: discoveryKey(topic),
|
|
112
|
-
initiator: true
|
|
113
|
-
}).stream,
|
|
99
|
+
stream: stream1,
|
|
114
100
|
sendSignal: async (signal) => {
|
|
115
101
|
await sleep(10);
|
|
116
102
|
await connection2.signal(signal);
|
|
@@ -118,36 +104,17 @@ describe('WebRTCTransportProxy', () => {
|
|
|
118
104
|
});
|
|
119
105
|
afterTest(() => connection1.errors.assertNoUnhandledErrors());
|
|
120
106
|
|
|
121
|
-
const
|
|
122
|
-
const protocolProvider2 = testProtocolProvider(topic.asBuffer(), peer2Id, plugin2);
|
|
107
|
+
const stream2 = new TestStream();
|
|
123
108
|
const { webRTCTransportProxy: connection2 } = await setupProxy({
|
|
124
109
|
initiator: false,
|
|
125
|
-
stream:
|
|
126
|
-
channel: discoveryKey(topic),
|
|
127
|
-
initiator: false
|
|
128
|
-
}).stream,
|
|
110
|
+
stream: stream2,
|
|
129
111
|
sendSignal: async (signal) => {
|
|
130
112
|
await sleep(10);
|
|
131
113
|
await connection1.signal(signal);
|
|
132
114
|
}
|
|
133
115
|
});
|
|
134
116
|
afterTest(() => connection2.errors.assertNoUnhandledErrors());
|
|
135
|
-
|
|
136
|
-
const received: any[] = [];
|
|
137
|
-
const mockReceive = (p: Protocol, s: string) => {
|
|
138
|
-
received.push(p, s);
|
|
139
|
-
return undefined;
|
|
140
|
-
};
|
|
141
|
-
plugin1.on('receive', mockReceive);
|
|
142
|
-
|
|
143
|
-
plugin2.on('connect', async (protocol) => {
|
|
144
|
-
await plugin2.send(peer1Id.asBuffer(), '{"message": "Hello"}');
|
|
145
|
-
});
|
|
146
|
-
await waitForExpect(() => {
|
|
147
|
-
expect(received.length).toBe(2);
|
|
148
|
-
expect(received[0]).toBeInstanceOf(Protocol);
|
|
149
|
-
expect(received[1]).toBe('{"message": "Hello"}');
|
|
150
|
-
});
|
|
117
|
+
await TestStream.assertConnectivity(stream1, stream2);
|
|
151
118
|
})
|
|
152
119
|
.timeout(2_000)
|
|
153
120
|
.retries(3);
|
|
@@ -192,18 +159,10 @@ describe('WebRTCTransportProxy', () => {
|
|
|
192
159
|
});
|
|
193
160
|
|
|
194
161
|
test('establish connection and send data through with protocol', async () => {
|
|
195
|
-
const
|
|
196
|
-
const peer1Id = PublicKey.random();
|
|
197
|
-
const peer2Id = PublicKey.random();
|
|
198
|
-
|
|
199
|
-
const plugin1 = new TestProtocolPlugin(peer1Id.asBuffer());
|
|
200
|
-
const protocolProvider1 = testProtocolProvider(topic.asBuffer(), peer1Id, plugin1);
|
|
162
|
+
const stream1 = new TestStream();
|
|
201
163
|
const proxy1 = new WebRTCTransportProxy({
|
|
202
164
|
initiator: true,
|
|
203
|
-
stream:
|
|
204
|
-
channel: discoveryKey(topic),
|
|
205
|
-
initiator: true
|
|
206
|
-
}).stream,
|
|
165
|
+
stream: stream1,
|
|
207
166
|
sendSignal: async (signal) => {
|
|
208
167
|
await sleep(10);
|
|
209
168
|
await proxy2.signal(signal);
|
|
@@ -212,14 +171,10 @@ describe('WebRTCTransportProxy', () => {
|
|
|
212
171
|
});
|
|
213
172
|
afterTest(() => proxy1.errors.assertNoUnhandledErrors());
|
|
214
173
|
|
|
215
|
-
const
|
|
216
|
-
const protocolProvider2 = testProtocolProvider(topic.asBuffer(), peer2Id, plugin2);
|
|
174
|
+
const stream2 = new TestStream();
|
|
217
175
|
const proxy2 = new WebRTCTransportProxy({
|
|
218
176
|
initiator: false,
|
|
219
|
-
stream:
|
|
220
|
-
channel: discoveryKey(topic),
|
|
221
|
-
initiator: false
|
|
222
|
-
}).stream,
|
|
177
|
+
stream: stream2,
|
|
223
178
|
sendSignal: async (signal) => {
|
|
224
179
|
await sleep(10);
|
|
225
180
|
await proxy1.signal(signal);
|
|
@@ -228,22 +183,7 @@ describe('WebRTCTransportProxy', () => {
|
|
|
228
183
|
});
|
|
229
184
|
afterTest(() => proxy2.errors.assertNoUnhandledErrors());
|
|
230
185
|
|
|
231
|
-
|
|
232
|
-
const mockReceive = (p: Protocol, s: string) => {
|
|
233
|
-
received.push(p, s);
|
|
234
|
-
return undefined;
|
|
235
|
-
};
|
|
236
|
-
plugin1.on('receive', mockReceive);
|
|
237
|
-
|
|
238
|
-
plugin2.on('connect', async (protocol) => {
|
|
239
|
-
await plugin2.send(peer1Id.asBuffer(), '{"message": "Hello"}');
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
await waitForExpect(() => {
|
|
243
|
-
expect(received.length).toBe(2);
|
|
244
|
-
expect(received[0]).toBeInstanceOf(Protocol);
|
|
245
|
-
expect(received[1]).toBe('{"message": "Hello"}');
|
|
246
|
-
});
|
|
186
|
+
await TestStream.assertConnectivity(stream1, stream2);
|
|
247
187
|
})
|
|
248
188
|
.timeout(5_000)
|
|
249
189
|
.retries(3);
|
|
@@ -4,15 +4,10 @@
|
|
|
4
4
|
|
|
5
5
|
import expect from 'expect';
|
|
6
6
|
import { Duplex } from 'stream';
|
|
7
|
-
import waitForExpect from 'wait-for-expect';
|
|
8
7
|
|
|
9
|
-
import { sleep } from '@dxos/async';
|
|
10
|
-
import {
|
|
11
|
-
import { PublicKey } from '@dxos/keys';
|
|
12
|
-
import { Protocol } from '@dxos/mesh-protocol';
|
|
13
|
-
import { describe, test, afterTest } from '@dxos/test';
|
|
8
|
+
import { sleep, TestStream } from '@dxos/async';
|
|
9
|
+
import { afterTest, describe, test } from '@dxos/test';
|
|
14
10
|
|
|
15
|
-
import { TestProtocolPlugin, testProtocolProvider } from '../testing';
|
|
16
11
|
import { WebRTCTransport } from './webrtc-transport';
|
|
17
12
|
|
|
18
13
|
describe('WebRTCTransport', () => {
|
|
@@ -40,18 +35,10 @@ describe('WebRTCTransport', () => {
|
|
|
40
35
|
.retries(3);
|
|
41
36
|
|
|
42
37
|
test('establish connection and send data through with protocol', async () => {
|
|
43
|
-
const
|
|
44
|
-
const peer1Id = PublicKey.random();
|
|
45
|
-
const peer2Id = PublicKey.random();
|
|
46
|
-
|
|
47
|
-
const plugin1 = new TestProtocolPlugin(peer1Id.asBuffer());
|
|
48
|
-
const protocolProvider1 = testProtocolProvider(topic.asBuffer(), peer1Id, plugin1);
|
|
38
|
+
const stream1 = new TestStream();
|
|
49
39
|
const connection1 = new WebRTCTransport({
|
|
50
40
|
initiator: true,
|
|
51
|
-
stream:
|
|
52
|
-
channel: discoveryKey(topic),
|
|
53
|
-
initiator: true
|
|
54
|
-
}).stream,
|
|
41
|
+
stream: stream1,
|
|
55
42
|
sendSignal: async (signal) => {
|
|
56
43
|
await sleep(10);
|
|
57
44
|
await connection2.signal(signal);
|
|
@@ -60,14 +47,10 @@ describe('WebRTCTransport', () => {
|
|
|
60
47
|
afterTest(() => connection1.destroy());
|
|
61
48
|
afterTest(() => connection1.errors.assertNoUnhandledErrors());
|
|
62
49
|
|
|
63
|
-
const
|
|
64
|
-
const protocolProvider2 = testProtocolProvider(topic.asBuffer(), peer2Id, plugin2);
|
|
50
|
+
const stream2 = new TestStream();
|
|
65
51
|
const connection2 = new WebRTCTransport({
|
|
66
52
|
initiator: false,
|
|
67
|
-
stream:
|
|
68
|
-
channel: discoveryKey(topic),
|
|
69
|
-
initiator: false
|
|
70
|
-
}).stream,
|
|
53
|
+
stream: stream2,
|
|
71
54
|
sendSignal: async (signal) => {
|
|
72
55
|
await sleep(10);
|
|
73
56
|
await connection1.signal(signal);
|
|
@@ -76,21 +59,7 @@ describe('WebRTCTransport', () => {
|
|
|
76
59
|
afterTest(() => connection2.destroy());
|
|
77
60
|
afterTest(() => connection2.errors.assertNoUnhandledErrors());
|
|
78
61
|
|
|
79
|
-
|
|
80
|
-
const mockReceive = (p: Protocol, s: string) => {
|
|
81
|
-
received.push(p, s);
|
|
82
|
-
return undefined;
|
|
83
|
-
};
|
|
84
|
-
plugin1.on('receive', mockReceive);
|
|
85
|
-
plugin2.on('connect', async (protocol) => {
|
|
86
|
-
await plugin2.send(peer1Id.asBuffer(), '{"message": "Hello"}');
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
await waitForExpect(() => {
|
|
90
|
-
expect(received.length).toBe(2);
|
|
91
|
-
expect(received[0]).toBeInstanceOf(Protocol);
|
|
92
|
-
expect(received[1]).toBe('{"message": "Hello"}');
|
|
93
|
-
});
|
|
62
|
+
await TestStream.assertConnectivity(stream1, stream2);
|
|
94
63
|
})
|
|
95
64
|
.timeout(2_000)
|
|
96
65
|
.retries(3);
|
package/src/wire-protocol.ts
CHANGED
|
@@ -4,13 +4,9 @@
|
|
|
4
4
|
|
|
5
5
|
import { Duplex } from 'node:stream';
|
|
6
6
|
|
|
7
|
-
import { discoveryKey } from '@dxos/crypto';
|
|
8
7
|
import { PublicKey } from '@dxos/keys';
|
|
9
|
-
import { Protocol } from '@dxos/mesh-protocol';
|
|
10
8
|
import { Teleport } from '@dxos/teleport';
|
|
11
9
|
|
|
12
|
-
import { MeshProtocolProvider } from './protocol-factory';
|
|
13
|
-
|
|
14
10
|
export type WireProtocolParams = {
|
|
15
11
|
initiator: boolean;
|
|
16
12
|
localPeerId: PublicKey;
|
|
@@ -27,30 +23,10 @@ export type WireProtocolProvider = (params: WireProtocolParams) => WireProtocol;
|
|
|
27
23
|
export interface WireProtocol {
|
|
28
24
|
stream: Duplex;
|
|
29
25
|
|
|
30
|
-
/**
|
|
31
|
-
* @deprecated Only for devtools comapatibility.
|
|
32
|
-
*/
|
|
33
|
-
protocol?: Protocol;
|
|
34
|
-
|
|
35
26
|
initialize(): Promise<void>;
|
|
36
27
|
destroy(): Promise<void>;
|
|
37
28
|
}
|
|
38
29
|
|
|
39
|
-
/**
|
|
40
|
-
* @deprecated
|
|
41
|
-
*/
|
|
42
|
-
export const adaptProtocolProvider =
|
|
43
|
-
(factory: MeshProtocolProvider): WireProtocolProvider =>
|
|
44
|
-
({ initiator, localPeerId, remotePeerId, topic }) => {
|
|
45
|
-
const protocol = factory({ channel: discoveryKey(topic), initiator });
|
|
46
|
-
return {
|
|
47
|
-
initialize: () => protocol.open(),
|
|
48
|
-
destroy: () => protocol.close(),
|
|
49
|
-
stream: protocol.stream,
|
|
50
|
-
protocol
|
|
51
|
-
};
|
|
52
|
-
};
|
|
53
|
-
|
|
54
30
|
/**
|
|
55
31
|
* Create a wire-protocol provider backed by a teleport instance.
|
|
56
32
|
* @param onConnection Called after teleport is initialized for the session. Protocol extensions could be attached here.
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import { PublicKey } from '@dxos/keys';
|
|
3
|
-
import { Extension, Protocol } from '@dxos/mesh-protocol';
|
|
4
|
-
interface ProtocolFactoryOptions {
|
|
5
|
-
plugins: any[];
|
|
6
|
-
getTopics: () => Buffer[];
|
|
7
|
-
session: Record<string, any>;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* @deprecated Replaced by WireProtocolProvider.
|
|
11
|
-
*/
|
|
12
|
-
export declare type MeshProtocolProvider = (params: {
|
|
13
|
-
channel: Buffer;
|
|
14
|
-
initiator: boolean;
|
|
15
|
-
}) => Protocol;
|
|
16
|
-
/**
|
|
17
|
-
* Returns a function that takes a channel parameter, returns a Protocol object
|
|
18
|
-
* with its context set to channel, plugins from plugins parameter and session
|
|
19
|
-
* set to session parameter.
|
|
20
|
-
*
|
|
21
|
-
* @param plugins array of Protocol plugin objects to add to created Protocol objects
|
|
22
|
-
* @param getTopics retrieve a list of known topic keys to encrypt by public key.
|
|
23
|
-
* @deprecated Use `createProtocolFactory`.
|
|
24
|
-
*/
|
|
25
|
-
export declare const protocolFactory: ({ session, plugins, getTopics }: ProtocolFactoryOptions) => MeshProtocolProvider;
|
|
26
|
-
export interface Plugin {
|
|
27
|
-
createExtension: () => Extension;
|
|
28
|
-
}
|
|
29
|
-
export declare const createProtocolFactory: (topic: PublicKey, peerId: PublicKey, plugins: Plugin[]) => MeshProtocolProvider;
|
|
30
|
-
/**
|
|
31
|
-
* Creates a ProtocolProvider for simple transport connections with only one protocol plugin.
|
|
32
|
-
* @deprecated Use `createProtocolFactory`.
|
|
33
|
-
*/
|
|
34
|
-
export declare const transportProtocolProvider: (rendezvousKey: Buffer, peerId: Buffer, protocolPlugin: any) => MeshProtocolProvider;
|
|
35
|
-
export {};
|
|
36
|
-
//# sourceMappingURL=protocol-factory.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"protocol-factory.d.ts","sourceRoot":"","sources":["../../../src/protocol-factory.ts"],"names":[],"mappings":";AAOA,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAE1D,UAAU,sBAAsB;IAC9B,OAAO,EAAE,GAAG,EAAE,CAAC;IACf,SAAS,EAAE,MAAM,MAAM,EAAE,CAAC;IAC1B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC9B;AAED;;GAEG;AACH,oBAAY,oBAAoB,GAAG,CAAC,MAAM,EAAE;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,OAAO,CAAA;CAAE,KAAK,QAAQ,CAAC;AAEjG;;;;;;;;GAQG;AACH,eAAO,MAAM,eAAe,oCAIzB,sBAAsB,KAAG,oBAyB3B,CAAC;AAEF,MAAM,WAAW,MAAM;IACrB,eAAe,EAAE,MAAM,SAAS,CAAC;CAClC;AAED,eAAO,MAAM,qBAAqB,UAAW,SAAS,UAAU,SAAS,WAAW,MAAM,EAAE,yBAKxF,CAAC;AAEL;;;GAGG;AACH,eAAO,MAAM,yBAAyB,kBACrB,MAAM,UACb,MAAM,kBACE,GAAG,KAClB,oBAMF,CAAC"}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
|
-
import { EventEmitter } from 'node:events';
|
|
4
|
-
import { PublicKey } from '@dxos/keys';
|
|
5
|
-
import { Extension, Protocol } from '@dxos/mesh-protocol';
|
|
6
|
-
export declare const getPeerId: (protocol: Protocol) => Buffer | undefined;
|
|
7
|
-
/**
|
|
8
|
-
* @deprecated
|
|
9
|
-
*/
|
|
10
|
-
export declare const testProtocolProvider: (topic: Buffer, peerId: PublicKey, protocolPlugin: any) => import("../protocol-factory").MeshProtocolProvider;
|
|
11
|
-
/**
|
|
12
|
-
* Object Implementing a p2p protocol (interaction across a set of peers using a common protocol)
|
|
13
|
-
* and an associated transport connection protocol (interaction with each individual peer over a single network
|
|
14
|
-
* transport connection).
|
|
15
|
-
*/
|
|
16
|
-
export declare class TestProtocolPlugin extends EventEmitter {
|
|
17
|
-
/** @type {Map<string, {Protocol}>} */
|
|
18
|
-
private readonly _peers;
|
|
19
|
-
/** @type {Buffer} */
|
|
20
|
-
_peerId: Buffer;
|
|
21
|
-
/** @type {Boolean} */
|
|
22
|
-
_uppercase: boolean;
|
|
23
|
-
initCalled: boolean;
|
|
24
|
-
/**
|
|
25
|
-
* Test Protocol. Passes through messages unchanged, or folds to upper case.
|
|
26
|
-
* Provides a way to test that two protocols with different behavior are
|
|
27
|
-
* indeed being used with two different swarm keys.
|
|
28
|
-
* @param {Buffer} peerId
|
|
29
|
-
* @param {Boolean} uppercase Fold payload case to upper if set.
|
|
30
|
-
*/
|
|
31
|
-
constructor(peerId: Buffer, uppercase?: boolean);
|
|
32
|
-
get peerId(): Buffer;
|
|
33
|
-
get peers(): any[];
|
|
34
|
-
/**
|
|
35
|
-
* Factory function for Extensions (per-connection object).
|
|
36
|
-
* Called when a new peer transport connection is established.
|
|
37
|
-
* @return {Extension}
|
|
38
|
-
*/
|
|
39
|
-
createExtension(): Extension;
|
|
40
|
-
private _init;
|
|
41
|
-
/**
|
|
42
|
-
* Send/Receive messages with peer when initiating a request/response interaction.
|
|
43
|
-
* @param peerId Must be the value passed to the constructor on the responding node.
|
|
44
|
-
* @param payload Message to send
|
|
45
|
-
* @return Message received from peer in response to our request.
|
|
46
|
-
*/
|
|
47
|
-
send(peerId: Buffer, payload: string): Promise<string>;
|
|
48
|
-
_receive(protocol: Protocol, data: any): Promise<void>;
|
|
49
|
-
_onPeerConnect(protocol: Protocol): Promise<void>;
|
|
50
|
-
_onPeerDisconnect(protocol: Protocol): Promise<void>;
|
|
51
|
-
}
|
|
52
|
-
//# sourceMappingURL=test-protocol.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"test-protocol.d.ts","sourceRoot":"","sources":["../../../../src/testing/test-protocol.ts"],"names":[],"mappings":";;AAOA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAQ1D,eAAO,MAAM,SAAS,aAAc,QAAQ,uBAO3C,CAAC;AAEF;;GAEG;AAEH,eAAO,MAAM,oBAAoB,UAAW,MAAM,UAAU,SAAS,kBAAkB,GAAG,uDAQzF,CAAC;AAEF;;;;GAIG;AACH,qBAAa,kBAAmB,SAAQ,YAAY;IAClD,sCAAsC;IACtC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAa;IAEpC,qBAAqB;IACrB,OAAO,SAAC;IAER,sBAAsB;IACtB,UAAU,UAAC;IAEX,UAAU,UAAS;IAEnB;;;;;;OAMG;gBACS,MAAM,EAAE,MAAM,EAAE,SAAS,UAAQ;IAU7C,IAAI,MAAM,WAET;IAGD,IAAI,KAAK,UAER;IAED;;;;OAIG;IACH,eAAe;YAYD,KAAK;IAInB;;;;;OAKG;IACG,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAetD,QAAQ,CAAC,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG;IAYtC,cAAc,CAAC,QAAQ,EAAE,QAAQ;IAgBjC,iBAAiB,CAAC,QAAQ,EAAE,QAAQ;CAU3C"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"presence.test.d.ts","sourceRoot":"","sources":["../../../../src/tests/presence.test.ts"],"names":[],"mappings":""}
|
package/src/protocol-factory.ts
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2020 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import assert from 'node:assert';
|
|
6
|
-
|
|
7
|
-
import { discoveryKey } from '@dxos/crypto';
|
|
8
|
-
import { PublicKey } from '@dxos/keys';
|
|
9
|
-
import { log } from '@dxos/log';
|
|
10
|
-
import { Extension, Protocol } from '@dxos/mesh-protocol';
|
|
11
|
-
|
|
12
|
-
interface ProtocolFactoryOptions {
|
|
13
|
-
plugins: any[];
|
|
14
|
-
getTopics: () => Buffer[];
|
|
15
|
-
session: Record<string, any>;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* @deprecated Replaced by WireProtocolProvider.
|
|
20
|
-
*/
|
|
21
|
-
export type MeshProtocolProvider = (params: { channel: Buffer; initiator: boolean }) => Protocol;
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Returns a function that takes a channel parameter, returns a Protocol object
|
|
25
|
-
* with its context set to channel, plugins from plugins parameter and session
|
|
26
|
-
* set to session parameter.
|
|
27
|
-
*
|
|
28
|
-
* @param plugins array of Protocol plugin objects to add to created Protocol objects
|
|
29
|
-
* @param getTopics retrieve a list of known topic keys to encrypt by public key.
|
|
30
|
-
* @deprecated Use `createProtocolFactory`.
|
|
31
|
-
*/
|
|
32
|
-
export const protocolFactory = ({
|
|
33
|
-
session = {},
|
|
34
|
-
plugins = [],
|
|
35
|
-
getTopics
|
|
36
|
-
}: ProtocolFactoryOptions): MeshProtocolProvider => {
|
|
37
|
-
assert(getTopics);
|
|
38
|
-
|
|
39
|
-
// eslint-disable-next-line no-unused-vars
|
|
40
|
-
return ({ channel, initiator }) => {
|
|
41
|
-
log('creating protocol');
|
|
42
|
-
const protocol = new Protocol({
|
|
43
|
-
streamOptions: { live: true },
|
|
44
|
-
discoveryToPublicKey: (dk) => {
|
|
45
|
-
const publicKey = getTopics().find((topic) => discoveryKey(topic).equals(dk));
|
|
46
|
-
if (publicKey) {
|
|
47
|
-
protocol.setContext({ topic: PublicKey.stringify(publicKey) });
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
assert(publicKey, 'PublicKey not found in discovery.');
|
|
51
|
-
return publicKey;
|
|
52
|
-
},
|
|
53
|
-
initiator,
|
|
54
|
-
userSession: session,
|
|
55
|
-
discoveryKey: channel
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
protocol.setExtensions(plugins.map((plugin) => plugin.createExtension())).init();
|
|
59
|
-
return protocol;
|
|
60
|
-
};
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
export interface Plugin {
|
|
64
|
-
createExtension: () => Extension;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export const createProtocolFactory = (topic: PublicKey, peerId: PublicKey, plugins: Plugin[]) =>
|
|
68
|
-
protocolFactory({
|
|
69
|
-
getTopics: () => [topic.asBuffer()],
|
|
70
|
-
session: { peerId: PublicKey.stringify(peerId.asBuffer()) },
|
|
71
|
-
plugins
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Creates a ProtocolProvider for simple transport connections with only one protocol plugin.
|
|
76
|
-
* @deprecated Use `createProtocolFactory`.
|
|
77
|
-
*/
|
|
78
|
-
export const transportProtocolProvider = (
|
|
79
|
-
rendezvousKey: Buffer,
|
|
80
|
-
peerId: Buffer,
|
|
81
|
-
protocolPlugin: any
|
|
82
|
-
): MeshProtocolProvider => {
|
|
83
|
-
return protocolFactory({
|
|
84
|
-
getTopics: () => [rendezvousKey],
|
|
85
|
-
session: { peerId: PublicKey.stringify(peerId) },
|
|
86
|
-
plugins: [protocolPlugin]
|
|
87
|
-
});
|
|
88
|
-
};
|