@libp2p/interface-compliance-tests 1.1.6 → 1.1.10
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.d.ts +13 -2
- package/dist/src/mocks/connection.d.ts.map +1 -1
- package/dist/src/mocks/connection.js +56 -24
- package/dist/src/mocks/connection.js.map +1 -1
- package/dist/src/mocks/multiaddr-connection.d.ts +2 -1
- package/dist/src/mocks/multiaddr-connection.d.ts.map +1 -1
- package/dist/src/mocks/multiaddr-connection.js +2 -2
- package/dist/src/mocks/multiaddr-connection.js.map +1 -1
- package/dist/src/mocks/muxer.d.ts +2 -2
- package/dist/src/mocks/muxer.d.ts.map +1 -1
- package/dist/src/mocks/muxer.js +175 -26
- package/dist/src/mocks/muxer.js.map +1 -1
- package/dist/src/mocks/registrar.d.ts +11 -2
- package/dist/src/mocks/registrar.d.ts.map +1 -1
- package/dist/src/mocks/registrar.js +47 -5
- package/dist/src/mocks/registrar.js.map +1 -1
- package/dist/src/mocks/upgrader.d.ts.map +1 -1
- package/dist/src/mocks/upgrader.js +8 -4
- package/dist/src/mocks/upgrader.js.map +1 -1
- package/dist/src/pubsub/api.d.ts +3 -2
- package/dist/src/pubsub/api.d.ts.map +1 -1
- package/dist/src/pubsub/api.js +23 -13
- package/dist/src/pubsub/api.js.map +1 -1
- package/dist/src/pubsub/connection-handlers.d.ts +3 -2
- package/dist/src/pubsub/connection-handlers.d.ts.map +1 -1
- package/dist/src/pubsub/connection-handlers.js +165 -69
- package/dist/src/pubsub/connection-handlers.js.map +1 -1
- package/dist/src/pubsub/emit-self.d.ts +3 -2
- package/dist/src/pubsub/emit-self.d.ts.map +1 -1
- package/dist/src/pubsub/emit-self.js +15 -4
- package/dist/src/pubsub/emit-self.js.map +1 -1
- package/dist/src/pubsub/index.d.ts +6 -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 +3 -2
- package/dist/src/pubsub/messages.d.ts.map +1 -1
- package/dist/src/pubsub/messages.js +42 -39
- package/dist/src/pubsub/messages.js.map +1 -1
- package/dist/src/pubsub/multiple-nodes.d.ts +3 -2
- package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -1
- package/dist/src/pubsub/multiple-nodes.js +163 -65
- package/dist/src/pubsub/multiple-nodes.js.map +1 -1
- package/dist/src/pubsub/two-nodes.d.ts +3 -2
- package/dist/src/pubsub/two-nodes.d.ts.map +1 -1
- package/dist/src/pubsub/two-nodes.js +80 -38
- package/dist/src/pubsub/two-nodes.js.map +1 -1
- package/dist/src/stream-muxer/base-test.js +1 -1
- package/dist/src/stream-muxer/base-test.js.map +1 -1
- package/dist/src/stream-muxer/close-test.d.ts.map +1 -1
- package/dist/src/stream-muxer/close-test.js +12 -10
- package/dist/src/stream-muxer/close-test.js.map +1 -1
- package/dist/src/transport/dial-test.d.ts.map +1 -1
- package/dist/src/transport/dial-test.js +2 -1
- package/dist/src/transport/dial-test.js.map +1 -1
- package/dist/src/transport/filter-test.js +1 -1
- package/dist/src/transport/filter-test.js.map +1 -1
- package/dist/src/transport/listen-test.d.ts.map +1 -1
- package/dist/src/transport/listen-test.js +2 -1
- package/dist/src/transport/listen-test.js.map +1 -1
- package/dist/src/utils/is-valid-tick.d.ts +6 -0
- package/dist/src/utils/is-valid-tick.d.ts.map +1 -0
- package/dist/src/utils/is-valid-tick.js +15 -0
- package/dist/src/utils/is-valid-tick.js.map +1 -0
- package/package.json +7 -5
- package/src/mocks/connection.ts +80 -24
- package/src/mocks/multiaddr-connection.ts +3 -2
- package/src/mocks/muxer.ts +230 -28
- package/src/mocks/registrar.ts +65 -7
- package/src/mocks/upgrader.ts +8 -5
- package/src/pubsub/api.ts +29 -15
- package/src/pubsub/connection-handlers.ts +186 -75
- package/src/pubsub/emit-self.ts +19 -7
- package/src/pubsub/index.ts +6 -3
- package/src/pubsub/messages.ts +59 -44
- package/src/pubsub/multiple-nodes.ts +189 -76
- package/src/pubsub/two-nodes.ts +94 -46
- package/src/stream-muxer/base-test.ts +1 -1
- package/src/stream-muxer/close-test.ts +13 -12
- package/src/transport/dial-test.ts +2 -1
- package/src/transport/filter-test.ts +1 -1
- package/src/transport/listen-test.ts +2 -1
- package/src/utils/is-valid-tick.ts +18 -0
- package/dist/src/pubsub/utils.d.ts +0 -3
- package/dist/src/pubsub/utils.d.ts.map +0 -1
- package/dist/src/pubsub/utils.js +0 -11
- package/dist/src/pubsub/utils.js.map +0 -1
- package/dist/src/transport/utils/index.d.ts +0 -15
- package/dist/src/transport/utils/index.d.ts.map +0 -1
- package/dist/src/transport/utils/index.js +0 -137
- package/dist/src/transport/utils/index.js.map +0 -1
- package/src/pubsub/utils.ts +0 -13
- package/src/transport/utils/index.ts +0 -172
package/src/pubsub/two-nodes.ts
CHANGED
|
@@ -5,13 +5,16 @@ 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 delay from 'delay'
|
|
8
12
|
import type { TestSetup } from '../index.js'
|
|
9
|
-
import type {
|
|
10
|
-
import {
|
|
11
|
-
first,
|
|
12
|
-
expectSet
|
|
13
|
-
} from './utils.js'
|
|
13
|
+
import type { Message, PubSubOptions } from '@libp2p/interfaces/pubsub'
|
|
14
14
|
import type { EventMap } from './index.js'
|
|
15
|
+
import type { PeerId } from '@libp2p/interfaces/src/peer-id'
|
|
16
|
+
import type { Registrar } from '@libp2p/interfaces/src/registrar'
|
|
17
|
+
import type { PubsubBaseProtocol } from '@libp2p/pubsub'
|
|
15
18
|
|
|
16
19
|
const topic = 'foo'
|
|
17
20
|
|
|
@@ -19,28 +22,50 @@ function shouldNotHappen () {
|
|
|
19
22
|
expect.fail()
|
|
20
23
|
}
|
|
21
24
|
|
|
22
|
-
export default (common: TestSetup<
|
|
25
|
+
export default (common: TestSetup<PubsubBaseProtocol<EventMap>, PubSubOptions>) => {
|
|
23
26
|
describe('pubsub with two nodes', () => {
|
|
24
|
-
let psA:
|
|
25
|
-
let psB:
|
|
27
|
+
let psA: PubsubBaseProtocol<EventMap>
|
|
28
|
+
let psB: PubsubBaseProtocol<EventMap>
|
|
29
|
+
let peerIdA: PeerId
|
|
30
|
+
let peerIdB: PeerId
|
|
31
|
+
let registrarA: Registrar
|
|
32
|
+
let registrarB: Registrar
|
|
26
33
|
|
|
27
34
|
// Create pubsub nodes and connect them
|
|
28
35
|
before(async () => {
|
|
29
|
-
|
|
30
|
-
|
|
36
|
+
peerIdA = await createEd25519PeerId()
|
|
37
|
+
peerIdB = await createEd25519PeerId()
|
|
31
38
|
|
|
32
|
-
|
|
33
|
-
|
|
39
|
+
registrarA = mockRegistrar()
|
|
40
|
+
registrarB = mockRegistrar()
|
|
41
|
+
|
|
42
|
+
psA = await common.setup({
|
|
43
|
+
peerId: peerIdA,
|
|
44
|
+
registrar: registrarA,
|
|
45
|
+
emitSelf: true
|
|
46
|
+
})
|
|
47
|
+
psB = await common.setup({
|
|
48
|
+
peerId: peerIdB,
|
|
49
|
+
registrar: registrarB
|
|
50
|
+
})
|
|
34
51
|
|
|
35
52
|
// Start pubsub and connect nodes
|
|
36
53
|
await psA.start()
|
|
37
54
|
await psB.start()
|
|
38
55
|
|
|
39
|
-
|
|
40
|
-
|
|
56
|
+
expect(psA.getPeers()).to.be.empty()
|
|
57
|
+
expect(psB.getPeers()).to.be.empty()
|
|
58
|
+
|
|
59
|
+
await connectPeers(psA.multicodecs[0], {
|
|
60
|
+
peerId: peerIdA,
|
|
61
|
+
registrar: registrarA
|
|
62
|
+
}, {
|
|
63
|
+
peerId: peerIdB,
|
|
64
|
+
registrar: registrarB
|
|
65
|
+
})
|
|
41
66
|
|
|
42
67
|
// Wait for peers to be ready in pubsub
|
|
43
|
-
await pWaitFor(() => psA.
|
|
68
|
+
await pWaitFor(() => psA.getPeers().length === 1 && psB.getPeers().length === 1)
|
|
44
69
|
})
|
|
45
70
|
|
|
46
71
|
after(async () => {
|
|
@@ -57,12 +82,12 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
57
82
|
|
|
58
83
|
psB.addEventListener('pubsub:subscription-change', (evt) => {
|
|
59
84
|
const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
|
|
60
|
-
|
|
61
|
-
expect(psB.
|
|
62
|
-
|
|
63
|
-
expect(changedPeerId
|
|
85
|
+
expect(psA.getTopics()).to.deep.equal([topic])
|
|
86
|
+
expect(psB.getPeers()).to.have.lengthOf(1)
|
|
87
|
+
expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdA.toString()])
|
|
88
|
+
expect(changedPeerId).to.deep.equal(psB.getPeers()[0])
|
|
64
89
|
expect(changedSubs).to.have.lengthOf(1)
|
|
65
|
-
expect(changedSubs[0].
|
|
90
|
+
expect(changedSubs[0].topic).to.equal(topic)
|
|
66
91
|
expect(changedSubs[0].subscribe).to.equal(true)
|
|
67
92
|
defer.resolve()
|
|
68
93
|
}, {
|
|
@@ -89,7 +114,9 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
89
114
|
once: true
|
|
90
115
|
})
|
|
91
116
|
|
|
92
|
-
|
|
117
|
+
await delay(100)
|
|
118
|
+
|
|
119
|
+
void psA.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('hey') }))
|
|
93
120
|
|
|
94
121
|
return await defer.promise
|
|
95
122
|
})
|
|
@@ -118,7 +145,9 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
118
145
|
once: true
|
|
119
146
|
})
|
|
120
147
|
|
|
121
|
-
|
|
148
|
+
await delay(100)
|
|
149
|
+
|
|
150
|
+
void psB.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('banana') }))
|
|
122
151
|
|
|
123
152
|
return await defer.promise
|
|
124
153
|
})
|
|
@@ -135,9 +164,9 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
135
164
|
function receivedMsg (evt: CustomEvent<Message>) {
|
|
136
165
|
const msg = evt.detail
|
|
137
166
|
expect(uint8ArrayToString(msg.data)).to.equal('banana')
|
|
138
|
-
expect(msg.from).to.
|
|
139
|
-
expect(msg.seqno).to.be.a('
|
|
140
|
-
expect(msg.
|
|
167
|
+
expect(msg.from.toString()).to.equal(peerIdB.toString())
|
|
168
|
+
expect(msg.seqno).to.be.a('BigInt')
|
|
169
|
+
expect(msg.topic).to.be.equal(topic)
|
|
141
170
|
|
|
142
171
|
if (++counter === 10) {
|
|
143
172
|
psA.removeEventListener(topic, receivedMsg)
|
|
@@ -147,52 +176,71 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
147
176
|
}
|
|
148
177
|
}
|
|
149
178
|
|
|
150
|
-
|
|
179
|
+
await delay(100)
|
|
180
|
+
|
|
181
|
+
Array.from({ length: 10 }, (_, i) => psB.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('banana') })))
|
|
151
182
|
|
|
152
183
|
return await defer.promise
|
|
153
184
|
})
|
|
154
185
|
|
|
155
186
|
it('Unsubscribe from topic in nodeA', async () => {
|
|
156
187
|
const defer = pDefer()
|
|
157
|
-
|
|
158
|
-
psA.unsubscribe(topic)
|
|
159
|
-
expect(psA.subscriptions.size).to.equal(0)
|
|
188
|
+
let callCount = 0
|
|
160
189
|
|
|
161
190
|
psB.addEventListener('pubsub:subscription-change', (evt) => {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
191
|
+
callCount++
|
|
192
|
+
|
|
193
|
+
if (callCount === 1) {
|
|
194
|
+
// notice subscribe
|
|
195
|
+
const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
|
|
196
|
+
expect(psB.getPeers()).to.have.lengthOf(1)
|
|
197
|
+
expect(psB.getTopics()).to.be.empty()
|
|
198
|
+
expect(changedPeerId).to.deep.equal(psB.getPeers()[0])
|
|
199
|
+
expect(changedSubs).to.have.lengthOf(1)
|
|
200
|
+
expect(changedSubs[0].topic).to.equal(topic)
|
|
201
|
+
expect(changedSubs[0].subscribe).to.equal(true)
|
|
202
|
+
} else {
|
|
203
|
+
// notice unsubscribe
|
|
204
|
+
const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
|
|
205
|
+
expect(psB.getPeers()).to.have.lengthOf(1)
|
|
206
|
+
expect(psB.getTopics()).to.be.empty()
|
|
207
|
+
expect(changedPeerId).to.deep.equal(psB.getPeers()[0])
|
|
208
|
+
expect(changedSubs).to.have.lengthOf(1)
|
|
209
|
+
expect(changedSubs[0].topic).to.equal(topic)
|
|
210
|
+
expect(changedSubs[0].subscribe).to.equal(false)
|
|
169
211
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
once: true
|
|
212
|
+
defer.resolve()
|
|
213
|
+
}
|
|
173
214
|
})
|
|
174
215
|
|
|
216
|
+
psA.subscribe(topic)
|
|
217
|
+
expect(psA.getTopics()).to.not.be.empty()
|
|
218
|
+
|
|
219
|
+
psA.unsubscribe(topic)
|
|
220
|
+
expect(psA.getTopics()).to.be.empty()
|
|
221
|
+
|
|
175
222
|
return await defer.promise
|
|
176
223
|
})
|
|
177
224
|
|
|
178
|
-
it('Publish to a topic:Z in nodeA nodeB', async () => {
|
|
225
|
+
it.skip('Publish to a topic:Z in nodeA nodeB', async () => {
|
|
179
226
|
const defer = pDefer()
|
|
227
|
+
const topic = 'Z'
|
|
180
228
|
|
|
181
|
-
psA.addEventListener(
|
|
229
|
+
psA.addEventListener(topic, shouldNotHappen, {
|
|
182
230
|
once: true
|
|
183
231
|
})
|
|
184
|
-
psB.addEventListener(
|
|
232
|
+
psB.addEventListener(topic, shouldNotHappen, {
|
|
185
233
|
once: true
|
|
186
234
|
})
|
|
187
235
|
|
|
188
236
|
setTimeout(() => {
|
|
189
|
-
psA.removeEventListener(
|
|
190
|
-
psB.removeEventListener(
|
|
237
|
+
psA.removeEventListener(topic, shouldNotHappen)
|
|
238
|
+
psB.removeEventListener(topic, shouldNotHappen)
|
|
191
239
|
defer.resolve()
|
|
192
240
|
}, 100)
|
|
193
241
|
|
|
194
|
-
void psB.
|
|
195
|
-
void psA.
|
|
242
|
+
void psB.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('banana') }))
|
|
243
|
+
void psA.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('banana') }))
|
|
196
244
|
|
|
197
245
|
return await defer.promise
|
|
198
246
|
})
|
|
@@ -7,7 +7,7 @@ import all from 'it-all'
|
|
|
7
7
|
import defer from 'p-defer'
|
|
8
8
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
9
9
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
10
|
-
import { isValidTick } from '../
|
|
10
|
+
import { isValidTick } from '../utils/is-valid-tick.js'
|
|
11
11
|
import type { DeferredPromise } from 'p-defer'
|
|
12
12
|
import type { TestSetup } from '../index.js'
|
|
13
13
|
import type { Stream } from '@libp2p/interfaces/connection'
|
|
@@ -4,14 +4,13 @@ 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
|
|
7
|
+
import { mockUpgrader } from '../mocks/upgrader.js'
|
|
8
|
+
import { mockMultiaddrConnection } from '../mocks/multiaddr-connection.js'
|
|
9
|
+
import { expect } from 'aegir/utils/chai.js'
|
|
10
|
+
import delay from 'delay'
|
|
8
11
|
import type { TestSetup } from '../index.js'
|
|
9
12
|
import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer'
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
async function pause (ms: number) {
|
|
13
|
-
return await new Promise(resolve => setTimeout(resolve, ms))
|
|
14
|
-
}
|
|
13
|
+
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
15
14
|
|
|
16
15
|
function randomBuffer () {
|
|
17
16
|
return uint8ArrayFromString(Math.random().toString())
|
|
@@ -21,7 +20,7 @@ const infiniteRandom = {
|
|
|
21
20
|
[Symbol.asyncIterator]: async function * () {
|
|
22
21
|
while (true) {
|
|
23
22
|
yield randomBuffer()
|
|
24
|
-
await
|
|
23
|
+
await delay(50)
|
|
25
24
|
}
|
|
26
25
|
}
|
|
27
26
|
}
|
|
@@ -29,9 +28,11 @@ const infiniteRandom = {
|
|
|
29
28
|
export default (common: TestSetup<Muxer, MuxerOptions>) => {
|
|
30
29
|
describe('close', () => {
|
|
31
30
|
it('closing underlying socket closes streams', async () => {
|
|
31
|
+
const localPeer = await createEd25519PeerId()
|
|
32
|
+
const remotePeer = await createEd25519PeerId()
|
|
32
33
|
const muxer = await common.setup({
|
|
33
34
|
onStream: (stream) => {
|
|
34
|
-
void pipe(stream,
|
|
35
|
+
void pipe(stream, drain)
|
|
35
36
|
}
|
|
36
37
|
})
|
|
37
38
|
const upgrader = mockUpgrader({ muxer })
|
|
@@ -42,8 +43,8 @@ export default (common: TestSetup<Muxer, MuxerOptions>) => {
|
|
|
42
43
|
returnOnAbort: true
|
|
43
44
|
})
|
|
44
45
|
|
|
45
|
-
await upgrader.upgradeInbound(mockMultiaddrConnection(abortableRemote))
|
|
46
|
-
const dialerConn = await upgrader.upgradeOutbound(mockMultiaddrConnection(local))
|
|
46
|
+
await upgrader.upgradeInbound(mockMultiaddrConnection(abortableRemote, localPeer))
|
|
47
|
+
const dialerConn = await upgrader.upgradeOutbound(mockMultiaddrConnection(local, remotePeer))
|
|
47
48
|
|
|
48
49
|
const s1 = await dialerConn.newStream([''])
|
|
49
50
|
const s2 = await dialerConn.newStream([''])
|
|
@@ -93,12 +94,12 @@ export default (common: TestSetup<Muxer, MuxerOptions>) => {
|
|
|
93
94
|
})
|
|
94
95
|
|
|
95
96
|
// Pause, and then send some data and close the first stream
|
|
96
|
-
await
|
|
97
|
+
await delay(50)
|
|
97
98
|
await pipe([randomBuffer()], stream, drain)
|
|
98
99
|
closed = true
|
|
99
100
|
|
|
100
101
|
// Abort all the other streams later
|
|
101
|
-
await
|
|
102
|
+
await delay(50)
|
|
102
103
|
controllers.forEach(c => c.abort())
|
|
103
104
|
|
|
104
105
|
// These should now all resolve without error
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { expect } from 'aegir/utils/chai.js'
|
|
2
|
-
import { isValidTick
|
|
2
|
+
import { isValidTick } from '../utils/is-valid-tick.js'
|
|
3
|
+
import { mockUpgrader } from '../mocks/upgrader.js'
|
|
3
4
|
import { goodbye } from 'it-goodbye'
|
|
4
5
|
import all from 'it-all'
|
|
5
6
|
import { pipe } from 'it-pipe'
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { expect } from 'aegir/utils/chai.js'
|
|
2
|
-
import { mockUpgrader } from '
|
|
2
|
+
import { mockUpgrader } from '../mocks/upgrader.js'
|
|
3
3
|
import type { TestSetup } from '../index.js'
|
|
4
4
|
import type { Transport } from '@libp2p/interfaces/transport'
|
|
5
5
|
import type { TransportTestFixtures, SetupArgs } from './index.js'
|
|
@@ -4,7 +4,8 @@ import sinon from 'sinon'
|
|
|
4
4
|
import pWaitFor from 'p-wait-for'
|
|
5
5
|
import { pipe } from 'it-pipe'
|
|
6
6
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
7
|
-
import { isValidTick
|
|
7
|
+
import { isValidTick } from '../utils/is-valid-tick.js'
|
|
8
|
+
import { mockUpgrader } from '../mocks/upgrader.js'
|
|
8
9
|
import defer from 'p-defer'
|
|
9
10
|
import { CustomEvent } from '@libp2p/interfaces'
|
|
10
11
|
import type { TestSetup } from '../index.js'
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* A tick is considered valid if it happened between now
|
|
4
|
+
* and `ms` milliseconds ago
|
|
5
|
+
*/
|
|
6
|
+
export function isValidTick (date?: number, ms: number = 5000) {
|
|
7
|
+
if (date == null) {
|
|
8
|
+
throw new Error('date must be a number')
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const now = Date.now()
|
|
12
|
+
|
|
13
|
+
if (date > now - ms && date <= now) {
|
|
14
|
+
return true
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return false
|
|
18
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/pubsub/utils.ts"],"names":[],"mappings":"AAEA,wBAAgB,KAAK,CAAE,CAAC,EAAG,GAAG,EAAE,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,GAAG,CAAC,CAE9C;AAED,wBAAgB,SAAS,CAAE,CAAC,EAAG,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,CAAC,EAAE,QAMtD"}
|
package/dist/src/pubsub/utils.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { expect } from 'aegir/utils/chai.js';
|
|
2
|
-
export function first(map) {
|
|
3
|
-
return map.values().next().value;
|
|
4
|
-
}
|
|
5
|
-
export function expectSet(set, subs) {
|
|
6
|
-
if ((set == null) || (subs == null)) {
|
|
7
|
-
throw new Error('No set or subs passed');
|
|
8
|
-
}
|
|
9
|
-
expect(Array.from(set.values())).to.eql(subs);
|
|
10
|
-
}
|
|
11
|
-
//# sourceMappingURL=utils.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/pubsub/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AAE5C,MAAM,UAAU,KAAK,CAAM,GAAgB;IACzC,OAAO,GAAG,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAA;AAClC,CAAC;AAED,MAAM,UAAU,SAAS,CAAM,GAAY,EAAE,IAAU;IACrD,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,EAAE;QACnC,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAA;KACzC;IAED,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;AAC/C,CAAC"}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { Upgrader, MultiaddrConnection } from '@libp2p/interfaces/transport';
|
|
2
|
-
import type { Muxer } from '@libp2p/interfaces/stream-muxer';
|
|
3
|
-
import type { Duplex } from 'it-stream-types';
|
|
4
|
-
/**
|
|
5
|
-
* A tick is considered valid if it happened between now
|
|
6
|
-
* and `ms` milliseconds ago
|
|
7
|
-
*/
|
|
8
|
-
export declare function isValidTick(date?: number, ms?: number): boolean;
|
|
9
|
-
export declare function mockMultiaddrConnection(source: Duplex<Uint8Array>): MultiaddrConnection;
|
|
10
|
-
export declare function mockMuxer(): Muxer;
|
|
11
|
-
export interface MockUpgraderOptions {
|
|
12
|
-
muxer?: Muxer;
|
|
13
|
-
}
|
|
14
|
-
export declare function mockUpgrader(options?: MockUpgraderOptions): Upgrader;
|
|
15
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/transport/utils/index.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,QAAQ,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAA;AAEjF,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,iCAAiC,CAAA;AAC5D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAE7C;;;GAGG;AACH,wBAAgB,WAAW,CAAE,IAAI,CAAC,EAAE,MAAM,EAAE,EAAE,GAAE,MAAa,WAY5D;AAED,wBAAgB,uBAAuB,CAAE,MAAM,EAAE,MAAM,CAAC,UAAU,CAAC,GAAG,mBAAmB,CAaxF;AAED,wBAAgB,SAAS,IAAK,KAAK,CAoClC;AAED,MAAM,WAAW,mBAAmB;IAClC,KAAK,CAAC,EAAE,KAAK,CAAA;CACd;AAED,wBAAgB,YAAY,CAAE,OAAO,GAAE,mBAAwB,YAuB9D"}
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import { expect } from 'aegir/utils/chai.js';
|
|
2
|
-
import { pair } from 'it-pair';
|
|
3
|
-
import { peerIdFromString } from '@libp2p/peer-id';
|
|
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
|
-
/**
|
|
10
|
-
* A tick is considered valid if it happened between now
|
|
11
|
-
* and `ms` milliseconds ago
|
|
12
|
-
*/
|
|
13
|
-
export function isValidTick(date, ms = 5000) {
|
|
14
|
-
if (date == null) {
|
|
15
|
-
throw new Error('date must be a number');
|
|
16
|
-
}
|
|
17
|
-
const now = Date.now();
|
|
18
|
-
if (date > now - ms && date <= now) {
|
|
19
|
-
return true;
|
|
20
|
-
}
|
|
21
|
-
return false;
|
|
22
|
-
}
|
|
23
|
-
export function mockMultiaddrConnection(source) {
|
|
24
|
-
const maConn = {
|
|
25
|
-
...source,
|
|
26
|
-
async close() {
|
|
27
|
-
},
|
|
28
|
-
timeline: {
|
|
29
|
-
open: Date.now()
|
|
30
|
-
},
|
|
31
|
-
remoteAddr: new Multiaddr('/ip4/127.0.0.1/tcp/4001')
|
|
32
|
-
};
|
|
33
|
-
return maConn;
|
|
34
|
-
}
|
|
35
|
-
export function mockMuxer() {
|
|
36
|
-
let streamId = 0;
|
|
37
|
-
let streams = [];
|
|
38
|
-
const p = pushable();
|
|
39
|
-
const muxer = {
|
|
40
|
-
source: p,
|
|
41
|
-
sink: async (source) => {
|
|
42
|
-
await drain(source);
|
|
43
|
-
},
|
|
44
|
-
get streams() {
|
|
45
|
-
return streams;
|
|
46
|
-
},
|
|
47
|
-
newStream: (name) => {
|
|
48
|
-
const echo = pair();
|
|
49
|
-
const id = `${streamId++}`;
|
|
50
|
-
const stream = {
|
|
51
|
-
id,
|
|
52
|
-
sink: echo.sink,
|
|
53
|
-
source: echo.source,
|
|
54
|
-
close: () => {
|
|
55
|
-
streams = streams.filter(s => s !== stream);
|
|
56
|
-
},
|
|
57
|
-
abort: () => { },
|
|
58
|
-
reset: () => { },
|
|
59
|
-
timeline: {
|
|
60
|
-
open: 0
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
return stream;
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
return muxer;
|
|
67
|
-
}
|
|
68
|
-
export function mockUpgrader(options = {}) {
|
|
69
|
-
const ensureProps = (multiaddrConnection) => {
|
|
70
|
-
['sink', 'source', 'remoteAddr', 'timeline', 'close'].forEach(prop => {
|
|
71
|
-
expect(multiaddrConnection).to.have.property(prop);
|
|
72
|
-
});
|
|
73
|
-
expect(isValidTick(multiaddrConnection.timeline.open)).to.equal(true);
|
|
74
|
-
return multiaddrConnection;
|
|
75
|
-
};
|
|
76
|
-
const muxer = options.muxer ?? mockMuxer();
|
|
77
|
-
const upgrader = {
|
|
78
|
-
async upgradeOutbound(multiaddrConnection) {
|
|
79
|
-
ensureProps(multiaddrConnection);
|
|
80
|
-
return await createConnection(multiaddrConnection, 'outbound', muxer);
|
|
81
|
-
},
|
|
82
|
-
async upgradeInbound(multiaddrConnection) {
|
|
83
|
-
ensureProps(multiaddrConnection);
|
|
84
|
-
return await createConnection(multiaddrConnection, 'inbound', muxer);
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
return upgrader;
|
|
88
|
-
}
|
|
89
|
-
async function createConnection(maConn, direction, muxer) {
|
|
90
|
-
const remoteAddr = maConn.remoteAddr;
|
|
91
|
-
const remotePeerIdStr = remoteAddr.getPeerId();
|
|
92
|
-
const remotePeer = remotePeerIdStr != null ? peerIdFromString(remotePeerIdStr) : await PeerIdFactory.createEd25519PeerId();
|
|
93
|
-
const streams = [];
|
|
94
|
-
let streamId = 0;
|
|
95
|
-
const registry = new Map();
|
|
96
|
-
void pipe(maConn, muxer, maConn);
|
|
97
|
-
return {
|
|
98
|
-
id: 'mock-connection',
|
|
99
|
-
remoteAddr,
|
|
100
|
-
remotePeer,
|
|
101
|
-
stat: {
|
|
102
|
-
status: 'OPEN',
|
|
103
|
-
direction,
|
|
104
|
-
timeline: maConn.timeline,
|
|
105
|
-
multiplexer: 'test-multiplexer',
|
|
106
|
-
encryption: 'yes-yes-very-secure'
|
|
107
|
-
},
|
|
108
|
-
registry,
|
|
109
|
-
tags: [],
|
|
110
|
-
streams,
|
|
111
|
-
newStream: async (protocols) => {
|
|
112
|
-
if (!Array.isArray(protocols)) {
|
|
113
|
-
protocols = [protocols];
|
|
114
|
-
}
|
|
115
|
-
if (protocols.length === 0) {
|
|
116
|
-
throw new Error('protocols must have a length');
|
|
117
|
-
}
|
|
118
|
-
const id = `${streamId++}`;
|
|
119
|
-
const stream = muxer.newStream(id);
|
|
120
|
-
const streamData = {
|
|
121
|
-
protocol: protocols[0],
|
|
122
|
-
stream
|
|
123
|
-
};
|
|
124
|
-
registry.set(id, streamData);
|
|
125
|
-
return streamData;
|
|
126
|
-
},
|
|
127
|
-
addStream: (stream, metadata) => {
|
|
128
|
-
},
|
|
129
|
-
removeStream: (id) => {
|
|
130
|
-
registry.delete(id);
|
|
131
|
-
},
|
|
132
|
-
close: async () => {
|
|
133
|
-
await maConn.close();
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/transport/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AAC5C,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAClD,OAAO,KAAK,aAAa,MAAM,yBAAyB,CAAA;AACxD,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AACtC,OAAO,KAAK,MAAM,UAAU,CAAA;AAC5B,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACnD,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAM9B;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAE,IAAa,EAAE,KAAa,IAAI;IAC3D,IAAI,IAAI,IAAI,IAAI,EAAE;QAChB,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAA;KACzC;IAED,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;IAEtB,IAAI,IAAI,GAAG,GAAG,GAAG,EAAE,IAAI,IAAI,IAAI,GAAG,EAAE;QAClC,OAAO,IAAI,CAAA;KACZ;IAED,OAAO,KAAK,CAAA;AACd,CAAC;AAED,MAAM,UAAU,uBAAuB,CAAE,MAA0B;IACjE,MAAM,MAAM,GAAwB;QAClC,GAAG,MAAM;QACT,KAAK,CAAC,KAAK;QAEX,CAAC;QACD,QAAQ,EAAE;YACR,IAAI,EAAE,IAAI,CAAC,GAAG,EAAE;SACjB;QACD,UAAU,EAAE,IAAI,SAAS,CAAC,yBAAyB,CAAC;KACrD,CAAA;IAED,OAAO,MAAM,CAAA;AACf,CAAC;AAED,MAAM,UAAU,SAAS;IACvB,IAAI,QAAQ,G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AAM,EAAE,MAAM;YACd,SAAS;YACT,QAAQ,EAAE,MAAM,CAAC,QAAQ;YACzB,WAAW,EAAE,kBAAkB;YAC/B,UAAU,EAAE,qBAAqB;SAClC;QACD,QAAQ;QACR,IAAI,EAAE,EAAE;QACR,OAAO;QACP,SAAS,EAAE,KAAK,EAAE,SAAS,EAAE,EAAE;YAC7B,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE;gBAC7B,SAAS,GAAG,CAAC,SAAS,CAAC,CAAA;aACxB;YAED,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE;gBAC1B,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAA;aAChD;YAED,MAAM,EAAE,GAAG,GAAG,QAAQ,EAAE,EAAE,CAAA;YAC1B,MAAM,MAAM,GAAW,KAAK,CAAC,SAAS,CAAC,EAAE,CAAC,CAAA;YAC1C,MAAM,UAAU,GAAmB;gBACjC,QAAQ,EAAE,SAAS,CAAC,CAAC,CAAC;gBACtB,MAAM;aACP,CAAA;YAED,QAAQ,CAAC,GAAG,CAAC,EAAE,EAAE,UAAU,CAAC,CAAA;YAE5B,OAAO,UAAU,CAAA;QACnB,CAAC;QACD,SAAS,EAAE,CAAC,MAAc,EAAE,QAAkB,EAAE,EAAE;QAElD,CAAC;QACD,YAAY,EAAE,CAAC,EAAU,EAAE,EAAE;YAC3B,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;QACrB,CAAC;QACD,KAAK,EAAE,KAAK,IAAI,EAAE;YAChB,MAAM,MAAM,CAAC,KAAK,EAAE,CAAA;QACtB,CAAC;KACF,CAAA;AACH,CAAC"}
|
package/src/pubsub/utils.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { expect } from 'aegir/utils/chai.js'
|
|
2
|
-
|
|
3
|
-
export function first <V> (map: Map<any, V>): V {
|
|
4
|
-
return map.values().next().value
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export function expectSet <T> (set?: Set<T>, subs?: T[]) {
|
|
8
|
-
if ((set == null) || (subs == null)) {
|
|
9
|
-
throw new Error('No set or subs passed')
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
expect(Array.from(set.values())).to.eql(subs)
|
|
13
|
-
}
|