1
1
import { EventEmitter } from '@libp2p/interfaces/events'
2
2
import type { Startable } from '@libp2p/interfaces/startable'
3
3
import type { Connection } from '@libp2p/interface-connection'
4
- import type { PeerId } from '@libp2p/interface-peer-id'
4
+ import { isPeerId , PeerId } from '@libp2p/interface-peer-id'
5
5
import type { ConnectionManager , ConnectionManagerEvents } from '@libp2p/interface-connection-manager'
6
6
import { connectionPair } from './connection.js'
7
7
import { CodeError } from '@libp2p/interfaces/errors'
8
8
import type { Registrar } from '@libp2p/interface-registrar'
9
9
import type { PubSub } from '@libp2p/interface-pubsub'
10
10
import { isMultiaddr , Multiaddr } from '@multiformats/multiaddr'
11
+ import { peerIdFromString } from '@libp2p/peer-id'
11
12
12
13
export interface MockNetworkComponents {
13
14
peerId : PeerId
@@ -23,10 +24,14 @@ class MockNetwork {
23
24
this . components . push ( components )
24
25
}
25
26
26
- getNode ( peerId : PeerId ) : MockNetworkComponents {
27
- for ( const components of this . components ) {
28
- if ( peerId . equals ( components . peerId ) ) {
29
- return components
27
+ getNode ( peerId : PeerId | Multiaddr [ ] ) : MockNetworkComponents {
28
+ if ( Array . isArray ( peerId ) && peerId . length > 0 ) {
29
+ peerId = peerIdFromString ( peerId [ 0 ] . getPeerId ( ) ?? '' )
30
+ } else if ( isPeerId ( peerId ) ) {
31
+ for ( const components of this . components ) {
32
+ if ( peerId . equals ( components . peerId ) ) {
33
+ return components
34
+ }
30
35
}
31
36
}
32
37
@@ -81,7 +86,7 @@ class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implem
81
86
return this . connections
82
87
}
83
88
84
- async openConnection ( peerId : PeerId | Multiaddr ) : Promise < Connection > {
89
+ async openConnection ( peerId : PeerId | Multiaddr | Multiaddr [ ] ) : Promise < Connection > {
85
90
if ( this . components == null ) {
86
91
throw new CodeError ( 'Not initialized' , 'ERR_NOT_INITIALIZED' )
87
92
}
@@ -90,7 +95,13 @@ class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implem
90
95
throw new CodeError ( 'Dialing multiaddrs not supported' , 'ERR_NOT_SUPPORTED' )
91
96
}
92
97
93
- const existingConnections = this . getConnections ( peerId )
98
+ let existingConnections : Connection [ ] = [ ]
99
+
100
+ if ( Array . isArray ( peerId ) && peerId . length > 0 ) {
101
+ existingConnections = this . getConnections ( peerIdFromString ( peerId [ 0 ] . getPeerId ( ) ?? '' ) )
102
+ } else if ( isPeerId ( peerId ) ) {
103
+ existingConnections = this . getConnections ( peerId )
104
+ }
94
105
95
106
if ( existingConnections . length > 0 ) {
96
107
return existingConnections [ 0 ]
0 commit comments