@libp2p/interface-compliance-tests 1.0.7 → 1.1.2

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 (103) hide show
  1. package/dist/src/peer-discovery/index.d.ts.map +1 -1
  2. package/dist/src/peer-discovery/index.js +5 -5
  3. package/dist/src/peer-discovery/index.js.map +1 -1
  4. package/dist/src/pubsub/api.d.ts +2 -2
  5. package/dist/src/pubsub/api.d.ts.map +1 -1
  6. package/dist/src/pubsub/api.js +6 -6
  7. package/dist/src/pubsub/api.js.map +1 -1
  8. package/dist/src/pubsub/connection-handlers.d.ts +2 -2
  9. package/dist/src/pubsub/connection-handlers.d.ts.map +1 -1
  10. package/dist/src/pubsub/connection-handlers.js +18 -9
  11. package/dist/src/pubsub/connection-handlers.js.map +1 -1
  12. package/dist/src/pubsub/emit-self.d.ts +2 -2
  13. package/dist/src/pubsub/emit-self.d.ts.map +1 -1
  14. package/dist/src/pubsub/emit-self.js +6 -2
  15. package/dist/src/pubsub/emit-self.js.map +1 -1
  16. package/dist/src/pubsub/index.d.ts +9 -3
  17. package/dist/src/pubsub/index.d.ts.map +1 -1
  18. package/dist/src/pubsub/index.js.map +1 -1
  19. package/dist/src/pubsub/messages.d.ts +2 -2
  20. package/dist/src/pubsub/messages.d.ts.map +1 -1
  21. package/dist/src/pubsub/messages.js.map +1 -1
  22. package/dist/src/pubsub/multiple-nodes.d.ts +2 -2
  23. package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -1
  24. package/dist/src/pubsub/multiple-nodes.js +58 -35
  25. package/dist/src/pubsub/multiple-nodes.js.map +1 -1
  26. package/dist/src/pubsub/two-nodes.d.ts +2 -2
  27. package/dist/src/pubsub/two-nodes.d.ts.map +1 -1
  28. package/dist/src/pubsub/two-nodes.js +44 -19
  29. package/dist/src/pubsub/two-nodes.js.map +1 -1
  30. package/dist/src/stream-muxer/base-test.d.ts.map +1 -1
  31. package/dist/src/stream-muxer/base-test.js +23 -17
  32. package/dist/src/stream-muxer/base-test.js.map +1 -1
  33. package/dist/src/stream-muxer/close-test.d.ts.map +1 -1
  34. package/dist/src/stream-muxer/close-test.js +14 -26
  35. package/dist/src/stream-muxer/close-test.js.map +1 -1
  36. package/dist/src/stream-muxer/index.d.ts +2 -2
  37. package/dist/src/stream-muxer/index.d.ts.map +1 -1
  38. package/dist/src/stream-muxer/index.js.map +1 -1
  39. package/dist/src/stream-muxer/mega-stress-test.js +1 -1
  40. package/dist/src/stream-muxer/mega-stress-test.js.map +1 -1
  41. package/dist/src/stream-muxer/spawner.js +2 -2
  42. package/dist/src/stream-muxer/spawner.js.map +1 -1
  43. package/dist/src/stream-muxer/stress-test.js +1 -1
  44. package/dist/src/stream-muxer/stress-test.js.map +1 -1
  45. package/dist/src/transport/listen-test.d.ts.map +1 -1
  46. package/dist/src/transport/listen-test.js +10 -7
  47. package/dist/src/transport/listen-test.js.map +1 -1
  48. package/dist/src/transport/utils/index.d.ts +9 -2
  49. package/dist/src/transport/utils/index.d.ts.map +1 -1
  50. package/dist/src/transport/utils/index.js +60 -17
  51. package/dist/src/transport/utils/index.js.map +1 -1
  52. package/dist/src/utils/mock-connection-gater.d.ts +12 -0
  53. package/dist/src/utils/mock-connection-gater.d.ts.map +1 -0
  54. package/dist/src/utils/mock-connection-gater.js +14 -0
  55. package/dist/src/utils/mock-connection-gater.js.map +1 -0
  56. package/dist/src/utils/mock-connection-manager.d.ts +18 -0
  57. package/dist/src/utils/mock-connection-manager.d.ts.map +1 -0
  58. package/dist/src/utils/mock-connection-manager.js +22 -0
  59. package/dist/src/utils/mock-connection-manager.js.map +1 -0
  60. package/dist/src/utils/mock-connection.d.ts +5 -0
  61. package/dist/src/utils/mock-connection.d.ts.map +1 -0
  62. package/dist/src/utils/mock-connection.js +52 -0
  63. package/dist/src/utils/mock-connection.js.map +1 -0
  64. package/dist/src/utils/mock-multiaddr-connection.d.ts +4 -0
  65. package/dist/src/utils/mock-multiaddr-connection.d.ts.map +1 -0
  66. package/dist/src/utils/mock-multiaddr-connection.js +14 -0
  67. package/dist/src/utils/mock-multiaddr-connection.js.map +1 -0
  68. package/dist/src/utils/mock-muxer.d.ts +3 -0
  69. package/dist/src/utils/mock-muxer.d.ts.map +1 -0
  70. package/dist/src/utils/mock-muxer.js +37 -0
  71. package/dist/src/utils/mock-muxer.js.map +1 -0
  72. package/dist/src/utils/mock-registrar.d.ts +14 -0
  73. package/dist/src/utils/mock-registrar.d.ts.map +1 -0
  74. package/dist/src/utils/mock-registrar.js +38 -0
  75. package/dist/src/utils/mock-registrar.js.map +1 -0
  76. package/dist/src/utils/mock-upgrader.d.ts +7 -0
  77. package/dist/src/utils/mock-upgrader.d.ts.map +1 -0
  78. package/dist/src/utils/mock-upgrader.js +24 -0
  79. package/dist/src/utils/mock-upgrader.js.map +1 -0
  80. package/package.json +43 -8
  81. package/src/peer-discovery/index.ts +5 -7
  82. package/src/pubsub/api.ts +10 -11
  83. package/src/pubsub/connection-handlers.ts +22 -13
  84. package/src/pubsub/emit-self.ts +9 -5
  85. package/src/pubsub/index.ts +10 -3
  86. package/src/pubsub/messages.ts +3 -3
  87. package/src/pubsub/multiple-nodes.ts +68 -45
  88. package/src/pubsub/two-nodes.ts +48 -23
  89. package/src/stream-muxer/base-test.ts +37 -26
  90. package/src/stream-muxer/close-test.ts +14 -28
  91. package/src/stream-muxer/index.ts +2 -2
  92. package/src/stream-muxer/mega-stress-test.ts +1 -1
  93. package/src/stream-muxer/spawner.ts +2 -2
  94. package/src/stream-muxer/stress-test.ts +1 -1
  95. package/src/transport/listen-test.ts +10 -7
  96. package/src/transport/utils/index.ts +85 -24
  97. package/src/utils/mock-connection-gater.ts +14 -0
  98. package/src/utils/mock-connection-manager.ts +30 -0
  99. package/src/utils/mock-connection.ts +66 -0
  100. package/src/utils/mock-multiaddr-connection.ts +18 -0
  101. package/src/utils/mock-muxer.ts +43 -0
  102. package/src/utils/mock-registrar.ts +49 -0
  103. package/src/utils/mock-upgrader.ts +33 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/interface-compliance-tests",
