@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.
Files changed (55) hide show
  1. package/dist/src/mocks/connection-manager.d.ts +0 -8
  2. package/dist/src/mocks/connection-manager.d.ts.map +1 -1
  3. package/dist/src/mocks/connection-manager.js +4 -16
  4. package/dist/src/mocks/connection-manager.js.map +1 -1
  5. package/dist/src/mocks/connection.d.ts +2 -1
  6. package/dist/src/mocks/connection.d.ts.map +1 -1
  7. package/dist/src/mocks/connection.js +4 -4
  8. package/dist/src/mocks/connection.js.map +1 -1
  9. package/dist/src/mocks/registrar.d.ts +2 -5
  10. package/dist/src/mocks/registrar.d.ts.map +1 -1
  11. package/dist/src/mocks/registrar.js +4 -4
  12. package/dist/src/mocks/registrar.js.map +1 -1
  13. package/dist/src/pubsub/api.d.ts +2 -2
  14. package/dist/src/pubsub/api.d.ts.map +1 -1
  15. package/dist/src/pubsub/api.js +18 -21
  16. package/dist/src/pubsub/api.js.map +1 -1
  17. package/dist/src/pubsub/connection-handlers.d.ts +2 -2
  18. package/dist/src/pubsub/connection-handlers.d.ts.map +1 -1
  19. package/dist/src/pubsub/connection-handlers.js +84 -162
  20. package/dist/src/pubsub/connection-handlers.js.map +1 -1
  21. package/dist/src/pubsub/emit-self.d.ts +2 -2
  22. package/dist/src/pubsub/emit-self.d.ts.map +1 -1
  23. package/dist/src/pubsub/emit-self.js +18 -12
  24. package/dist/src/pubsub/emit-self.js.map +1 -1
  25. package/dist/src/pubsub/index.d.ts +2 -3
  26. package/dist/src/pubsub/index.d.ts.map +1 -1
  27. package/dist/src/pubsub/index.js.map +1 -1
  28. package/dist/src/pubsub/messages.d.ts +2 -2
  29. package/dist/src/pubsub/messages.d.ts.map +1 -1
  30. package/dist/src/pubsub/messages.js +13 -74
  31. package/dist/src/pubsub/messages.js.map +1 -1
  32. package/dist/src/pubsub/multiple-nodes.d.ts +2 -2
  33. package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -1
  34. package/dist/src/pubsub/multiple-nodes.js +87 -167
  35. package/dist/src/pubsub/multiple-nodes.js.map +1 -1
  36. package/dist/src/pubsub/two-nodes.d.ts +2 -2
  37. package/dist/src/pubsub/two-nodes.d.ts.map +1 -1
  38. package/dist/src/pubsub/two-nodes.js +51 -84
  39. package/dist/src/pubsub/two-nodes.js.map +1 -1
  40. package/dist/src/pubsub/utils.d.ts +5 -2
  41. package/dist/src/pubsub/utils.d.ts.map +1 -1
  42. package/dist/src/pubsub/utils.js +12 -2
  43. package/dist/src/pubsub/utils.js.map +1 -1
  44. package/package.json +1 -1
  45. package/src/mocks/connection-manager.ts +4 -20
  46. package/src/mocks/connection.ts +5 -5
  47. package/src/mocks/registrar.ts +6 -10
  48. package/src/pubsub/api.ts +20 -24
  49. package/src/pubsub/connection-handlers.ts +97 -180
  50. package/src/pubsub/emit-self.ts +23 -15
  51. package/src/pubsub/index.ts +2 -3
  52. package/src/pubsub/messages.ts +18 -99
  53. package/src/pubsub/multiple-nodes.ts +106 -184
  54. package/src/pubsub/two-nodes.ts +61 -98
  55. package/src/pubsub/utils.ts +16 -5
@@ -5,17 +5,12 @@ 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, mockRegistrar } from '../mocks/registrar.js'
9
- import { createEd25519PeerId } from '@libp2p/peer-id-factory'
10
- import { CustomEvent } from '@libp2p/interfaces'
11
- import { waitForSubscriptionUpdate } from './utils.js'
8
+ import { connectPeers } from '../mocks/registrar.js'
9
+ import { createComponents, waitForSubscriptionUpdate } from './utils.js'
12
10
  import type { TestSetup } from '../index.js'
