@@ -11,7 +11,7 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
11
11
import { createConnection } from '../../src/connection.js'
12
12
import { UnhandledProtocolError } from '../../src/errors.ts'
13
13
import type { ConnectionComponents , ConnectionInit } from '../../src/connection.js'
14
- import type { MultiaddrConnection , PeerStore , StreamMuxer } from '@libp2p/interface'
14
+ import type { MultiaddrConnection , PeerStore , Stream , StreamMuxer } from '@libp2p/interface'
15
15
import type { Registrar } from '@libp2p/interface-internal'
16
16
import type { StubbedInstance } from 'sinon-ts'
17
17
@@ -38,6 +38,7 @@ describe('connection', () => {
38
38
} ,
39
39
options : { }
40
40
} )
41
+ registrar . getMiddleware . withArgs ( ECHO_PROTOCOL ) . returns ( [ ] )
41
42
42
43
components = {
43
44
peerStore,
@@ -223,6 +224,7 @@ describe('connection', () => {
223
224
}
224
225
} )
225
226
registrar . getProtocols . returns ( [ protocol ] )
227
+ registrar . getMiddleware . withArgs ( protocol ) . returns ( [ ] )
226
228
227
229
const connection = createConnection ( components , init )
228
230
expect ( connection . streams ) . to . have . lengthOf ( 0 )
@@ -259,6 +261,7 @@ describe('connection', () => {
259
261
}
260
262
} )
261
263
registrar . getProtocols . returns ( [ protocol ] )
264
+ registrar . getMiddleware . withArgs ( protocol ) . returns ( [ ] )
262
265
263
266
const connection = createConnection ( components , init )
264
267
expect ( connection . streams ) . to . have . lengthOf ( 0 )
@@ -274,6 +277,7 @@ describe('connection', () => {
274
277
const protocol = '/test/protocol'
275
278
276
279
registrar . getHandler . withArgs ( protocol ) . throws ( new UnhandledProtocolError ( ) )
280
+ registrar . getMiddleware . withArgs ( protocol ) . returns ( [ ] )
277
281
278
282
const connection = createConnection ( components , init )
279
283
expect ( connection . streams ) . to . have . lengthOf ( 0 )
@@ -289,4 +293,102 @@ describe('connection', () => {
289
293
await expect ( connection . newStream ( protocol , opts ) ) . to . eventually . be . rejected
290
294
. with . property ( 'name' , 'TooManyOutboundProtocolStreamsError' )
291
295
} )
296
+
297
+ it ( 'should support outgoing stream middleware' , async ( ) => {
298
+ const streamProtocol = '/test/protocol'
299
+
300
+ const middleware1 = Sinon . stub ( ) . callsFake ( ( stream , connection , next ) => {
301
+ next ( stream , connection )
302
+ } )
303
+ const middleware2 = Sinon . stub ( ) . callsFake ( ( stream , connection , next ) => {
304
+ next ( stream , connection )
305
+ } )
306
+
307
+ const middleware = [
308
+ middleware1 ,
309
+ middleware2
310
+ ]
311
+
312
+ registrar . getMiddleware . withArgs ( streamProtocol ) . returns ( middleware )
313
+ registrar . getHandler . withArgs ( streamProtocol ) . returns ( {
314
+ handler : ( ) => { } ,
315
+ options : { }
316
+ } )
317
+
318
+ const connection = createConnection ( components , init )
319
+
320
+ await connection . newStream ( streamProtocol )
321
+
322
+ expect ( middleware1 . called ) . to . be . true ( )
323
+ expect ( middleware2 . called ) . to . be . true ( )
324
+ } )
325
+
326
+ it ( 'should support incoming stream middleware' , async ( ) => {
327
+ const streamProtocol = '/test/protocol'
328
+
329
+ const middleware1 = Sinon . stub ( ) . callsFake ( ( stream , connection , next ) => {
330
+ next ( stream , connection )
331
+ } )
332
+ const middleware2 = Sinon . stub ( ) . callsFake ( ( stream , connection , next ) => {
333
+ next ( stream , connection )
334
+ } )
335
+
336
+ const middleware = [
337
+ middleware1 ,
338
+ middleware2
339
+ ]
340
+
341
+ registrar . getMiddleware . withArgs ( streamProtocol ) . returns ( middleware )
342
+ registrar . getHandler . withArgs ( streamProtocol ) . returns ( {
343
+ handler : ( ) => { } ,
344
+ options : { }
345
+ } )
346
+
347
+ const muxer = stubInterface < StreamMuxer > ( {
348
+ streams : [ ]
349
+ } )
350
+
351
+ createConnection ( components , {
352
+ ...init ,
353
+ muxer
354
+ } )
355
+
356
+ expect ( muxer . addEventListener . getCall ( 0 ) . args [ 0 ] ) . to . equal ( 'stream' )
357
+ const onIncomingStream = muxer . addEventListener . getCall ( 0 ) . args [ 1 ]
358
+
359
+ if ( onIncomingStream == null ) {
360
+ throw new Error ( 'No incoming stream handler registered' )
361
+ }
362
+
363
+ const incomingStream = stubInterface < Stream > ( {
364
+ protocol : streamProtocol
365
+ } )
366
+
367
+ if ( typeof onIncomingStream !== 'function' ) {
368
+ throw new Error ( 'Stream handler was not function' )
369
+ }
370
+
371
+ onIncomingStream ( new CustomEvent ( 'stream' , {
372
+ detail : incomingStream
373
+ } ) )
374
+ /*
375
+ const incomingStream = stubInterface<Stream>({
376
+ id: 'stream-id',
377
+ log: logger('test-stream'),
378
+ direction: 'outbound',
379
+ sink: async (source) => drain(source),
380
+ source: map((async function * () {
381
+ yield '/multistream/1.0.0\n'
382
+ yield `${streamProtocol}\n`
383
+ })(), str => encode.single(uint8ArrayFromString(str)))
384
+ })
385
+ */
386
+ // onIncomingStream?.(incomingStream)
387
+
388
+ // incoming stream is opened asynchronously
389
+ await delay ( 100 )
390
+
391
+ expect ( middleware1 . called ) . to . be . true ( )
392
+ expect ( middleware2 . called ) . to . be . true ( )
393
+ } )
292
394
} )
0 commit comments