Skip to content

Commit d222968

Browse files
Khwahish29maschadachingbrain
authored
docs: add TSDoc comments interface-internal module (#2949)
Closes #2113 --------- Co-authored-by: Chad Nehemiah <[email protected]> Co-authored-by: Alex Potsides <[email protected]>
1 parent b818882 commit d222968

File tree

6 files changed

+159
-33
lines changed

6 files changed

+159
-33
lines changed

packages/interface-internal/src/address-manager.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,49 +57,66 @@ export interface ConfirmAddressOptions {
5757
type?: AddressType
5858
}
5959

60+
/**
61+
* The `AddressManager` provides an interface for managing peer addresses
62+
* in libp2p. It supports handling multiple types of addresses, verifying their validity,
63+
* and storing mappings between internal and external addresses.
64+
*/
6065
export interface AddressManager {
6166
/**
6267
* Get peer listen multiaddrs
68+
* @returns An array of `Multiaddr` objects representing listen addresses.
6369
*/
6470
getListenAddrs(): Multiaddr[]
6571

6672
/**
6773
* Get peer announcing multiaddrs
74+
* @returns An array of `Multiaddr` objects representing announce addresses.
6875
*/
6976
getAnnounceAddrs(): Multiaddr[]
7077

7178
/**
7279
* Get observed multiaddrs - these addresses may not have been confirmed as
7380
* publicly dialable yet
81+
* @returns An array of `Multiaddr` objects representing observed addresses.
7482
*/
7583
getObservedAddrs(): Multiaddr[]
7684

7785
/**
7886
* Signal that we have confidence an observed multiaddr is publicly dialable -
7987
* this will make it appear in the output of getAddresses()
88+
*
89+
* @param addr - The observed address.
90+
* @param options - Additional options for confirmation.
8091
*/
8192
confirmObservedAddr(addr: Multiaddr, options?: ConfirmAddressOptions): void
8293

8394
/**
8495
* Signal that we do not have confidence an observed multiaddr is publicly dialable -
8596
* this will remove it from the output of getObservedAddrs()
97+
*
98+
* @param addr - The observed address to remove.
8699
*/
87100
removeObservedAddr(addr: Multiaddr): void
88101

89102
/**
90103
* Add peer observed addresses. These will then appear in the output of getObservedAddrs
91104
* but not getAddresses() until their dialability has been confirmed via a call to
92105
* confirmObservedAddr.
106+
*
107+
* @param addr - The observed address to add.
93108
*/
94109
addObservedAddr(addr: Multiaddr): void
95110

96111
/**
97112
* Get the current node's addresses
113+
* @returns An array of `Multiaddr` objects representing node addresses.
98114
*/
99115
getAddresses(): Multiaddr[]
100116

101117
/**
102118
* Return all known addresses with metadata
119+
* @returns An array of `NodeAddress` objects.
103120
*/
104121
getAddressesWithMetadata(): NodeAddress[]
105122

@@ -108,11 +125,16 @@ export interface AddressManager {
108125
* `getAddresses` is invoked, where the IP addresses are present in a
109126
* multiaddr, an additional multiaddr will be added with `ip4` and `ip6`
110127
* tuples replaced with `dns4` and `dns6 ones respectively.
128+
*
129+
* @param domain - The domain name to map.
130+
* @param ipAddresses - The associated IP addresses.
111131
*/
112132
addDNSMapping(domain: string, ipAddresses: string[]): void
113133

114134
/**
115135
* Remove a mapping previously added with `addDNSMapping`.
136+
*
137+
* @param domain - The domain name mapping to remove.
116138
*/
117139
removeDNSMapping(domain: string): void
118140

@@ -125,11 +147,23 @@ export interface AddressManager {
125147
* It's possible to add a IPv6 address here and have it added to the address
126148
* list, this is for the case when a router has an external IPv6 address with
127149
* port forwarding configured, but it does IPv6 -> IPv4 NAT.
150+
*
151+
* @param internalIp - The internal IP address.
152+
* @param internalPort - The internal port number.
153+
* @param externalIp - The external IP address.
154+
* @param externalPort - The external port number (optional).
155+
* @param protocol - The transport protocol (`tcp` or `udp`).
128156
*/
129157
addPublicAddressMapping (internalIp: string, internalPort: number, externalIp: string, externalPort?: number, protocol?: 'tcp' | 'udp'): void
130158

131159
/**
132160
* Remove a publicly routable address that this node is no longer reachable on
161+
*
162+
* @param internalIp - The internal IP address.
163+
* @param internalPort - The internal port number.
164+
* @param externalIp - The external IP address.
165+
* @param externalPort - The external port number (optional).
166+
* @param protocol - The transport protocol (`tcp` or `udp`).
133167
*/
134168
removePublicAddressMapping (internalIp: string, internalPort: number, externalIp: string, externalPort?: number, protocol?: 'tcp' | 'udp'): void
135169
}

packages/interface-internal/src/connection-manager.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import type { PeerMap } from '@libp2p/peer-collections'
33
import type { Multiaddr } from '@multiformats/multiaddr'
44
import type { ProgressOptions } from 'progress-events'
55

6+
/**
7+
* Options for opening a connection to a remote peer.
8+
*/
69
export interface OpenConnectionOptions extends AbortOptions, ProgressOptions<OpenConnectionProgressEvents> {
710
/**
811
* Connection requests with a higher priority will be executed before those
@@ -34,49 +37,49 @@ export interface OpenConnectionOptions extends AbortOptions, ProgressOptions<Ope
3437
initiator?: boolean
3538
}
3639

40+
/**
41+
* The `ConnectionManager` handles managing connections between peers in a libp2p network.
42+
* It provides methods for opening, closing, and querying connections.This also provides methods
43+
* for accessing the dial queue.
44+
*/
3745
export interface ConnectionManager {
3846
/**
3947
* Return connections, optionally filtering by a PeerId
4048
*
41-
* @example
42-
*
43-
* ```TypeScript
44-
* const connections = libp2p.connectionManager.get(peerId)
45-
* // []
46-
* ```
49+
* @param peerId - The PeerId to filter connections (optional).
50+
* @returns An array of active `Connection` objects.
4751
*/
4852
getConnections(peerId?: PeerId): Connection[]
4953

5054
/**
5155
* Return a map of all connections with their associated PeerIds
5256
*
53-
* @example
54-
*
55-
* ```TypeScript
56-
* const connectionsMap = libp2p.connectionManager.getConnectionsMap()
57-
* ```
57+
* @returns A `PeerMap` containing `Connection[]` objects.
5858
*/
5959
getConnectionsMap(): PeerMap<Connection[]>
6060

6161
/**
6262
* Returns the configured maximum number of connections this connection
6363
* manager will accept
64+
* @returns The maximum connection limit.
6465
*/
6566
getMaxConnections(): number
6667

6768
/**
6869
* Open a connection to a remote peer
6970
*
70-
* @example
71-
*
72-
* ```TypeScript
73-
* const connection = await libp2p.connectionManager.openConnection(peerId)
74-
* ```
71+
* @param peer - The target `PeerId`, `Multiaddr`, or an array of `Multiaddr`s.
72+
* @param options - Optional parameters for connection handling.
73+
* @returns A promise that resolves to a `Connection` object.
7574
*/
7675
openConnection(peer: PeerId | Multiaddr | Multiaddr[], options?: OpenConnectionOptions): Promise<Connection>
7776

7877
/**
7978
* Close our connections to a peer
79+
*
80+
* @param peer - The `PeerId` whose connections should be closed.
81+
* @param options - Optional abort options.
82+
* @returns A promise that resolves once the connections are closed.
8083
*/
8184
closeConnections(peer: PeerId, options?: AbortOptions): Promise<void>
8285

@@ -85,6 +88,9 @@ export interface ConnectionManager {
8588
* exchanged, this lets the ConnectionManager check we have sufficient
8689
* resources to accept the connection in which case it will return true,
8790
* otherwise it will return false.
91+
*
92+
* @param maConn - The multiaddr connection to evaluate.
93+
* @returns A promise that resolves to `true` if the connection can be accepted, `false` otherwise.
8894
*/
8995
acceptIncomingConnection(maConn: MultiaddrConnection): Promise<boolean>
9096

@@ -96,11 +102,7 @@ export interface ConnectionManager {
96102
/**
97103
* Return the list of in-progress or queued dials
98104
*
99-
* @example
100-
*
101-
* ```TypeScript
102-
* const dials = libp2p.connectionManager.getDialQueue()
103-
* ```
105+
* @returns An array of `PendingDial` objects.
104106
*/
105107
getDialQueue(): PendingDial[]
106108

@@ -112,6 +114,9 @@ export interface ConnectionManager {
112114
* would not block the dial attempt.
113115
*
114116
* This may involve resolving DNS addresses so you should pass an AbortSignal.
117+
* @param multiaddr - The target multiaddr or an array of multiaddrs.
118+
* @param options - Optional parameters for dialability check.
119+
* @returns A promise that resolves to `true` if the multiaddr is dialable, `false` otherwise.
115120
*/
116121
isDialable(multiaddr: Multiaddr | Multiaddr[], options?: IsDialableOptions): Promise<boolean>
117122
}

packages/interface-internal/src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* @packageDocumentation
3+
*
4+
* This module serves as the entry point for `@libp2p/interface-internal`,
5+
* re-exporting key components such as `AddressManager`, `ConnectionManager`,
6+
* `RandomWalk`, `Registrar`, and `TransportManager`.
7+
*
8+
* These interfaces and classes define the core internal behaviors of libp2p.
9+
*/
10+
111
export * from './address-manager.js'
212
export * from './connection-manager.js'
313
export * from './random-walk.js'
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import type { AbortOptions, PeerInfo } from '@libp2p/interface'
22

33
/**
4-
* RandomWalk finds random peers on the network and dials them. Use this after
5-
* registering a Topology if you need to discover common network services.
4+
* The `RandomWalk` component uses the libp2p peer routing to find arbitrary
5+
* network peers. Consumers may then dial these peers, causing the Identify
6+
* protocol to run and any registered topologies to be notified of their
7+
* supported protocols.
68
*/
79
export interface RandomWalk {
810
/**
9-
* Begin or join an existing walk. Abort the passed signal if you wish to
10-
* abort the walk early.
11+
* Initiates a random walk for peer discovery.
12+
*
13+
* This method either begins a new random walk or joins an existing one. The process
14+
* continues to find and return random peers until it is aborted.
15+
*
16+
* @param options - Optional `AbortOptions` to allow early termination of the walk.
17+
* @returns An `AsyncGenerator` that yields discovered `PeerInfo` objects.
18+
*
1119
*/
1220
walk(options?: AbortOptions): AsyncGenerator<PeerInfo>
1321
}

packages/interface-internal/src/registrar.ts

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,49 @@ export type {
2222
StreamHandlerRecord
2323
}
2424

25+
/**
26+
* The `Registrar` provides an interface for registering protocol handlers -
27+
* these are invoked when remote peers open streams on the local node with the
28+
* corresponding protocol name.
29+
*
30+
* It also allows configuring network topologies for a given protocol(s). The
31+
* topology callbacks are invoked when a peer that supports those protocols
32+
* connects or disconnects.
33+
*
34+
* The Identify protocol must be configured on the current node for topologies
35+
* to function.
36+
*/
2537
export interface Registrar {
2638
/**
27-
* Return the list of protocols with registered handlers
39+
* Retrieve the list of registered protocol handlers.
40+
*
41+
* @returns An array of protocol strings.
2842
*/
2943
getProtocols(): string[]
3044

3145
/**
32-
* Add a protocol handler
46+
* Register a handler for a specific protocol.
47+
*
48+
* @param protocol - The protocol string (e.g., `/my-protocol/1.0.0`).
49+
* @param handler - The function that handles incoming streams.
50+
* @param options - Optional configuration options for the handler.
51+
* @returns A promise that resolves once the handler is registered.
3352
*/
3453
handle(protocol: string, handler: StreamHandler, options?: StreamHandlerOptions): Promise<void>
3554

3655
/**
37-
* Remove a protocol handler
56+
* Remove a registered protocol handler.
57+
*
58+
* @param protocol - The protocol to unhandle.
59+
* @returns A promise that resolves once the handler is removed.
3860
*/
3961
unhandle(protocol: string): Promise<void>
4062

4163
/**
42-
* Return the handler for the passed protocol
64+
* Retrieve the registered handler for a given protocol.
65+
*
66+
* @param protocol - The protocol to query.
67+
* @returns A `StreamHandlerRecord` containing the handler and options.
4368
*/
4469
getHandler(protocol: string): StreamHandlerRecord
4570

@@ -50,17 +75,26 @@ export interface Registrar {
5075
*
5176
* An id will be returned that can later be used to unregister the
5277
* topology.
78+
*
79+
* @param protocol - The protocol to register.
80+
* @param topology - The topology handler to register.
81+
* @returns A promise resolving to a unique ID for the registered topology.
5382
*/
5483
register(protocol: string, topology: Topology): Promise<string>
5584

5685
/**
57-
* Remove the topology handler with the passed id.
86+
* Unregister a topology handler using its unique ID.
87+
*
88+
* @param id - The ID of the topology to unregister.
5889
*/
5990
unregister(id: string): void
6091

6192
/**
62-
* Return all topology handlers that wish to be informed about peers
63-
* that support the passed protocol.
93+
* Retrieve all topology handlers that are interested in peers
94+
* supporting a given protocol.
95+
*
96+
* @param protocol - The protocol to query.
97+
* @returns An array of registered `Topology` handlers.
6498
*/
6599
getTopologies(protocol: string): Topology[]
66100
}

0 commit comments

Comments
 (0)