3
- "version": "1.0.7",
3
+ "version": "1.1.2",
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",
@@ -73,8 +73,12 @@
73
73
  "types": "./dist/src/stream-muxer/index.d.ts"
74
74
  },
75
75
  "./topology": {
76
- "import": "./dist/src/topology/index.js",
77
- "types": "./dist/src/topology/index.d.ts"
76
+ "import": "./dist/src/topology/topology.js",
77
+ "types": "./dist/src/topology/topology.d.ts"
78
+ },
79
+ "./topology/multicodec-toplogy": {
80
+ "import": "./dist/src/topology/multicodec-toplogy.js",
81
+ "types": "./dist/src/topology/multicodec-toplogy.d.ts"
78
82
  },
79
83
  "./transport": {
80
84
  "import": "./dist/src/transport/index.js",
@@ -84,6 +88,38 @@
84
88
  "import": "./dist/src/transport/utils/index.js",
85
89
  "types": "./dist/src/transport/utils/index.d.ts"
86
90
  },
91
+ "./utils/mock-connection": {
92
+ "import": "./dist/src/utils/mock-connection.js",
93
+ "types": "./dist/src/utils/mock-connection.d.ts"
94
+ },
95
+ "./utils/mock-connection-gater": {
96
+ "import": "./dist/src/utils/mock-connection-gater.js",
97
+ "types": "./dist/src/utils/mock-connection-gater.d.ts"
98
+ },
99
+ "./utils/mock-connection-manager": {
100
+ "import": "./dist/src/utils/mock-connection-manager.js",
101
+ "types": "./dist/src/utils/mock-connection-manager.d.ts"
102
+ },
103
+ "./utils/mock-multiaddr-connection": {
104
+ "import": "./dist/src/utils/mock-multiaddr-connection.js",
105
+ "types": "./dist/src/utils/mock-multiaddr-connection.d.ts"
106
+ },
107
+ "./utils/mock-muxer": {
108
+ "import": "./dist/src/utils/mock-muxer.js",
109
+ "types": "./dist/src/utils/mock-muxer.d.ts"
110
+ },
111
+ "./utils/mock-peer-store": {
112
+ "import": "./dist/src/utils/mock-peer-store.js",
113
+ "types": "./dist/src/utils/mock-peer-store.d.ts"
114
+ },
115
+ "./utils/mock-registrar": {
116
+ "import": "./dist/src/utils/mock-registrar.js",
117
+ "types": "./dist/src/utils/mock-registrar.d.ts"
118
+ },
119
+ "./utils/mock-upgrader": {
120
+ "import": "./dist/src/utils/mock-upgrader.js",
121
+ "types": "./dist/src/utils/mock-upgrader.d.ts"
122
+ },
87
123
  "./utils/peers": {
88
124
  "import": "./dist/src/utils/peers.js",
89
125
  "types": "./dist/src/utils/peers.d.ts"
@@ -178,10 +214,10 @@
178
214
  },
