@libp2p/interface-compliance-tests 1.1.22 → 1.1.25
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/src/mocks/connection-manager.d.ts +0 -8
- package/dist/src/mocks/connection-manager.d.ts.map +1 -1
- package/dist/src/mocks/connection-manager.js +4 -16
- package/dist/src/mocks/connection-manager.js.map +1 -1
- package/dist/src/mocks/connection.d.ts +2 -1
- package/dist/src/mocks/connection.d.ts.map +1 -1
- package/dist/src/mocks/connection.js +4 -4
- package/dist/src/mocks/connection.js.map +1 -1
- package/dist/src/mocks/registrar.d.ts +2 -5
- package/dist/src/mocks/registrar.d.ts.map +1 -1
- package/dist/src/mocks/registrar.js +4 -4
- package/dist/src/mocks/registrar.js.map +1 -1
- package/dist/src/pubsub/api.d.ts +2 -2
- package/dist/src/pubsub/api.d.ts.map +1 -1
- package/dist/src/pubsub/api.js +18 -21
- package/dist/src/pubsub/api.js.map +1 -1
- package/dist/src/pubsub/connection-handlers.d.ts +2 -2
- package/dist/src/pubsub/connection-handlers.d.ts.map +1 -1
- package/dist/src/pubsub/connection-handlers.js +84 -162
- package/dist/src/pubsub/connection-handlers.js.map +1 -1
- package/dist/src/pubsub/emit-self.d.ts +2 -2
- package/dist/src/pubsub/emit-self.d.ts.map +1 -1
- package/dist/src/pubsub/emit-self.js +18 -12
- package/dist/src/pubsub/emit-self.js.map +1 -1
- package/dist/src/pubsub/index.d.ts +2 -3
- package/dist/src/pubsub/index.d.ts.map +1 -1
- package/dist/src/pubsub/index.js.map +1 -1
- package/dist/src/pubsub/messages.d.ts +2 -2
- package/dist/src/pubsub/messages.d.ts.map +1 -1
- package/dist/src/pubsub/messages.js +13 -74
- package/dist/src/pubsub/messages.js.map +1 -1
- package/dist/src/pubsub/multiple-nodes.d.ts +2 -2
- package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -1
- package/dist/src/pubsub/multiple-nodes.js +87 -167
- package/dist/src/pubsub/multiple-nodes.js.map +1 -1
- package/dist/src/pubsub/two-nodes.d.ts +2 -2
- package/dist/src/pubsub/two-nodes.d.ts.map +1 -1
- package/dist/src/pubsub/two-nodes.js +51 -84
- package/dist/src/pubsub/two-nodes.js.map +1 -1
- package/dist/src/pubsub/utils.d.ts +5 -2
- package/dist/src/pubsub/utils.d.ts.map +1 -1
- package/dist/src/pubsub/utils.js +12 -2
- package/dist/src/pubsub/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/mocks/connection-manager.ts +4 -20
- package/src/mocks/connection.ts +5 -5
- package/src/mocks/registrar.ts +6 -10
- package/src/pubsub/api.ts +20 -24
- package/src/pubsub/connection-handlers.ts +97 -180
- package/src/pubsub/emit-self.ts +23 -15
- package/src/pubsub/index.ts +2 -3
- package/src/pubsub/messages.ts +18 -99
- package/src/pubsub/multiple-nodes.ts +106 -184
- package/src/pubsub/two-nodes.ts +61 -98
- package/src/pubsub/utils.ts +16 -5
|
@@ -5,11 +5,8 @@ import pDefer from 'p-defer';
|
|
|
5
5
|
import pWaitFor from 'p-wait-for';
|
|
6
6
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
|
|
7
7
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
|
8
|
-
import { connectPeers
|
|
9
|
-
import {
|
|
10
|
-
import { CustomEvent } from '@libp2p/interfaces';
|
|
11
|
-
import { waitForSubscriptionUpdate } from './utils.js';
|
|
12
|
-
import { Components } from '@libp2p/interfaces/components';
|
|
8
|
+
import { connectPeers } from '../mocks/registrar.js';
|
|
9
|
+
import { createComponents, waitForSubscriptionUpdate } from './utils.js';
|
|
13
10
|
import { start, stop } from '../index.js';
|
|
14
11
|
const topic = 'foo';
|
|
15
12
|
function shouldNotHappen() {
|
|
@@ -19,30 +16,20 @@ export default (common) => {
|
|
|
19
16
|
describe('pubsub with two nodes', () => {
|
|
20
17
|
let psA;
|
|
21
18
|
let psB;
|
|
22
|
-
let
|
|
23
|
-
let
|
|
24
|
-
let registrarA;
|
|
25
|
-
let registrarB;
|
|
19
|
+
let componentsA;
|
|
20
|
+
let componentsB;
|
|
26
21
|
// Create pubsub nodes and connect them
|
|
27
22
|
beforeEach(async () => {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
registrarA = mockRegistrar();
|
|
31
|
-
registrarB = mockRegistrar();
|
|
23
|
+
componentsA = await createComponents();
|
|
24
|
+
componentsB = await createComponents();
|
|
32
25
|
psA = await common.setup({
|
|
33
|
-
components:
|
|
34
|
-
peerId: peerIdA,
|
|
35
|
-
registrar: registrarA
|
|
36
|
-
}),
|
|
26
|
+
components: componentsA,
|
|
37
27
|
init: {
|
|
38
28
|
emitSelf: true
|
|
39
29
|
}
|
|
40
30
|
});
|
|
41
31
|
psB = await common.setup({
|
|
42
|
-
components:
|
|
43
|
-
peerId: peerIdB,
|
|
44
|
-
registrar: registrarB
|
|
45
|
-
}),
|
|
32
|
+
components: componentsB,
|
|
46
33
|
init: {
|
|
47
34
|
emitSelf: false
|
|
48
35
|
}
|
|
@@ -51,13 +38,7 @@ export default (common) => {
|
|
|
51
38
|
await start(psA, psB);
|
|
52
39
|
expect(psA.getPeers()).to.be.empty();
|
|
53
40
|
expect(psB.getPeers()).to.be.empty();
|
|
54
|
-
await connectPeers(psA.multicodecs[0],
|
|
55
|
-
peerId: peerIdA,
|
|
56
|
-
registrar: registrarA
|
|
57
|
-
}, {
|
|
58
|
-
peerId: peerIdB,
|
|
59
|
-
registrar: registrarB
|
|
60
|
-
});
|
|
41
|
+
await connectPeers(psA.multicodecs[0], componentsA, componentsB);
|
|
61
42
|
// Wait for peers to be ready in pubsub
|
|
62
43
|
await pWaitFor(() => psA.getPeers().length === 1 && psB.getPeers().length === 1);
|
|
63
44
|
});
|
|
@@ -68,11 +49,11 @@ export default (common) => {
|
|
|
68
49
|
});
|
|
69
50
|
it('Subscribe to a topic in nodeA', async () => {
|
|
70
51
|
const defer = pDefer();
|
|
71
|
-
psB.addEventListener('
|
|
52
|
+
psB.addEventListener('subscription-change', (evt) => {
|
|
72
53
|
const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail;
|
|
73
54
|
expect(psA.getTopics()).to.deep.equal([topic]);
|
|
74
55
|
expect(psB.getPeers()).to.have.lengthOf(1);
|
|
75
|
-
expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([
|
|
56
|
+
expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([componentsA.getPeerId().toString()]);
|
|
76
57
|
expect(changedPeerId).to.deep.equal(psB.getPeers()[0]);
|
|
77
58
|
expect(changedSubs).to.have.lengthOf(1);
|
|
78
59
|
expect(changedSubs[0].topic).to.equal(topic);
|
|
@@ -86,80 +67,88 @@ export default (common) => {
|
|
|
86
67
|
});
|
|
87
68
|
it('Publish to a topic in nodeA', async () => {
|
|
88
69
|
const defer = pDefer();
|
|
89
|
-
psA.addEventListener(
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
70
|
+
psA.addEventListener('message', (evt) => {
|
|
71
|
+
if (evt.detail.topic === topic) {
|
|
72
|
+
const msg = evt.detail;
|
|
73
|
+
expect(uint8ArrayToString(msg.data)).to.equal('hey');
|
|
74
|
+
psB.removeEventListener('message', shouldNotHappen);
|
|
75
|
+
defer.resolve();
|
|
76
|
+
}
|
|
94
77
|
}, {
|
|
95
78
|
once: true
|
|
96
79
|
});
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
});
|
|
80
|
+
psA.subscribe(topic);
|
|
81
|
+
psB.subscribe(topic);
|
|
100
82
|
await Promise.all([
|
|
101
|
-
waitForSubscriptionUpdate(psA,
|
|
102
|
-
waitForSubscriptionUpdate(psB,
|
|
83
|
+
waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
|
|
84
|
+
waitForSubscriptionUpdate(psB, componentsA.getPeerId())
|
|
103
85
|
]);
|
|
104
|
-
|
|
86
|
+
await psA.publish(topic, uint8ArrayFromString('hey'));
|
|
105
87
|
return await defer.promise;
|
|
106
88
|
});
|
|
107
89
|
it('Publish to a topic in nodeB', async () => {
|
|
108
90
|
const defer = pDefer();
|
|
109
|
-
psA.addEventListener(
|
|
91
|
+
psA.addEventListener('message', (evt) => {
|
|
92
|
+
if (evt.detail.topic !== topic) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
110
95
|
const msg = evt.detail;
|
|
111
|
-
psA.addEventListener(
|
|
96
|
+
psA.addEventListener('message', (evt) => {
|
|
97
|
+
if (evt.detail.topic === topic) {
|
|
98
|
+
shouldNotHappen();
|
|
99
|
+
}
|
|
100
|
+
}, {
|
|
112
101
|
once: true
|
|
113
102
|
});
|
|
114
103
|
expect(uint8ArrayToString(msg.data)).to.equal('banana');
|
|
115
104
|
setTimeout(() => {
|
|
116
|
-
psA.removeEventListener(
|
|
117
|
-
psB.removeEventListener(
|
|
105
|
+
psA.removeEventListener('message');
|
|
106
|
+
psB.removeEventListener('message');
|
|
118
107
|
defer.resolve();
|
|
119
108
|
}, 100);
|
|
120
109
|
}, {
|
|
121
110
|
once: true
|
|
122
111
|
});
|
|
123
|
-
psB.addEventListener(
|
|
124
|
-
|
|
125
|
-
|
|
112
|
+
psB.addEventListener('message', shouldNotHappen);
|
|
113
|
+
psA.subscribe(topic);
|
|
114
|
+
psB.subscribe(topic);
|
|
126
115
|
await Promise.all([
|
|
127
|
-
waitForSubscriptionUpdate(psA,
|
|
128
|
-
waitForSubscriptionUpdate(psB,
|
|
116
|
+
waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
|
|
117
|
+
waitForSubscriptionUpdate(psB, componentsA.getPeerId())
|
|
129
118
|
]);
|
|
130
|
-
|
|
119
|
+
await psB.publish(topic, uint8ArrayFromString('banana'));
|
|
131
120
|
return await defer.promise;
|
|
132
121
|
});
|
|
133
122
|
it('Publish 10 msg to a topic in nodeB', async () => {
|
|
134
123
|
const defer = pDefer();
|
|
135
124
|
let counter = 0;
|
|
136
|
-
psB.addEventListener(
|
|
137
|
-
|
|
138
|
-
});
|
|
139
|
-
psA.addEventListener(topic, receivedMsg);
|
|
125
|
+
psB.addEventListener('message', shouldNotHappen);
|
|
126
|
+
psA.addEventListener('message', receivedMsg);
|
|
140
127
|
function receivedMsg(evt) {
|
|
141
128
|
const msg = evt.detail;
|
|
142
129
|
expect(uint8ArrayToString(msg.data)).to.equal('banana');
|
|
143
|
-
expect(msg.from.toString()).to.equal(
|
|
130
|
+
expect(msg.from.toString()).to.equal(componentsB.getPeerId().toString());
|
|
144
131
|
expect(msg.sequenceNumber).to.be.a('BigInt');
|
|
145
132
|
expect(msg.topic).to.be.equal(topic);
|
|
146
133
|
if (++counter === 10) {
|
|
147
|
-
psA.removeEventListener(
|
|
148
|
-
psB.removeEventListener(
|
|
134
|
+
psA.removeEventListener('message', receivedMsg);
|
|
135
|
+
psB.removeEventListener('message', shouldNotHappen);
|
|
149
136
|
defer.resolve();
|
|
150
137
|
}
|
|
151
138
|
}
|
|
139
|
+
psA.subscribe(topic);
|
|
140
|
+
psB.subscribe(topic);
|
|
152
141
|
await Promise.all([
|
|
153
|
-
waitForSubscriptionUpdate(psA,
|
|
154
|
-
waitForSubscriptionUpdate(psB,
|
|
142
|
+
waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
|
|
143
|
+
waitForSubscriptionUpdate(psB, componentsA.getPeerId())
|
|
155
144
|
]);
|
|
156
|
-
Array.from({ length: 10 }, (_, i) => psB.
|
|
145
|
+
await Promise.all(Array.from({ length: 10 }, async (_, i) => await psB.publish(topic, uint8ArrayFromString('banana'))));
|
|
157
146
|
return await defer.promise;
|
|
158
147
|
});
|
|
159
148
|
it('Unsubscribe from topic in nodeA', async () => {
|
|
160
149
|
const defer = pDefer();
|
|
161
150
|
let callCount = 0;
|
|
162
|
-
psB.addEventListener('
|
|
151
|
+
psB.addEventListener('subscription-change', (evt) => {
|
|
163
152
|
callCount++;
|
|
164
153
|
if (callCount === 1) {
|
|
165
154
|
// notice subscribe
|
|
@@ -189,28 +178,6 @@ export default (common) => {
|
|
|
189
178
|
expect(psA.getTopics()).to.be.empty();
|
|
190
179
|
return await defer.promise;
|
|
191
180
|
});
|
|
192
|
-
it.skip('Publish to a topic:Z in nodeA nodeB', async () => {
|
|
193
|
-
const defer = pDefer();
|
|
194
|
-
const topic = 'Z';
|
|
195
|
-
psA.addEventListener(topic, shouldNotHappen, {
|
|
196
|
-
once: true
|
|
197
|
-
});
|
|
198
|
-
psB.addEventListener(topic, shouldNotHappen, {
|
|
199
|
-
once: true
|
|
200
|
-
});
|
|
201
|
-
await Promise.all([
|
|
202
|
-
waitForSubscriptionUpdate(psA, psB),
|
|
203
|
-
waitForSubscriptionUpdate(psB, psA)
|
|
204
|
-
]);
|
|
205
|
-
setTimeout(() => {
|
|
206
|
-
psA.removeEventListener(topic, shouldNotHappen);
|
|
207
|
-
psB.removeEventListener(topic, shouldNotHappen);
|
|
208
|
-
defer.resolve();
|
|
209
|
-
}, 100);
|
|
210
|
-
void psB.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('banana') }));
|
|
211
|
-
void psA.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('banana') }));
|
|
212
|
-
return await defer.promise;
|
|
213
|
-
});
|
|
214
181
|
});
|
|
215
182
|
};
|
|
216
183
|
//# sourceMappingURL=two-nodes.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"two-nodes.js","sourceRoot":"","sources":["../../../src/pubsub/two-nodes.ts"],"names":[],"mappings":"AAAA,+CAA+C;AAC/C,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AACnC,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,MAAM,MAAM,SAAS,CAAA;AAC5B,OAAO,QAAQ,MAAM,YAAY,CAAA;AACjC,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AACtE,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,YAAY,EAAE,
|
|
1
|
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
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id';
|
|
4
|
+
export declare function waitForSubscriptionUpdate(a: PubSub, b: PeerId): Promise<void>;
|
|
5
|
+
export declare function createComponents(): Promise<Components>;
|
|
3
6
|
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/pubsub/utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/pubsub/utils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAA;AAC1D,OAAO,KAAK,EAAE,MAAM,EAA0B,MAAM,2BAA2B,CAAA;AAC/E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AAIxD,wBAAsB,yBAAyB,CAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,iBAMpE;AAED,wBAAsB,gBAAgB,IAAK,OAAO,CAAC,UAAU,CAAC,CAM7D"}
|
package/dist/src/pubsub/utils.js
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
import { pEvent } from 'p-event';
|
|
2
2
|
import pWaitFor from 'p-wait-for';
|
|
3
|
+
import { Components } from '@libp2p/interfaces/components';
|
|
4
|
+
import { createEd25519PeerId } from '@libp2p/peer-id-factory';
|
|
5
|
+
import { mockConnectionManager, mockRegistrar } from '../mocks/index.js';
|
|
3
6
|
export async function waitForSubscriptionUpdate(a, b) {
|
|
4
7
|
await pWaitFor(async () => {
|
|
5
|
-
const event = await pEvent(a, '
|
|
6
|
-
return event.detail.peerId.equals(b
|
|
8
|
+
const event = await pEvent(a, 'subscription-change');
|
|
9
|
+
return event.detail.peerId.equals(b);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
export async function createComponents() {
|
|
13
|
+
return new Components({
|
|
14
|
+
peerId: await createEd25519PeerId(),
|
|
15
|
+
registrar: mockRegistrar(),
|
|
16
|
+
connectionManager: mockConnectionManager()
|
|
7
17
|
});
|
|
8
18
|
}
|
|
9
19
|
//# sourceMappingURL=utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/pubsub/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAChC,OAAO,QAAQ,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/pubsub/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAChC,OAAO,QAAQ,MAAM,YAAY,CAAA;AACjC,OAAO,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAA;AAG1D,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAA;AAC7D,OAAO,EAAE,qBAAqB,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAExE,MAAM,CAAC,KAAK,UAAU,yBAAyB,CAAE,CAAS,EAAE,CAAS;IACnE,MAAM,QAAQ,CAAC,KAAK,IAAI,EAAE;QACxB,MAAM,KAAK,GAAG,MAAM,MAAM,CAA6D,CAAC,EAAE,qBAAqB,CAAC,CAAA;QAEhH,OAAO,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;IACtC,CAAC,CAAC,CAAA;AACJ,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,gBAAgB;IACpC,OAAO,IAAI,UAAU,CAAC;QACpB,MAAM,EAAE,MAAM,mBAAmB,EAAE;QACnC,SAAS,EAAE,aAAa,EAAE;QAC1B,iBAAiB,EAAE,qBAAqB,EAAE;KAC3C,CAAC,CAAA;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/interface-compliance-tests",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.25",
|
|
4
4
|
"description": "Compliance tests for JS libp2p interfaces",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p-interfaces/tree/master/packages/libp2p-interface-compliance-tests#readme",
|
|
@@ -5,35 +5,19 @@ import type { ConnectionManager, ConnectionManagerEvents } from '@libp2p/interfa
|
|
|
5
5
|
|
|
6
6
|
class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implements ConnectionManager {
|
|
7
7
|
getConnectionMap (): Map<string, Connection[]> {
|
|
8
|
-
|
|
8
|
+
return new Map<string, Connection[]>()
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
getConnectionList (): Connection[] {
|
|
12
|
-
|
|
12
|
+
return []
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
getConnections (): Connection[] {
|
|
16
|
-
|
|
16
|
+
return []
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
getConnection (peerId: PeerId): Connection | undefined {
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
listenerCount (type: string): number {
|
|
24
|
-
throw new Error('Method not implemented.')
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
addEventListener<U extends keyof ConnectionManagerEvents>(type: U, callback: ((evt: ConnectionManagerEvents[U]) => void) | { handleEvent: (evt: ConnectionManagerEvents[U]) => void } | null, options?: boolean | AddEventListenerOptions): void {
|
|
28
|
-
throw new Error('Method not implemented.')
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
removeEventListener<U extends keyof ConnectionManagerEvents>(type: U, callback: (((evt: ConnectionManagerEvents[U]) => void) | { handleEvent: (evt: ConnectionManagerEvents[U]) => void } | null) | undefined, options?: boolean | EventListenerOptions): void {
|
|
32
|
-
throw new Error('Method not implemented.')
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
dispatchEvent (event: Event): boolean {
|
|
36
|
-
throw new Error('Method not implemented.')
|
|
20
|
+
return undefined
|
|
37
21
|
}
|
|
38
22
|
}
|
|
39
23
|
|
package/src/mocks/connection.ts
CHANGED
|
@@ -184,18 +184,18 @@ export interface Peer {
|
|
|
184
184
|
registrar: Registrar
|
|
185
185
|
}
|
|
186
186
|
|
|
187
|
-
export function connectionPair (a:
|
|
187
|
+
export function connectionPair (a: Components, b: Components): [ Connection, Connection ] {
|
|
188
188
|
const [peerBtoPeerA, peerAtoPeerB] = duplexPair<Uint8Array>()
|
|
189
189
|
|
|
190
190
|
return [
|
|
191
191
|
mockConnection(
|
|
192
|
-
mockMultiaddrConnection(peerAtoPeerB, b.
|
|
193
|
-
registrar: a.
|
|
192
|
+
mockMultiaddrConnection(peerAtoPeerB, b.getPeerId()), {
|
|
193
|
+
registrar: a.getRegistrar()
|
|
194
194
|
}
|
|
195
195
|
),
|
|
196
196
|
mockConnection(
|
|
197
|
-
mockMultiaddrConnection(peerBtoPeerA, a.
|
|
198
|
-
registrar: b.
|
|
197
|
+
mockMultiaddrConnection(peerBtoPeerA, a.getPeerId()), {
|
|
198
|
+
registrar: b.getRegistrar()
|
|
199
199
|
}
|
|
200
200
|
)
|
|
201
201
|
]
|
package/src/mocks/registrar.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { Connection } from '@libp2p/interfaces/connection'
|
|
|
3
3
|
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
4
4
|
import type { Topology } from '@libp2p/interfaces/topology'
|
|
5
5
|
import { connectionPair } from './connection.js'
|
|
6
|
+
import type { Components } from '@libp2p/interfaces/src/components'
|
|
6
7
|
|
|
7
8
|
export class MockRegistrar implements Registrar {
|
|
8
9
|
private readonly topologies: Map<string, { topology: Topology, protocols: string[] }> = new Map()
|
|
@@ -106,20 +107,15 @@ export async function mockIncomingStreamEvent (protocol: string, conn: Connectio
|
|
|
106
107
|
}
|
|
107
108
|
}
|
|
108
109
|
|
|
109
|
-
export
|
|
110
|
-
peerId: PeerId
|
|
111
|
-
registrar: Registrar
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export async function connectPeers (protocol: string, a: Peer, b: Peer) {
|
|
110
|
+
export async function connectPeers (protocol: string, a: Components, b: Components) {
|
|
115
111
|
// Notify peers of connection
|
|
116
112
|
const [aToB, bToA] = connectionPair(a, b)
|
|
117
113
|
|
|
118
|
-
for (const topology of a.
|
|
119
|
-
await topology.onConnect(b.
|
|
114
|
+
for (const topology of a.getRegistrar().getTopologies(protocol)) {
|
|
115
|
+
await topology.onConnect(b.getPeerId(), aToB)
|
|
120
116
|
}
|
|
121
117
|
|
|
122
|
-
for (const topology of b.
|
|
123
|
-
await topology.onConnect(a.
|
|
118
|
+
for (const topology of b.getRegistrar().getTopologies(protocol)) {
|
|
119
|
+
await topology.onConnect(a.getPeerId(), bToA)
|
|
124
120
|
}
|
|
125
121
|
}
|
package/src/pubsub/api.ts
CHANGED
|
@@ -3,34 +3,27 @@ import sinon from 'sinon'
|
|
|
3
3
|
import pDefer from 'p-defer'
|
|
4
4
|
import pWaitFor from 'p-wait-for'
|
|
5
5
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
6
|
-
import { mockRegistrar } from '../mocks/registrar.js'
|
|
7
|
-
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
8
6
|
import delay from 'delay'
|
|
9
|
-
import { CustomEvent } from '@libp2p/interfaces'
|
|
10
7
|
import type { TestSetup } from '../index.js'
|
|
11
8
|
import type { PubSub } from '@libp2p/interfaces/pubsub'
|
|
12
9
|
import type { PubSubArgs } from './index.js'
|
|
13
|
-
import type {
|
|
14
|
-
import
|
|
15
|
-
import { Components } from '@libp2p/interfaces/components'
|
|
10
|
+
import type { Components } from '@libp2p/interfaces/components'
|
|
11
|
+
import { createComponents } from './utils.js'
|
|
16
12
|
|
|
17
13
|
const topic = 'foo'
|
|
18
14
|
const data = uint8ArrayFromString('bar')
|
|
19
15
|
|
|
20
|
-
export default (common: TestSetup<
|
|
16
|
+
export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
21
17
|
describe('pubsub api', () => {
|
|
22
18
|
let pubsub: PubSub
|
|
23
|
-
let
|
|
19
|
+
let components: Components
|
|
24
20
|
|
|
25
21
|
// Create pubsub router
|
|
26
22
|
beforeEach(async () => {
|
|
27
|
-
|
|
23
|
+
components = await createComponents()
|
|
28
24
|
|
|
29
25
|
pubsub = await common.setup({
|
|
30
|
-
components
|
|
31
|
-
peerId: await createEd25519PeerId(),
|
|
32
|
-
registrar
|
|
33
|
-
}),
|
|
26
|
+
components,
|
|
34
27
|
init: {
|
|
35
28
|
emitSelf: true
|
|
36
29
|
}
|
|
@@ -44,22 +37,22 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
44
37
|
})
|
|
45
38
|
|
|
46
39
|
it('can start correctly', async () => {
|
|
47
|
-
sinon.spy(
|
|
40
|
+
sinon.spy(components.getRegistrar(), 'register')
|
|
48
41
|
|
|
49
42
|
await pubsub.start()
|
|
50
43
|
|
|
51
44
|
expect(pubsub.isStarted()).to.equal(true)
|
|
52
|
-
expect(
|
|
45
|
+
expect(components.getRegistrar().register).to.have.property('callCount', 1)
|
|
53
46
|
})
|
|
54
47
|
|
|
55
48
|
it('can stop correctly', async () => {
|
|
56
|
-
sinon.spy(
|
|
49
|
+
sinon.spy(components.getRegistrar(), 'unregister')
|
|
57
50
|
|
|
58
51
|
await pubsub.start()
|
|
59
52
|
await pubsub.stop()
|
|
60
53
|
|
|
61
54
|
expect(pubsub.isStarted()).to.equal(false)
|
|
62
|
-
expect(
|
|
55
|
+
expect(components.getRegistrar().unregister).to.have.property('callCount', 1)
|
|
63
56
|
})
|
|
64
57
|
|
|
65
58
|
it('can subscribe and unsubscribe correctly', async () => {
|
|
@@ -68,19 +61,21 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
68
61
|
}
|
|
69
62
|
|
|
70
63
|
await pubsub.start()
|
|
71
|
-
pubsub.
|
|
64
|
+
pubsub.subscribe(topic)
|
|
65
|
+
pubsub.addEventListener('message', handler)
|
|
72
66
|
|
|
73
67
|
await pWaitFor(() => {
|
|
74
68
|
const topics = pubsub.getTopics()
|
|
75
69
|
return topics.length === 1 && topics[0] === topic
|
|
76
70
|
})
|
|
77
71
|
|
|
78
|
-
pubsub.removeEventListener(
|
|
72
|
+
pubsub.removeEventListener('message', handler)
|
|
73
|
+
pubsub.unsubscribe(topic)
|
|
79
74
|
|
|
80
75
|
await pWaitFor(() => pubsub.getTopics().length === 0)
|
|
81
76
|
|
|
82
77
|
// Publish to guarantee the handler is not called
|
|
83
|
-
pubsub.
|
|
78
|
+
await pubsub.publish(topic, data)
|
|
84
79
|
|
|
85
80
|
// handlers are called async
|
|
86
81
|
await delay(100)
|
|
@@ -93,12 +88,13 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
93
88
|
|
|
94
89
|
await pubsub.start()
|
|
95
90
|
|
|
96
|
-
pubsub.
|
|
97
|
-
|
|
98
|
-
expect(
|
|
91
|
+
pubsub.subscribe(topic)
|
|
92
|
+
pubsub.addEventListener('message', (evt) => {
|
|
93
|
+
expect(evt).to.have.nested.property('detail.topic', topic)
|
|
94
|
+
expect(evt).to.have.deep.nested.property('detail.data', data)
|
|
99
95
|
defer.resolve()
|
|
100
96
|
})
|
|
101
|
-
pubsub.
|
|
97
|
+
await pubsub.publish(topic, data)
|
|
102
98
|
await defer.promise
|
|
103
99
|
|
|
104
100
|
await pubsub.stop()
|