@@ -6,7 +6,7 @@ import { CONNECTION_CLOSE_TIMEOUT, PROTOCOL_NEGOTIATION_TIMEOUT } from './connec
66import { isDirect } from './connection-manager/utils.ts'
77import { MuxerUnavailableError } from './errors.ts'
88import { DEFAULT_MAX_INBOUND_STREAMS , DEFAULT_MAX_OUTBOUND_STREAMS } from './registrar.ts'
9- import type { AbortOptions , Logger , MessageStreamDirection , Connection as ConnectionInterface , Stream , NewStreamOptions , PeerId , ConnectionLimits , StreamMuxer , Metrics , PeerStore , MultiaddrConnection , MessageStreamEvents , MultiaddrConnectionTimeline , ConnectionStatus , MessageStream } from '@libp2p/interface'
9+ import type { AbortOptions , Logger , MessageStreamDirection , Connection as ConnectionInterface , Stream , NewStreamOptions , PeerId , ConnectionLimits , StreamMuxer , Metrics , PeerStore , MultiaddrConnection , MessageStreamEvents , MultiaddrConnectionTimeline , ConnectionStatus , MessageStream , StreamMiddleware } from '@libp2p/interface'
1010import type { Registrar } from '@libp2p/interface-internal'
1111import type { Multiaddr } from '@multiformats/multiaddr'
1212
@@ -126,7 +126,7 @@ export class Connection extends TypedEventEmitter<MessageStreamEvents> implement
126126 }
127127
128128 this . log . trace ( 'starting new stream for protocols %s' , protocols )
129- let muxedStream = await this . muxer . createStream ( {
129+ const muxedStream = await this . muxer . createStream ( {
130130 ...options ,
131131
132132 // most underlying transports only support negotiating a single protocol
@@ -179,23 +179,7 @@ export class Connection extends TypedEventEmitter<MessageStreamEvents> implement
179179
180180 const middleware = this . components . registrar . getMiddleware ( muxedStream . protocol )
181181
182- middleware . push ( ( stream , connection , next ) => {
183- next ( stream , connection )
184- } )
185-
186- let i = 0
187- let connection : ConnectionInterface = this
188-
189- while ( i < middleware . length ) {
190- // eslint-disable-next-line no-loop-func
191- middleware [ i ] ( muxedStream , connection , ( s , c ) => {
192- muxedStream = s
193- connection = c
194- i ++
195- } )
196- }
197-
198- return muxedStream
182+ return await this . runMiddlewareChain ( muxedStream , this , middleware )
199183 } catch ( err : any ) {
200184 if ( muxedStream . status === 'open' ) {
201185 muxedStream . abort ( err )
@@ -208,7 +192,7 @@ export class Connection extends TypedEventEmitter<MessageStreamEvents> implement
208192 }
209193
210194 private async onIncomingStream ( evt : CustomEvent < Stream > ) : Promise < void > {
211- let muxedStream = evt . detail
195+ const muxedStream = evt . detail
212196
213197 const signal = AbortSignal . timeout ( this . inboundStreamProtocolNegotiationTimeout )
214198 setMaxListeners ( Infinity , signal )
@@ -260,20 +244,40 @@ export class Connection extends TypedEventEmitter<MessageStreamEvents> implement
260244 next ( stream , connection )
261245 } )
262246
263- let connection : ConnectionInterface = this
264-
265- for ( const m of middleware ) {
266- // eslint-disable-next-line no-loop-func
267- await m ( muxedStream , connection , ( s , c ) => {
268- muxedStream = s
269- connection = c
270- } )
271- }
247+ await this . runMiddlewareChain ( muxedStream , this , middleware )
272248 } catch ( err : any ) {
273249 muxedStream . abort ( err )
274250 }
275251 }
276252
253+ private async runMiddlewareChain ( stream : Stream , connection : ConnectionInterface , middleware : StreamMiddleware [ ] ) : Promise < Stream > {
254+ for ( let i = 0 ; i < middleware . length ; i ++ ) {
255+ const mw = middleware [ i ]
256+ stream . log . trace ( 'running middleware' , i , mw )
257+
258+ // eslint-disable-next-line no-loop-func
259+ await new Promise < void > ( ( resolve , reject ) => {
260+ try {
261+ const result = mw ( stream , connection , ( s , c ) => {
262+ stream = s
263+ connection = c
264+ resolve ( )
265+ } )
266+
267+ if ( result instanceof Promise ) {
268+ result . catch ( reject )
269+ }
270+ } catch ( err ) {
271+ reject ( err )
272+ }
273+ } )
274+
275+ stream . log . trace ( 'ran middleware' , i , mw )
276+ }
277+
278+ return stream
279+ }
280+
277281 /**
278282 * Close the connection
279283 */
0 commit comments