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

chore: add typedefs peerstore book template #821

Closed
wants to merge 18 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: address review
vasco-santos committed Nov 26, 2020

Verified

This commit was signed with the committer’s verified signature.
renovate-bot Mend Renovate
commit 7e05c4a43be48b3a5046108afc5c12d532ccf5be
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -61,7 +61,7 @@
"it-pipe": "^1.1.0",
"it-protocol-buffers": "^0.2.0",
"libp2p-crypto": "^0.18.0",
"libp2p-interfaces": "^0.7.2",
"libp2p-interfaces": "libp2p/js-libp2p-interfaces#chore/add-duplex-iterable-type-to-connection",
"libp2p-utils": "^0.2.2",
"mafmt": "^8.0.0",
"merge-options": "^2.0.0",
12 changes: 8 additions & 4 deletions src/address-manager/index.js
Original file line number Diff line number Diff line change
@@ -6,10 +6,14 @@ log.error = debug('libp2p:addresses:error')

const multiaddr = require('multiaddr')

/**
* @typedef {import('multiaddr')} Multiaddr
*/

/**
* @typedef {Object} AddressManagerOptions
* @property {Array<string>} [listen = []] - list of multiaddrs string representation to listen.
* @property {Array<string>} [announce = []] - list of multiaddrs string representation to announce.
* @property {string[]} [listen = []] - list of multiaddrs string representation to listen.
* @property {string[]} [announce = []] - list of multiaddrs string representation to announce.
*/
class AddressManager {
/**
@@ -29,7 +33,7 @@ class AddressManager {
/**
* Get peer listen multiaddrs.
*
* @returns {Array<multiaddr>}
* @returns {Multiaddr[]}
*/
getListenAddrs () {
return Array.from(this.listen).map((a) => multiaddr(a))
@@ -38,7 +42,7 @@ class AddressManager {
/**
* Get peer announcing multiaddrs.
*
* @returns {Array<multiaddr>}
* @returns {Multiaddr[]}
*/
getAnnounceAddrs () {
return Array.from(this.announce).map((a) => multiaddr(a))
4 changes: 2 additions & 2 deletions src/circuit/auto-relay.js
Original file line number Diff line number Diff line change
@@ -69,7 +69,7 @@ class AutoRelay {
*
* @param {Object} props
* @param {PeerId} props.peerId
* @param {Array<string>} props.protocols
* @param {string[]} props.protocols
* @returns {Promise<void>}
*/
async _onProtocolChange ({ peerId, protocols }) {
@@ -182,7 +182,7 @@ class AutoRelay {
* 2. Dial and try to listen on the peers we know that support hop but are not connected.
* 3. Search the network.
*
* @param {Array<string>} [peersToIgnore]
* @param {string[]} [peersToIgnore]
* @returns {Promise<void>}
*/
async _listenOnAvailableHopRelays (peersToIgnore = []) {
15 changes: 10 additions & 5 deletions src/circuit/transport.js
Original file line number Diff line number Diff line change
@@ -18,14 +18,19 @@ const { handleCanHop, handleHop, hop } = require('./circuit/hop')
const { handleStop } = require('./circuit/stop')
const StreamHandler = require('./circuit/stream-handler')

/**
* @typedef {import('multiaddr')} Multiaddr
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
*/

class Circuit {
/**
* Creates an instance of the Circuit Transport.
*
* @class
* @param {object} options
* @param {Libp2p} options.libp2p
* @param {Upgrader} options.upgrader
* @param {import('../')} options.libp2p
* @param {import('../upgrader')} options.upgrader
*/
constructor ({ libp2p, upgrader }) {
this._dialer = libp2p.dialer
@@ -101,7 +106,7 @@ class Circuit {
/**
* Dial a peer over a relay
*
* @param {multiaddr} ma - the multiaddr of the peer to dial
* @param {Multiaddr} ma - the multiaddr of the peer to dial
* @param {Object} options - dial options
* @param {AbortSignal} [options.signal] - An optional abort signal
* @returns {Connection} - the connection
@@ -176,8 +181,8 @@ class Circuit {
/**
* Filter check for all Multiaddrs that this transport can dial on
*
* @param {Array<Multiaddr>} multiaddrs
* @returns {Array<Multiaddr>}
* @param {Multiaddr[]} multiaddrs
* @returns {Multiaddr[]}
*/
filter (multiaddrs) {
multiaddrs = Array.isArray(multiaddrs) ? multiaddrs : [multiaddrs]
9 changes: 6 additions & 3 deletions src/connection-manager/index.js
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ const defaultOptions = {
*/

/**
* @extends {EventEmitter}
* @extends EventEmitter
*
* @fires ConnectionManager#peer:connect Emitted when a new peer is connected.
* @fires ConnectionManager#peer:disconnect Emitted when a peer is disconnected.
@@ -87,7 +87,7 @@ class ConnectionManager extends EventEmitter {
/**
* Map of connections per peer
*
* @type {Map<string, Array<Connection>>}
* @type {Map<string, Connection[]>}
*/
this.connections = new Map()

@@ -168,6 +168,7 @@ class ConnectionManager extends EventEmitter {
*
* @param {PeerId} peerId
* @param {number} value - A number between 0 and 1
* @returns {void}
*/
setPeerValue (peerId, value) {
if (value < 0 || value > 1) {
@@ -201,6 +202,7 @@ class ConnectionManager extends EventEmitter {
* Tracks the incoming connection and check the connection limit
*
* @param {Connection} connection
* @returns {void}
*/
onConnect (connection) {
const peerId = connection.remotePeer
@@ -227,6 +229,7 @@ class ConnectionManager extends EventEmitter {
* Removes the connection from tracking
*
* @param {Connection} connection
* @returns {void}
*/
onDisconnect (connection) {
const peerId = connection.remotePeer.toB58String()
@@ -260,7 +263,7 @@ class ConnectionManager extends EventEmitter {
* Get all open connections with a peer.
*
* @param {PeerId} peerId
* @returns {Array<Connection>}
* @returns {Connection[]}
*/
getAll (peerId) {
if (!PeerId.isPeerId(peerId)) {
14 changes: 10 additions & 4 deletions src/content-routing.js
Original file line number Diff line number Diff line change
@@ -8,7 +8,13 @@ const pAny = require('p-any')

/**
* @typedef {import('peer-id')} PeerId
* @typedef {import('multiaddr')} multiaddr
* @typedef {import('multiaddr')} Multiaddr
*/

/**
* @typedef {Object} GetData
* @property {PeerId} from
* @property {Uint8Array} val
*/

module.exports = (node) => {
@@ -29,7 +35,7 @@ module.exports = (node) => {
* @param {object} [options]
* @param {number} [options.timeout] - How long the query should run
* @param {number} [options.maxNumProviders] - maximum number of providers to find
* @returns {AsyncIterable<{ id: PeerId, multiaddrs: Array<multiaddr> }>}
* @returns {AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>}
*/
async * findProviders (key, options) {
if (!routers.length) {
@@ -91,7 +97,7 @@ module.exports = (node) => {
* @param {Uint8Array} key
* @param {Object} [options] - get options
* @param {number} [options.timeout] - optional timeout (default: 60000)
* @returns {Promise<{from: PeerId, val: Uint8Array}>}
* @returns {Promise<GetData>}
*/
async get (key, options) { // eslint-disable-line require-await
if (!node.isStarted() || !dht.isStarted) {
@@ -108,7 +114,7 @@ module.exports = (node) => {
* @param {number} nVals
* @param {Object} [options] - get options
* @param {number} [options.timeout] - optional timeout (default: 60000)
* @returns {Promise<Array<{from: PeerId, val: Uint8Array}>>}
* @returns {Promise<GetData[]>}
*/
async getMany (key, nVals, options) { // eslint-disable-line require-await
if (!node.isStarted() || !dht.isStarted) {
1 change: 1 addition & 0 deletions src/dialer/dial-request.js
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ const pAny = require('p-any')

/**
* @typedef {import('./')} Dialer
* @typedef {import('multiaddr')} Multiaddr
*/

/**
17 changes: 9 additions & 8 deletions src/dialer/index.js
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ const {
} = require('../constants')

/**
* @typedef {import('multiaddr')} Multiaddr
* @typedef {import('peer-id')} PeerId
* @typedef {import('../peer-store')} PeerStore
* @typedef {import('../transport-manager')} TransportManager
@@ -32,15 +33,15 @@ const {
* @property {TransportManager} transportManager
*
* @typedef {Object} DialerOptions
* @param {(addresses: Array<Address) => Array<Address>} [options.addressSorter = publicAddressesFirst] - Sort the known addresses of a peer before trying to dial.
* @param {(addresses: Address[]) => Address[]} [options.addressSorter = publicAddressesFirst] - Sort the known addresses of a peer before trying to dial.
* @property {number} [concurrency = MAX_PARALLEL_DIALS] - Number of max concurrent dials.
* @property {number} [perPeerLimit = MAX_PER_PEER_DIALS] - Number of max concurrent dials per peer.
* @property {number} [timeout = DIAL_TIMEOUT] - How long a dial attempt is allowed to take.
* @property {Object} [resolvers = {}] - multiaddr resolvers to use when dialing
*
* @typedef DialTarget
* @property {string} id
* @property {Array<multiaddr>} addrs
* @property {Multiaddr[]} addrs
*
* @typedef PendingDial
* @property {DialRequest} dialRequest
@@ -96,7 +97,7 @@ class Dialer {
* The dial to the first address that is successfully able to upgrade a connection
* will be used.
*
* @param {PeerId|multiaddr|string} peer - The peer to dial
* @param {PeerId|Multiaddr|string} peer - The peer to dial
* @param {object} [options]
* @param {AbortSignal} [options.signal] - An AbortController signal
* @returns {Promise<Connection>}
@@ -131,7 +132,7 @@ class Dialer {
* If a multiaddr is received it should be the first address attempted.
*
* @private
* @param {PeerId|multiaddr|string} peer - A PeerId or Multiaddr
* @param {PeerId|Multiaddr|string} peer - A PeerId or Multiaddr
* @returns {Promise<DialTarget>}
*/
async _createDialTarget (peer) {
@@ -219,8 +220,8 @@ class Dialer {
/**
* Resolve multiaddr recursively.
*
* @param {multiaddr} ma
* @returns {Promise<Array<multiaddr>>}
* @param {Multiaddr} ma
* @returns {Promise<Multiaddr[]>}
*/
async _resolve (ma) {
// TODO: recursive logic should live in multiaddr once dns4/dns6 support is in place
@@ -248,8 +249,8 @@ class Dialer {
/**
* Resolve a given multiaddr. If this fails, an empty array will be returned
*
* @param {multiaddr} ma
* @returns {Promise<Array<multiaddr>>}
* @param {Multiaddr} ma
* @returns {Promise<Multiaddr[]>}
*/
async _resolveRecord (ma) {
try {
4 changes: 2 additions & 2 deletions src/get-peer.js
Original file line number Diff line number Diff line change
@@ -8,15 +8,15 @@ const { codes } = require('./errors')

/**
* @typedef {import('peer-id')} PeerId
* @typedef {import('multiaddr')} multiaddr
* @typedef {import('multiaddr')} Multiaddr
*/

/**
* Converts the given `peer` to a `Peer` object.
* If a multiaddr is received, the addressBook is updated.
*
* @param {PeerId|multiaddr|string} peer
* @returns {{ id: PeerId, multiaddrs: Array<multiaddr> }}
* @returns {{ id: PeerId, multiaddrs: Multiaddr[] }}
*/
function getPeer (peer) {
if (typeof peer === 'string') {
13 changes: 6 additions & 7 deletions src/identify/index.js
Original file line number Diff line number Diff line change
@@ -30,16 +30,15 @@ const {
const { codes } = require('../errors')

/**
* @typedef {import('../')} Libp2p
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
* @typedef {import('../').DuplexIterable} DuplexIterable
* @typedef {import('libp2p-interfaces/src/connection/connection').DuplexIterableStream} DuplexIterableStream
*/

class IdentifyService {
/**
* @class
* @param {Object} options
* @param {Libp2p} options.libp2p
* @param {import('../')} options.libp2p
*/
constructor ({ libp2p }) {
this._libp2p = libp2p
@@ -74,7 +73,7 @@ class IdentifyService {
/**
* Send an Identify Push update to the list of connections
*
* @param {Array<Connection>} connections
* @param {Connection[]} connections
* @returns {Promise<void>}
*/
async push (connections) {
@@ -204,7 +203,7 @@ class IdentifyService {
*
* @param {Object} options
* @param {string} options.protocol
* @param {DuplexIterable} options.stream
* @param {DuplexIterableStream} options.stream
* @param {Connection} options.connection
* @returns {Promise<void>}
*/
@@ -225,7 +224,7 @@ class IdentifyService {
*
* @private
* @param {Object} options
* @param {DuplexIterable} options.stream
* @param {DuplexIterableStream} options.stream
* @param {Connection} options.connection
*/
async _handleIdentify ({ connection, stream }) {
@@ -264,7 +263,7 @@ class IdentifyService {
*
* @private
* @param {object} options
* @param {DuplexIterable} options.stream
* @param {DuplexIterableStream} options.stream
* @param {Connection} options.connection
*/
async _handlePush ({ connection, stream }) {
Loading