@libp2p/kad-dht 12.0.14 → 12.0.15-4ad63bb79

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/dist/index.min.js +3 -3
  2. package/dist/src/constants.d.ts.map +1 -1
  3. package/dist/src/constants.js +1 -1
  4. package/dist/src/constants.js.map +1 -1
  5. package/dist/src/index.d.ts +5 -0
  6. package/dist/src/index.d.ts.map +1 -1
  7. package/dist/src/index.js.map +1 -1
  8. package/dist/src/kad-dht.d.ts.map +1 -1
  9. package/dist/src/kad-dht.js +3 -19
  10. package/dist/src/kad-dht.js.map +1 -1
  11. package/dist/src/network.d.ts +3 -0
  12. package/dist/src/network.d.ts.map +1 -1
  13. package/dist/src/network.js +33 -8
  14. package/dist/src/network.js.map +1 -1
  15. package/dist/src/peer-routing/index.js +1 -1
  16. package/dist/src/peer-routing/index.js.map +1 -1
  17. package/dist/src/routing-table/index.d.ts.map +1 -1
  18. package/dist/src/routing-table/index.js +7 -0
  19. package/dist/src/routing-table/index.js.map +1 -1
  20. package/dist/src/routing-table/refresh.d.ts +1 -1
  21. package/dist/src/routing-table/refresh.d.ts.map +1 -1
  22. package/dist/src/routing-table/refresh.js +1 -1
  23. package/dist/src/routing-table/refresh.js.map +1 -1
  24. package/dist/src/rpc/handlers/find-node.d.ts +3 -0
  25. package/dist/src/rpc/handlers/find-node.d.ts.map +1 -1
  26. package/dist/src/rpc/handlers/find-node.js +10 -1
  27. package/dist/src/rpc/handlers/find-node.js.map +1 -1
  28. package/package.json +12 -12
  29. package/src/constants.ts +1 -1
  30. package/src/index.ts +6 -0
  31. package/src/kad-dht.ts +19 -21
  32. package/src/network.ts +38 -9
  33. package/src/peer-routing/index.ts +1 -1
  34. package/src/routing-table/index.ts +9 -0
  35. package/src/routing-table/refresh.ts +1 -1
  36. package/src/rpc/handlers/find-node.ts +13 -1
  37. package/dist/typedoc-urls.json +0 -55
@@ -1,14 +1,18 @@
1
1
  import { CodeError } from '@libp2p/interface';
2
+ import { protocols } from '@multiformats/multiaddr';
3
+ import { equals as uint8ArrayEquals } from 'uint8arrays';
2
4
  import { MessageType } from '../../message/dht.js';
