@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
@@ -11,7 +11,7 @@ import {
11
11
  first,
12
12
  expectSet
13
13
  } from './utils.js'
14
- import type { Startable } from '@libp2p/interfaces'
14
+ import type { EventMap } from './index.js'
15
15
 
16
16
  const topic = 'foo'
17
17
 
@@ -19,10 +19,10 @@ function shouldNotHappen () {
19
19
  expect.fail()
20
20
  }
21
21
 
22
- export default (common: TestSetup<PubSub & Startable>) => {
22
+ export default (common: TestSetup<PubSub<EventMap>>) => {
23
23
  describe('pubsub with two nodes', () => {
24
- let psA: PubSub & Startable
25
- let psB: PubSub & Startable
24
+ let psA: PubSub<EventMap>
25
+ let psB: PubSub<EventMap>
26
26
 
27
27
  // Create pubsub nodes and connect them
28
28
  before(async () => {
@@ -55,7 +55,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
55
55
  it('Subscribe to a topic in nodeA', async () => {
56
56
  const defer = pDefer()
57
57
 
58
- psB.once('pubsub:subscription-change', ({ peerId: changedPeerId, subscriptions: changedSubs }) => {
58
+ psB.addEventListener('pubsub:subscription-change', (evt) => {
59
+ const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
59
60
  expectSet(psA.subscriptions, [topic])
60
61
  expect(psB.peers.size).to.equal(1)
61
62
  expectSet(psB.topics.get(topic), [psA.peerId.toString()])
@@ -64,6 +65,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
64
65
  expect(changedSubs[0].topicID).to.equal(topic)
65
66
  expect(changedSubs[0].subscribe).to.equal(true)
66
67
  defer.resolve()
68
+ }, {
69
+ once: true
67
70
  })
68
71
  psA.subscribe(topic)
69
72
 
@@ -73,13 +76,18 @@ export default (common: TestSetup<PubSub & Startable>) => {
73
76
  it('Publish to a topic in nodeA', async () => {
74
77
  const defer = pDefer()
75
78
 
76
- psA.once(topic, (msg) => {
79
+ psA.addEventListener(topic, (evt) => {
80
+ const msg = evt.detail
77
81
  expect(uint8ArrayToString(msg.data)).to.equal('hey')
78
- psB.removeListener(topic, shouldNotHappen)
82
+ psB.removeEventListener(topic, shouldNotHappen)
79
83
  defer.resolve()
84
+ }, {
85
+ once: true
80
86
  })
81
87
 
82
- psB.once(topic, shouldNotHappen)
88
+ psB.addEventListener(topic, shouldNotHappen, {
89
+ once: true
90
+ })
83
91
 
84
92
  void psA.publish(topic, uint8ArrayFromString('hey'))
85
93
 
@@ -89,19 +97,26 @@ export default (common: TestSetup<PubSub & Startable>) => {
89
97
  it('Publish to a topic in nodeB', async () => {
90
98
  const defer = pDefer()
91
99
 
92
- psA.once(topic, (msg) => {
93
- psA.once(topic, shouldNotHappen)
100
+ psA.addEventListener(topic, (evt) => {
101
+ const msg = evt.detail
102
+ psA.addEventListener(topic, shouldNotHappen, {
103
+ once: true
104
+ })
94
105
  expect(uint8ArrayToString(msg.data)).to.equal('banana')
95
106
 
96
107
  setTimeout(() => {
97
- psA.removeListener(topic, shouldNotHappen)
98
- psB.removeListener(topic, shouldNotHappen)
108
+ psA.removeEventListener(topic, shouldNotHappen)
109
+ psB.removeEventListener(topic, shouldNotHappen)
99
110
 
100
111
  defer.resolve()
101
112
  }, 100)
113
+ }, {
114
+ once: true
102
115
  })
103
116
 
104
- psB.once(topic, shouldNotHappen)
117
+ psB.addEventListener(topic, shouldNotHappen, {
118
+ once: true
119
+ })
105
120
 
106
121
  void psB.publish(topic, uint8ArrayFromString('banana'))
107
122
 
@@ -112,18 +127,21 @@ export default (common: TestSetup<PubSub & Startable>) => {
112
127
  const defer = pDefer()
113
128
  let counter = 0
114
129
 
115
- psB.once(topic, shouldNotHappen)
116
- psA.on(topic, receivedMsg)
130
+ psB.addEventListener(topic, shouldNotHappen, {
131
+ once: true
132
+ })
133
+ psA.addEventListener(topic, receivedMsg)
117
134
 
118
- function receivedMsg (msg: Message) {
135
+ function receivedMsg (evt: CustomEvent<Message>) {
136
+ const msg = evt.detail
119
137
  expect(uint8ArrayToString(msg.data)).to.equal('banana')
120
138
  expect(msg.from).to.be.eql(psB.peerId.toString())
121
139
  expect(msg.seqno).to.be.a('Uint8Array')
122
140
  expect(msg.topicIDs).to.be.eql([topic])
123
141
 
124
142
  if (++counter === 10) {
125
- psA.removeListener(topic, receivedMsg)
126
- psB.removeListener(topic, shouldNotHappen)
143
+ psA.removeEventListener(topic, receivedMsg)
144
+ psB.removeEventListener(topic, shouldNotHappen)
127
145
 
128
146
  defer.resolve()
129
147
  }
@@ -140,7 +158,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
140
158
  psA.unsubscribe(topic)
141
159
  expect(psA.subscriptions.size).to.equal(0)
142
160
 
143
- psB.once('pubsub:subscription-change', ({ peerId: changedPeerId, subscriptions: changedSubs }) => {
161
+ psB.addEventListener('pubsub:subscription-change', (evt) => {
162
+ const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
144
163
  expect(psB.peers.size).to.equal(1)
145
164
  expectSet(psB.topics.get(topic), [])
146
165
  expect(changedPeerId.toString()).to.equal(first(psB.peers).id.toString())
@@ -149,6 +168,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
149
168
  expect(changedSubs[0].subscribe).to.equal(false)
150
169
 
151
170
  defer.resolve()
171
+ }, {
172
+ once: true
152
173
  })
153
174
 
154
175
  return await defer.promise
@@ -157,12 +178,16 @@ export default (common: TestSetup<PubSub & Startable>) => {
157
178
  it('Publish to a topic:Z in nodeA nodeB', async () => {
158
179
  const defer = pDefer()
159
180
 
160
- psA.once('Z', shouldNotHappen)
161
- psB.once('Z', shouldNotHappen)
181
+ psA.addEventListener('Z', shouldNotHappen, {
182
+ once: true
183
+ })
184
+ psB.addEventListener('Z', shouldNotHappen, {
185
+ once: true
186
+ })
162
187
 
163
188
  setTimeout(() => {
164
- psA.removeListener('Z', shouldNotHappen)
165
- psB.removeListener('Z', shouldNotHappen)
189
+ psA.removeEventListener('Z', shouldNotHappen)
190
+ psB.removeEventListener('Z', shouldNotHappen)
166
191
  defer.resolve()
167
192
  }, 100)
168
193
 
@@ -10,10 +10,11 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
10
10
  import { isValidTick } from '../transport/utils/index.js'
11
11
  import type { DeferredPromise } from 'p-defer'
12
12
  import type { TestSetup } from '../index.js'
13
- import type { Muxer, MuxerOptions, MuxedStream } from '@libp2p/interfaces/stream-muxer'
14
- import type { Source } from 'it-stream-types'
13
+ import type { Stream } from '@libp2p/interfaces/connection'
14
+ import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer'
15
+ import type { Source, Duplex } from 'it-stream-types'
15
16
 
16
- async function close (stream: MuxedStream) {
17
+ async function drainAndClose (stream: Duplex<Uint8Array>) {
17
18
  return await pipe([], stream, drain)
18
19
  }
19
20
 
@@ -22,8 +23,8 @@ export default (common: TestSetup<Muxer, MuxerOptions>) => {
22
23
  it('Open a stream from the dialer', async () => {
23
24
  const p = duplexPair<Uint8Array>()
24
25
  const dialer = await common.setup()
25
- const onStreamPromise: DeferredPromise<MuxedStream> = defer()
26
- const onStreamEndPromise: DeferredPromise<MuxedStream> = defer()
26
+ const onStreamPromise: DeferredPromise<Stream> = defer()
27
+ const onStreamEndPromise: DeferredPromise<Stream> = defer()
27
28
 
28
29
  const listener = await common.setup({
29
30
  onStream: stream => {
@@ -34,18 +35,21 @@ export default (common: TestSetup<Muxer, MuxerOptions>) => {
34
35
  }
35
36
  })
36
37
 
37
- void pipe(p[0], dialer.newStream('/test/stream'), p[0])
38
- void pipe(p[1], listener.newStream('/test/stream'), p[1])
38
+ void pipe(p[0], dialer, p[0])
39
+ void pipe(p[1], listener, p[1])
39
40
 
40
41
  const conn = dialer.newStream()
41
42
  expect(dialer.streams).to.include(conn)
42
43
  expect(isValidTick(conn.timeline.open)).to.equal(true)
43
44
 
45
+ void drainAndClose(conn)
46
+
44
47
  const stream = await onStreamPromise.promise
45
48
  expect(isValidTick(stream.timeline.open)).to.equal(true)
46
49
  // Make sure the stream is being tracked
47
50
  expect(listener.streams).to.include(stream)
48
- void close(stream)
51
+
52
+ void drainAndClose(stream)
49
53
 
50
54
  // Make sure stream is closed properly
51
55
  const endedStream = await onStreamEndPromise.promise
@@ -58,7 +62,8 @@ export default (common: TestSetup<Muxer, MuxerOptions>) => {
58
62
  // Make sure the stream is removed from tracking
59
63
  expect(isValidTick(endedStream.timeline.close)).to.equal(true)
60
64
 
61
- await close(conn)
65
+ await drainAndClose(dialer)
66
+ await drainAndClose(listener)
62
67
 
63
68
  // ensure we have no streams left
64
69
  expect(dialer.streams).to.have.length(0)
@@ -67,7 +72,7 @@ export default (common: TestSetup<Muxer, MuxerOptions>) => {
67
72
 
68
73
  it('Open a stream from the listener', async () => {
69
74
  const p = duplexPair<Uint8Array>()
70
- const onStreamPromise: DeferredPromise<MuxedStream> = defer()
75
+ const onStreamPromise: DeferredPromise<Stream> = defer()
71
76
  const dialer = await common.setup({
72
77
  onStream: stream => {
73
78
  onStreamPromise.resolve(stream)
@@ -76,24 +81,27 @@ export default (common: TestSetup<Muxer, MuxerOptions>) => {
76
81
 
77
82
  const listener = await common.setup()
78
83
 
79
- void pipe(p[0], dialer.newStream('/test/stream'), p[0])
80
- void pipe(p[1], listener.newStream('/test/stream'), p[1])
84
+ void pipe(p[0], dialer, p[0])
85
+ void pipe(p[1], listener, p[1])
81
86
 
82
87
  const conn = listener.newStream()
83
88
 
89
+ void drainAndClose(conn)
90
+
84
91
  const stream = await onStreamPromise.promise
85
92
  expect(isValidTick(stream.timeline.open)).to.equal(true)
86
93
  expect(listener.streams).to.include(conn)
87
94
  expect(isValidTick(conn.timeline.open)).to.equal(true)
88
- await close(stream)
95
+ void drainAndClose(stream)
89
96
 
90
- await close(conn)
97
+ await drainAndClose(dialer)
98
+ await drainAndClose(listener)
91
99
  })
92
100
 
93
101
  it('Open a stream on both sides', async () => {
94
102
  const p = duplexPair<Uint8Array>()
95
- const onDialerStreamPromise: DeferredPromise<MuxedStream> = defer()
96
- const onListenerStreamPromise: DeferredPromise<MuxedStream> = defer()
103
+ const onDialerStreamPromise: DeferredPromise<Stream> = defer()
104
+ const onListenerStreamPromise: DeferredPromise<Stream> = defer()
97
105
  const dialer = await common.setup({
98
106
  onStream: stream => {
99
107
  onDialerStreamPromise.resolve(stream)
@@ -105,27 +113,30 @@ export default (common: TestSetup<Muxer, MuxerOptions>) => {
105
113
  }
106
114
  })
107
115
 
108
- void pipe(p[0], dialer.newStream('/test/stream'), p[0])
109
- void pipe(p[1], listener.newStream('/test/stream'), p[1])
116
+ void pipe(p[0], dialer, p[0])
117
+ void pipe(p[1], listener, p[1])
110
118
 
111
119
  const listenerConn = listener.newStream()
112
120
  const dialerConn = dialer.newStream()
113
121
 
122
+ void drainAndClose(dialerConn)
123
+ void drainAndClose(listenerConn)
124
+
114
125
  const dialerStream = await onDialerStreamPromise.promise
115
126
  const listenerStream = await onListenerStreamPromise.promise
116
127
 
117
- await close(dialerStream)
118
- await close(listenerStream)
128
+ await drainAndClose(dialerStream)
129
+ await drainAndClose(listenerStream)
119
130
 
120
- await close(dialerConn)
121
- await close(listenerConn)
131
+ await drainAndClose(dialer)
132
+ await drainAndClose(listener)
122
133
  })
123
134
 
124
135
  it('Open a stream on one side, write, open a stream on the other side', async () => {
125
136
  const toString = (source: Source<Uint8Array>) => map(source, (u) => uint8ArrayToString(u))
126
137
  const p = duplexPair<Uint8Array>()
127
- const onDialerStreamPromise: DeferredPromise<MuxedStream> = defer()
128
- const onListenerStreamPromise: DeferredPromise<MuxedStream> = defer()
138
+ const onDialerStreamPromise: DeferredPromise<Stream> = defer()
139
+ const onListenerStreamPromise: DeferredPromise<Stream> = defer()
129
140
  const dialer = await common.setup({
130
141
  onStream: stream => {
131
142
  onDialerStreamPromise.resolve(stream)
@@ -137,8 +148,8 @@ export default (common: TestSetup<Muxer, MuxerOptions>) => {
137
148
  }
138
149
  })
139
150
 
140
- void pipe(p[0], dialer.newStream('/test/stream'), p[0])
141
- void pipe(p[1], listener.newStream('/test/stream'), p[1])
151
+ void pipe(p[0], dialer, p[0])
152
+ void pipe(p[1], listener, p[1])
142
153
 
143
154
  const dialerConn = dialer.newStream()
144
155
  const listenerConn = listener.newStream()
@@ -4,9 +4,10 @@ import { duplexPair } from 'it-pair/duplex'
4
4
  import { abortableSource, abortableDuplex } from 'abortable-iterator'
5
5
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
6
6
  import drain from 'it-drain'
7
+ import { mockUpgrader, mockMultiaddrConnection } from '../transport/utils/index.js'
7
8
  import type { TestSetup } from '../index.js'
8
9
  import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer'
9
- import type { Connection } from '@libp2p/interfaces/connection'
10
+ import { expect } from 'aegir/utils/chai.js'
10
11
 
11
12
  async function pause (ms: number) {
12
13
  return await new Promise(resolve => setTimeout(resolve, ms))
@@ -27,30 +28,13 @@ const infiniteRandom = {
27
28
 
28
29
  export default (common: TestSetup<Muxer, MuxerOptions>) => {
29
30
  describe('close', () => {
30
- it('closing underlying socket closes streams (tcp)', async () => {
31
- const mockConn = (muxer: Muxer): Connection => {
32
- // @ts-expect-error not all Connection methods are implemented
33
- const connection: Connection = {
34
- newStream: async (multicodecs) => {
35
- return {
36
- protocol: multicodecs[0],
37
- stream: muxer.newStream(`${multicodecs[0]}`)
38
- }
39
- }
31
+ it('closing underlying socket closes streams', async () => {
32
+ const muxer = await common.setup({
33
+ onStream: (stream) => {
34
+ void pipe(stream, stream)
40
35
  }
41
-
42
- return connection
43
- }
44
-
45
- const mockUpgrade = async (maConn: any) => {
46
- const muxer = await common.setup({
47
- onStream: (stream) => {
48
- void pipe(stream, stream)
49
- }
50
- })
51
- pipe(maConn, muxer.newStream('/test/stream'), maConn)
52
- return mockConn(muxer)
53
- }
36
+ })
37
+ const upgrader = mockUpgrader({ muxer })
54
38
 
55
39
  const [local, remote] = duplexPair<Uint8Array>()
56
40
  const controller = new AbortController()
@@ -58,8 +42,8 @@ export default (common: TestSetup<Muxer, MuxerOptions>) => {
58
42
  returnOnAbort: true
59
43
  })
60
44
 
61
- await mockUpgrade(abortableRemote)
62
- const dialerConn = await mockUpgrade(local)
45
+ await upgrader.upgradeInbound(mockMultiaddrConnection(abortableRemote))
46
+ const dialerConn = await upgrader.upgradeOutbound(mockMultiaddrConnection(local))
63
47
 
64
48
  const s1 = await dialerConn.newStream([''])
65
49
  const s2 = await dialerConn.newStream([''])
@@ -73,6 +57,8 @@ export default (common: TestSetup<Muxer, MuxerOptions>) => {
73
57
  // test is complete when all muxed streams have closed
74
58
  await s1Result
75
59
  await s2Result
60
+
61
+ expect(muxer.streams).to.be.empty()
76
62
  })
77
63
 
78
64
  it('closing one of the muxed streams doesn\'t close others', async () => {
@@ -84,8 +70,8 @@ export default (common: TestSetup<Muxer, MuxerOptions>) => {
84
70
  onStream: async (stream) => await pipe(stream, stream)
85
71
  })
86
72
 
87
- void pipe(p[0], dialer.newStream('/test/stream'), p[0])
88
- void pipe(p[1], listener.newStream('/test/stream'), p[1])
73
+ void pipe(p[0], dialer, p[0])
74
+ void pipe(p[1], listener, p[1])
89
75
 
90
76
  const stream = dialer.newStream()
91
77
  const streams = Array.from(Array(5), () => dialer.newStream())
@@ -3,9 +3,9 @@ import closeTest from './close-test.js'
3
3
  import stressTest from './stress-test.js'
4
4
  import megaStressTest from './mega-stress-test.js'
5
5
  import type { TestSetup } from '../index.js'
6
- import type { Muxer } from '@libp2p/interfaces/stream-muxer'
6
+ import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer'
7
7
 
8
- export default (common: TestSetup<Muxer>) => {
8
+ export default (common: TestSetup<Muxer, MuxerOptions>) => {
9
9
  describe('interface-stream-muxer', () => {
10
10
  baseTest(common)
11
11
  closeTest(common)
@@ -1,4 +1,4 @@
1
- import spawn from './spawner'
1
+ import spawn from './spawner.js'
2
2
  import type { TestSetup } from '../index.js'
3
3
  import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer'
4
4
 
@@ -25,8 +25,8 @@ export default async (createMuxer: (options?: MuxerOptions) => Promise<Muxer>, n
25
25
 
26
26
  const dialer = await createMuxer()
27
27
 
28
- void pipe(listenerSocket, listener.newStream('/test/stream'), listenerSocket)
29
- void pipe(dialerSocket, dialer.newStream('/test/stream'), dialerSocket)
28
+ void pipe(listenerSocket, listener, listenerSocket)
29
+ void pipe(dialerSocket, dialer, dialerSocket)
30
30
 
31
31
  const spawnStream = async () => {
32
32
  const stream = dialer.newStream()
@@ -1,4 +1,4 @@
1
- import spawn from './spawner'
1
+ import spawn from './spawner.js'
2
2
  import type { TestSetup } from '../index.js'
3
3
  import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer'
4
4
 
@@ -6,6 +6,7 @@ import { pipe } from 'it-pipe'
6
6
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
7
7
  import { isValidTick, mockUpgrader } from './utils/index.js'
8
8
  import defer from 'p-defer'
9
+ import { CustomEvent } from '@libp2p/interfaces'
9
10
  import type { TestSetup } from '../index.js'
10
11
  import type { Transport } from '@libp2p/interfaces/transport'
11
12
  import type { TransportTestFixtures, SetupArgs } from './index.js'
@@ -108,8 +109,8 @@ export default (common: TestSetup<TransportTestFixtures, SetupArgs>) => {
108
109
  const deferred = defer()
109
110
  let conn
110
111
 
111
- listener.on('connection', (c) => {
112
- conn = c
112
+ listener.addEventListener('connection', (evt) => {
113
+ conn = evt.detail
113
114
  deferred.resolve()
114
115
  })
115
116
 
@@ -127,7 +128,7 @@ export default (common: TestSetup<TransportTestFixtures, SetupArgs>) => {
127
128
 
128
129
  it('listening', (done) => {
129
130
  const listener = transport.createListener()
130
- listener.on('listening', () => {
131
+ listener.addEventListener('listening', () => {
131
132
  listener.close().then(done, done)
132
133
  })
133
134
  void listener.listen(addrs[0])
@@ -135,16 +136,18 @@ export default (common: TestSetup<TransportTestFixtures, SetupArgs>) => {
135
136
 
136
137
  it('error', (done) => {
137
138
  const listener = transport.createListener()
138
- listener.on('error', (err) => {
139
- expect(err).to.exist()
139
+ listener.addEventListener('error', (evt) => {
140
+ expect(evt.detail).to.be.an.instanceOf(Error)
140
141
  listener.close().then(done, done)
141
142
  })
142
- listener.emit('error', new Error('my err'))
143
+ listener.dispatchEvent(new CustomEvent('error', {
144
+ detail: new Error('my err')
145
+ }))
143
146
  })
144
147
 
145
148
  it('close', (done) => {
146
149
  const listener = transport.createListener()
147
- listener.on('close', done)
150
+ listener.addEventListener('close', () => done())
148
151
 
149
152
  void (async () => {
150
153
  await listener.listen(addrs[0])
@@ -1,10 +1,16 @@
1
1
  import { expect } from 'aegir/utils/chai.js'
2
- import type { Upgrader, MultiaddrConnection } from '@libp2p/interfaces/transport'
3
- import type { Connection, StreamData } from '@libp2p/interfaces/connection'
4
- import type { MuxedStream } from '@libp2p/interfaces/stream-muxer'
5
2
  import { pair } from 'it-pair'
6
3
  import { PeerId } from '@libp2p/peer-id'
7
4
  import * as PeerIdFactory from '@libp2p/peer-id-factory'
5
+ import { pushable } from 'it-pushable'
6
+ import drain from 'it-drain'
7
+ import { Multiaddr } from '@multiformats/multiaddr'
8
+ import { pipe } from 'it-pipe'
9
+ import type { Upgrader, MultiaddrConnection } from '@libp2p/interfaces/transport'
10
+ import type { Connection, Stream, Metadata, ProtocolStream } from '@libp2p/interfaces/connection'
11
+ import type { Muxer } from '@libp2p/interfaces/stream-muxer'
12
+ import type { Duplex } from 'it-stream-types'
13
+
8
14
  /**
9
15
  * A tick is considered valid if it happened between now
10
16
  * and `ms` milliseconds ago
@@ -23,7 +29,64 @@ export function isValidTick (date?: number, ms: number = 5000) {
23
29
  return false
24
30
  }
25
31
 
26
- export function mockUpgrader () {
32
+ export function mockMultiaddrConnection (source: Duplex<Uint8Array>): MultiaddrConnection {
33
+ const maConn: MultiaddrConnection = {
34
+ ...source,
35
+ async close () {
36
+
37
+ },
38
+ timeline: {
39
+ open: Date.now()
40
+ },
41
+ remoteAddr: new Multiaddr('/ip4/127.0.0.1/tcp/4001')
42
+ }
43
+
44
+ return maConn
45
+ }
46
+
47
+ export function mockMuxer (): Muxer {
48
+ let streamId = 0
49
+ let streams: Stream[] = []
50
+ const p = pushable<Uint8Array>()
51
+
52
+ const muxer: Muxer = {
53
+ source: p,
54
+ sink: async (source) => {
55
+ await drain(source)
56
+ },
57
+ get streams () {
58
+ return streams
59
+ },
60
+ newStream: (name?: string) => {
61
+ const echo = pair<Uint8Array>()
62
+
63
+ const id = `${streamId++}`
64
+ const stream: Stream = {
65
+ id,
66
+ sink: echo.sink,
67
+ source: echo.source,
68
+ close: () => {
69
+ streams = streams.filter(s => s !== stream)
70
+ },
71
+ abort: () => {},
72
+ reset: () => {},
73
+ timeline: {
74
+ open: 0
75
+ }
76
+ }
77
+
78
+ return stream
79
+ }
80
+ }
81
+
82
+ return muxer
83
+ }
84
+
85
+ export interface MockUpgraderOptions {
86
+ muxer?: Muxer
87
+ }
88
+
89
+ export function mockUpgrader (options: MockUpgraderOptions = {}) {
27
90
  const ensureProps = (multiaddrConnection: MultiaddrConnection) => {
28
91
  ['sink', 'source', 'remoteAddr', 'timeline', 'close'].forEach(prop => {
29
92
  expect(multiaddrConnection).to.have.property(prop)
@@ -31,30 +94,37 @@ export function mockUpgrader () {
31
94
  expect(isValidTick(multiaddrConnection.timeline.open)).to.equal(true)
32
95
  return multiaddrConnection
33
96
  }
97
+
98
+ const muxer = options.muxer ?? mockMuxer()
99
+
34
100
  const upgrader: Upgrader = {
35
101
  async upgradeOutbound (multiaddrConnection) {
36
102
  ensureProps(multiaddrConnection)
37
- return await createConnection(multiaddrConnection, 'outbound')
103
+ return await createConnection(multiaddrConnection, 'outbound', muxer)
38
104
  },
39
105
  async upgradeInbound (multiaddrConnection) {
40
106
  ensureProps(multiaddrConnection)
41
- return await createConnection(multiaddrConnection, 'inbound')
107
+ return await createConnection(multiaddrConnection, 'inbound', muxer)
42
108
  }
43
109
  }
44
110
 
45
111
  return upgrader
46
112
  }
47
113
 
48
- async function createConnection (maConn: MultiaddrConnection, direction: 'inbound' | 'outbound'): Promise<Connection> {
114
+ async function createConnection (maConn: MultiaddrConnection, direction: 'inbound' | 'outbound', muxer: Muxer): Promise<Connection> {
49
115
  const remoteAddr = maConn.remoteAddr
50
116
  const remotePeerIdStr = remoteAddr.getPeerId()
51
117
  const remotePeer = remotePeerIdStr != null ? PeerId.fromString(remotePeerIdStr) : await PeerIdFactory.createEd25519PeerId()
52
118
 
53
- const streams: MuxedStream[] = []
119
+ const streams: Stream[] = []
54
120
  let streamId = 0
55
121
 
56
122
  const registry = new Map()
57
123
 
124
+ void pipe(
125
+ maConn, muxer, maConn
126
+ )
127
+
58
128
  return {
59
129
  id: 'mock-connection',
60
130
  remoteAddr,
@@ -70,26 +140,17 @@ async function createConnection (maConn: MultiaddrConnection, direction: 'inboun
70
140
  tags: [],
71
141
  streams,
72
142
  newStream: async (protocols) => {
143
+ if (!Array.isArray(protocols)) {
144
+ protocols = [protocols]
145
+ }
146
+
73
147
  if (protocols.length === 0) {
74
148
  throw new Error('protocols must have a length')
75
149
  }
76
150
 
77
- const echo = pair<Uint8Array>()
78
-
79
151
  const id = `${streamId++}`
80
- const stream: MuxedStream = {
81
- id,
82
- sink: echo.sink,
83
- source: echo.source,
84
- close: () => {},
85
- abort: () => {},
86
- reset: () => {},
87
- timeline: {
88
- open: 0
89
- }
90
- }
91
-
92
- const streamData = {
152
+ const stream: Stream = muxer.newStream(id)
153
+ const streamData: ProtocolStream = {
93
154
  protocol: protocols[0],
94
155
  stream
95
156
  }
@@ -98,7 +159,7 @@ async function createConnection (maConn: MultiaddrConnection, direction: 'inboun
98
159
 
99
160
  return streamData
100
161
  },
101
- addStream: (muxedStream: MuxedStream, streamData: StreamData) => {
162
+ addStream: (stream: Stream, metadata: Metadata) => {
102
163
 
103
164
  },
104
165
  removeStream: (id: string) => {
@@ -0,0 +1,14 @@
1
+
2
+ export function mockConnectionGater () {
3
+ return {
4
+ denyDialPeer: async () => await Promise.resolve(false),
5
+ denyDialMultiaddr: async () => await Promise.resolve(false),
6
+ denyInboundConnection: async () => await Promise.resolve(false),
7
+ denyOutboundConnection: async () => await Promise.resolve(false),
8
+ denyInboundEncryptedConnection: async () => await Promise.resolve(false),
9
+ denyOutboundEncryptedConnection: async () => await Promise.resolve(false),
10
+ denyInboundUpgradedConnection: async () => await Promise.resolve(false),
11
+ denyOutboundUpgradedConnection: async () => await Promise.resolve(false),
12
+ filterMultiaddrForPeer: async () => await Promise.resolve(true)
13
+ }
14
+ }