Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
feat: expose get connection map method of connection manager (#372)
Browse files Browse the repository at this point in the history
Exposes the collection of managed connections as a peer map.
  • Loading branch information
achingbrain authored Apr 14, 2023
1 parent 0c407aa commit fc7245b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/interface-connection-manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
"@libp2p/interface-connection": "^4.0.0",
"@libp2p/interface-peer-id": "^2.0.0",
"@libp2p/interfaces": "^3.0.0",
"@libp2p/peer-collections": "^3.0.1",
"@multiformats/multiaddr": "^12.0.0"
},
"devDependencies": {
Expand Down
12 changes: 12 additions & 0 deletions packages/interface-connection-manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { EventEmitter } from '@libp2p/interfaces/events'
import type { Connection, MultiaddrConnection } from '@libp2p/interface-connection'
import type { PeerId } from '@libp2p/interface-peer-id'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { PeerMap } from '@libp2p/peer-collections'

export type PendingDialStatus = 'queued' | 'active' | 'error' | 'success'

Expand Down Expand Up @@ -74,6 +75,17 @@ export interface ConnectionManager extends EventEmitter<ConnectionManagerEvents>
*/
getConnections: (peerId?: PeerId) => Connection[]

/**
* Return a map of all connections with their associated PeerIds
*
* @example
*
* ```js
* const connectionsMap = libp2p.connectionManager.getConnectionsMap()
* ```
*/
getConnectionsMap: () => PeerMap<Connection[]>

/**
* Open a connection to a remote peer
*
Expand Down
1 change: 1 addition & 0 deletions packages/interface-mocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
"@libp2p/interfaces": "^3.0.0",
"@libp2p/logger": "^2.0.0",
"@libp2p/multistream-select": "^3.0.0",
"@libp2p/peer-collections": "^3.0.1",
"@libp2p/peer-id": "^2.0.0",
"@libp2p/peer-id-factory": "^2.0.0",
"@multiformats/multiaddr": "^12.0.0",
Expand Down
14 changes: 14 additions & 0 deletions packages/interface-mocks/src/connection-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { Registrar } from '@libp2p/interface-registrar'
import type { PubSub } from '@libp2p/interface-pubsub'
import { isMultiaddr, Multiaddr } from '@multiformats/multiaddr'
import { peerIdFromString } from '@libp2p/peer-id'
import { PeerMap } from '@libp2p/peer-collections'

export interface MockNetworkComponents {
peerId: PeerId
Expand Down Expand Up @@ -86,6 +87,19 @@ class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implem
return this.connections
}

getConnectionsMap (): PeerMap<Connection[]> {
const map = new PeerMap<Connection[]>()

for (const conn of this.connections) {
const conns: Connection[] = map.get(conn.remotePeer) ?? []
conns.push(conn)

map.set(conn.remotePeer, conns)
}

return map
}

async openConnection (peerId: PeerId | Multiaddr | Multiaddr[]): Promise<Connection> {
if (this.components == null) {
throw new CodeError('Not initialized', 'ERR_NOT_INITIALIZED')
Expand Down

0 comments on commit fc7245b

Please sign in to comment.