Skip to content

Commit 28b9f85

Browse files
committed
chore: add types checker script and more fixes on the jsdocs
1 parent 7e05c4a commit 28b9f85

37 files changed

+246
-165
lines changed

Diff for: package.json

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44
"description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
55
"leadMaintainer": "Jacob Heun <[email protected]>",
66
"main": "src/index.js",
7+
"types": "dist/src/index.d.ts",
8+
"typesVersions": {
9+
"*": {
10+
"src/*": [
11+
"dist/src/*",
12+
"dist/src/*/index"
13+
]
14+
}
15+
},
716
"files": [
817
"dist",
918
"src"
@@ -15,6 +24,7 @@
1524
"test:node": "aegir test -t node -f \"./test/**/*.{node,spec}.js\"",
1625
"test:browser": "aegir test -t browser",
1726
"test:examples": "cd examples && npm run test:all",
27+
"test:types": "aegir ts -p check",
1828
"release": "aegir release -t node -t browser",
1929
"release-minor": "aegir release --type minor -t node -t browser",
2030
"release-major": "aegir release --type major -t node -t browser",
@@ -88,7 +98,7 @@
8898
"devDependencies": {
8999
"@nodeutils/defaults-deep": "^1.1.0",
90100
"abortable-iterator": "^3.0.0",
91-
"aegir": "^27.0.0",
101+
"aegir": "^29.1.0",
92102
"chai-bytes": "^0.1.2",
93103
"chai-string": "^1.5.0",
94104
"delay": "^4.3.0",

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

-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
'use strict'
22

3-
const debug = require('debug')
4-
const log = debug('libp2p:addresses')
5-
log.error = debug('libp2p:addresses:error')
6-
73
const multiaddr = require('multiaddr')
84

95
/**

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict'
22

33
const debug = require('debug')
4-
const log = debug('libp2p:auto-relay')
5-
log.error = debug('libp2p:auto-relay:error')
4+
const log = Object.assign(debug('libp2p:auto-relay'), {
5+
error: debug('libp2p:auto-relay:err')
6+
})
67

78
const uint8ArrayFromString = require('uint8arrays/from-string')
89
const uint8ArrayToString = require('uint8arrays/to-string')

Diff for: src/circuit/circuit/hop.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
'use strict'
22

33
const debug = require('debug')
4-
const log = debug('libp2p:circuit:hop')
5-
log.error = debug('libp2p:circuit:hop:error')
4+
const log = Object.assign(debug('libp2p:circuit:hop'), {
5+
error: debug('libp2p:circuit:hop:err')
6+
})
7+
const errCode = require('err-code')
68

79
const PeerId = require('peer-id')
810
const { validateAddrs } = require('./utils')
911
const StreamHandler = require('./stream-handler')
1012
const { CircuitRelay: CircuitPB } = require('../protocol')
11-
const pipe = require('it-pipe')
12-
const errCode = require('err-code')
13+
const { pipe } = require('it-pipe')
1314
const { codes: Errors } = require('../../errors')
1415

1516
const { stop } = require('./stop')
1617

1718
const multicodec = require('./../multicodec')
1819

20+
/**
21+
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
22+
*/
23+
1924
module.exports.handleHop = async function handleHop ({
2025
connection,
2126
request,

Diff for: src/circuit/circuit/stop.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
'use strict'
22

3+
const debug = require('debug')
4+
const log = Object.assign(debug('libp2p:circuit:stop'), {
5+
error: debug('libp2p:circuit:stop:err')
6+
})
7+
38
const { CircuitRelay: CircuitPB } = require('../protocol')
49
const multicodec = require('../multicodec')
510
const StreamHandler = require('./stream-handler')
611
const { validateAddrs } = require('./utils')
712

8-
const debug = require('debug')
9-
const log = debug('libp2p:circuit:stop')
10-
log.error = debug('libp2p:circuit:stop:error')
13+
/**
14+
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
15+
*/
1116

1217
/**
1318
* Handles incoming STOP requests
1419
*
1520
* @private
16-
* @param {*} options
21+
* @param {object} options
1722
* @param {Connection} options.connection
1823
* @param {*} options.request - The CircuitRelay protobuf request (unencoded)
1924
* @param {StreamHandler} options.streamHandler
20-
* @returns {Promise<*>} Resolves a duplex iterable
25+
* @returns {Promise<*>|void} Resolves a duplex iterable
2126
*/
2227
module.exports.handleStop = function handleStop ({
2328
connection,
@@ -44,7 +49,7 @@ module.exports.handleStop = function handleStop ({
4449
* Creates a STOP request
4550
*
4651
* @private
47-
* @param {*} options
52+
* @param {object} options
4853
* @param {Connection} options.connection
4954
* @param {*} options.request - The CircuitRelay protobuf request (unencoded)
5055
* @returns {Promise<*>} Resolves a duplex iterable

Diff for: src/circuit/circuit/stream-handler.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
'use strict'
22

3+
const debug = require('debug')
4+
const log = Object.assign(debug('libp2p:circuit:stream-handler'), {
5+
error: debug('libp2p:circuit:stream-handler:err')
6+
})
7+
38
const lp = require('it-length-prefixed')
49
const handshake = require('it-handshake')
510
const { CircuitRelay: CircuitPB } = require('../protocol')
611

7-
const debug = require('debug')
8-
const log = debug('libp2p:circuit:stream-handler')
9-
log.error = debug('libp2p:circuit:stream-handler:error')
10-
1112
class StreamHandler {
1213
/**
1314
* Create a stream handler for connection
@@ -27,7 +28,7 @@ class StreamHandler {
2728
* Read and decode message
2829
*
2930
* @async
30-
* @returns {void}
31+
* @returns {Promise<void>}
3132
*/
3233
async read () {
3334
const msg = await this.decoder.next()

Diff for: src/circuit/circuit/utils.js

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
const multiaddr = require('multiaddr')
44
const { CircuitRelay } = require('../protocol')
55

6+
/**
7+
* @typedef {import('./stream-handler')} StreamHandler
8+
*/
9+
610
/**
711
* Write a response
812
*

Diff for: src/circuit/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict'
22

33
const debug = require('debug')
4-
const log = debug('libp2p:relay')
5-
log.error = debug('libp2p:relay:error')
4+
const log = Object.assign(debug('libp2p:relay'), {
5+
error: debug('libp2p:relay:err')
6+
})
67

78
const {
89
setDelayedInterval,

Diff for: src/circuit/listener.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'use strict'
22

3-
const EventEmitter = require('events')
3+
const { EventEmitter } = require('events')
44
const multiaddr = require('multiaddr')
55

6-
const debug = require('debug')
7-
const log = debug('libp2p:circuit:listener')
8-
log.err = debug('libp2p:circuit:error:listener')
6+
/**
7+
* @typedef {import('multiaddr')} Multiaddr
8+
*/
99

1010
/**
11-
* @param {Libp2p} libp2p
11+
* @param {import('../')} libp2p
1212
* @returns {Listener} a transport listener
1313
*/
1414
module.exports = (libp2p) => {

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

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict'
22

33
const debug = require('debug')
4-
const log = debug('libp2p:connection-manager')
5-
log.error = debug('libp2p:connection-manager:error')
4+
const log = Object.assign(debug('libp2p:connection-manager'), {
5+
error: debug('libp2p:connection-manager:err')
6+
})
67

78
const errcode = require('err-code')
89
const mergeOptions = require('merge-options')
@@ -14,7 +15,7 @@ const { EventEmitter } = require('events')
1415
const PeerId = require('peer-id')
1516

1617
const {
17-
ERR_INVALID_PARAMETERS
18+
codes: { ERR_INVALID_PARAMETERS }
1819
} = require('../errors')
1920

2021
const defaultOptions = {
@@ -187,15 +188,17 @@ class ConnectionManager extends EventEmitter {
187188
* @private
188189
*/
189190
_checkMetrics () {
190-
const movingAverages = this._libp2p.metrics.global.movingAverages
191-
const received = movingAverages.dataReceived[this._options.movingAverageInterval].movingAverage()
192-
this._checkMaxLimit('maxReceivedData', received)
193-
const sent = movingAverages.dataSent[this._options.movingAverageInterval].movingAverage()
194-
this._checkMaxLimit('maxSentData', sent)
195-
const total = received + sent
196-
this._checkMaxLimit('maxData', total)
197-
log('metrics update', total)
198-
this._timer = retimer(this._checkMetrics, this._options.pollInterval)
191+
if (this._libp2p.metrics) {
192+
const movingAverages = this._libp2p.metrics.global.movingAverages
193+
const received = movingAverages.dataReceived[this._options.movingAverageInterval].movingAverage()
194+
this._checkMaxLimit('maxReceivedData', received)
195+
const sent = movingAverages.dataSent[this._options.movingAverageInterval].movingAverage()
196+
this._checkMaxLimit('maxSentData', sent)
197+
const total = received + sent
198+
this._checkMaxLimit('maxData', total)
199+
log('metrics update', total)
200+
this._timer = retimer(this._checkMetrics, this._options.pollInterval)
201+
}
199202
}
200203

201204
/**
@@ -249,7 +252,7 @@ class ConnectionManager extends EventEmitter {
249252
* Get a connection with a peer.
250253
*
251254
* @param {PeerId} peerId
252-
* @returns {Connection}
255+
* @returns {Connection|null}
253256
*/
254257
get (peerId) {
255258
const connections = this.getAll(peerId)

Diff for: src/content-routing.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const pAny = require('p-any')
99
/**
1010
* @typedef {import('peer-id')} PeerId
1111
* @typedef {import('multiaddr')} Multiaddr
12+
* @typedef {import('cids')} CID
1213
*/
1314

1415
/**
@@ -63,7 +64,7 @@ module.exports = (node) => {
6364
* a provider of the given key.
6465
*
6566
* @param {CID} key - The CID key of the content to find
66-
* @returns {Promise<void>}
67+
* @returns {Promise<void[]>}
6768
*/
6869
async provide (key) { // eslint-disable-line require-await
6970
if (!routers.length) {

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
'use strict'
22

3+
const errCode = require('err-code')
34
const AbortController = require('abort-controller')
45
const anySignal = require('any-signal')
5-
const debug = require('debug')
6-
const errCode = require('err-code')
7-
const log = debug('libp2p:dialer:request')
8-
log.error = debug('libp2p:dialer:request:error')
96
const FIFO = require('p-fifo')
107
const pAny = require('p-any')
118

129
/**
10+
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
1311
* @typedef {import('./')} Dialer
1412
* @typedef {import('multiaddr')} Multiaddr
1513
*/
@@ -45,7 +43,7 @@ class DialRequest {
4543
* @async
4644
* @param {object} [options]
4745
* @param {AbortSignal} [options.signal] - An AbortController signal
48-
* @returns {Connection}
46+
* @returns {Promise<Connection>}
4947
*/
5048
async run (options) {
5149
const tokens = this.dialer.getTokens(this.addrs.length)

Diff for: src/dialer/index.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
'use strict'
22

3-
const multiaddr = require('multiaddr')
3+
const debug = require('debug')
4+
const log = Object.assign(debug('libp2p:dialer'), {
5+
error: debug('libp2p:dialer:err')
6+
})
47
const errCode = require('err-code')
8+
const multiaddr = require('multiaddr')
59
const TimeoutController = require('timeout-abort-controller')
610
const anySignal = require('any-signal')
7-
const debug = require('debug')
8-
const log = debug('libp2p:dialer')
9-
log.error = debug('libp2p:dialer:error')
1011

1112
const { DialRequest } = require('./dial-request')
1213
const { publicAddressesFirst } = require('libp2p-utils/src/address-sort')
@@ -20,9 +21,11 @@ const {
2021
} = require('../constants')
2122

2223
/**
24+
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
2325
* @typedef {import('multiaddr')} Multiaddr
2426
* @typedef {import('peer-id')} PeerId
2527
* @typedef {import('../peer-store')} PeerStore
28+
* @typedef {import('../peer-store/address-book').Address} Address
2629
* @typedef {import('../transport-manager')} TransportManager
2730
* @typedef {import('./dial-request')} DialRequest
2831
*/
@@ -172,7 +175,7 @@ class Dialer {
172175
* @param {AbortSignal} [options.signal] - An AbortController signal
173176
* @returns {PendingDial}
174177
*/
175-
_createPendingDial (dialTarget, options) {
178+
_createPendingDial (dialTarget, options = {}) {
176179
const dialAction = (addr, options) => {
177180
if (options.signal.aborted) throw errCode(new Error('already aborted'), codes.ERR_ALREADY_ABORTED)
178181
return this.transportManager.dial(addr, options)

Diff for: src/get-peer.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const errCode = require('err-code')
77
const { codes } = require('./errors')
88

99
/**
10-
* @typedef {import('peer-id')} PeerId
1110
* @typedef {import('multiaddr')} Multiaddr
1211
*/
1312

@@ -16,7 +15,7 @@ const { codes } = require('./errors')
1615
* If a multiaddr is received, the addressBook is updated.
1716
*
1817
* @param {PeerId|multiaddr|string} peer
19-
* @returns {{ id: PeerId, multiaddrs: Multiaddr[] }}
18+
* @returns {{ id: PeerId, multiaddrs: Multiaddr[]|undefined }}
2019
*/
2120
function getPeer (peer) {
2221
if (typeof peer === 'string') {

0 commit comments

Comments
 (0)