Skip to content

Commit 1c54160

Browse files
committed
chore: add typedefs
1 parent c6fd23a commit 1c54160

30 files changed

+383
-184
lines changed

Diff for: src/address-manager/index.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ log.error = debug('libp2p:addresses:error')
77
const multiaddr = require('multiaddr')
88

99
/**
10-
* Responsible for managing the peer addresses.
11-
* Peers can specify their listen and announce addresses.
12-
* The listen addresses will be used by the libp2p transports to listen for new connections,
13-
* while the announce addresses will be used for the peer addresses' to other peers in the network.
10+
* @typedef {Object} AddressManagerOptions
11+
* @property {Array<string>} [listen = []] - list of multiaddrs string representation to listen.
12+
* @property {Array<string>} [announce = []] - list of multiaddrs string representation to announce.
1413
*/
1514
class AddressManager {
1615
/**
16+
* Responsible for managing the peer addresses.
17+
* Peers can specify their listen and announce addresses.
18+
* The listen addresses will be used by the libp2p transports to listen for new connections,
19+
* while the announce addresses will be used for the peer addresses' to other peers in the network.
20+
*
1721
* @class
18-
* @param {object} [options]
19-
* @param {Array<string>} [options.listen = []] - list of multiaddrs string representation to listen.
20-
* @param {Array<string>} [options.announce = []] - list of multiaddrs string representation to announce.
22+
* @param {AddressManagerOptions} [options]
2123
*/
2224
constructor ({ listen = [], announce = [] } = {}) {
2325
this.listen = new Set(listen)
@@ -27,7 +29,7 @@ class AddressManager {
2729
/**
2830
* Get peer listen multiaddrs.
2931
*
30-
* @returns {Array<Multiaddr>}
32+
* @returns {Array<multiaddr>}
3133
*/
3234
getListenAddrs () {
3335
return Array.from(this.listen).map((a) => multiaddr(a))
@@ -36,7 +38,7 @@ class AddressManager {
3638
/**
3739
* Get peer announcing multiaddrs.
3840
*
39-
* @returns {Array<Multiaddr>}
41+
* @returns {Array<multiaddr>}
4042
*/
4143
getAnnounceAddrs () {
4244
return Array.from(this.announce).map((a) => multiaddr(a))

Diff for: src/circuit/auto-relay.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,25 @@ const {
1919
RELAY_RENDEZVOUS_NS
2020
} = require('./constants')
2121

22+
/**
23+
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
24+
* @typedef {import('../peer-store/address-book').Address} Address
25+
*/
26+
27+
/**
28+
* @typedef {Object} AutoRelayProperties
29+
* @property {import('../')} libp2p
30+
*
31+
* @typedef {Object} AutoRelayOptions
32+
* @property {number} [maxListeners = 1] - maximum number of relays to listen.
33+
*/
34+
2235
class AutoRelay {
2336
/**
2437
* Creates an instance of AutoRelay.
2538
*
2639
* @class
27-
* @param {object} props
28-
* @param {Libp2p} props.libp2p
29-
* @param {number} [props.maxListeners = 1] - maximum number of relays to listen.
40+
* @param {AutoRelayProperties & AutoRelayOptions} props
3041
*/
3142
constructor ({ libp2p, maxListeners = 1 }) {
3243
this._libp2p = libp2p

Diff for: src/circuit/index.js

+17
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@ const {
1212
RELAY_RENDEZVOUS_NS
1313
} = require('./constants')
1414

15+
/**
16+
* @typedef {import('../')} Libp2p
17+
*
18+
* @typedef {Object} RelayAdvertiseOptions
19+
* @property {number} [bootDelay = ADVERTISE_BOOT_DELAY]
20+
* @property {boolean} [enabled = true]
21+
* @property {number} [ttl = ADVERTISE_TTL]
22+
*
23+
* @typedef {Object} HopOptions
24+
* @property {boolean} [enabled = false]
25+
* @property {boolean} [active = false]
26+
*
27+
* @typedef {Object} AutoRelayOptions
28+
* @property {number} [maxListeners = 2] - maximum number of relays to listen.
29+
* @property {boolean} [enabled = false]
30+
*/
31+
1532
class Relay {
1633
/**
1734
* Creates an instance of Relay.

Diff for: src/connection-manager/index.js

+26-17
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,40 @@ const defaultOptions = {
3131
}
3232

3333
/**
34-
* Responsible for managing known connections.
34+
* @typedef {import('../')} Libp2p
35+
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
36+
*/
37+
38+
/**
39+
* @typedef {Object} ConnectionManagerOptions
40+
* @property {number} [maxConnections = Infinity] - The maximum number of connections allowed.
41+
* @property {number} [minConnections = 0] - The minimum number of connections to avoid pruning.
42+
* @property {number} [maxData = Infinity] - The max data (in and out), per average interval to allow.
43+
* @property {number} [maxSentData = Infinity] - The max outgoing data, per average interval to allow.
44+
* @property {number} [maxReceivedData = Infinity] - The max incoming data, per average interval to allow.
45+
* @property {number} [maxEventLoopDelay = Infinity] - The upper limit the event loop can take to run.
46+
* @property {number} [pollInterval = 2000] - How often, in milliseconds, metrics and latency should be checked.
47+
* @property {number} [movingAverageInterval = 60000] - How often, in milliseconds, to compute averages.
48+
* @property {number} [defaultPeerValue = 1] - The value of the peer.
49+
* @property {boolean} [autoDial = true] - Should preemptively guarantee connections are above the low watermark.
50+
* @property {number} [autoDialInterval = 10000] - How often, in milliseconds, it should preemptively guarantee connections are above the low watermark.
51+
*/
52+
53+
/**
54+
* @extends {EventEmitter}
3555
*
3656
* @fires ConnectionManager#peer:connect Emitted when a new peer is connected.
3757
* @fires ConnectionManager#peer:disconnect Emitted when a peer is disconnected.
3858
*/
3959
class ConnectionManager extends EventEmitter {
4060
/**
61+
* Responsible for managing known connections.
62+
*
4163
* @class
4264
* @param {Libp2p} libp2p
43-
* @param {object} options
44-
* @param {number} options.maxConnections - The maximum number of connections allowed. Default=Infinity
45-
* @param {number} options.minConnections - The minimum number of connections to avoid pruning. Default=0
46-
* @param {number} options.maxData - The max data (in and out), per average interval to allow. Default=Infinity
47-
* @param {number} options.maxSentData - The max outgoing data, per average interval to allow. Default=Infinity
48-
* @param {number} options.maxReceivedData - The max incoming data, per average interval to allow.. Default=Infinity
49-
* @param {number} options.maxEventLoopDelay - The upper limit the event loop can take to run. Default=Infinity
50-
* @param {number} options.pollInterval - How often, in milliseconds, metrics and latency should be checked. Default=2000
51-
* @param {number} options.movingAverageInterval - How often, in milliseconds, to compute averages. Default=60000
52-
* @param {number} options.defaultPeerValue - The value of the peer. Default=1
53-
* @param {boolean} options.autoDial - Should preemptively guarantee connections are above the low watermark. Default=true
54-
* @param {number} options.autoDialInterval - How often, in milliseconds, it should preemptively guarantee connections are above the low watermark. Default=10000
65+
* @param {ConnectionManagerOptions} options
5566
*/
56-
constructor (libp2p, options) {
67+
constructor (libp2p, options = {}) {
5768
super()
5869

5970
this._libp2p = libp2p
@@ -66,8 +77,6 @@ class ConnectionManager extends EventEmitter {
6677

6778
log('options: %j', this._options)
6879

69-
this._libp2p = libp2p
70-
7180
/**
7281
* Map of peer identifiers to their peer value for pruning connections.
7382
*
@@ -78,7 +87,7 @@ class ConnectionManager extends EventEmitter {
7887
/**
7988
* Map of connections per peer
8089
*
81-
* @type {Map<string, Array<conn>>}
90+
* @type {Map<string, Array<Connection>>}
8291
*/
8392
this.connections = new Map()
8493

Diff for: src/connection-manager/latency-monitor.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/* global window */
88
const globalThis = require('ipfs-utils/src/globalthis')
9-
const EventEmitter = require('events')
9+
const { EventEmitter } = require('events')
1010
const VisibilityChangeEmitter = require('./visibility-change-emitter')
1111
const debug = require('debug')('latency-monitor:LatencyMonitor')
1212

@@ -17,13 +17,21 @@ const debug = require('debug')('latency-monitor:LatencyMonitor')
1717
* @property {number} maxMS What was the max time for a cb to be called
1818
* @property {number} avgMs What was the average time for a cb to be called
1919
* @property {number} lengthMs How long this interval was in ms
20+
*
21+
* @typedef {Object} LatencyMonitorOptions
22+
* @property {number} [latencyCheckIntervalMs=500] - How often to add a latency check event (ms)
23+
* @property {number} [dataEmitIntervalMs=5000] - How often to summarize latency check events. null or 0 disables event firing
24+
* @property {Function} [asyncTestFn] - What cb-style async function to use
25+
* @property {number} [latencyRandomPercentage=5] - What percent (+/-) of latencyCheckIntervalMs should we randomly use? This helps avoid alignment to other events.
2026
*/
2127

2228
/**
2329
* A class to monitor latency of any async function which works in a browser or node. This works by periodically calling
2430
* the asyncTestFn and timing how long it takes the callback to be called. It can also periodically emit stats about this.
2531
* This can be disabled and stats can be pulled via setting dataEmitIntervalMs = 0.
2632
*
33+
* @extends {EventEmitter}
34+
*
2735
* The default implementation is an event loop latency monitor. This works by firing periodic events into the event loop
2836
* and timing how long it takes to get back.
2937
*
@@ -37,11 +45,8 @@ const debug = require('debug')('latency-monitor:LatencyMonitor')
3745
*/
3846
class LatencyMonitor extends EventEmitter {
3947
/**
40-
* @param {object} [options]
41-
* @param {number} [options.latencyCheckIntervalMs=500] - How often to add a latency check event (ms)
42-
* @param {number} [options.dataEmitIntervalMs=5000] - How often to summarize latency check events. null or 0 disables event firing
43-
* @param {Function} [options.asyncTestFn] - What cb-style async function to use
44-
* @param {number} [options.latencyRandomPercentage=5] - What percent (+/-) of latencyCheckIntervalMs should we randomly use? This helps avoid alignment to other events.
48+
* @class
49+
* @param {LatencyMonitorOptions} [options]
4550
*/
4651
constructor ({ latencyCheckIntervalMs, dataEmitIntervalMs, asyncTestFn, latencyRandomPercentage } = {}) {
4752
super()
@@ -91,6 +96,7 @@ class LatencyMonitor extends EventEmitter {
9196
// See: http://stackoverflow.com/questions/6032429/chrome-timeouts-interval-suspended-in-background-tabs
9297
if (isBrowser()) {
9398
that._visibilityChangeEmitter = new VisibilityChangeEmitter()
99+
94100
that._visibilityChangeEmitter.on('visibilityChange', (pageInFocus) => {
95101
if (pageInFocus) {
96102
that._startTimers()

Diff for: src/connection-manager/visibility-change-emitter.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
* This code is based on `latency-monitor` (https://github.com/mlucool/latency-monitor) by `mlucool` (https://github.com/mlucool), available under Apache License 2.0 (https://github.com/mlucool/latency-monitor/blob/master/LICENSE)
55
*/
66
'use strict'
7-
const EventEmitter = require('events')
7+
8+
const { EventEmitter } = require('events')
89

910
const debug = require('debug')('latency-monitor:VisibilityChangeEmitter')
1011

1112
/**
1213
* Listen to page visibility change events (i.e. when the page is focused / blurred) by an event emitter.
1314
*
15+
* @extends {EventEmitter}
16+
*
1417
* Warning: This does not work on all browsers, but should work on all modern browsers
1518
*
1619
* @example
@@ -29,12 +32,12 @@ const debug = require('debug')('latency-monitor:VisibilityChangeEmitter')
2932
* });
3033
* // To access the visibility state directly, call:
3134
* console.log('Am I focused now? ' + myVisibilityEmitter.isVisible());
32-
*
33-
* @class VisibilityChangeEmitter
3435
*/
35-
module.exports = class VisibilityChangeEmitter extends EventEmitter {
36+
class VisibilityChangeEmitter extends EventEmitter {
3637
/**
3738
* Creates a VisibilityChangeEmitter
39+
*
40+
* @class
3841
*/
3942
constructor () {
4043
super()
@@ -119,3 +122,5 @@ module.exports = class VisibilityChangeEmitter extends EventEmitter {
119122
this.emit('visibilityChange', visible)
120123
}
121124
}
125+
126+
module.exports = VisibilityChangeEmitter

Diff for: src/content-routing.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ const { messages, codes } = require('./errors')
66
const all = require('it-all')
77
const pAny = require('p-any')
88

9+
/**
10+
* @typedef {import('peer-id')} PeerId
11+
* @typedef {import('multiaddr')} multiaddr
12+
*/
13+
914
module.exports = (node) => {
1015
const routers = node._modules.contentRouting || []
1116
const dht = node._dht
@@ -24,7 +29,7 @@ module.exports = (node) => {
2429
* @param {object} [options]
2530
* @param {number} [options.timeout] - How long the query should run
2631
* @param {number} [options.maxNumProviders] - maximum number of providers to find
27-
* @returns {AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>}
32+
* @returns {AsyncIterable<{ id: PeerId, multiaddrs: Array<multiaddr> }>}
2833
*/
2934
async * findProviders (key, options) {
3035
if (!routers.length) {

Diff for: src/dialer/dial-request.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ log.error = debug('libp2p:dialer:request:error')
99
const FIFO = require('p-fifo')
1010
const pAny = require('p-any')
1111

12+
/**
13+
* @typedef {import('./')} Dialer
14+
*/
15+
16+
/**
17+
* @typedef {Object} DialRequestOptions
18+
* @property {Multiaddr[]} addrs
19+
* @property {function(Multiaddr):Promise<Connection>} dialAction
20+
* @property {Dialer} dialer
21+
*/
1222
class DialRequest {
1323
/**
1424
* Manages running the `dialAction` on multiple provided `addrs` in parallel
@@ -17,10 +27,8 @@ class DialRequest {
1727
* started using `DialRequest.run(options)`. Once a single dial has succeeded,
1828
* all other dials in the request will be cancelled.
1929
*
20-
* @param {object} options
21-
* @param {Multiaddr[]} options.addrs
22-
* @param {function(Multiaddr):Promise<Connection>} options.dialAction
23-
* @param {Dialer} options.dialer
30+
* @class
31+
* @param {DialRequestOptions} options
2432
*/
2533
constructor ({
2634
addrs,
@@ -34,8 +42,8 @@ class DialRequest {
3442

3543
/**
3644
* @async
37-
* @param {object} options
38-
* @param {AbortSignal} options.signal - An AbortController signal
45+
* @param {object} [options]
46+
* @param {AbortSignal} [options.signal] - An AbortController signal
3947
* @returns {Connection}
4048
*/
4149
async run (options) {

0 commit comments

Comments
 (0)