3
5
  export class FindNodeHandler {
4
6
  peerRouting;
5
7
  peerInfoMapper;
6
8
  peerId;
9
+ addressManager;
7
10
  log;
8
11
  constructor(components, init) {
9
12
  const { peerRouting, logPrefix } = init;
10
13
  this.log = components.logger.forComponent(`${logPrefix}:rpc:handlers:find-node`);
11
14
  this.peerId = components.peerId;
15
+ this.addressManager = components.addressManager;
12
16
  this.peerRouting = peerRouting;
13
17
  this.peerInfoMapper = init.peerInfoMapper;
14
18
  }
@@ -21,13 +25,18 @@ export class FindNodeHandler {
21
25
  throw new CodeError('Invalid FIND_NODE message received - key was missing', 'ERR_INVALID_MESSAGE');
22
26
  }
23
27
  const closer = await this.peerRouting.getCloserPeersOffline(msg.key, peerId);
28
+ if (uint8ArrayEquals(this.peerId.toBytes(), msg.key)) {
29
+ closer.push({
30
+ id: this.peerId,
31
+ multiaddrs: this.addressManager.getAddresses().map(ma => ma.decapsulateCode(protocols('p2p').code))
32
+ });
33
+ }
24
34
  const response = {
25
35
  type: MessageType.FIND_NODE,
26
36
  clusterLevel: msg.clusterLevel,
27
37
  closer: closer
28
38
  .map(this.peerInfoMapper)
29
39
  .filter(({ multiaddrs }) => multiaddrs.length)
30
- .filter(({ id }) => !id.equals(this.peerId))
31
40
  .map(peerInfo => ({
32
41
  id: peerInfo.id.toBytes(),
33
42
  multiaddrs: peerInfo.multiaddrs.map(ma => ma.bytes)
@@ -1 +1 @@
1
- {"version":3,"file":"find-node.js","sourceRoot":"","sources":["../../../../src/rpc/handlers/find-node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAkBlD,MAAM,OAAO,eAAe;IACT,WAAW,CAAa;IACxB,cAAc,CAAg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CAAC,CAAC;YACL,SAAS,EAAE,EAAE;SACd,CAAA;QAED,IAAI,QAAQ,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACjC,IAAI,CAAC,GAAG,CAAC,+CAA+C,EAAE,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;QAC5E,CAAC;QAED,OAAO,QAAQ,CAAA;IACjB,CAAC;CACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/kad-dht",
3
- "version": "12.0.14",
3
+ "version": "12.0.15-4ad63bb79",
4
4
  "description": "JavaScript implementation of the Kad-DHT for libp2p",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/kad-dht#readme",
@@ -57,13 +57,13 @@
57
57
  "doc-check": "aegir doc-check"
58
58
  },
59
59
  "dependencies": {
60
- "@libp2p/crypto": "^4.1.0",
61
- "@libp2p/interface": "^1.3.0",
62
- "@libp2p/interface-internal": "^1.1.1",
63
- "@libp2p/peer-collections": "^5.1.11",
64
- "@libp2p/peer-id": "^4.1.0",
65
- "@libp2p/record": "^4.0.1",
66
- "@libp2p/utils": "^5.3.2",
60
+ "@libp2p/crypto": "4.1.1-4ad63bb79",
61
+ "@libp2p/interface": "1.3.1-4ad63bb79",
62
+ "@libp2p/interface-internal": "1.2.0-4ad63bb79",
63
+ "@libp2p/peer-collections": "5.2.0-4ad63bb79",
64
+ "@libp2p/peer-id": "4.1.1-4ad63bb79",
65
+ "@libp2p/record": "4.0.1-4ad63bb79",
66
+ "@libp2p/utils": "5.4.0-4ad63bb79",
67
67
  "@multiformats/multiaddr": "^12.2.1",
68
68
  "any-signal": "^4.1.1",
69
69
  "hashlru": "^2.3.0",
@@ -90,10 +90,10 @@
90
90
  "uint8arrays": "^5.0.3"
91
91
  },
92
92
  "devDependencies": {
93
- "@libp2p/interface-compliance-tests": "^5.4.2",
94
- "@libp2p/logger": "^4.0.11",
95
- "@libp2p/peer-id-factory": "^4.1.0",
96
- "@libp2p/peer-store": "^10.0.16",
93
+ "@libp2p/interface-compliance-tests": "5.4.3-4ad63bb79",
94
+ "@libp2p/logger": "4.0.12-4ad63bb79",
95
+ "@libp2p/peer-id-factory": "4.1.1-4ad63bb79",
96
+ "@libp2p/peer-store": "10.0.17-4ad63bb79",
97
97
  "@types/lodash.random": "^3.2.9",
98
98
  "@types/lodash.range": "^3.2.9",
99
99
  "@types/sinon": "^17.0.3",
package/src/constants.ts CHANGED
@@ -50,4 +50,4 @@ export const TABLE_REFRESH_INTERVAL = 5 * minute
50
50
  export const TABLE_REFRESH_QUERY_TIMEOUT = 30 * second
51
51
 
52
52
  // When a timeout is not specified, run a query for this long
53
- export const DEFAULT_QUERY_TIMEOUT = 30 * second
53
+ export const DEFAULT_QUERY_TIMEOUT = 180 * second
package/src/index.ts CHANGED
@@ -86,6 +86,7 @@ import { removePrivateAddressesMapper, removePublicAddressesMapper, passthroughM
86
86
  import type { ProvidersInit } from './providers.js'
87
87
  import type { Libp2pEvents, ComponentLogger, TypedEventTarget, Metrics, PeerId, PeerInfo, PeerStore, RoutingOptions } from '@libp2p/interface'
88
88
  import type { AddressManager, ConnectionManager, Registrar } from '@libp2p/interface-internal'
89
+ import type { AdaptiveTimeoutInit } from '@libp2p/utils/src/adaptive-timeout.js'
89
90
  import type { Datastore } from 'interface-datastore'
90
91
  import type { CID } from 'multiformats/cid'
91
92
  import type { ProgressEvent } from 'progress-events'
@@ -405,6 +406,11 @@ export interface KadDHTInit {
405
406
  * with this filter.
406
407
  */
407
408
  peerInfoMapper?(peer: PeerInfo): PeerInfo
409
+
410
+ /**
411
+ * Dynamic network timeout settings for sending messages to peers
412
+ */
413
+ networkDialTimeout?: Omit<AdaptiveTimeoutInit, 'metricsName' | 'metrics'>
408
414
  }
409
415
 
410
416
  export interface KadDHTComponents {
package/src/kad-dht.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CodeError, CustomEvent, TypedEventEmitter, contentRoutingSymbol, peerDiscoverySymbol, peerRoutingSymbol } from '@libp2p/interface'
1
+ import { CodeError, CustomEvent, TypedEventEmitter, contentRoutingSymbol, peerDiscoverySymbol, peerRoutingSymbol, start, stop } from '@libp2p/interface'
2
2
  import drain from 'it-drain'
3
3
  import pDefer from 'p-defer'
4
4
  import { PROTOCOL } from './constants.js'
@@ -379,16 +379,15 @@ export class KadDHT extends TypedEventEmitter<PeerDiscoveryEvents> implements Ka
379
379
  // Only respond to queries when not in client mode
380
380
  await this.setMode(this.clientMode ? 'client' : 'server')
381
381
 
382
- this.querySelf.start()
383
-
384
- await Promise.all([
385
- this.providers.start(),
386
- this.queryManager.start(),
387
- this.network.start(),
388
- this.routingTable.start(),
389
- this.topologyListener.start(),
390
- this.routingTableRefresh.start()
391
- ])
382
+ await start(
383
+ this.querySelf,
384
+ this.providers,
385
+ this.queryManager,
386
+ this.network,
387
+ this.routingTable,
388
+ this.topologyListener,
389
+ this.routingTableRefresh
390
+ )
392
391
  }
393
392
 
394
393
  /**
@@ -398,16 +397,15 @@ export class KadDHT extends TypedEventEmitter<PeerDiscoveryEvents> implements Ka
398
397
  async stop (): Promise<void> {
399
398
  this.running = false
400
399
 
401
- this.querySelf.stop()
402
-
403
- await Promise.all([
404
- this.providers.stop(),
405
- this.queryManager.stop(),
406
- this.network.stop(),
407
- this.routingTable.stop(),
408
- this.routingTableRefresh.stop(),
409
- this.topologyListener.stop()
410
- ])
400
+ await stop(
401
+ this.querySelf,
402
+ this.providers,
403
+ this.queryManager,
404
+ this.network,
405
+ this.routingTable,
406
+ this.routingTableRefresh,
407
+ this.topologyListener
408
+ )
411
409
  }
412
410
 
413
411
  /**
package/src/network.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { TypedEventEmitter } from '@libp2p/interface'
2
2
  import { Libp2pRecord } from '@libp2p/record'
3
+ import { AdaptiveTimeout, type AdaptiveTimeoutInit } from '@libp2p/utils/adaptive-timeout'
3
4
  import { pbStream } from 'it-protobuf-stream'
4
5
  import { CodeError } from 'protons-runtime'
5
6
  import { Message } from './message/dht.js'
@@ -16,6 +17,7 @@ import type { AbortOptions, Logger, Stream, PeerId, PeerInfo, Startable, Routing
16
17
  export interface NetworkInit {
17
18
  protocol: string
18
19
  logPrefix: string
20
+ timeout?: Omit<AdaptiveTimeoutInit, 'metricsName' | 'metrics'>
19
21
  }
20
22
 
21
23
  interface NetworkEvents {
@@ -30,6 +32,7 @@ export class Network extends TypedEventEmitter<NetworkEvents> implements Startab
30
32
  private readonly protocol: string
31
33
  private running: boolean
32
34
  private readonly components: KadDHTComponents
35
+ private readonly timeout: AdaptiveTimeout
33
36
 
34
37
  /**
35
38
  * Create a new network
@@ -42,6 +45,11 @@ export class Network extends TypedEventEmitter<NetworkEvents> implements Startab
42
45
  this.log = components.logger.forComponent(`${init.logPrefix}:network`)
43
46
  this.running = false
44
47
  this.protocol = protocol
48
+ this.timeout = new AdaptiveTimeout({
49
+ ...(init.timeout ?? {}),
50
+ metrics: components.metrics,
51
+ metricName: `${init.logPrefix.replaceAll(':', '_')}_network_message_send_times_milliseconds`
52
+ })
45
53
  }
46
54
 
47
55
  /**
@@ -88,13 +96,24 @@ export class Network extends TypedEventEmitter<NetworkEvents> implements Startab
88
96
  yield sendQueryEvent({ to, type }, options)
89
97
 
90
98
  let stream: Stream | undefined
99
+ const signal = this.timeout.getTimeoutSignal(options)
100
+
101
+ options = {
102
+ ...options,
103
+ signal
104
+ }
91
105
 
92
106
  try {
93
107
  const connection = await this.components.connectionManager.openConnection(to, options)
94
- const stream = await connection.newStream(this.protocol, options)
95
-
108
+ stream = await connection.newStream(this.protocol, options)
96
109
  const response = await this._writeReadMessage(stream, msg, options)
97
110
 
111
+ stream.close(options)
112
+ .catch(err => {
113
+ this.log.error('error closing stream to %p', to, err)
114
+ stream?.abort(err)
115
+ })
116
+
98
117
  yield peerResponseEvent({
99
118
  from: to,
100
119
  messageType: response.type,
@@ -103,12 +122,11 @@ export class Network extends TypedEventEmitter<NetworkEvents> implements Startab
103
122
  record: response.record == null ? undefined : Libp2pRecord.deserialize(response.record)
104
123
  }, options)
105
124
  } catch (err: any) {
125
+ stream?.abort(err)
106
126
  this.log.error('could not send %s to %p', msg.type, to, err)
107
127
  yield queryErrorEvent({ from: to, error: err }, options)
108
128
  } finally {
109
- if (stream != null) {
110
- await stream.close()
111
- }
129
+ this.timeout.cleanUp(signal)
112
130
  }
113
131
  }
114
132
 
@@ -131,20 +149,31 @@ export class Network extends TypedEventEmitter<NetworkEvents> implements Startab
131
149
  yield sendQueryEvent({ to, type }, options)
132
150
 
133
151
  let stream: Stream | undefined
152
+ const signal = this.timeout.getTimeoutSignal(options)
153
+
154
+ options = {
155
+ ...options,
156
+ signal
157
+ }
134
158
 
135
159
  try {
136
160
  const connection = await this.components.connectionManager.openConnection(to, options)
137
- const stream = await connection.newStream(this.protocol, options)
161
+ stream = await connection.newStream(this.protocol, options)
138
162
 
139
163
  await this._writeMessage(stream, msg, options)
140
164
 
165
+ stream.close(options)
166
+ .catch(err => {
167
+ this.log.error('error closing stream to %p', to, err)
168
+ stream?.abort(err)
169
+ })
170
+
141
171
  yield peerResponseEvent({ from: to, messageType: type }, options)
142
172
  } catch (err: any) {
173
+ stream?.abort(err)
143
174
  yield queryErrorEvent({ from: to, error: err }, options)
144
175
  } finally {
145
- if (stream != null) {
146
- await stream.close()
147
- }
176
+ this.timeout.cleanUp(signal)
148
177
  }
149
178
  }
150
179
 
@@ -319,7 +319,7 @@ export class PeerRouting {
319
319
  if (output.length > 0) {
320
320
  this.log('getCloserPeersOffline found %d peer(s) closer to %b than %p', output.length, key, closerThan)
321
321
  } else {
322
- this.log('getCloserPeersOffline could not find peer closer to %b than %p', key, closerThan)
322
+ this.log('getCloserPeersOffline could not find peer closer to %b than %p with %d peers in the routing table', key, closerThan, this.routingTable.size)
323
323
  }
324
324
 
325
325
  return output
@@ -110,6 +110,15 @@ export class RoutingTable extends TypedEventEmitter<RoutingTableEvents> implemen
110
110
  })
111
111
  })
112
112
 
113
+ // add existing peers from the peer store to routing table
114
+ for (const peer of await this.components.peerStore.all()) {
115
+ if (peer.protocols.includes(this.protocol)) {
116
+ const id = await utils.convertPeerId(peer.id)
117
+
118
+ this.kb.add({ id, peer: peer.id })
119
+ }
120
+ }
121
+
113
122
  // tag kad-close peers
114
123
  this._tagPeers(kBuck)
115
124
  }
@@ -51,7 +51,7 @@ export class RoutingTableRefresh {
51
51
  this.refreshTable = this.refreshTable.bind(this)
52
52
  }
53
53
 
54
- async start (): Promise<void> {
54
+ async afterStart (): Promise<void> {
55
55
  this.log(`refreshing routing table every ${this.refreshInterval}ms`)
56
56
  this.refreshTable(true)
57
57
  }
@@ -1,10 +1,13 @@
1
1
  import { CodeError } from '@libp2p/interface'
2
+ import { protocols } from '@multiformats/multiaddr'
3
+ import { equals as uint8ArrayEquals } from 'uint8arrays'
2
4
  import { MessageType } from '../../message/dht.js'
3
5
  import type { PeerInfoMapper } from '../../index.js'
4
6
  import type { Message } from '../../message/dht.js'
5
7
  import type { PeerRouting } from '../../peer-routing/index.js'
6
8
  import type { DHTMessageHandler } from '../index.js'
7
9
  import type { ComponentLogger, Logger, PeerId, PeerInfo } from '@libp2p/interface'
10
+ import type { AddressManager } from '@libp2p/interface-internal'
8
11
 
9
12
  export interface FindNodeHandlerInit {
10
13
  peerRouting: PeerRouting
@@ -14,6 +17,7 @@ export interface FindNodeHandlerInit {
14
17
 
15
18
  export interface FindNodeHandlerComponents {
16
19
  peerId: PeerId
20
+ addressManager: AddressManager
17
21
  logger: ComponentLogger
18
22
  }
19
23
 
@@ -21,6 +25,7 @@ export class FindNodeHandler implements DHTMessageHandler {
21
25
  private readonly peerRouting: PeerRouting
22
26
  private readonly peerInfoMapper: PeerInfoMapper
23
27
  private readonly peerId: PeerId
28
+ private readonly addressManager: AddressManager
24
29
  private readonly log: Logger
25
30
 
26
31
  constructor (components: FindNodeHandlerComponents, init: FindNodeHandlerInit) {
@@ -28,6 +33,7 @@ export class FindNodeHandler implements DHTMessageHandler {
28
33
 
29
34
  this.log = components.logger.forComponent(`${logPrefix}:rpc:handlers:find-node`)
30
35
  this.peerId = components.peerId
36
+ this.addressManager = components.addressManager
31
37
  this.peerRouting = peerRouting
32
38
  this.peerInfoMapper = init.peerInfoMapper
33
39
  }
@@ -44,13 +50,19 @@ export class FindNodeHandler implements DHTMessageHandler {
44
50
 
45
51
  const closer: PeerInfo[] = await this.peerRouting.getCloserPeersOffline(msg.key, peerId)
46
52
 
53
+ if (uint8ArrayEquals(this.peerId.toBytes(), msg.key)) {
54
+ closer.push({
55
+ id: this.peerId,
56
+ multiaddrs: this.addressManager.getAddresses().map(ma => ma.decapsulateCode(protocols('p2p').code))
57
+ })
58
+ }
59
+
47
60
  const response: Message = {
48
61
  type: MessageType.FIND_NODE,
49
62
  clusterLevel: msg.clusterLevel,
50
63
  closer: closer
51
64
  .map(this.peerInfoMapper)
52
65
  .filter(({ multiaddrs }) => multiaddrs.length)
53
- .filter(({ id }) => !id.equals(this.peerId))
54
66
  .map(peerInfo => ({
55
67
  id: peerInfo.id.toBytes(),
56
68
  multiaddrs: peerInfo.multiaddrs.map(ma => ma.bytes)
@@ -1,55 +0,0 @@
1
- {
2
- "codec": "https://libp2p.github.io/js-libp2p/functions/_libp2p_kad_dht.MessageType.codec.html",
3
- "EventTypes": "https://libp2p.github.io/js-libp2p/enums/_libp2p_kad_dht.EventTypes.html",
4
- ".:EventTypes": "https://libp2p.github.io/js-libp2p/enums/_libp2p_kad_dht.EventTypes.html",
5
- "MessageType": "https://libp2p.github.io/js-libp2p/enums/_libp2p_kad_dht.MessageType-1.html",
6
- "AddPeerEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.AddPeerEvent.html",
7
- ".:AddPeerEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.AddPeerEvent.html",
8
- "DHTRecord": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.DHTRecord.html",
9
- ".:DHTRecord": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.DHTRecord.html",
10
- "DialPeerEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.DialPeerEvent.html",
11
- ".:DialPeerEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.DialPeerEvent.html",
12
- "FinalPeerEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.FinalPeerEvent.html",
13
- ".:FinalPeerEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.FinalPeerEvent.html",
14
- "KadDHT": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.KadDHT.html",
15
- ".:KadDHT": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.KadDHT.html",
16
- "KadDHTComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.KadDHTComponents.html",
17
- ".:KadDHTComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.KadDHTComponents.html",
18
- "KadDHTInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.KadDHTInit.html",
19
- ".:KadDHTInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.KadDHTInit.html",
20
- "PeerInfoMapper": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.PeerInfoMapper.html",
21
- ".:PeerInfoMapper": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.PeerInfoMapper.html",
22
- "PeerResponseEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.PeerResponseEvent.html",
23
- ".:PeerResponseEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.PeerResponseEvent.html",
24
- "ProviderEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.ProviderEvent.html",
25
- ".:ProviderEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.ProviderEvent.html",
26
- "QueryErrorEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.QueryErrorEvent.html",
27
- ".:QueryErrorEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.QueryErrorEvent.html",
28
- "RoutingTable": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.RoutingTable.html",
29
- ".:RoutingTable": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.RoutingTable.html",
30
- "SelectFn": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.SelectFn.html",
31
- ".:SelectFn": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.SelectFn.html",
32
- "SendQueryEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.SendQueryEvent.html",
33
- ".:SendQueryEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.SendQueryEvent.html",
34
- "SingleKadDHT": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.SingleKadDHT.html",
35
- ".:SingleKadDHT": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.SingleKadDHT.html",
36
- "ValidateFn": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.ValidateFn.html",
37
- ".:ValidateFn": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.ValidateFn.html",
38
- "ValueEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.ValueEvent.html",
39
- ".:ValueEvent": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_kad_dht.ValueEvent.html",
40
- "DHTProgressEvents": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.DHTProgressEvents.html",
41
- ".:DHTProgressEvents": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.DHTProgressEvents.html",
42
- "MessageName": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.MessageName.html",
43
- ".:MessageName": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.MessageName.html",
44
- "QueryEvent": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.QueryEvent.html",
45
- ".:QueryEvent": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.QueryEvent.html",
46
- "Selectors": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.Selectors.html",
47
- ".:Selectors": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.Selectors.html",
48
- "Validators": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.Validators.html",
49
- ".:Validators": "https://libp2p.github.io/js-libp2p/types/_libp2p_kad_dht.Validators.html",
50
- "kadDHT": "https://libp2p.github.io/js-libp2p/functions/_libp2p_kad_dht.kadDHT-1.html",
51
- ".:kadDHT": "https://libp2p.github.io/js-libp2p/functions/_libp2p_kad_dht.kadDHT-1.html",
52
- "passthroughMapper": "https://libp2p.github.io/js-libp2p/functions/_libp2p_kad_dht.passthroughMapper.html",
53
- "removePrivateAddressesMapper": "https://libp2p.github.io/js-libp2p/functions/_libp2p_kad_dht.removePrivateAddressesMapper.html",
54
- "removePublicAddressesMapper": "https://libp2p.github.io/js-libp2p/functions/_libp2p_kad_dht.removePublicAddressesMapper.html"
55
- }