diff --git a/src/registrar.ts b/src/registrar.ts index 41d1a194d7..5cc8a7e710 100644 --- a/src/registrar.ts +++ b/src/registrar.ts @@ -2,6 +2,7 @@ import { logger } from '@libp2p/logger' import errCode from 'err-code' import { codes } from './errors.js' import { isTopology, StreamHandlerOptions, StreamHandlerRecord } from '@libp2p/interface-registrar' +import merge from 'merge-options' import type { Registrar, StreamHandler, Topology } from '@libp2p/interface-registrar' import type { PeerProtocolsChangeData } from '@libp2p/interface-peer-store' import type { Connection } from '@libp2p/interface-connection' @@ -9,6 +10,9 @@ import type { Components } from '@libp2p/components' const log = logger('libp2p:registrar') +const DEFAULT_MAX_INCOMING_STREAMS = 1 +const DEFAULT_MAX_OUTGOING_STREAMS = 1 + /** * Responsible for notifying registered protocols of events in the network. */ @@ -63,11 +67,16 @@ export class DefaultRegistrar implements Registrar { /** * Registers the `handler` for each protocol */ - async handle (protocol: string, handler: StreamHandler, options: StreamHandlerOptions = { maxConcurrentStreams: 1 }): Promise { + async handle (protocol: string, handler: StreamHandler, opts?: StreamHandlerOptions): Promise { if (this.handlers.has(protocol)) { throw errCode(new Error(`Handler already registered for protocol ${protocol}`), codes.ERR_PROTOCOL_HANDLER_ALREADY_REGISTERED) } + const options = merge({ + maxIncomingStreams: DEFAULT_MAX_INCOMING_STREAMS, + maxOutgoingStreams: DEFAULT_MAX_OUTGOING_STREAMS + }, opts) + this.handlers.set(protocol, { handler, options