Skip to content

Commit 64124f1

Browse files
committed
chore: add typedefs (#802)
1 parent 117f2b9 commit 64124f1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1255
-765
lines changed

.github/workflows/main.yml

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
name: ci
2+
on:
3+
push:
4+
branches:
5+
- master
6+
pull_request:
7+
branches:
8+
- '**'
9+
10+
jobs:
11+
check:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v2
15+
- run: yarn
16+
- run: yarn lint
17+
- uses: gozala/[email protected]
18+
- run: yarn build
19+
- run: yarn aegir dep-check
20+
- uses: ipfs/aegir/actions/bundle-size@master
21+
name: size
22+
with:
23+
github_token: ${{ secrets.GITHUB_TOKEN }}
24+
test-node:
25+
needs: check
26+
runs-on: ${{ matrix.os }}
27+
strategy:
28+
matrix:
29+
os: [windows-latest, ubuntu-latest, macos-latest]
30+
node: [12, 14]
31+
fail-fast: true
32+
steps:
33+
- uses: actions/checkout@v2
34+
- uses: actions/setup-node@v1
35+
with:
36+
node-version: ${{ matrix.node }}
37+
- run: yarn
38+
- run: npx nyc --reporter=lcov aegir test -t node -- --bail
39+
- uses: codecov/codecov-action@v1
40+
test-chrome:
41+
needs: check
42+
runs-on: ubuntu-latest
43+
steps:
44+
- uses: actions/checkout@v2
45+
- run: yarn
46+
- run: npx aegir test -t browser -t webworker --bail
47+
test-firefox:
48+
needs: check
49+
runs-on: ubuntu-latest
50+
steps:
51+
- uses: actions/checkout@v2
52+
- run: yarn
53+
- run: npx aegir test -t browser -t webworker --bail -- --browsers FirefoxHeadless
54+
test-interop:
55+
needs: check
56+
runs-on: ubuntu-latest
57+
steps:
58+
- uses: actions/checkout@v2
59+
- run: yarn
60+
- run: cd node_modules/interop-libp2p && yarn && LIBP2P_JS=${GITHUB_WORKSPACE}/src/index.js npx aegir test -t node --bail
61+
test-auto-relay-example:
62+
needs: check
63+
runs-on: ubuntu-latest
64+
steps:
65+
- uses: actions/checkout@v2
66+
- run: yarn
67+
- run: cd examples && yarn && npm run test -- auto-relay

.travis.yml

-58
This file was deleted.

package.json

+12-3
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"
@@ -53,15 +62,15 @@
5362
"events": "^3.1.0",
5463
"hashlru": "^2.3.0",
5564
"interface-datastore": "^2.0.0",
56-
"ipfs-utils": "^2.2.0",
65+
"ipfs-utils": "^5.0.1",
5766
"it-all": "^1.0.1",
5867
"it-buffer": "^0.1.2",
5968
"it-handshake": "^1.0.1",
6069
"it-length-prefixed": "^3.0.1",
6170
"it-pipe": "^1.1.0",
6271
"it-protocol-buffers": "^0.2.0",
6372
"libp2p-crypto": "^0.18.0",
64-
"libp2p-interfaces": "^0.7.2",
73+
"libp2p-interfaces": "^0.8.0",
6574
"libp2p-utils": "^0.2.2",
6675
"mafmt": "^8.0.0",
6776
"merge-options": "^2.0.0",
@@ -88,7 +97,7 @@
8897
"devDependencies": {
8998
"@nodeutils/defaults-deep": "^1.1.0",
9099
"abortable-iterator": "^3.0.0",
91-
"aegir": "^27.0.0",
100+
"aegir": "^29.2.0",
92101
"chai-bytes": "^0.1.2",
93102
"chai-string": "^1.5.0",
94103
"delay": "^4.3.0",

src/address-manager/index.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
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
/**
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.
6+
* @typedef {import('multiaddr')} Multiaddr
7+
*/
8+
9+
/**
10+
* @typedef {Object} AddressManagerOptions
11+
* @property {string[]} [listen = []] - list of multiaddrs string representation to listen.
12+
* @property {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 {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 {Multiaddr[]}
4042
*/
4143
getAnnounceAddrs () {
4244
return Array.from(this.announce).map((a) => multiaddr(a))

src/circuit/auto-relay.js

+22-7
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')
@@ -19,14 +20,25 @@ const {
1920
RELAY_RENDEZVOUS_NS
2021
} = require('./constants')
2122

23+
/**
24+
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
25+
* @typedef {import('../peer-store/address-book').Address} Address
26+
*/
27+
28+
/**
29+
* @typedef {Object} AutoRelayProperties
30+
* @property {import('../')} libp2p
31+
*
32+
* @typedef {Object} AutoRelayOptions
33+
* @property {number} [maxListeners = 1] - maximum number of relays to listen.
34+
*/
35+
2236
class AutoRelay {
2337
/**
2438
* Creates an instance of AutoRelay.
2539
*
2640
* @class
27-
* @param {object} props
28-
* @param {Libp2p} props.libp2p
29-
* @param {number} [props.maxListeners = 1] - maximum number of relays to listen.
41+
* @param {AutoRelayProperties & AutoRelayOptions} props
3042
*/
3143
constructor ({ libp2p, maxListeners = 1 }) {
3244
this._libp2p = libp2p
@@ -58,7 +70,7 @@ class AutoRelay {
5870
*
5971
* @param {Object} props
6072
* @param {PeerId} props.peerId
61-
* @param {Array<string>} props.protocols
73+
* @param {string[]} props.protocols
6274
* @returns {Promise<void>}
6375
*/
6476
async _onProtocolChange ({ peerId, protocols }) {
@@ -78,6 +90,9 @@ class AutoRelay {
7890
// If protocol, check if can hop, store info in the metadataBook and listen on it
7991
try {
8092
const connection = this._connectionManager.get(peerId)
93+
if (!connection) {
94+
return
95+
}
8196

8297
// Do not hop on a relayed connection
8398
if (connection.remoteAddr.protoCodes().includes(CIRCUIT_PROTO_CODE)) {
@@ -171,7 +186,7 @@ class AutoRelay {
171186
* 2. Dial and try to listen on the peers we know that support hop but are not connected.
172187
* 3. Search the network.
173188
*
174-
* @param {Array<string>} [peersToIgnore]
189+
* @param {string[]} [peersToIgnore]
175190
* @returns {Promise<void>}
176191
*/
177192
async _listenOnAvailableHopRelays (peersToIgnore = []) {

0 commit comments

Comments
 (0)