@libp2p/interface-compliance-tests 6.5.0 → 7.0.0-049bfa0fa

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 (133) hide show
  1. package/dist/src/connection-encryption/index.d.ts.map +1 -1
  2. package/dist/src/connection-encryption/index.js +24 -15
  3. package/dist/src/connection-encryption/index.js.map +1 -1
  4. package/dist/src/stream-muxer/base-test.d.ts.map +1 -1
  5. package/dist/src/stream-muxer/base-test.js +62 -345
  6. package/dist/src/stream-muxer/base-test.js.map +1 -1
  7. package/dist/src/stream-muxer/close-test.d.ts.map +1 -1
  8. package/dist/src/stream-muxer/close-test.js +254 -320
  9. package/dist/src/stream-muxer/close-test.js.map +1 -1
  10. package/dist/src/stream-muxer/index.js +2 -2
  11. package/dist/src/stream-muxer/index.js.map +1 -1
  12. package/dist/src/stream-muxer/{mega-stress-test.d.ts → stream-test.d.ts} +2 -2
  13. package/dist/src/stream-muxer/stream-test.d.ts.map +1 -0
  14. package/dist/src/stream-muxer/stream-test.js +290 -0
  15. package/dist/src/stream-muxer/stream-test.js.map +1 -0
  16. package/dist/src/stream-muxer/stress-test.d.ts.map +1 -1
  17. package/dist/src/stream-muxer/stress-test.js +70 -16
  18. package/dist/src/stream-muxer/stress-test.js.map +1 -1
  19. package/dist/src/transport/index.d.ts.map +1 -1
  20. package/dist/src/transport/index.js +235 -205
  21. package/dist/src/transport/index.js.map +1 -1
  22. package/dist/src/transport/utils.d.ts.map +1 -1
  23. package/dist/src/transport/utils.js +3 -2
  24. package/dist/src/transport/utils.js.map +1 -1
  25. package/package.json +23 -51
  26. package/src/connection-encryption/index.ts +27 -20
  27. package/src/stream-muxer/base-test.ts +75 -413
  28. package/src/stream-muxer/close-test.ts +305 -343
  29. package/src/stream-muxer/index.ts +2 -2
  30. package/src/stream-muxer/stream-test.ts +381 -0
  31. package/src/stream-muxer/stress-test.ts +92 -18
  32. package/src/transport/index.ts +281 -241
  33. package/src/transport/utils.ts +3 -2
  34. package/dist/src/connection-encryption/utils/index.d.ts +0 -3
  35. package/dist/src/connection-encryption/utils/index.d.ts.map +0 -1
  36. package/dist/src/connection-encryption/utils/index.js +0 -21
  37. package/dist/src/connection-encryption/utils/index.js.map +0 -1
  38. package/dist/src/matchers.d.ts +0 -12
  39. package/dist/src/matchers.d.ts.map +0 -1
  40. package/dist/src/matchers.js +0 -14
  41. package/dist/src/matchers.js.map +0 -1
  42. package/dist/src/mocks/connection-manager.d.ts +0 -27
  43. package/dist/src/mocks/connection-manager.d.ts.map +0 -1
  44. package/dist/src/mocks/connection-manager.js +0 -147
  45. package/dist/src/mocks/connection-manager.js.map +0 -1
  46. package/dist/src/mocks/connection.d.ts +0 -41
  47. package/dist/src/mocks/connection.d.ts.map +0 -1
  48. package/dist/src/mocks/connection.js +0 -236
  49. package/dist/src/mocks/connection.js.map +0 -1
  50. package/dist/src/mocks/duplex.d.ts +0 -4
  51. package/dist/src/mocks/duplex.d.ts.map +0 -1
  52. package/dist/src/mocks/duplex.js +0 -9
  53. package/dist/src/mocks/duplex.js.map +0 -1
  54. package/dist/src/mocks/index.d.ts +0 -12
  55. package/dist/src/mocks/index.d.ts.map +0 -1
  56. package/dist/src/mocks/index.js +0 -8
  57. package/dist/src/mocks/index.js.map +0 -1
  58. package/dist/src/mocks/multiaddr-connection.d.ts +0 -17
  59. package/dist/src/mocks/multiaddr-connection.d.ts.map +0 -1
  60. package/dist/src/mocks/multiaddr-connection.js +0 -64
  61. package/dist/src/mocks/multiaddr-connection.js.map +0 -1
  62. package/dist/src/mocks/muxer.d.ts +0 -36
  63. package/dist/src/mocks/muxer.d.ts.map +0 -1
  64. package/dist/src/mocks/muxer.js +0 -234
  65. package/dist/src/mocks/muxer.js.map +0 -1
  66. package/dist/src/mocks/registrar.d.ts +0 -16
  67. package/dist/src/mocks/registrar.d.ts.map +0 -1
  68. package/dist/src/mocks/registrar.js +0 -66
  69. package/dist/src/mocks/registrar.js.map +0 -1
  70. package/dist/src/mocks/upgrader.d.ts +0 -9
  71. package/dist/src/mocks/upgrader.d.ts.map +0 -1
  72. package/dist/src/mocks/upgrader.js +0 -46
  73. package/dist/src/mocks/upgrader.js.map +0 -1
  74. package/dist/src/pubsub/api.d.ts +0 -6
  75. package/dist/src/pubsub/api.d.ts.map +0 -1
  76. package/dist/src/pubsub/api.js +0 -88
  77. package/dist/src/pubsub/api.js.map +0 -1
  78. package/dist/src/pubsub/connection-handlers.d.ts +0 -6
  79. package/dist/src/pubsub/connection-handlers.d.ts.map +0 -1
  80. package/dist/src/pubsub/connection-handlers.js +0 -329
  81. package/dist/src/pubsub/connection-handlers.js.map +0 -1
  82. package/dist/src/pubsub/emit-self.d.ts +0 -6
  83. package/dist/src/pubsub/emit-self.d.ts.map +0 -1
  84. package/dist/src/pubsub/emit-self.js +0 -80
  85. package/dist/src/pubsub/emit-self.js.map +0 -1
  86. package/dist/src/pubsub/index.d.ts +0 -18
  87. package/dist/src/pubsub/index.d.ts.map +0 -1
  88. package/dist/src/pubsub/index.js +0 -17
  89. package/dist/src/pubsub/index.js.map +0 -1
  90. package/dist/src/pubsub/messages.d.ts +0 -6
  91. package/dist/src/pubsub/messages.d.ts.map +0 -1
  92. package/dist/src/pubsub/messages.js +0 -48
  93. package/dist/src/pubsub/messages.js.map +0 -1
  94. package/dist/src/pubsub/multiple-nodes.d.ts +0 -6
  95. package/dist/src/pubsub/multiple-nodes.d.ts.map +0 -1
  96. package/dist/src/pubsub/multiple-nodes.js +0 -350
  97. package/dist/src/pubsub/multiple-nodes.js.map +0 -1
  98. package/dist/src/pubsub/two-nodes.d.ts +0 -6
  99. package/dist/src/pubsub/two-nodes.d.ts.map +0 -1
  100. package/dist/src/pubsub/two-nodes.js +0 -216
  101. package/dist/src/pubsub/two-nodes.js.map +0 -1
  102. package/dist/src/pubsub/utils.d.ts +0 -5
  103. package/dist/src/pubsub/utils.d.ts.map +0 -1
  104. package/dist/src/pubsub/utils.js +0 -27
  105. package/dist/src/pubsub/utils.js.map +0 -1
  106. package/dist/src/stream-muxer/mega-stress-test.d.ts.map +0 -1
  107. package/dist/src/stream-muxer/mega-stress-test.js +0 -11
  108. package/dist/src/stream-muxer/mega-stress-test.js.map +0 -1
  109. package/dist/src/stream-muxer/spawner.d.ts +0 -4
  110. package/dist/src/stream-muxer/spawner.d.ts.map +0 -1
  111. package/dist/src/stream-muxer/spawner.js +0 -39
  112. package/dist/src/stream-muxer/spawner.js.map +0 -1
  113. package/dist/typedoc-urls.json +0 -44
  114. package/src/connection-encryption/utils/index.ts +0 -27
  115. package/src/matchers.ts +0 -18
  116. package/src/mocks/connection-manager.ts +0 -216
  117. package/src/mocks/connection.ts +0 -309
  118. package/src/mocks/duplex.ts +0 -11
  119. package/src/mocks/index.ts +0 -11
  120. package/src/mocks/multiaddr-connection.ts +0 -80
  121. package/src/mocks/muxer.ts +0 -331
  122. package/src/mocks/registrar.ts +0 -86
  123. package/src/mocks/upgrader.ts +0 -65
  124. package/src/pubsub/api.ts +0 -116
  125. package/src/pubsub/connection-handlers.ts +0 -413
  126. package/src/pubsub/emit-self.ts +0 -99
  127. package/src/pubsub/index.ts +0 -34
  128. package/src/pubsub/messages.ts +0 -59
  129. package/src/pubsub/multiple-nodes.ts +0 -440
  130. package/src/pubsub/two-nodes.ts +0 -272
  131. package/src/pubsub/utils.ts +0 -34
  132. package/src/stream-muxer/mega-stress-test.ts +0 -14
  133. package/src/stream-muxer/spawner.ts +0 -57
