Skip to content

Commit f4d88b7

Browse files
committed
fix: Added Registrar, Dialer and Connection Manager to Libp2p object (libp2p#338)
1 parent 817701a commit f4d88b7

File tree

2 files changed

+16
-84
lines changed

2 files changed

+16
-84
lines changed

packages/interface-libp2p/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
},
134134
"dependencies": {
135135
"@libp2p/interface-connection": "^3.0.0",
136+
"@libp2p/interface-connection-manager": "^1.3.7",
136137
"@libp2p/interface-content-routing": "^2.0.0",
137138
"@libp2p/interface-dht": "^2.0.0",
138139
"@libp2p/interface-keychain": "^2.0.0",

packages/interface-libp2p/src/index.ts

+15-84
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,34 @@
1515
*/
1616

1717
import type { AbortOptions } from '@libp2p/interfaces'
18-
import type { EventEmitter } from '@libp2p/interfaces/events'
1918
import type { Startable } from '@libp2p/interfaces/startable'
2019
import type { Multiaddr } from '@multiformats/multiaddr'
2120
import type { DualDHT } from '@libp2p/interface-dht'
2221
import type { PeerStore } from '@libp2p/interface-peer-store'
2322
import type { PeerId } from '@libp2p/interface-peer-id'
24-
import type { Connection, Stream } from '@libp2p/interface-connection'
23+
import type { Stream } from '@libp2p/interface-connection'
2524
import type { PeerRouting } from '@libp2p/interface-peer-routing'
2625
import type { ContentRouting } from '@libp2p/interface-content-routing'
2726
import type { PubSub } from '@libp2p/interface-pubsub'
28-
import type { StreamHandler, StreamHandlerOptions, Topology } from '@libp2p/interface-registrar'
27+
import type { Registrar, StreamHandler, StreamHandlerOptions, Topology } from '@libp2p/interface-registrar'
2928
import type { Metrics } from '@libp2p/interface-metrics'
30-
import type { PeerInfo } from '@libp2p/interface-peer-info'
3129
import type { KeyChain } from '@libp2p/interface-keychain'
30+
import type { ConnectionManager, ConnectionManagerEvents, Dialer } from '@libp2p/interface-connection-manager'
31+
import type { PeerInfo } from '@libp2p/interface-peer-info/src'
32+
import type { EventEmitter } from '@libp2p/interfaces/src/events'
33+
3234

3335
/**
34-
* Once you have a libp2p instance, you can listen to several events it emits, so that you can be notified of relevant network events.
36+
* Fetch service lookup function
3537
*/
36-
export interface Libp2pEvents {
38+
export interface LookupFunction {
39+
(key: string): Promise<Uint8Array | null>
40+
}
41+
42+
/**
43+
* Libp2p nodes implement this interface.
44+
*/
45+
export interface Libp2p extends ConnectionManager, Dialer, Registrar, EventEmitter<ConnectionManagerEvents>, Startable {
3746
/**
3847
* @example
3948
*
@@ -46,48 +55,6 @@ export interface Libp2pEvents {
4655
*/
4756
'peer:discovery': CustomEvent<PeerInfo>
4857

49-
/**
50-
* This event will be triggered anytime a new Connection is established to another peer.
51-
*
52-
* @example
53-
*
54-
* ```js
55-
* libp2p.connectionManager.addEventListener('peer:connect', (event) => {
56-
* const connection = event.detail
57-
* // ...
58-
* })
59-
* ```
60-
*/
61-
'peer:connect': CustomEvent<Connection>
62-
63-
/**
64-
* This event will be triggered anytime we are disconnected from another peer, regardless of
65-
* the circumstances of that disconnection. If we happen to have multiple connections to a
66-
* peer, this event will **only** be triggered when the last connection is closed.
67-
*
68-
* @example
69-
*
70-
* ```js
71-
* libp2p.connectionManager.addEventListener('peer:disconnect', (event) => {
72-
* const connection = event.detail
73-
* // ...
74-
* })
75-
* ```
76-
*/
77-
'peer:disconnect': CustomEvent<Connection>
78-
}
79-
80-
/**
81-
* Fetch service lookup function
82-
*/
83-
export interface LookupFunction {
84-
(key: string): Promise<Uint8Array | null>
85-
}
86-
87-
/**
88-
* Libp2p nodes implement this interface.
89-
*/
90-
export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
9158
/**
9259
* The PeerId is a unique identifier for a node on the network.
9360
*
@@ -308,47 +275,11 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
308275
*/
309276
getProtocols: () => string[]
310277

311-
/**
312-
* Return a list of all connections this node has open, optionally filtering
313-
* by a PeerId
314-
*
315-
* @example
316-
*
317-
* ```js
318-
* for (const connection of libp2p.getConnections()) {
319-
* console.log(peerId, connection.remoteAddr.toString())
320-
* // Logs the PeerId string and the observed remote multiaddr of each Connection
321-
* }
322-
* ```
323-
*/
324-
getConnections: (peerId?: PeerId) => Connection[]
325-
326278
/**
327279
* Return a list of all peers we currently have a connection open to
328280
*/
329281
getPeers: () => PeerId[]
330282

331-
/**
332-
* Dials to the provided peer. If successful, the known metadata of the
333-
* peer will be added to the nodes `peerStore`.
334-
*
335-
* If a PeerId is passed as the first argument, the peer will need to have known multiaddrs for it in the PeerStore.
336-
*
337-
* @example
338-
*
339-
* ```js
340-
* const conn = await libp2p.dial(remotePeerId)
341-
*
342-
* // create a new stream within the connection
343-
* const { stream, protocol } = await conn.newStream(['/echo/1.1.0', '/echo/1.0.0'])
344-
*
345-
* // protocol negotiated: 'echo/1.0.0' means that the other party only supports the older version
346-
*
347-
* // ...
348-
* await conn.close()
349-
* ```
350-
*/
351-
dial: (peer: PeerId | Multiaddr, options?: AbortOptions) => Promise<Connection>
352283

353284
/**
354285
* Dials to the provided peer and tries to handshake with the given protocols in order.

0 commit comments

Comments
 (0)