diff --git a/src/index.ts b/src/index.ts index db2a467ee9..7c454cd6dd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -41,6 +41,7 @@ import type { KeyChainInit } from '@libp2p/keychain' import type { NatManagerInit } from './nat-manager.js' import type { AddressManagerInit } from './address-manager/index.js' import type { PeerRoutingInit } from './peer-routing.js' +import type { ConnectionManager } from '@libp2p/interface-connection-manager' import type { ConnectionManagerInit } from './connection-manager/index.js' /** @@ -149,6 +150,11 @@ export interface Libp2pInit { * A ConnectionProtector can be used to create a secure overlay on top of the network using pre-shared keys */ connectionProtector?: (components: Components) => ConnectionProtector + + /** + * Pass a custom ConnectionManager implementation to enable custom connection management + */ + customConnectionManager?: (components: Components) => ConnectionManager } /** diff --git a/src/libp2p.ts b/src/libp2p.ts index 819f6c3e53..457eeafba2 100644 --- a/src/libp2p.ts +++ b/src/libp2p.ts @@ -133,7 +133,8 @@ export class Libp2pNode extends EventEmitter implements Libp2p { this.components.dialer = new DefaultDialer(this.components, init.connectionManager) // Create the Connection Manager - this.connectionManager = this.components.connectionManager = new DefaultConnectionManager(this.components, init.connectionManager) + // if a connection manager is not provided, create a default one + this.connectionManager = (init.customConnectionManager != null) ? init.customConnectionManager(this.components) : this.components.connectionManager = new DefaultConnectionManager(this.components, init.connectionManager) // forward connection manager events this.components.connectionManager.addEventListener('peer:disconnect', (event) => {