Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add TSDoc comments interface-internal module #2949

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
38 changes: 38 additions & 0 deletions packages/interface-internal/src/address-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,49 +57,70 @@ export interface ConfirmAddressOptions {
type?: AddressType
}

/**
* The `AddressManager` module provides an interface for managing peer addresses
* in libp2p. It supports handling multiple types of addresses, verifying their validity,
* and storing mappings between internal and external addresses.
*/
/**
* Interface for managing peer addresses in libp2p.
*/

export interface AddressManager {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be one comment, not two, otherwise only the second will appear in the generated docs.

You can check this by running npm run docs in the root of the repo and looking at the HTML that appears in the .docs folder.

Suggested change
/**
* The `AddressManager` module provides an interface for managing peer addresses
* in libp2p. It supports handling multiple types of addresses, verifying their validity,
* and storing mappings between internal and external addresses.
*/
/**
* Interface for managing peer addresses in libp2p.
*/
export interface AddressManager {
/**
* The `AddressManager` provides an interface for managing peer addresses
* in libp2p. It supports handling multiple types of addresses, verifying their validity,
* and storing mappings between internal and external addresses.
*/
export interface AddressManager {

/**
* Get peer listen multiaddrs
* @returns An array of `Multiaddr` objects representing listen addresses.
*/
getListenAddrs(): Multiaddr[]

/**
* Get peer announcing multiaddrs
* @returns An array of `Multiaddr` objects representing announce addresses.
*/
getAnnounceAddrs(): Multiaddr[]

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

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

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

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

/**
* Get the current node's addresses
* @returns An array of `Multiaddr` objects representing node addresses.
*/
getAddresses(): Multiaddr[]

/**
* Return all known addresses with metadata
* @returns An array of `NodeAddress` objects.
*/
getAddressesWithMetadata(): NodeAddress[]

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

/**
* Remove a mapping previously added with `addDNSMapping`.
*
* @param domain - The domain name mapping to remove.
*/
removeDNSMapping(domain: string): void

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

/**
* Remove a publicly routable address that this node is no longer reachable on
*
* @param internalIp - The internal IP address.
* @param internalPort - The internal port number.
* @param externalIp - The external IP address.
* @param externalPort - The external port number (optional).
* @param protocol - The transport protocol (`tcp` or `udp`).
*/
removePublicAddressMapping (internalIp: string, internalPort: number, externalIp: string, externalPort?: number, protocol?: 'tcp' | 'udp'): void
}
51 changes: 30 additions & 21 deletions packages/interface-internal/src/connection-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import type { PeerMap } from '@libp2p/peer-collections'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { ProgressOptions } from 'progress-events'

/**
* Options for opening a connection to a remote peer.
*/
export interface OpenConnectionOptions extends AbortOptions, ProgressOptions<OpenConnectionProgressEvents> {
/**
* Connection requests with a higher priority will be executed before those
Expand Down Expand Up @@ -34,49 +37,53 @@ export interface OpenConnectionOptions extends AbortOptions, ProgressOptions<Ope
initiator?: boolean
}

/**
maschad marked this conversation as resolved.
Show resolved Hide resolved
* The `ConnectionManager` module handles managing connections between peers in a libp2p network.
* It provides methods for opening, closing, and querying connections.This also provides methods
* for accessing the dial queue.
*/
/**
* Manages the dialing, opening, and closing of connections.
*/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must be a single comment, not two:

Suggested change
/**
* The `ConnectionManager` module handles managing connections between peers in a libp2p network.
* It provides methods for opening, closing, and querying connections.This also provides methods
* for accessing the dial queue.
*/
/**
* Manages the dialing, opening, and closing of connections.
*/
/**
* The `ConnectionManager` handles managing connections between peers in a libp2p network.
* It provides methods for opening, closing, and querying connections.This also provides methods
* for accessing the dial queue.
*/

export interface ConnectionManager {
/**
* Return connections, optionally filtering by a PeerId
*
* @example
*
* ```TypeScript
* const connections = libp2p.connectionManager.get(peerId)
* // []
* ```
* @param peerId - The PeerId to filter connections (optional).
* @returns An array of active `Connection` objects.
*/
getConnections(peerId?: PeerId): Connection[]

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

/**
* Returns the configured maximum number of connections this connection
* manager will accept
* @returns The maximum connection limit.
*/
getMaxConnections(): number

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

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

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

Expand All @@ -96,11 +106,7 @@ export interface ConnectionManager {
/**
* Return the list of in-progress or queued dials
*
* @example
*
* ```TypeScript
* const dials = libp2p.connectionManager.getDialQueue()
* ```
* @returns An array of `PendingDial` objects.
*/
getDialQueue(): PendingDial[]

Expand All @@ -112,6 +118,9 @@ export interface ConnectionManager {
* would not block the dial attempt.
*
* This may involve resolving DNS addresses so you should pass an AbortSignal.
* @param multiaddr - The target multiaddr or an array of multiaddrs.
* @param options - Optional parameters for dialability check.
* @returns A promise that resolves to `true` if the multiaddr is dialable, `false` otherwise.
*/
isDialable(multiaddr: Multiaddr | Multiaddr[], options?: IsDialableOptions): Promise<boolean>
}
10 changes: 10 additions & 0 deletions packages/interface-internal/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/**
* @packageDocumentation
*
* This module serves as the entry point for `@libp2p/interface-internal`,
* re-exporting key components such as `AddressManager`, `ConnectionManager`,
* `RandomWalk`, `Registrar`, and `TransportManager`.
*
* These interfaces and classes define the core internal behaviors of libp2p.
*/

export * from './address-manager.js'
export * from './connection-manager.js'
export * from './random-walk.js'
Expand Down
20 changes: 16 additions & 4 deletions packages/interface-internal/src/random-walk.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import type { AbortOptions, PeerInfo } from '@libp2p/interface'

/**
* RandomWalk finds random peers on the network and dials them. Use this after
* registering a Topology if you need to discover common network services.
* The `RandomWalk` module facilitates peer discovery by randomly finding and dialing peers
* on the libp2p network. It is useful in conjunction with a registered `Topology` to
* discover common network services.
*/

/**
* The `RandomWalk` interface provides a mechanism for discovering and connecting to
* random peers on the libp2p network.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must be a single comment, not two.

Also, the docs were wrong here - RandomWalk only discovers peers, it doesn't dial them.

Suggested change
* The `RandomWalk` module facilitates peer discovery by randomly finding and dialing peers
* on the libp2p network. It is useful in conjunction with a registered `Topology` to
* discover common network services.
*/
/**
* The `RandomWalk` interface provides a mechanism for discovering and connecting to
* random peers on the libp2p network.
* The `RandomWalk` component uses the libp2p peer routing to find arbitrary
* network peers. Consumers may then dial these peers, causing the Identify
* protocol to run and any registered topologies to be notified of their
* supported protocols.

*/
export interface RandomWalk {
/**
* Begin or join an existing walk. Abort the passed signal if you wish to
* abort the walk early.
* Initiates a random walk for peer discovery.
*
* This method either begins a new random walk or joins an existing one. The process
* continues to find and return random peers until it is aborted.
*
* @param options - Optional `AbortOptions` to allow early termination of the walk.
* @returns An `AsyncGenerator` that yields discovered `PeerInfo` objects.
*
*/
walk(options?: AbortOptions): AsyncGenerator<PeerInfo>
}
58 changes: 46 additions & 12 deletions packages/interface-internal/src/registrar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { StreamHandler, StreamHandlerOptions, StreamHandlerRecord, Topology, IncomingStreamData } from '@libp2p/interface'

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this comment is necessary - these are named exports so will get their own documentation blocks.

Please can you check the output of npm run docs (look in the .docs folder) and delete this comment if it doesn't appear anywhere.

* Deprecated types that should be imported from `@libp2p/interface` directly.
*
* These exports ensure backward compatibility but should be avoided in new code.
*/
export type {
/**
* @deprecated This type should be imported from @libp2p/interface directly
Expand All @@ -21,46 +26,75 @@ export type {
*/
StreamHandlerRecord
}
/**
* The `Registrar` module provides an interface for managing protocol handlers
* and topologies in a libp2p network. It enables registering and managing
* protocol-specific handlers, ensuring efficient peer-to-peer communication.
*/
/**
* The `Registrar` interface allows modules to register, manage, and remove
* protocol handlers and topology discovery mechanisms in libp2p.
*/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must be a single comment, not two.

Also we can be a bit more specific in the comment:

Suggested change
/**
* The `Registrar` module provides an interface for managing protocol handlers
* and topologies in a libp2p network. It enables registering and managing
* protocol-specific handlers, ensuring efficient peer-to-peer communication.
*/
/**
* The `Registrar` interface allows modules to register, manage, and remove
* protocol handlers and topology discovery mechanisms in libp2p.
*/
/**
* The `Registrar` provides an interface for registering protocol handlers -
* these are invoked when remote peers open streams on the local node with the
* corresponding protocol name.
*
* It also allows configuring network topologies for a given protocol(s). The
* topology callbacks are invoked when a peer that supports those protocols
* connects or disconnects.
*
* The Identify protocol must be configured on the current node for topologies
* to function.
*/

export interface Registrar {
/**
* Return the list of protocols with registered handlers
* Retrieve the list of registered protocol handlers.
*
* @returns An array of protocol strings.
*/
getProtocols(): string[]

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

/**
* Remove a protocol handler
* Remove a registered protocol handler.
*
* @param protocol - The protocol to unhandle.
* @returns A promise that resolves once the handler is removed.
*/
unhandle(protocol: string): Promise<void>

/**
* Return the handler for the passed protocol
* Retrieve the registered handler for a given protocol.
*
* @param protocol - The protocol to query.
* @returns A `StreamHandlerRecord` containing the handler and options.
*/
getHandler(protocol: string): StreamHandlerRecord

/**
* Register a topology handler for a protocol - the topology will be
* invoked when peers are discovered on the network that support the
* passed protocol.
* Register a topology handler for a specific protocol.
*
* An id will be returned that can later be used to unregister the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part about the id being used to unregister the topology later is important, please do not delete it.

* topology.
* The topology will be notified when peers supporting the protocol
* are discovered on the network.
*
* @param protocol - The protocol to register.
* @param topology - The topology handler to register.
* @returns A promise resolving to a unique ID for the registered topology.
*/
register(protocol: string, topology: Topology): Promise<string>

/**
* Remove the topology handler with the passed id.
* Unregister a topology handler using its unique ID.
*
* @param id - The ID of the topology to unregister.
*/
unregister(id: string): void

/**
* Return all topology handlers that wish to be informed about peers
* that support the passed protocol.
* Retrieve all topology handlers that are interested in peers
* supporting a given protocol.
*
* @param protocol - The protocol to query.
* @returns An array of registered `Topology` handlers.
*/
getTopologies(protocol: string): Topology[]
}
Loading