13
- import type { Message } from '@libp2p/interfaces/pubsub'
11
+ import type { Message, PubSub } from '@libp2p/interfaces/pubsub'
14
12
  import type { PubSubArgs } from './index.js'
15
- import type { PeerId } from '@libp2p/interfaces/peer-id'
16
- import type { Registrar } from '@libp2p/interfaces/registrar'
17
- import type { PubSubBaseProtocol } from '@libp2p/pubsub'
18
- import { Components } from '@libp2p/interfaces/components'
13
+ import type { Components } from '@libp2p/interfaces/components'
19
14
  import { start, stop } from '../index.js'
20
15
 
21
16
  const topic = 'foo'
@@ -24,37 +19,26 @@ function shouldNotHappen () {
24
19
  expect.fail()
25
20
  }
26
21
 
27
- export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
22
+ export default (common: TestSetup<PubSub, PubSubArgs>) => {
28
23
  describe('pubsub with two nodes', () => {
29
- let psA: PubSubBaseProtocol
30
- let psB: PubSubBaseProtocol
31
- let peerIdA: PeerId
32
- let peerIdB: PeerId
33
- let registrarA: Registrar
34
- let registrarB: Registrar
24
+ let psA: PubSub
25
+ let psB: PubSub
26
+ let componentsA: Components
27
+ let componentsB: Components
35
28
 
36
29
  // Create pubsub nodes and connect them
37
30
  beforeEach(async () => {
38
- peerIdA = await createEd25519PeerId()
39
- peerIdB = await createEd25519PeerId()
40
-
41
- registrarA = mockRegistrar()
42
- registrarB = mockRegistrar()
31
+ componentsA = await createComponents()
32
+ componentsB = await createComponents()
43
33
 
44
34
  psA = await common.setup({
45
- components: new Components({
46
- peerId: peerIdA,
47
- registrar: registrarA
48
- }),
35
+ components: componentsA,
49
36
  init: {
50
37
  emitSelf: true
51
38
  }
52
39
  })
53
40
  psB = await common.setup({
54
- components: new Components({
55
- peerId: peerIdB,
56
- registrar: registrarB
57
- }),
41
+ components: componentsB,
58
42
  init: {
59
43
  emitSelf: false
60
44
  }
@@ -66,13 +50,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
66
50
  expect(psA.getPeers()).to.be.empty()
67
51
  expect(psB.getPeers()).to.be.empty()
68
52
 
69
- await connectPeers(psA.multicodecs[0], {
70
- peerId: peerIdA,
71
- registrar: registrarA
72
- }, {
73
- peerId: peerIdB,
74
- registrar: registrarB
75
- })
53
+ await connectPeers(psA.multicodecs[0], componentsA, componentsB)
76
54
 
77
55
  // Wait for peers to be ready in pubsub
78
56
  await pWaitFor(() => psA.getPeers().length === 1 && psB.getPeers().length === 1)
@@ -89,11 +67,11 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
89
67
  it('Subscribe to a topic in nodeA', async () => {
90
68
  const defer = pDefer()
91
69
 
92
- psB.addEventListener('pubsub:subscription-change', (evt) => {
70
+ psB.addEventListener('subscription-change', (evt) => {
93
71
  const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
94
72
  expect(psA.getTopics()).to.deep.equal([topic])
95
73
  expect(psB.getPeers()).to.have.lengthOf(1)
96
- expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdA.toString()])
74
+ expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([componentsA.getPeerId().toString()])
97
75
  expect(changedPeerId).to.deep.equal(psB.getPeers()[0])
98
76
  expect(changedSubs).to.have.lengthOf(1)
99
77
  expect(changedSubs[0].topic).to.equal(topic)
@@ -110,25 +88,26 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
110
88
  it('Publish to a topic in nodeA', async () => {
111
89
  const defer = pDefer()
112
90
 
113
- psA.addEventListener(topic, (evt) => {
114
- const msg = evt.detail
115
- expect(uint8ArrayToString(msg.data)).to.equal('hey')
116
- psB.removeEventListener(topic, shouldNotHappen)
117
- defer.resolve()
91
+ psA.addEventListener('message', (evt) => {
92
+ if (evt.detail.topic === topic) {
93
+ const msg = evt.detail
94
+ expect(uint8ArrayToString(msg.data)).to.equal('hey')
95
+ psB.removeEventListener('message', shouldNotHappen)
96
+ defer.resolve()
97
+ }
118
98
  }, {
119
99
  once: true
120
100
  })
121
101
 
122
- psB.addEventListener(topic, shouldNotHappen, {
123
- once: true
124
- })
102
+ psA.subscribe(topic)
103
+ psB.subscribe(topic)
125
104
 
126
105
  await Promise.all([
127
- waitForSubscriptionUpdate(psA, psB),
128
- waitForSubscriptionUpdate(psB, psA)
106
+ waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
107
+ waitForSubscriptionUpdate(psB, componentsA.getPeerId())
129
108
  ])
130
109
 
131
- void psA.dispatchEvent(new CustomEvent<Uint8Array>(topic, { detail: uint8ArrayFromString('hey') }))
110
+ await psA.publish(topic, uint8ArrayFromString('hey'))
132
111
 
133
112
  return await defer.promise
134
113
  })
@@ -136,16 +115,24 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
136
115
  it('Publish to a topic in nodeB', async () => {
137
116
  const defer = pDefer()
138
117
 
139
- psA.addEventListener(topic, (evt) => {
118
+ psA.addEventListener('message', (evt) => {
119
+ if (evt.detail.topic !== topic) {
120
+ return
121
+ }
122
+
140
123
  const msg = evt.detail
141
- psA.addEventListener(topic, shouldNotHappen, {
124
+ psA.addEventListener('message', (evt) => {
125
+ if (evt.detail.topic === topic) {
126
+ shouldNotHappen()
127
+ }
128
+ }, {
142
129
  once: true
143
130
  })
144
131
  expect(uint8ArrayToString(msg.data)).to.equal('banana')
145
132
 
146
133
  setTimeout(() => {
147
- psA.removeEventListener(topic, shouldNotHappen)
148
- psB.removeEventListener(topic, shouldNotHappen)
134
+ psA.removeEventListener('message')
135
+ psB.removeEventListener('message')
149
136
 
150
137
  defer.resolve()
151
138
  }, 100)
@@ -153,16 +140,17 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
153
140
  once: true
154
141
  })
155
142
 
156
- psB.addEventListener(topic, shouldNotHappen, {
157
- once: true
158
- })
143
+ psB.addEventListener('message', shouldNotHappen)
144
+
145
+ psA.subscribe(topic)
146
+ psB.subscribe(topic)
159
147
 
160
148
  await Promise.all([
161
- waitForSubscriptionUpdate(psA, psB),
162
- waitForSubscriptionUpdate(psB, psA)
149
+ waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
150
+ waitForSubscriptionUpdate(psB, componentsA.getPeerId())
163
151
  ])
164
152
 
165
- void psB.dispatchEvent(new CustomEvent<Uint8Array>(topic, { detail: uint8ArrayFromString('banana') }))
153
+ await psB.publish(topic, uint8ArrayFromString('banana'))
166
154
 
167
155
  return await defer.promise
168
156
  })
@@ -171,32 +159,35 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
171
159
  const defer = pDefer()
172
160
  let counter = 0
173
161
 
174
- psB.addEventListener(topic, shouldNotHappen, {
175
- once: true
176
- })
177
- psA.addEventListener(topic, receivedMsg)
162
+ psB.addEventListener('message', shouldNotHappen)
163
+ psA.addEventListener('message', receivedMsg)
178
164
 
179
165
  function receivedMsg (evt: CustomEvent<Message>) {
180
166
  const msg = evt.detail
181
167
  expect(uint8ArrayToString(msg.data)).to.equal('banana')
182
- expect(msg.from.toString()).to.equal(peerIdB.toString())
168
+ expect(msg.from.toString()).to.equal(componentsB.getPeerId().toString())
183
169
  expect(msg.sequenceNumber).to.be.a('BigInt')
184
170
  expect(msg.topic).to.be.equal(topic)
185
171
 
186
172
  if (++counter === 10) {
187
- psA.removeEventListener(topic, receivedMsg)
188
- psB.removeEventListener(topic, shouldNotHappen)
173
+ psA.removeEventListener('message', receivedMsg)
174
+ psB.removeEventListener('message', shouldNotHappen)
189
175
 
190
176
  defer.resolve()
191
177
  }
192
178
  }
193
179
 
180
+ psA.subscribe(topic)
181
+ psB.subscribe(topic)
182
+
194
183
  await Promise.all([
195
- waitForSubscriptionUpdate(psA, psB),
196
- waitForSubscriptionUpdate(psB, psA)
184
+ waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
185
+ waitForSubscriptionUpdate(psB, componentsA.getPeerId())
197
186
  ])
198
187
 
199
- Array.from({ length: 10 }, (_, i) => psB.dispatchEvent(new CustomEvent<Uint8Array>(topic, { detail: uint8ArrayFromString('banana') })))
188
+ await Promise.all(
189
+ Array.from({ length: 10 }, async (_, i) => await psB.publish(topic, uint8ArrayFromString('banana')))
190
+ )
200
191
 
201
192
  return await defer.promise
202
193
  })
@@ -205,7 +196,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
205
196
  const defer = pDefer()
206
197
  let callCount = 0
207
198
 
208
- psB.addEventListener('pubsub:subscription-change', (evt) => {
199
+ psB.addEventListener('subscription-change', (evt) => {
209
200
  callCount++
210
201
 
211
202
  if (callCount === 1) {
@@ -239,33 +230,5 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
239
230
 
240
231
  return await defer.promise
241
232
  })
242
-
243
- it.skip('Publish to a topic:Z in nodeA nodeB', async () => {
244
- const defer = pDefer()
245
- const topic = 'Z'
246
-
247
- psA.addEventListener(topic, shouldNotHappen, {
248
- once: true
249
- })
250
- psB.addEventListener(topic, shouldNotHappen, {
251
- once: true
252
- })
253
-
254
- await Promise.all([
255
- waitForSubscriptionUpdate(psA, psB),
256
- waitForSubscriptionUpdate(psB, psA)
257
- ])
258
-
259
- setTimeout(() => {
260
- psA.removeEventListener(topic, shouldNotHappen)
261
- psB.removeEventListener(topic, shouldNotHappen)
262
- defer.resolve()
263
- }, 100)
264
-
265
- void psB.dispatchEvent(new CustomEvent<Uint8Array>(topic, { detail: uint8ArrayFromString('banana') }))
266
- void psA.dispatchEvent(new CustomEvent<Uint8Array>(topic, { detail: uint8ArrayFromString('banana') }))
267
-
268
- return await defer.promise
269
- })
270
233
  })
271
234
  }
@@ -1,12 +1,23 @@
1
1
  import { pEvent } from 'p-event'
2
2
  import pWaitFor from 'p-wait-for'
3
- import type { SubscriptionChangeData } from '@libp2p/interfaces/pubsub'
4
- import type { PubSubBaseProtocol } from '@libp2p/pubsub'
3
+ import { Components } from '@libp2p/interfaces/components'
4
+ import type { PubSub, SubscriptionChangeData } from '@libp2p/interfaces/pubsub'
5
+ import type { PeerId } from '@libp2p/interfaces/peer-id'
6
+ import { createEd25519PeerId } from '@libp2p/peer-id-factory'
7
+ import { mockConnectionManager, mockRegistrar } from '../mocks/index.js'
5
8
 
6
- export async function waitForSubscriptionUpdate (a: PubSubBaseProtocol, b: PubSubBaseProtocol) {
9
+ export async function waitForSubscriptionUpdate (a: PubSub, b: PeerId) {
7
10
  await pWaitFor(async () => {
8
- const event = await pEvent<'pubsub:subscription-change', CustomEvent<SubscriptionChangeData>>(a, 'pubsub:subscription-change')
11
+ const event = await pEvent<'subscription-change', CustomEvent<SubscriptionChangeData>>(a, 'subscription-change')
9
12
 
10
- return event.detail.peerId.equals(b.components.getPeerId())
13
+ return event.detail.peerId.equals(b)
14
+ })
15
+ }
16
+
17
+ export async function createComponents (): Promise<Components> {
18
+ return new Components({
19
+ peerId: await createEd25519PeerId(),
20
+ registrar: mockRegistrar(),
21
+ connectionManager: mockConnectionManager()
11
22
  })
12
23
  }