@@ -11,7 +11,7 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
1111import { createConnection } from '../../src/connection.js'
1212import { UnhandledProtocolError } from '../../src/errors.ts'
1313import 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'
1515import type { Registrar } from '@libp2p/interface-internal'
1616import type { StubbedInstance } from 'sinon-ts'
1717
@@ -38,6 +38,7 @@ describe('connection', () => {
3838 } ,
3939 options : { }
4040 } )
41+ registrar . getMiddleware . withArgs ( ECHO_PROTOCOL ) . returns ( [ ] )
4142
4243 components = {
4344 peerStore,
@@ -223,6 +224,7 @@ describe('connection', () => {
223224 }
224225 } )
225226 registrar . getProtocols . returns ( [ protocol ] )
227+ registrar . getMiddleware . withArgs ( protocol ) . returns ( [ ] )
226228
227229 const connection = createConnection ( components , init )
228230 expect ( connection . streams ) . to . have . lengthOf ( 0 )
@@ -259,6 +261,7 @@ describe('connection', () => {
259261 }
260262 } )
261263 registrar . getProtocols . returns ( [ protocol ] )
264+ registrar . getMiddleware . withArgs ( protocol ) . returns ( [ ] )
262265
263266 const connection = createConnection ( components , init )
264267 expect ( connection . streams ) . to . have . lengthOf ( 0 )
@@ -274,6 +277,7 @@ describe('connection', () => {
274277 const protocol = '/test/protocol'
275278
276279 registrar . getHandler . withArgs ( protocol ) . throws ( new UnhandledProtocolError ( ) )
280+ registrar . getMiddleware . withArgs ( protocol ) . returns ( [ ] )
277281
278282 const connection = createConnection ( components , init )
279283 expect ( connection . streams ) . to . have . lengthOf ( 0 )
@@ -289,4 +293,102 @@ describe('connection', () => {
289293 await expect ( connection . newStream ( protocol , opts ) ) . to . eventually . be . rejected
290294 . with . property ( 'name' , 'TooManyOutboundProtocolStreamsError' )
291295 } )
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+ } )
292394} )
0 commit comments