Skip to content

Commit 141f2b3

Browse files
committed
chore: config typescript
1 parent b894452 commit 141f2b3

File tree

12 files changed

+141
-77
lines changed

12 files changed

+141
-77
lines changed

doc/CONFIGURATION.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ const TCP = require('libp2p-tcp')
599599
const MPLEX = require('libp2p-mplex')
600600
const { NOISE } = require('libp2p-noise')
601601

602-
const { FaultTolerance } = require('libp2p/src/transport-manager')}
602+
const { FaultTolerance } = require('libp2p/src/transport-manager')
603603

604604
const node = await Libp2p.create({
605605
modules: {

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"src"
1919
],
2020
"scripts": {
21+
"prepare": "aegir build --no-bundle",
2122
"lint": "aegir lint",
2223
"build": "aegir build",
2324
"test": "npm run test:node && npm run test:browser",
@@ -81,7 +82,7 @@
8182
"it-protocol-buffers": "^0.2.0",
8283
"it-take": "1.0.0",
8384
"libp2p-crypto": "^0.19.0",
84-
"libp2p-interfaces": "^0.8.1",
85+
"libp2p-interfaces": "libp2p/js-libp2p-interfaces#chore/update-types",
8586
"libp2p-utils": "^0.2.2",
8687
"mafmt": "^8.0.0",
8788
"merge-options": "^3.0.4",

src/dialer/index.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ const {
3838
*
3939
* @typedef {Object} DialerOptions
4040
* @property {(addresses: Address[]) => Address[]} [options.addressSorter = publicAddressesFirst] - Sort the known addresses of a peer before trying to dial.
41-
* @property {number} [concurrency = MAX_PARALLEL_DIALS] - Number of max concurrent dials.
42-
* @property {number} [perPeerLimit = MAX_PER_PEER_DIALS] - Number of max concurrent dials per peer.
43-
* @property {number} [timeout = DIAL_TIMEOUT] - How long a dial attempt is allowed to take.
41+
* @property {number} [maxParallelDials = MAX_PARALLEL_DIALS] - Number of max concurrent dials.
42+
* @property {number} [maxDialsPerPeer = MAX_PER_PEER_DIALS] - Number of max concurrent dials per peer.
43+
* @property {number} [dialTimeout = DIAL_TIMEOUT] - How long a dial attempt is allowed to take.
4444
* @property {Record<string, Resolver>} [resolvers = {}] - multiaddr resolvers to use when dialing
4545
*
4646
* @typedef DialTarget
@@ -63,18 +63,18 @@ class Dialer {
6363
transportManager,
6464
peerStore,
6565
addressSorter = publicAddressesFirst,
66-
concurrency = MAX_PARALLEL_DIALS,
67-
timeout = DIAL_TIMEOUT,
68-
perPeerLimit = MAX_PER_PEER_DIALS,
66+
maxParallelDials = MAX_PARALLEL_DIALS,
67+
dialTimeout = DIAL_TIMEOUT,
68+
maxDialsPerPeer = MAX_PER_PEER_DIALS,
6969
resolvers = {}
7070
}) {
7171
this.transportManager = transportManager
7272
this.peerStore = peerStore
7373
this.addressSorter = addressSorter
74-
this.concurrency = concurrency
75-
this.timeout = timeout
76-
this.perPeerLimit = perPeerLimit
77-
this.tokens = [...new Array(concurrency)].map((_, index) => index)
74+
this.maxParallelDials = maxParallelDials
75+
this.timeout = dialTimeout
76+
this.maxDialsPerPeer = maxDialsPerPeer
77+
this.tokens = [...new Array(maxParallelDials)].map((_, index) => index)
7878
this._pendingDials = new Map()
7979

8080
for (const [key, value] of Object.entries(resolvers)) {
@@ -208,7 +208,7 @@ class Dialer {
208208
}
209209

210210
getTokens (num) {
211-
const total = Math.min(num, this.perPeerLimit, this.tokens.length)
211+
const total = Math.min(num, this.maxDialsPerPeer, this.tokens.length)
212212
const tokens = this.tokens.splice(0, total)
213213
log('%d tokens request, returning %d, %d remaining', num, total, this.tokens.length)
214214
return tokens

src/identify/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ const { codes } = require('../errors')
3434
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
3535
*/
3636

37+
/**
38+
* @typedef {Object} HostProperties
39+
* @property {string} agentVersion
40+
*/
41+
3742
class IdentifyService {
3843
/**
3944
* @class

src/index.js

+45-16
Original file line numberDiff line numberDiff line change
@@ -43,41 +43,75 @@ const { updateSelfPeerRecord } = require('./record/utils')
4343
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
4444
* @typedef {import('libp2p-interfaces/src/transport/types').TransportFactory} TransportFactory
4545
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxerFactory} MuxerFactory
46+
* @typedef {import('libp2p-interfaces/src/content-routing/types').ContentRouting} ContentRoutingFactory
47+
* @typedef {import('libp2p-interfaces/src/peer-discovery/types').PeerDiscovery} PeerDiscoveryFactory
48+
* @typedef {import('libp2p-interfaces/src/peer-routing/types').PeerRouting} PeerRoutingFactory
4649
* @typedef {import('libp2p-interfaces/src/crypto/types').Crypto} Crypto
4750
* @typedef {import('libp2p-interfaces/src/pubsub')} Pubsub
51+
* @typedef {import('libp2p-interfaces/src/pubsub').PubsubOptions} PubsubOptions
52+
* @typedef {import('interface-datastore').Datastore} Datastore
4853
*/
4954

5055
/**
56+
* @typedef {Object} RandomWalkOptions
57+
* @property {boolean} [enabled = false]
58+
* @property {number} [queriesPerPeriod = 1]
59+
* @property {number} [interval = 300e3]
60+
* @property {number} [timeout = 10e3]
61+
*
62+
* @typedef {Object} DhtOptions
63+
* @property {boolean} [enabled = false]
64+
* @property {number} [kBucketSize = 20]
65+
* @property {RandomWalkOptions} [randomWalk]
66+
*
67+
* @typedef {Object} KeychainOptions
68+
* @property {Datastore} [datastore]
69+
*
5170
* @typedef {Object} PeerStoreOptions
5271
* @property {boolean} persistence
5372
*
54-
* @typedef {Object} RelayOptions
73+
* @typedef {Object} PubsubLocalOptions
74+
* @property {boolean} enabled
75+
*
76+
* @typedef {Object} MetricsOptions
5577
* @property {boolean} enabled
56-
* @property {import('./circuit').RelayAdvertiseOptions} advertise
57-
* @property {import('./circuit').HopOptions} hop
58-
* @property {import('./circuit').AutoRelayOptions} autoRelay
78+
*
79+
* @typedef {Object} RelayOptions
80+
* @property {boolean} [enabled = true]
81+
* @property {import('./circuit').RelayAdvertiseOptions} [advertise]
82+
* @property {import('./circuit').HopOptions} [hop]
83+
* @property {import('./circuit').AutoRelayOptions} [autoRelay]
5984
*
6085
* @typedef {Object} Libp2pConfig
61-
* @property {Object} [dht] dht module options
62-
* @property {Object} [peerDiscovery]
63-
* @property {Pubsub} [pubsub] pubsub module options
86+
* @property {DhtOptions} [dht] dht module options
87+
* @property {import('./nat-manager').NatManagerOptions} [nat]
88+
* @property {Record<string, Object|boolean>} [peerDiscovery]
89+
* @property {PubsubLocalOptions & PubsubOptions} [pubsub] pubsub module options
6490
* @property {RelayOptions} [relay]
6591
* @property {Record<string, Object>} [transport] transport options indexed by transport key
6692
*
6793
* @typedef {Object} Libp2pModules
6894
* @property {TransportFactory[]} transport
6995
* @property {MuxerFactory[]} streamMuxer
7096
* @property {Crypto[]} connEncryption
97+
* @property {PeerDiscoveryFactory[]} [peerDiscovery]
98+
* @property {PeerRoutingFactory[]} [peerRouting]
99+
* @property {ContentRoutingFactory[]} [contentRouting]
100+
* @property {Object} [dht]
101+
* @property {Pubsub} [pubsub]
71102
*
72103
* @typedef {Object} Libp2pOptions
73104
* @property {Libp2pModules} modules libp2p modules to use
74105
* @property {import('./address-manager').AddressManagerOptions} [addresses]
75106
* @property {import('./connection-manager').ConnectionManagerOptions} [connectionManager]
107+
* @property {Datastore} [datastore]
76108
* @property {import('./dialer').DialerOptions} [dialer]
77-
* @property {import('./metrics').MetricsOptions} [metrics]
78-
* @property {Object} [keychain]
79-
* @property {import('./transport-manager').TransportManagerOptions} [transportManager]
109+
* @property {import('./identify/index').HostProperties} [host] libp2p host
110+
* @property {KeychainOptions & import('./keychain/index').KeychainOptions} [keychain]
111+
* @property {MetricsOptions & import('./metrics').MetricsOptions} [metrics]
112+
* @property {import('./peer-routing').PeerRoutingOptions} [peerRouting]
80113
* @property {PeerStoreOptions & import('./peer-store/persistent').PersistentPeerStoreOptions} [peerStore]
114+
* @property {import('./transport-manager').TransportManagerOptions} [transportManager]
81115
* @property {Libp2pConfig} [config]
82116
*
83117
* @typedef {Object} constructorOptions
@@ -175,7 +209,6 @@ class Libp2p extends EventEmitter {
175209
const keychainOpts = Keychain.generateOptions()
176210

177211
this.keychain = new Keychain(this._options.keychain.datastore, {
178-
passPhrase: this._options.keychain.pass,
179212
...keychainOpts,
180213
...this._options.keychain
181214
})
@@ -227,11 +260,7 @@ class Libp2p extends EventEmitter {
227260
this.dialer = new Dialer({
228261
transportManager: this.transportManager,
229262
peerStore: this.peerStore,
230-
concurrency: this._options.dialer.maxParallelDials,
231-
perPeerLimit: this._options.dialer.maxDialsPerPeer,
232-
timeout: this._options.dialer.dialTimeout,
233-
resolvers: this._options.dialer.resolvers,
234-
addressSorter: this._options.dialer.addressSorter
263+
...this._options.dialer
235264
})
236265

237266
this._modules.transport.forEach((Transport) => {

src/keychain/index.js

+25-13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@ require('node-forge/lib/sha512')
1717
* @typedef {import('interface-datastore').Datastore} Datastore
1818
*/
1919

20+
/**
21+
* @typedef {Object} DekOptions
22+
* @property {string} hash
23+
* @property {string} salt
24+
* @property {number} iterationCount
25+
* @property {number} keyLength
26+
*
27+
* @typedef {Object} KeychainOptions
28+
* @property {string} pass
29+
* @property {DekOptions} [dek]
30+
*/
31+
32+
/**
33+
* Information about a key.
34+
*
35+
* @typedef {Object} KeyInfo
36+
* @property {string} id - The universally unique key id.
37+
* @property {string} name - The local key name.
38+
*/
39+
2040
const keyPrefix = '/pkcs8/'
2141
const infoPrefix = '/info/'
2242
const privates = new WeakMap()
@@ -85,14 +105,6 @@ function DsInfoName (name) {
85105
return new Key(infoPrefix + name)
86106
}
87107

88-
/**
89-
* Information about a key.
90-
*
91-
* @typedef {Object} KeyInfo
92-
* @property {string} id - The universally unique key id.
93-
* @property {string} name - The local key name.
94-
*/
95-
96108
/**
97109
* Manages the lifecycle of a key. Keys are encrypted at rest using PKCS #8.
98110
*
@@ -106,7 +118,7 @@ class Keychain {
106118
* Creates a new instance of a key chain.
107119
*
108120
* @param {Datastore} store - where the key are.
109-
* @param {object} options
121+
* @param {KeychainOptions} options
110122
* @class
111123
*/
112124
constructor (store, options) {
@@ -118,8 +130,8 @@ class Keychain {
118130
this.opts = mergeOptions(defaultOptions, options)
119131

120132
// Enforce NIST SP 800-132
121-
if (this.opts.passPhrase && this.opts.passPhrase.length < 20) {
122-
throw new Error('passPhrase must be least 20 characters')
133+
if (this.opts.pass && this.opts.pass.length < 20) {
134+
throw new Error('pass must be least 20 characters')
123135
}
124136
if (this.opts.dek.keyLength < NIST.minKeyLength) {
125137
throw new Error(`dek.keyLength must be least ${NIST.minKeyLength} bytes`)
@@ -131,8 +143,8 @@ class Keychain {
131143
throw new Error(`dek.iterationCount must be least ${NIST.minIterationCount}`)
132144
}
133145

134-
const dek = this.opts.passPhrase ? crypto.pbkdf2(
135-
this.opts.passPhrase,
146+
const dek = this.opts.pass ? crypto.pbkdf2(
147+
this.opts.pass,
136148
this.opts.dek.salt,
137149
this.opts.dek.iterationCount,
138150
this.opts.dek.keyLength,

src/nat-manager.js

+20-14
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,39 @@ const {
1717
} = require('./errors')
1818
const isLoopback = require('libp2p-utils/src/multiaddr/is-loopback')
1919

20+
const DEFAULT_TTL = 7200
21+
2022
/**
2123
* @typedef {import('peer-id')} PeerId
2224
* @typedef {import('./transport-manager')} TransportManager
2325
* @typedef {import('./address-manager')} AddressManager
2426
*/
2527

28+
/**
29+
* @typedef {Object} NatManagerProperties
30+
* @property {PeerId} peerId - The peer ID of the current node
31+
* @property {TransportManager} transportManager - A transport manager
32+
* @property {AddressManager} addressManager - An address manager
33+
*
34+
* @typedef {Object} NatManagerOptions
35+
* @property {boolean} enabled - Whether to enable the NAT manager
36+
* @property {string} [externalIp] - Pass a value to use instead of auto-detection
37+
* @property {string} [description] - A string value to use for the port mapping description on the gateway
38+
* @property {number} [ttl = DEFAULT_TTL] - How long UPnP port mappings should last for in seconds (minimum 1200)
39+
* @property {boolean} [keepAlive] - Whether to automatically refresh UPnP port mappings when their TTL is reached
40+
* @property {string} [gateway] - Pass a value to use instead of auto-detection
41+
* @property {object} [pmp] - PMP options
42+
* @property {boolean} [pmp.enabled] - Whether to enable PMP as well as UPnP
43+
*/
44+
2645
function highPort (min = 1024, max = 65535) {
2746
return Math.floor(Math.random() * (max - min + 1) + min)
2847
}
2948

30-
const DEFAULT_TTL = 7200
31-
3249
class NatManager {
3350
/**
3451
* @class
35-
* @param {object} options
36-
* @param {PeerId} options.peerId - The peer ID of the current node
37-
* @param {TransportManager} options.transportManager - A transport manager
38-
* @param {AddressManager} options.addressManager - An address manager
39-
* @param {boolean} options.enabled - Whether to enable the NAT manager
40-
* @param {string} [options.externalIp] - Pass a value to use instead of auto-detection
41-
* @param {string} [options.description] - A string value to use for the port mapping description on the gateway
42-
* @param {number} [options.ttl] - How long UPnP port mappings should last for in seconds (minimum 1200)
43-
* @param {boolean} [options.keepAlive] - Whether to automatically refresh UPnP port mappings when their TTL is reached
44-
* @param {string} [options.gateway] - Pass a value to use instead of auto-detection
45-
* @param {object} [options.pmp] - PMP options
46-
* @param {boolean} [options.pmp.enabled] - Whether to enable PMP as well as UPnP
52+
* @param {NatManagerProperties & NatManagerOptions} options
4753
*/
4854
constructor ({ peerId, addressManager, transportManager, ...options }) {
4955
this._peerId = peerId

src/peer-routing.js

+11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ const PeerId = require('peer-id')
2525
/**
2626
* @typedef {import('multiaddr')} Multiaddr
2727
*/
28+
29+
/**
30+
* @typedef {Object} RefreshManagerOptions
31+
* @property {boolean} [enabled = true] - Whether to enable the Refresh manager
32+
* @property {number} [bootDelay = 6e5] - Boot delay to start the Refresh Manager (in ms)
33+
* @property {number} [interval = 10e3] - Interval between each Refresh Manager run (in ms)
34+
*
35+
* @typedef {Object} PeerRoutingOptions
36+
* @property {RefreshManagerOptions} [refreshManager]
37+
*/
38+
2839
class PeerRouting {
2940
/**
3041
* @class

src/peer-store/persistent/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const Protocols = require('./pb/proto-book.proto')
2424
/**
2525
* @typedef {Object} PersistentPeerStoreProperties
2626
* @property {PeerId} peerId
27-
* @property {any} datastore
27+
* @property {import('interface-datastore').Datastore} datastore
2828
*
2929
* @typedef {Object} PersistentPeerStoreOptions
3030
* @property {number} [threshold = 5] - Number of dirty peers allowed before commit data.

test/dialing/direct.node.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ describe('Dialing (direct, TCP)', () => {
147147
const dialer = new Dialer({
148148
transportManager: localTM,
149149
peerStore,
150-
timeout: 50
150+
dialTimeout: 50
151151
})
152152
sinon.stub(localTM, 'dial').callsFake(async (addr, options) => {
153153
expect(options.signal).to.exist()
@@ -171,7 +171,7 @@ describe('Dialing (direct, TCP)', () => {
171171
]
172172
const dialer = new Dialer({
173173
transportManager: localTM,
174-
concurrency: 2,
174+
maxParallelDials: 2,
175175
peerStore: {
176176
addressBook: {
177177
add: () => {},

0 commit comments

Comments
 (0)