179
215
  "scripts": {
180
216
  "lint": "aegir lint",
181
- "dep-check": "aegir dep-check dist/src/**/*.js",
217
+ "dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js",
182
218
  "build": "tsc",
183
219
  "pretest": "npm run build",
184
- "test": "aegir test -f ./dist/test/**/*.js",
220
+ "test": "aegir test -f ./dist/test/*.js -f ./dist/test/**/*.js",
185
221
  "test:chrome": "npm run test -- -t browser",
186
222
  "test:chrome-webworker": "npm run test -- -t webworker",
187
223
  "test:firefox": "npm run test -- -t browser -- --browser firefox",
@@ -190,13 +226,11 @@
190
226
  "test:electron-main": "npm run test -- -t electron-main"
191
227
  },
192
228
  "dependencies": {
193
- "@libp2p/connection": "^1.0.0",
194
229
  "@libp2p/crypto": "^0.22.2",
195
230
  "@libp2p/interfaces": "^1.0.0",
196
231
  "@libp2p/peer-id": "^1.0.0",
197
232
  "@libp2p/peer-id-factory": "^1.0.0",
198
- "@libp2p/pubsub": "^1.0.0",
199
- "@libp2p/topology": "^1.0.0",
233
+ "@libp2p/pubsub": "^1.1.0",
200
234
  "@multiformats/multiaddr": "^10.1.1",
201
235
  "abortable-iterator": "^4.0.0",
202
236
  "aegir": "^36.1.3",
@@ -207,6 +241,7 @@
207
241
  "it-map": "^1.0.6",
208
242
  "it-pair": "^2.0.0",
209
243
  "it-pipe": "^2.0.2",
244
+ "it-pushable": "^2.0.1",
210
245
  "multiformats": "^9.4.10",
211
246
  "p-defer": "^4.0.0",
212
247
  "p-limit": "^4.0.0",
@@ -16,9 +16,6 @@ export default (common: TestSetup<PeerDiscovery & Startable>) => {
16
16
 
17
17
  afterEach('ensure discovery was stopped', async () => {
18
18
  await discovery.stop()
19
-
20
- discovery.removeAllListeners()
21
-
22
19
  await common.teardown()
23
20
  })
24
21
 
@@ -44,7 +41,8 @@ export default (common: TestSetup<PeerDiscovery & Startable>) => {
44
41
  const defer = pDefer()
45
42
  await discovery.start()
46
43
 
47
- discovery.on('peer', ({ id, multiaddrs }) => {
44
+ discovery.addEventListener('peer', (evt) => {
45
+ const { id, multiaddrs } = evt.detail
48
46
  expect(id).to.exist()
49
47
  expect(id).to.have.property('type').that.is.oneOf(['RSA', 'Ed25519', 'secp256k1'])
50
48
  expect(multiaddrs).to.exist()
@@ -58,7 +56,7 @@ export default (common: TestSetup<PeerDiscovery & Startable>) => {
58
56
  })
59
57
 
60
58
  it('should not receive a peer event before start', async () => {
61
- discovery.on('peer', () => {
59
+ discovery.addEventListener('peer', () => {
62
60
  throw new Error('should not receive a peer event before start')
63
61
  })
64
62
 
@@ -70,14 +68,14 @@ export default (common: TestSetup<PeerDiscovery & Startable>) => {
70
68
 
71
69
  await discovery.start()
72
70
 
73
- discovery.on('peer', () => {
71
+ discovery.addEventListener('peer', () => {
74
72
  deferStart.resolve()
75
73
  })
76
74
 
77
75
  await deferStart.promise
78
76
  await discovery.stop()
79
77
 
80
- discovery.on('peer', () => {
78
+ discovery.addEventListener('peer', () => {
81
79
  throw new Error('should not receive a peer event after stop')
82
80
  })
83
81
 
package/src/pubsub/api.ts CHANGED
@@ -4,15 +4,15 @@ import pDefer from 'p-defer'
4
4
  import pWaitFor from 'p-wait-for'
5
5
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
6
6
  import type { TestSetup } from '../index.js'
7
- import type { PubSub, Message } from '@libp2p/interfaces/pubsub'
8
- import type { Startable } from '@libp2p/interfaces'
7
+ import type { PubSub } from '@libp2p/interfaces/pubsub'
8
+ import type { EventMap } from './index.js'
9
9
 
10
10
  const topic = 'foo'
11
11
  const data = uint8ArrayFromString('bar')
12
12
 
13
- export default (common: TestSetup<PubSub & Startable>) => {
13
+ export default (common: TestSetup<PubSub<EventMap>>) => {
14
14
  describe('pubsub api', () => {
15
- let pubsub: PubSub & Startable
15
+ let pubsub: PubSub<EventMap>
16
16
 
17
17
  // Create pubsub router
18
18
  beforeEach(async () => {
@@ -51,7 +51,7 @@ export default (common: TestSetup<PubSub & Startable>) => {
51
51
 
52
52
  await pubsub.start()
53
53
  pubsub.subscribe(topic)
54
- pubsub.on('topic', handler)
54
+ pubsub.addEventListener('topic', handler)
55
55
 
56
56
  await pWaitFor(() => {
57
57
  const topics = pubsub.getTopics()
@@ -71,15 +71,14 @@ export default (common: TestSetup<PubSub & Startable>) => {
71
71
  it('can subscribe and publish correctly', async () => {
72
72
  const defer = pDefer()
73
73
 
74
- const handler = (msg: Message) => {
75
- expect(msg).to.not.eql(undefined)
76
- defer.resolve()
77
- }
78
-
79
74
  await pubsub.start()
80
75
 
81
76
  pubsub.subscribe(topic)
82
- pubsub.on(topic, handler)
77
+ pubsub.addEventListener(topic, (evt) => {
78
+ const msg = evt.detail
79
+ expect(msg).to.not.eql(undefined)
80
+ defer.resolve()
81
+ })
83
82
  await pubsub.publish(topic, data)
84
83
  await defer.promise
85
84
 
@@ -7,12 +7,12 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
7
7
  import { expectSet } from './utils.js'
8
8
  import type { TestSetup } from '../index.js'
9
9
  import type { PubSub, Message } from '@libp2p/interfaces/pubsub'
10
- import type { Startable } from '@libp2p/interfaces'
10
+ import type { EventMap } from './index.js'
11
11
 
12
- export default (common: TestSetup<PubSub & Startable>) => {
12
+ export default (common: TestSetup<PubSub<EventMap>>) => {
13
13
  describe('pubsub connection handlers', () => {
14
- let psA: PubSub & Startable
15
- let psB: PubSub & Startable
14
+ let psA: PubSub<EventMap>
15
+ let psB: PubSub<EventMap>
16
16
 
17
17
  describe('nodes send state on connection', () => {
18
18
  // Create pubsub nodes and connect them
@@ -48,8 +48,12 @@ export default (common: TestSetup<PubSub & Startable>) => {
48
48
  await Promise.all([
49
49
  // @ts-expect-error protected fields
50
50
  psA._libp2p.dial(psB.peerId),
51
- new Promise((resolve) => psA.once('pubsub:subscription-change', resolve)),
52
- new Promise((resolve) => psB.once('pubsub:subscription-change', resolve))
51
+ new Promise((resolve) => psA.addEventListener('pubsub:subscription-change', resolve, {
52
+ once: true
53
+ })),
54
+ new Promise((resolve) => psB.addEventListener('pubsub:subscription-change', resolve, {
55
+ once: true
56
+ }))
53
57
  ])
54
58
 
55
59
  expect(psA.peers.size).to.equal(1)
@@ -103,7 +107,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
103
107
  let subscribedTopics = psA.getTopics()
104
108
  expect(subscribedTopics).to.not.include(topic)
105
109
 
106
- psA.on(topic, (msg) => {
110
+ psA.addEventListener(topic, (evt) => {
111
+ const msg = evt.detail
107
112
  expect(msg.data).to.equalBytes(data)
108
113
  defer.resolve()
109
114
  })
@@ -174,7 +179,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
174
179
  let subscribedTopics = psA.getTopics()
175
180
  expect(subscribedTopics).to.not.include(topic)
176
181
 
177
- psA.on(topic, (msg) => {
182
+ psA.addEventListener(topic, (evt) => {
183
+ const msg = evt.detail
178
184
  expect(msg.data).to.equalBytes(data)
179
185
  defer.resolve()
180
186
  })
@@ -228,7 +234,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
228
234
  let subscribedTopics = psA.getTopics()
229
235
  expect(subscribedTopics).to.not.include(topic)
230
236
 
231
- psA.on(topic, (msg) => {
237
+ psA.addEventListener(topic, (evt) => {
238
+ const msg = evt.detail
232
239
  expect(msg.data).to.equalBytes(data)
233
240
  counter++
234
241
  counter === 1 ? defer1.resolve() : defer2.resolve()
@@ -285,7 +292,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
285
292
  let bReceivedFirstMessageFromA = false
286
293
  let bReceivedSecondMessageFromA = false
287
294
 
288
- const handlerSpyA = (message: Message) => {
295
+ const handlerSpyA = (evt: CustomEvent<Message>) => {
296
+ const message = evt.detail
289
297
  const data = uint8ArrayToString(message.data)
290
298
 
291
299
  if (data === 'message-from-b-1') {
@@ -296,7 +304,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
296
304
  aReceivedSecondMessageFromB = true
297
305
  }
298
306
  }
299
- const handlerSpyB = (message: Message) => {
307
+ const handlerSpyB = (evt: CustomEvent<Message>) => {
308
+ const message = evt.detail
300
309
  const data = uint8ArrayToString(message.data)
301
310
 
302
311
  if (data === 'message-from-a-1') {
@@ -310,8 +319,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
310
319
 
311
320
  const topic = 'reconnect-channel'
312
321
 
313
- psA.on(topic, handlerSpyA)
314
- psB.on(topic, handlerSpyB)
322
+ psA.addEventListener(topic, handlerSpyA)
323
+ psB.addEventListener(topic, handlerSpyB)
315
324
  psA.subscribe(topic)
316
325
  psB.subscribe(topic)
317
326
 
@@ -3,15 +3,15 @@ import sinon from 'sinon'
3
3
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
4
4
  import type { TestSetup } from '../index.js'
5
5
  import type { PubSub, PubsubOptions } from '@libp2p/interfaces/pubsub'
6
- import type { Startable } from '@libp2p/interfaces'
6
+ import type { EventMap } from './index.js'
7
7
 
8
8
  const topic = 'foo'
9
9
  const data = uint8ArrayFromString('bar')
10
10
  const shouldNotHappen = () => expect.fail()
11
11
 
12
- export default (common: TestSetup<PubSub & Startable, Partial<PubsubOptions>>) => {
12
+ export default (common: TestSetup<PubSub<EventMap>, Partial<PubsubOptions>>) => {
13
13
  describe('emit self', () => {
14
- let pubsub: PubSub & Startable
14
+ let pubsub: PubSub<EventMap>
15
15
 
16
16
  describe('enabled', () => {
17
17
  before(async () => {
@@ -30,7 +30,9 @@ export default (common: TestSetup<PubSub & Startable, Partial<PubsubOptions>>) =
30
30
  })
31
31
 
32
32
  it('should emit to self on publish', async () => {
33
- const promise = new Promise((resolve) => pubsub.once(topic, resolve))
33
+ const promise = new Promise((resolve) => pubsub.addEventListener(topic, resolve, {
34
+ once: true
35
+ }))
34
36
 
35
37
  void pubsub.publish(topic, data)
36
38
 
@@ -55,7 +57,9 @@ export default (common: TestSetup<PubSub & Startable, Partial<PubsubOptions>>) =
55
57
  })
56
58
 
57
59
  it('should not emit to self on publish', async () => {
58
- pubsub.once(topic, () => shouldNotHappen)
60
+ pubsub.addEventListener(topic, () => shouldNotHappen, {
61
+ once: true
62
+ })
59
63
 
60
64
  void pubsub.publish(topic, data)
61
65
 
@@ -5,10 +5,17 @@ import connectionHandlersTest from './connection-handlers.js'
5
5
  import twoNodesTest from './two-nodes.js'
6
6
  import multipleNodesTest from './multiple-nodes.js'
7
7
  import type { TestSetup } from '../index.js'
8
- import type { PubSub } from '@libp2p/interfaces/pubsub'
9
- import type { Startable } from '@libp2p/interfaces'
8
+ import type { PubSub, Message, PubsubEvents } from '@libp2p/interfaces/pubsub'
10
9
 
11
- export default (common: TestSetup<PubSub & Startable>) => {
10
+ export interface EventMap extends PubsubEvents {
11
+ 'topic': CustomEvent<Message>
12
+ 'foo': CustomEvent<Message>
13
+ 'test-topic': CustomEvent<Message>
14
+ 'reconnect-channel': CustomEvent<Message>
15
+ 'Z': CustomEvent<Message>
16
+ }
17
+
18
+ export default (common: TestSetup<PubSub<EventMap>>) => {
12
19
  describe('interface-pubsub compliance tests', () => {
13
20
  apiTest(common)
14
21
  emitSelfTest(common)
@@ -6,14 +6,14 @@ import * as utils from '@libp2p/pubsub/utils'
6
6
  import { PeerStreams } from '@libp2p/pubsub/peer-streams'
7
7
  import type { TestSetup } from '../index.js'
8
8
  import type { PubSub } from '@libp2p/interfaces/pubsub'
9
- import type { Startable } from '@libp2p/interfaces'
9
+ import type { EventMap } from './index.js'
10
10
 
11
11
  const topic = 'foo'
12
12
  const data = uint8ArrayFromString('bar')
13
13
 
14
- export default (common: TestSetup<PubSub & Startable>) => {
14
+ export default (common: TestSetup<PubSub<EventMap>>) => {
15
15
  describe('messages', () => {
16
- let pubsub: PubSub & Startable
16
+ let pubsub: PubSub<EventMap>
17
17
 
18
18
  // Create pubsub router
19
19
  beforeEach(async () => {
@@ -9,18 +9,18 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
9
9
  import { expectSet } from './utils.js'
10
10
  import type { TestSetup } from '../index.js'
11
11
  import type { PubSub, Message } from '@libp2p/interfaces/pubsub'
12
- import type { Startable } from '@libp2p/interfaces'
12
+ import type { EventMap } from './index.js'
13
13
 
14
- export default (common: TestSetup<PubSub & Startable>) => {
14
+ export default (common: TestSetup<PubSub<EventMap>>) => {
15
15
  describe('pubsub with multiple nodes', function () {
16
16
  describe('every peer subscribes to the topic', () => {
17
17
  describe('line', () => {
18
18
  // line
19
19
  // ◉────◉────◉
20
20
  // a b c
21
- let psA: PubSub & Startable
22
- let psB: PubSub & Startable
23
- let psC: PubSub & Startable
21
+ let psA: PubSub<EventMap>
22
+ let psB: PubSub<EventMap>
23
+ let psC: PubSub<EventMap>
24
24
 
25
25
  // Create and start pubsub nodes
26
26
  beforeEach(async () => {
@@ -60,7 +60,9 @@ export default (common: TestSetup<PubSub & Startable>) => {
60
60
  psA.subscribe(topic)
61
61
  expectSet(psA.subscriptions, [topic])
62
62
 
63
- await new Promise((resolve) => psB.once('pubsub:subscription-change', resolve))
63
+ await new Promise((resolve) => psB.addEventListener('pubsub:subscription-change', resolve, {
64
+ once: true
65
+ }))
64
66
  expect(psB.peers.size).to.equal(2)
65
67
 
66
68
  const aPeerId = psA.peerId.toString()
@@ -76,8 +78,12 @@ export default (common: TestSetup<PubSub & Startable>) => {
76
78
  expectSet(psB.subscriptions, [topic])
77
79
 
78
80
  await Promise.all([
79
- new Promise((resolve) => psA.once('pubsub:subscription-change', resolve)),
80
- new Promise((resolve) => psC.once('pubsub:subscription-change', resolve))
81
+ new Promise((resolve) => psA.addEventListener('pubsub:subscription-change', resolve, {
82
+ once: true
83
+ })),
84
+ new Promise((resolve) => psC.addEventListener('pubsub:subscription-change', resolve, {
85
+ once: true
86
+ }))
81
87
  ])
82
88
 
83
89
  expect(psA.peers.size).to.equal(1)
@@ -94,12 +100,14 @@ export default (common: TestSetup<PubSub & Startable>) => {
94
100
  psC.subscribe(topic)
95
101
  expectSet(psC.subscriptions, [topic])
96
102
 
97
- psB.once('pubsub:subscription-change', () => {
103
+ psB.addEventListener('pubsub:subscription-change', () => {
98
104
  expect(psA.peers.size).to.equal(1)
99
105
  expect(psB.peers.size).to.equal(2)
100
106
  expectSet(psB.topics.get(topic), [psC.peerId.toString()])
101
107
 
102
108
  defer.resolve()
109
+ }, {
110
+ once: true
103
111
  })
104
112
 
105
113
  return await defer.promise
@@ -115,9 +123,15 @@ export default (common: TestSetup<PubSub & Startable>) => {
115
123
 
116
124
  // await subscription change
117
125
  await Promise.all([
118
- new Promise(resolve => psA.once('pubsub:subscription-change', () => resolve(null))),
119
- new Promise(resolve => psB.once('pubsub:subscription-change', () => resolve(null))),
120
- new Promise(resolve => psC.once('pubsub:subscription-change', () => resolve(null)))
126
+ new Promise(resolve => psA.addEventListener('pubsub:subscription-change', () => resolve(null), {
127
+ once: true
128
+ })),
129
+ new Promise(resolve => psB.addEventListener('pubsub:subscription-change', () => resolve(null), {
130
+ once: true
131
+ })),
132
+ new Promise(resolve => psC.addEventListener('pubsub:subscription-change', () => resolve(null), {
133
+ once: true
134
+ }))
121
135
  ])
122
136
 
123
137
  // await a cycle
@@ -125,22 +139,23 @@ export default (common: TestSetup<PubSub & Startable>) => {
125
139
 
126
140
  let counter = 0
127
141
 
128
- psA.on(topic, incMsg)
129
- psB.on(topic, incMsg)
130
- psC.on(topic, incMsg)
142
+ psA.addEventListener(topic, incMsg)
143
+ psB.addEventListener(topic, incMsg)
144
+ psC.addEventListener(topic, incMsg)
131
145
 
132
146
  void psA.publish(topic, uint8ArrayFromString('hey'))
133
147
 
134
- function incMsg (msg: Message) {
148
+ function incMsg (evt: CustomEvent<Message>) {
149
+ const msg = evt.detail
135
150
  expect(uint8ArrayToString(msg.data)).to.equal('hey')
136
151
  check()
137
152
  }
138
153
 
139
154
  function check () {
140
155
  if (++counter === 3) {
141
- psA.removeListener(topic, incMsg)
142
- psB.removeListener(topic, incMsg)
143
- psC.removeListener(topic, incMsg)
156
+ psA.removeEventListener(topic, incMsg)
157
+ psB.removeEventListener(topic, incMsg)
158
+ psC.removeEventListener(topic, incMsg)
144
159
  defer.resolve()
145
160
  }
146
161
  }
@@ -168,30 +183,37 @@ export default (common: TestSetup<PubSub & Startable>) => {
168
183
 
169
184
  // await subscription change
170
185
  await Promise.all([
171
- new Promise(resolve => psA.once('pubsub:subscription-change', () => resolve(null))),
172
- new Promise(resolve => psB.once('pubsub:subscription-change', () => resolve(null))),
173
- new Promise(resolve => psC.once('pubsub:subscription-change', () => resolve(null)))
186
+ new Promise(resolve => psA.addEventListener('pubsub:subscription-change', () => resolve(null), {
187
+ once: true
188
+ })),
189
+ new Promise(resolve => psB.addEventListener('pubsub:subscription-change', () => resolve(null), {
190
+ once: true
191
+ })),
192
+ new Promise(resolve => psC.addEventListener('pubsub:subscription-change', () => resolve(null), {
193
+ once: true
194
+ }))
174
195
  ])
175
196
 
176
- psA.on(topic, incMsg)
177
- psB.on(topic, incMsg)
178
- psC.on(topic, incMsg)
197
+ psA.addEventListener(topic, incMsg)
198
+ psB.addEventListener(topic, incMsg)
199
+ psC.addEventListener(topic, incMsg)
179
200
 
180
201
  // await a cycle
181
202
  await delay(1000)
182
203
 
183
204
  void psB.publish(topic, uint8ArrayFromString('hey'))
184
205
 
185
- function incMsg (msg: Message) {
206
+ function incMsg (evt: CustomEvent<Message>) {
207
+ const msg = evt.detail
186
208
  expect(uint8ArrayToString(msg.data)).to.equal('hey')
187
209
  check()
188
210
  }
189
211
 
190
212
  function check () {
191
213
  if (++counter === 3) {
192
- psA.removeListener(topic, incMsg)
193
- psB.removeListener(topic, incMsg)
194
- psC.removeListener(topic, incMsg)
214
+ psA.removeEventListener(topic, incMsg)
215
+ psB.removeEventListener(topic, incMsg)
216
+ psC.removeEventListener(topic, incMsg)
195
217
  defer.resolve()
196
218
  }
197
219
  }
@@ -209,11 +231,11 @@ export default (common: TestSetup<PubSub & Startable>) => {
209
231
  // │b d│
210
232
  // ◉─┘ └─◉
211
233
  // a
212
- let psA: PubSub & Startable
213
- let psB: PubSub & Startable
214
- let psC: PubSub & Startable
215
- let psD: PubSub & Startable
216
- let psE: PubSub & Startable
234
+ let psA: PubSub<EventMap>
235
+ let psB: PubSub<EventMap>
236
+ let psC: PubSub<EventMap>
237
+ let psD: PubSub<EventMap>
238
+ let psE: PubSub<EventMap>
217
239
 
218
240
  // Create and start pubsub nodes
219
241
  beforeEach(async () => {
@@ -271,22 +293,22 @@ export default (common: TestSetup<PubSub & Startable>) => {
271
293
  let counter = 0
272
294
 
273
295
  psA.subscribe('Z')
274
- psA.on('Z', incMsg)
296
+ psA.addEventListener('Z', incMsg)
275
297
  psB.subscribe('Z')
276
- psB.on('Z', incMsg)
298
+ psB.addEventListener('Z', incMsg)
277
299
  psC.subscribe('Z')
278
- psC.on('Z', incMsg)
300
+ psC.addEventListener('Z', incMsg)
279
301
  psD.subscribe('Z')
280
- psD.on('Z', incMsg)
302
+ psD.addEventListener('Z', incMsg)
281
303
  psE.subscribe('Z')
282
- psE.on('Z', incMsg)
304
+ psE.addEventListener('Z', incMsg)
283
305
 
284
306
  await Promise.all([
285
- new Promise((resolve) => psA.once('pubsub:subscription-change', resolve)),
286
- new Promise((resolve) => psB.once('pubsub:subscription-change', resolve)),
287
- new Promise((resolve) => psC.once('pubsub:subscription-change', resolve)),
288
- new Promise((resolve) => psD.once('pubsub:subscription-change', resolve)),
289
- new Promise((resolve) => psE.once('pubsub:subscription-change', resolve))
307
+ new Promise((resolve) => psA.addEventListener('pubsub:subscription-change', resolve)),
308
+ new Promise((resolve) => psB.addEventListener('pubsub:subscription-change', resolve)),
309
+ new Promise((resolve) => psC.addEventListener('pubsub:subscription-change', resolve)),
310
+ new Promise((resolve) => psD.addEventListener('pubsub:subscription-change', resolve)),
311
+ new Promise((resolve) => psE.addEventListener('pubsub:subscription-change', resolve))
290
312
  ])
291
313
 
292
314
  // await a cycle
@@ -294,7 +316,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
294
316
 
295
317
  void psC.publish('Z', uint8ArrayFromString('hey from c'))
296
318
 
297
- function incMsg (msg: Message) {
319
+ function incMsg (evt: CustomEvent<Message>) {
320
+ const msg = evt.detail
298
321
  expect(uint8ArrayToString(msg.data)).to.equal('hey from c')
299
322
  check()
300
323
  }