@@ -1,331 +0,0 @@
1
- import { defaultLogger, logger } from '@libp2p/logger'
2
- import { AbstractStream } from '@libp2p/utils/abstract-stream'
3
- import { abortableSource } from 'abortable-iterator'
4
- import map from 'it-map'
5
- import * as ndjson from 'it-ndjson'
6
- import { pipe } from 'it-pipe'
7
- import { pushable } from 'it-pushable'
8
- import { Uint8ArrayList } from 'uint8arraylist'
9
- import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
10
- import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
11
- import type { AbortOptions, Direction, Stream, StreamMuxer, StreamMuxerFactory, StreamMuxerInit } from '@libp2p/interface'
12
- import type { Logger } from '@libp2p/logger'
13
- import type { AbstractStreamInit } from '@libp2p/utils/abstract-stream'
14
- import type { Pushable } from 'it-pushable'
15
- import type { Source } from 'it-stream-types'
16
-
17
- let muxers = 0
18
- let streams = 0
19
-
20
- interface DataMessage {
21
- id: string
22
- type: 'data'
23
- direction: Direction
24
- chunk: string
25
- }
26
-
27
- interface ResetMessage {
28
- id: string
29
- type: 'reset'
30
- direction: Direction
31
- }
32
-
33
- interface CloseWriteMessage {
34
- id: string
35
- type: 'closeWrite'
36
- direction: Direction
37
- }
38
-
39
- interface CloseReadMessage {
40
- id: string
41
- type: 'closeRead'
42
- direction: Direction
43
- }
44
-
45
- interface CreateMessage {
46
- id: string
47
- type: 'create'
48
- direction: 'outbound'
49
- }
50
-
51
- type StreamMessage = DataMessage | ResetMessage | CloseWriteMessage | CloseReadMessage | CreateMessage
52
-
53
- export interface MockMuxedStreamInit extends AbstractStreamInit {
54
- push: Pushable<StreamMessage>
55
- }
56
-
57
- class MuxedStream extends AbstractStream {
58
- private readonly push: Pushable<StreamMessage>
59
-
60
- constructor (init: MockMuxedStreamInit) {
61
- super(init)
62
-
63
- this.push = init.push
64
- }
65
-
66
- sendNewStream (): void {
67
- // If initiator, open a new stream
68
- const createMsg: CreateMessage = {
69
- id: this.id,
70
- type: 'create',
71
- direction: 'outbound'
72
- }
73
- this.push.push(createMsg)
74
- }
75
-
76
- sendData (data: Uint8ArrayList): void {
77
- const dataMsg: DataMessage = {
78
- id: this.id,
79
- type: 'data',
80
- chunk: uint8ArrayToString(data.subarray(), 'base64pad'),
81
- direction: this.direction
82
- }
83
- this.push.push(dataMsg)
84
- }
85
-
86
- sendReset (): void {
87
- const resetMsg: ResetMessage = {
88
- id: this.id,
89
- type: 'reset',
90
- direction: this.direction
91
- }
92
- this.push.push(resetMsg)
93
- }
94
-
95
- sendCloseWrite (): void {
96
- const closeMsg: CloseWriteMessage = {
97
- id: this.id,
98
- type: 'closeWrite',
99
- direction: this.direction
100
- }
101
- this.push.push(closeMsg)
102
- }
103
-
104
- sendCloseRead (): void {
105
- const closeMsg: CloseReadMessage = {
106
- id: this.id,
107
- type: 'closeRead',
108
- direction: this.direction
109
- }
110
- this.push.push(closeMsg)
111
- }
112
- }
113
-
114
- class MockMuxer implements StreamMuxer {
115
- public source: AsyncGenerator<Uint8Array>
116
- public input: Pushable<Uint8Array>
117
- public streamInput: Pushable<StreamMessage>
118
- public name: string
119
- public protocol: string = '/mock-muxer/1.0.0'
120
-
121
- private readonly closeController: AbortController
122
- private readonly registryInitiatorStreams: Map<string, MuxedStream>
123
- private readonly registryRecipientStreams: Map<string, MuxedStream>
124
- private readonly options: StreamMuxerInit
125
-
126
- private readonly log: Logger
127
-
128
- constructor (init?: StreamMuxerInit) {
129
- this.name = `muxer:${muxers++}`
130
- this.log = logger(`libp2p:mock-muxer:${this.name}`)
131
- this.registryInitiatorStreams = new Map()
132
- this.registryRecipientStreams = new Map()
133
- this.log('create muxer')
134
- this.options = init ?? { direction: 'inbound', log: defaultLogger().forComponent('mock-muxer') }
135
- this.closeController = new AbortController()
136
- // receives data from the muxer at the other end of the stream
137
- this.source = this.input = pushable({
138
- onEnd: () => {
139
- for (const stream of this.registryInitiatorStreams.values()) {
140
- stream.destroy()
141
- }
142
-
143
- for (const stream of this.registryRecipientStreams.values()) {
144
- stream.destroy()
145
- }
146
- }
147
- })
148
-
149
- // receives messages from all of the muxed streams
150
- this.streamInput = pushable<StreamMessage>({
151
- objectMode: true
152
- })
153
- }
154
-
155
- // receive incoming messages
156
- async sink (source: Source<Uint8ArrayList | Uint8Array>): Promise<void> {
157
- try {
158
- await pipe(
159
- abortableSource(source, this.closeController.signal),
160
- (source) => map(source, buf => uint8ArrayToString(buf.subarray())),
161
- ndjson.parse<StreamMessage>,
162
- async (source) => {
163
- for await (const message of source) {
164
- this.log.trace('-> %s %s %s', message.type, message.direction, message.id)
165
- this.handleMessage(message)
166
- }
167
- }
168
- )
169
-
170
- this.log('muxer ended')
171
- this.input.end()
172
- } catch (err: any) {
173
- this.log.error('muxer errored - %e', err)
174
- this.input.end(err)
175
- }
176
- }
177
-
178
- handleMessage (message: StreamMessage): void {
179
- let muxedStream: MuxedStream | undefined
180
-
181
- const registry = message.direction === 'outbound' ? this.registryRecipientStreams : this.registryInitiatorStreams
182
-
183
- if (message.type === 'create') {
184
- if (registry.has(message.id)) {
185
- throw new Error(`Already had stream for ${message.id}`)
186
- }
187
-
188
- muxedStream = this.createStream(message.id, 'inbound')
189
- registry.set(muxedStream.id, muxedStream)
190
-
191
- if (this.options.onIncomingStream != null) {
192
- this.options.onIncomingStream(muxedStream)
193
- }
194
- }
195
-
196
- muxedStream = registry.get(message.id)
197
-
198
- if (muxedStream == null) {
199
- this.log.error(`No stream found for ${message.id}`)
200
-
201
- return
202
- }
203
-
204
- if (message.type === 'data') {
205
- muxedStream.sourcePush(new Uint8ArrayList(uint8ArrayFromString(message.chunk, 'base64pad')))
206
- } else if (message.type === 'reset') {
207
- this.log('-> reset stream %s %s', muxedStream.direction, muxedStream.id)
208
- muxedStream.reset()
209
- } else if (message.type === 'closeWrite') {
210
- this.log('-> closing writeable end of stream %s %s', muxedStream.direction, muxedStream.id)
211
- muxedStream.remoteCloseWrite()
212
- } else if (message.type === 'closeRead') {
213
- this.log('-> closing readable end of stream %s %s', muxedStream.direction, muxedStream.id)
214
- muxedStream.remoteCloseRead()
215
- }
216
- }
217
-
218
- get streams (): Stream[] {
219
- return Array.from(this.registryRecipientStreams.values())
220
- .concat(Array.from(this.registryInitiatorStreams.values()))
221
- }
222
-
223
- newStream (name?: string): Stream {
224
- if (this.closeController.signal.aborted) {
225
- throw new Error('Muxer already closed')
226
- }
227
- this.log('newStream %s', name)
228
- const storedStream = this.createStream(name, 'outbound')
229
- this.registryInitiatorStreams.set(storedStream.id, storedStream)
230
-
231
- return storedStream
232
- }
233
-
234
- createStream (name?: string, direction: Direction = 'outbound'): MuxedStream {
235
- const id = name ?? `${streams++}`
236
-
237
- this.log('createStream %s %s', direction, id)
238
-
239
- const muxedStream: MuxedStream = new MuxedStream({
240
- id,
241
- direction,
242
- push: this.streamInput,
243
- onEnd: () => {
244
- this.log('stream ended')
245
-
246
- if (direction === 'outbound') {
247
- this.registryInitiatorStreams.delete(muxedStream.id)
248
- } else {
249
- this.registryRecipientStreams.delete(muxedStream.id)
250
- }
251
-
252
- if (this.options.onStreamEnd != null) {
253
- this.options.onStreamEnd(muxedStream)
254
- }
255
- },
256
- log: logger(`libp2p:mock-muxer:stream:${direction}:${id}`)
257
- })
258
-
259
- return muxedStream
260
- }
261
-
262
- async close (options?: AbortOptions): Promise<void> {
263
- if (this.closeController.signal.aborted) {
264
- return
265
- }
266
-
267
- const signal = options?.signal ?? AbortSignal.timeout(10)
268
-
269
- try {
270
- // try to gracefully close all streams
271
- await Promise.all(
272
- this.streams.map(async s => s.close({
273
- signal
274
- }))
275
- )
276
-
277
- this.input.end()
278
-
279
- // try to gracefully close the muxer
280
- await this.input.onEmpty({
281
- signal
282
- })
283
-
284
- this.closeController.abort()
285
- } catch (err: any) {
286
- this.abort(err)
287
- }
288
- }
289
-
290
- abort (err: Error): void {
291
- if (this.closeController.signal.aborted) {
292
- return
293
- }
294
-
295
- this.log('aborting muxed streams')
296
-
297
- this.streams.forEach(s => {
298
- s.abort(err)
299
- })
300
-
301
- this.closeController.abort(err)
302
- this.input.end(err)
303
- }
304
- }
305
-
306
- class MockMuxerFactory implements StreamMuxerFactory {
307
- public protocol: string = '/mock-muxer/1.0.0'
308
-
309
- createStreamMuxer (init?: StreamMuxerInit): StreamMuxer {
310
- const mockMuxer = new MockMuxer(init)
311
-
312
- void Promise.resolve().then(async () => {
313
- void pipe(
314
- mockMuxer.streamInput,
315
- ndjson.stringify,
316
- (source) => map(source, str => new Uint8ArrayList(uint8ArrayFromString(str))),
317
- async (source) => {
318
- for await (const buf of source) {
319
- mockMuxer.input.push(buf.subarray())
320
- }
321
- }
322
- )
323
- })
324
-
325
- return mockMuxer
326
- }
327
- }
328
-
329
- export function mockMuxer (): StreamMuxerFactory {
330
- return new MockMuxerFactory()
331
- }
@@ -1,86 +0,0 @@
1
- import { mergeOptions } from '@libp2p/utils/merge-options'
2
- import type { Connection, PeerId, Topology, IncomingStreamData, StreamHandler, StreamHandlerOptions, StreamHandlerRecord } from '@libp2p/interface'
3
- import type { Registrar } from '@libp2p/interface-internal'
4
-
5
- export class MockRegistrar implements Registrar {
6
- private readonly topologies = new Map<string, Array<{ id: string, topology: Topology }>>()
7
- private readonly handlers = new Map<string, StreamHandlerRecord>()
8
-
9
- getProtocols (): string[] {
10
- return Array.from(this.handlers.keys()).sort()
11
- }
12
-
13
- async handle (protocol: string, handler: StreamHandler, opts?: StreamHandlerOptions): Promise<void> {
14
- const options = mergeOptions.bind({ ignoreUndefined: true })({
15
- maxInboundStreams: 1,
16
- maxOutboundStreams: 1
17
- }, opts)
18
-
19
- if (this.handlers.has(protocol)) {
20
- throw new Error(`Handler already registered for protocol ${protocol}`)
21
- }
22
-
23
- this.handlers.set(protocol, {
24
- handler,
25
- options
26
- })
27
- }
28
-
29
- async unhandle (protocol: string): Promise<void> {
30
- this.handlers.delete(protocol)
31
- }
32
-
33
- getHandler (protocol: string): StreamHandlerRecord {
34
- const handler = this.handlers.get(protocol)
35
-
36
- if (handler == null) {
37
- throw new Error(`No handler registered for protocol ${protocol}`)
38
- }
39
-
40
- return handler
41
- }
42
-
43
- async register (protocol: string, topology: Topology): Promise<string> {
44
- const id = `topology-id-${Math.random()}`
45
- let topologies = this.topologies.get(protocol)
46
-
47
- if (topologies == null) {
48
- topologies = []
49
- }
50
-
51
- topologies.push({
52
- id,
53
- topology
54
- })
55
-
56
- this.topologies.set(protocol, topologies)
57
-
58
- return id
59
- }
60
-
61
- unregister (id: string | string[]): void {
62
- if (!Array.isArray(id)) {
63
- id = [id]
64
- }
65
-
66
- id.forEach(id => this.topologies.delete(id))
67
- }
68
-
69
- getTopologies (protocol: string): Topology[] {
70
- return (this.topologies.get(protocol) ?? []).map(t => t.topology)
71
- }
72
- }
73
-
74
- export function mockRegistrar (): Registrar {
75
- return new MockRegistrar()
76
- }
77
-
78
- export async function mockIncomingStreamEvent (protocol: string, conn: Connection, remotePeer: PeerId): Promise<IncomingStreamData> {
79
- return {
80
- ...await conn.newStream([protocol]),
81
- // @ts-expect-error incomplete implementation
82
- connection: {
83
- remotePeer
84
- }
85
- }
86
- }
@@ -1,65 +0,0 @@
1
- import { anySignal } from 'any-signal'
2
- import { setMaxListeners } from 'main-event'
3
- import { mockConnection } from './connection.js'
4
- import type { Libp2pEvents, Connection, MultiaddrConnection, Upgrader, UpgraderOptions, ClearableSignal, ConnectionEncrypter, StreamMuxerFactory } from '@libp2p/interface'
5
- import type { Registrar } from '@libp2p/interface-internal'
6
- import type { TypedEventTarget } from 'main-event'
7
-
8
- export interface MockUpgraderInit {
9
- registrar?: Registrar
10
- events?: TypedEventTarget<Libp2pEvents>
11
- }
12
-
13
- class MockUpgrader implements Upgrader {
14
- private readonly registrar?: Registrar
15
- private readonly events?: TypedEventTarget<Libp2pEvents>
16
-
17
- constructor (init: MockUpgraderInit) {
18
- this.registrar = init.registrar
19
- this.events = init.events
20
- }
21
-
22
- async upgradeOutbound (multiaddrConnection: MultiaddrConnection, opts: UpgraderOptions): Promise<Connection> {
23
- const connection = mockConnection(multiaddrConnection, {
24
- direction: 'outbound',
25
- registrar: this.registrar,
26
- ...opts
27
- })
28
-
29
- this.events?.safeDispatchEvent('connection:open', { detail: connection })
30
-
31
- return connection
32
- }
33
-
34
- async upgradeInbound (multiaddrConnection: MultiaddrConnection, opts: UpgraderOptions): Promise<void> {
35
- const connection = mockConnection(multiaddrConnection, {
36
- direction: 'inbound',
37
- registrar: this.registrar,
38
- ...opts
39
- })
40
-
41
- this.events?.safeDispatchEvent('connection:open', { detail: connection })
42
- }
43
-
44
- createInboundAbortSignal (signal?: AbortSignal): ClearableSignal {
45
- const output = anySignal([
46
- AbortSignal.timeout(10_000),
47
- signal
48
- ])
49
- setMaxListeners(Infinity, output)
50
-
51
- return output
52
- }
53
-
54
- getConnectionEncrypters (): Map<string, ConnectionEncrypter<unknown>> {
55
- return new Map()
56
- }
57
-
58
- getStreamMuxers (): Map<string, StreamMuxerFactory> {
59
- return new Map()
60
- }
61
- }
62
-
63
- export function mockUpgrader (init: MockUpgraderInit = {}): Upgrader {
64
- return new MockUpgrader(init)
65
- }
package/src/pubsub/api.ts DELETED
@@ -1,116 +0,0 @@
1
- import { isPubSub, isStartable, start, stop } from '@libp2p/interface'
2
- import { expect } from 'aegir/chai'
3
- import delay from 'delay'
4
- import pDefer from 'p-defer'
5
- import pWaitFor from 'p-wait-for'
6
- import sinon from 'sinon'
7
- import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
8
- import { mockNetwork } from '../mocks/index.js'
9
- import { createComponents } from './utils.js'
10
- import type { PubSubArgs, PubSubComponents } from './index.js'
11
- import type { TestSetup } from '../index.js'
12
- import type { PubSub } from '@libp2p/interface'
13
-
14
- const topic = 'foo'
15
- const data = uint8ArrayFromString('bar')
16
-
17
- export default (common: TestSetup<PubSub, PubSubArgs>): void => {
18
- describe('pubsub api', () => {
19
- let pubsub: PubSub
20
- let components: PubSubComponents
21
-
22
- // Create pubsub router
23
- beforeEach(async () => {
24
- mockNetwork.reset()
25
- components = await createComponents()
26
-
27
- pubsub = components.pubsub = await common.setup({
28
- components,
29
- init: {
30
- emitSelf: true
31
- }
32
- })
33
- })
34
-
35
- afterEach(async () => {
36
- sinon.restore()
37
- await stop(...Object.values(components))
38
- await common.teardown()
39
- mockNetwork.reset()
40
- })
41
-
42
- it('is a PubSub implementation', () => {
43
- expect(isPubSub(pubsub)).to.be.true()
44
- })
45
-
46
- it('can start correctly', async () => {
47
- if (!isStartable(pubsub)) {
48
- return
49
- }
50
-
51
- sinon.spy(components.registrar, 'register')
52
-
53
- await start(...Object.values(components))
54
-
55
- expect(components.registrar.register).to.have.property('callCount', 1)
56
- })
57
-
58
- it('can stop correctly', async () => {
59
- if (!isStartable(pubsub)) {
60
- return
61
- }
62
-
63
- sinon.spy(components.registrar, 'unregister')
64
-
65
- await start(...Object.values(components))
66
- await stop(...Object.values(components))
67
-
68
- expect(components.registrar.unregister).to.have.property('callCount', 1)
69
- })
70
-
71
- it('can subscribe and unsubscribe correctly', async () => {
72
- const handler = (): void => {
73
- throw new Error('a message should not be received')
74
- }
75
-
76
- await start(...Object.values(components))
77
- pubsub.subscribe(topic)
78
- pubsub.addEventListener('message', handler)
79
-
80
- await pWaitFor(() => {
81
- const topics = pubsub.getTopics()
82
- return topics.length === 1 && topics[0] === topic
83
- })
84
-
85
- pubsub.removeEventListener('message', handler)
86
- pubsub.unsubscribe(topic)
87
-
88
- await pWaitFor(() => pubsub.getTopics().length === 0)
89
-
90
- // Publish to guarantee the handler is not called
91
- await pubsub.publish(topic, data)
92
-
93
- // handlers are called async
94
- await delay(100)
95
-
96
- await stop(...Object.values(components))
97
- })
98
-
99
- it('can subscribe and publish correctly', async () => {
100
- const defer = pDefer()
101
-
102
- await start(...Object.values(components))
103
-
104
- pubsub.subscribe(topic)
105
- pubsub.addEventListener('message', (evt) => {
106
- expect(evt).to.have.nested.property('detail.topic', topic)
107
- expect(evt).to.have.deep.nested.property('detail.data', data)
108
- defer.resolve()
109
- })
110
- await pubsub.publish(topic, data)
111
- await defer.promise
112
-
113
- await stop(...Object.values(components))
114
- })
115
- })
116
- }