libp2p 2.10.0-da78fa851 → 2.10.0-e8398d97e

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/src/connection.ts CHANGED
@@ -6,7 +6,7 @@ import { CONNECTION_CLOSE_TIMEOUT, PROTOCOL_NEGOTIATION_TIMEOUT } from './connec
6
6
  import { isDirect } from './connection-manager/utils.ts'
7
7
  import { MuxerUnavailableError } from './errors.ts'
8
8
  import { 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'
10
10
  import type { Registrar } from '@libp2p/interface-internal'
11
11
  import type { Multiaddr } from '@multiformats/multiaddr'
12
12
 
@@ -126,7 +126,7 @@ export class Connection extends TypedEventEmitter<MessageStreamEvents> implement
126
126
  }
127
127
 
128
128
  this.log.trace('starting new stream for protocols %s', protocols)
129
- let muxedStream = await this.muxer.createStream({
129
+ const muxedStream = await this.muxer.createStream({
130
130
  ...options,
131
131
 
132
132
  // most underlying transports only support negotiating a single protocol
@@ -179,23 +179,7 @@ export class Connection extends TypedEventEmitter<MessageStreamEvents> implement
179
179
 
180
180
  const middleware = this.components.registrar.getMiddleware(muxedStream.protocol)
181
181
 
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)
199
183
  } catch (err: any) {
200
184
  if (muxedStream.status === 'open') {
201
185
  muxedStream.abort(err)
@@ -208,7 +192,7 @@ export class Connection extends TypedEventEmitter<MessageStreamEvents> implement
208
192
  }
209
193
 
210
194
  private async onIncomingStream (evt: CustomEvent<Stream>): Promise<void> {
211
- let muxedStream = evt.detail
195
+ const muxedStream = evt.detail
212
196
 
213
197
  const signal = AbortSignal.timeout(this.inboundStreamProtocolNegotiationTimeout)
214
198
  setMaxListeners(Infinity, signal)
@@ -260,20 +244,40 @@ export class Connection extends TypedEventEmitter<MessageStreamEvents> implement
260
244
  next(stream, connection)
261
245
  })
262
246
 
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)
272
248
  } catch (err: any) {
273
249
  muxedStream.abort(err)
274
250
  }
275
251
  }
276
252
 
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
+
277
281
  /**
278
282
  * Close the connection
279
283
  */
package/src/libp2p.ts CHANGED
@@ -119,8 +119,8 @@ export class Libp2p<T extends ServiceMap = ServiceMap> extends TypedEventEmitter
119
119
  connectionEncrypters: (init.connectionEncrypters ?? []).map((fn, index) => this.configureComponent(`connection-encryption-${index}`, fn(this.components))),
120
120
  streamMuxers: (init.streamMuxers ?? []).map((fn, index) => this.configureComponent(`stream-muxers-${index}`, fn(this.components))),
121
121
  inboundUpgradeTimeout: init.connectionManager?.inboundUpgradeTimeout,
122
- inboundStreamProtocolNegotiationTimeout: init.connectionManager?.inboundStreamProtocolNegotiationTimeout ?? init.connectionManager?.protocolNegotiationTimeout,
123
- outboundStreamProtocolNegotiationTimeout: init.connectionManager?.outboundStreamProtocolNegotiationTimeout ?? init.connectionManager?.protocolNegotiationTimeout,
122
+ inboundStreamProtocolNegotiationTimeout: init.connectionManager?.inboundStreamProtocolNegotiationTimeout,
123
+ outboundStreamProtocolNegotiationTimeout: init.connectionManager?.outboundStreamProtocolNegotiationTimeout,
124
124
  connectionCloseTimeout: init.connectionManager?.connectionCloseTimeout
125
125
  })
126
126
 
package/src/registrar.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { InvalidParametersError } from '@libp2p/interface'
2
- import { mergeOptions, trackedMap } from '@libp2p/utils'
2
+ import { trackedMap } from '@libp2p/utils'
3
3
  import { DuplicateProtocolHandlerError, UnhandledProtocolError } from './errors.js'
4
4
  import type { IdentifyResult, Libp2pEvents, Logger, PeerUpdate, PeerId, PeerStore, Topology, StreamHandler, StreamHandlerRecord, StreamHandlerOptions, AbortOptions, Metrics, StreamMiddleware } from '@libp2p/interface'
5
5
  import type { Registrar as RegistrarInterface } from '@libp2p/interface-internal'
@@ -95,14 +95,13 @@ export class Registrar implements RegistrarInterface {
95
95
  throw new DuplicateProtocolHandlerError(`Handler already registered for protocol ${protocol}`)
96
96
  }
97
97
 
98
- const options = mergeOptions.bind({ ignoreUndefined: true })({
99
- maxInboundStreams: DEFAULT_MAX_INBOUND_STREAMS,
100
- maxOutboundStreams: DEFAULT_MAX_OUTBOUND_STREAMS
101
- }, opts)
102
-
103
98
  this.handlers.set(protocol, {
104
99
  handler,
105
- options
100
+ options: {
101
+ maxInboundStreams: DEFAULT_MAX_INBOUND_STREAMS,
102
+ maxOutboundStreams: DEFAULT_MAX_OUTBOUND_STREAMS,
103
+ ...opts
104
+ }
106
105
  })
107
106
 
108
107
  // Add new protocol to self protocols in the peer store
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '2.10.0-da78fa851'
1
+ export const version = '2.10.0-e8398d97e'
2
2
  export const name = 'js-libp2p'