@libp2p/kad-dht 4.0.2 → 5.0.0

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 (102) hide show
  1. package/README.md +10 -12
  2. package/dist/src/content-fetching/index.d.ts +4 -5
  3. package/dist/src/content-fetching/index.d.ts.map +1 -1
  4. package/dist/src/content-fetching/index.js +8 -12
  5. package/dist/src/content-fetching/index.js.map +1 -1
  6. package/dist/src/content-routing/index.d.ts +4 -5
  7. package/dist/src/content-routing/index.d.ts.map +1 -1
  8. package/dist/src/content-routing/index.js +7 -11
  9. package/dist/src/content-routing/index.js.map +1 -1
  10. package/dist/src/dual-kad-dht.d.ts +6 -7
  11. package/dist/src/dual-kad-dht.d.ts.map +1 -1
  12. package/dist/src/dual-kad-dht.js +3 -9
  13. package/dist/src/dual-kad-dht.js.map +1 -1
  14. package/dist/src/index.d.ts +16 -2
  15. package/dist/src/index.d.ts.map +1 -1
  16. package/dist/src/index.js +7 -4
  17. package/dist/src/index.js.map +1 -1
  18. package/dist/src/kad-dht.d.ts +4 -6
  19. package/dist/src/kad-dht.d.ts.map +1 -1
  20. package/dist/src/kad-dht.js +16 -31
  21. package/dist/src/kad-dht.js.map +1 -1
  22. package/dist/src/network.d.ts +4 -5
  23. package/dist/src/network.d.ts.map +1 -1
  24. package/dist/src/network.js +4 -8
  25. package/dist/src/network.js.map +1 -1
  26. package/dist/src/peer-routing/index.d.ts +4 -5
  27. package/dist/src/peer-routing/index.d.ts.map +1 -1
  28. package/dist/src/peer-routing/index.js +12 -16
  29. package/dist/src/peer-routing/index.js.map +1 -1
  30. package/dist/src/providers.d.ts +7 -5
  31. package/dist/src/providers.d.ts.map +1 -1
  32. package/dist/src/providers.js +6 -10
  33. package/dist/src/providers.js.map +1 -1
  34. package/dist/src/query/manager.d.ts +8 -5
  35. package/dist/src/query/manager.d.ts.map +1 -1
  36. package/dist/src/query/manager.js +5 -9
  37. package/dist/src/query/manager.js.map +1 -1
  38. package/dist/src/query-self.d.ts +4 -5
  39. package/dist/src/query-self.d.ts.map +1 -1
  40. package/dist/src/query-self.js +3 -7
  41. package/dist/src/query-self.js.map +1 -1
  42. package/dist/src/routing-table/index.d.ts +12 -5
  43. package/dist/src/routing-table/index.d.ts.map +1 -1
  44. package/dist/src/routing-table/index.js +11 -15
  45. package/dist/src/routing-table/index.js.map +1 -1
  46. package/dist/src/routing-table/refresh.d.ts +1 -3
  47. package/dist/src/routing-table/refresh.d.ts.map +1 -1
  48. package/dist/src/routing-table/refresh.js +0 -2
  49. package/dist/src/routing-table/refresh.js.map +1 -1
  50. package/dist/src/rpc/handlers/add-provider.d.ts +1 -3
  51. package/dist/src/rpc/handlers/add-provider.d.ts.map +1 -1
  52. package/dist/src/rpc/handlers/add-provider.js +0 -2
  53. package/dist/src/rpc/handlers/add-provider.js.map +1 -1
  54. package/dist/src/rpc/handlers/find-node.d.ts +8 -6
  55. package/dist/src/rpc/handlers/find-node.d.ts.map +1 -1
  56. package/dist/src/rpc/handlers/find-node.js +5 -9
  57. package/dist/src/rpc/handlers/find-node.js.map +1 -1
  58. package/dist/src/rpc/handlers/get-providers.d.ts +7 -5
  59. package/dist/src/rpc/handlers/get-providers.d.ts.map +1 -1
  60. package/dist/src/rpc/handlers/get-providers.js +3 -7
  61. package/dist/src/rpc/handlers/get-providers.js.map +1 -1
  62. package/dist/src/rpc/handlers/get-value.d.ts +9 -5
  63. package/dist/src/rpc/handlers/get-value.d.ts.map +1 -1
  64. package/dist/src/rpc/handlers/get-value.js +5 -9
  65. package/dist/src/rpc/handlers/get-value.js.map +1 -1
  66. package/dist/src/rpc/handlers/ping.d.ts +1 -3
  67. package/dist/src/rpc/handlers/ping.d.ts.map +1 -1
  68. package/dist/src/rpc/handlers/ping.js +0 -2
  69. package/dist/src/rpc/handlers/ping.js.map +1 -1
  70. package/dist/src/rpc/handlers/put-value.d.ts +7 -5
  71. package/dist/src/rpc/handlers/put-value.d.ts.map +1 -1
  72. package/dist/src/rpc/handlers/put-value.js +3 -7
  73. package/dist/src/rpc/handlers/put-value.js.map +1 -1
  74. package/dist/src/rpc/index.d.ts +9 -5
  75. package/dist/src/rpc/index.d.ts.map +1 -1
  76. package/dist/src/rpc/index.js +5 -10
  77. package/dist/src/rpc/index.js.map +1 -1
  78. package/dist/src/topology-listener.d.ts +4 -5
  79. package/dist/src/topology-listener.d.ts.map +1 -1
  80. package/dist/src/topology-listener.js +4 -8
  81. package/dist/src/topology-listener.js.map +1 -1
  82. package/package.json +8 -8
  83. package/src/content-fetching/index.ts +12 -14
  84. package/src/content-routing/index.ts +10 -13
  85. package/src/dual-kad-dht.ts +8 -13
  86. package/src/index.ts +25 -4
  87. package/src/kad-dht.ts +19 -35
  88. package/src/network.ts +7 -10
  89. package/src/peer-routing/index.ts +15 -18
  90. package/src/providers.ts +12 -12
  91. package/src/query/manager.ts +14 -11
  92. package/src/query-self.ts +6 -9
  93. package/src/routing-table/index.ts +23 -17
  94. package/src/routing-table/refresh.ts +1 -6
  95. package/src/rpc/handlers/add-provider.ts +1 -6
  96. package/src/rpc/handlers/find-node.ts +14 -12
  97. package/src/rpc/handlers/get-providers.ts +10 -9
  98. package/src/rpc/handlers/get-value.ts +14 -11
  99. package/src/rpc/handlers/ping.ts +1 -6
  100. package/src/rpc/handlers/put-value.ts +10 -9
  101. package/src/rpc/index.ts +15 -19
  102. package/src/topology-listener.ts +7 -10
@@ -24,7 +24,7 @@ import type { Network } from '../network.js'
24
24
  import type { Logger } from '@libp2p/logger'
25
25
  import type { AbortOptions } from '@libp2p/interfaces'
26
26
  import type { QueryFunc } from '../query/types.js'
27
- import { Components, Initializable } from '@libp2p/components'
27
+ import type { KadDHTComponents } from '../index.js'
28
28
 
29
29
  export interface ContentFetchingInit {
30
30
  validators: Validators
@@ -36,9 +36,9 @@ export interface ContentFetchingInit {
36
36
  lan: boolean
37
37
  }
38
38
 
39
- export class ContentFetching implements Initializable {
39
+ export class ContentFetching {
40
40
  private readonly log: Logger
41
- private components: Components = new Components()
41
+ private readonly components: KadDHTComponents
42
42
  private readonly validators: Validators
43
43
  private readonly selectors: Selectors
44
44
  private readonly peerRouting: PeerRouting
@@ -46,8 +46,10 @@ export class ContentFetching implements Initializable {
46
46
  private readonly routingTable: RoutingTable
47
47
  private readonly network: Network
48
48
 
49
- constructor (init: ContentFetchingInit) {
49
+ constructor (components: KadDHTComponents, init: ContentFetchingInit) {
50
50
  const { validators, selectors, peerRouting, queryManager, routingTable, network, lan } = init
51
+
52
+ this.components = components
51
53
  this.log = logger(`libp2p:kad-dht:${lan ? 'lan' : 'wan'}:content-fetching`)
52
54
  this.validators = validators
53
55
  this.selectors = selectors
@@ -57,13 +59,9 @@ export class ContentFetching implements Initializable {
57
59
  this.network = network
58
60
  }
59
61
 
60
- init (components: Components): void {
61
- this.components = components
62
- }
63
-
64
62
  async putLocal (key: Uint8Array, rec: Uint8Array) { // eslint-disable-line require-await
65
63
  const dsKey = bufferToRecordKey(key)
66
- await this.components.getDatastore().put(dsKey, rec)
64
+ await this.components.datastore.put(dsKey, rec)
67
65
  }
68
66
 
69
67
  /**
@@ -77,7 +75,7 @@ export class ContentFetching implements Initializable {
77
75
 
78
76
  this.log('fetching record for key %k', dsKey)
79
77
 
80
- const raw = await this.components.getDatastore().get(dsKey)
78
+ const raw = await this.components.datastore.get(dsKey)
81
79
  this.log('found %k in local datastore', dsKey)
82
80
 
83
81
  const rec = Libp2pRecord.deserialize(raw)
@@ -102,11 +100,11 @@ export class ContentFetching implements Initializable {
102
100
  }
103
101
 
104
102
  // correct ourself
105
- if (this.components.getPeerId().equals(from)) {
103
+ if (this.components.peerId.equals(from)) {
106
104
  try {
107
105
  const dsKey = bufferToRecordKey(key)
108
106
  this.log(`Storing corrected record for key ${dsKey.toString()}`)
109
- await this.components.getDatastore().put(dsKey, fixupRec.subarray())
107
+ await this.components.datastore.put(dsKey, fixupRec.subarray())
110
108
  } catch (err: any) {
111
109
  this.log.error('Failed error correcting self', err)
112
110
  }
@@ -147,7 +145,7 @@ export class ContentFetching implements Initializable {
147
145
  // store the record locally
148
146
  const dsKey = bufferToRecordKey(key)
149
147
  this.log(`storing record for key ${dsKey.toString()}`)
150
- await this.components.getDatastore().put(dsKey, record.subarray())
148
+ await this.components.datastore.put(dsKey, record.subarray())
151
149
 
152
150
  // put record to the closest peers
153
151
  yield * pipe(
@@ -246,7 +244,7 @@ export class ContentFetching implements Initializable {
246
244
 
247
245
  yield valueEvent({
248
246
  value: localRec.value,
249
- from: this.components.getPeerId()
247
+ from: this.components.peerId
250
248
  })
251
249
  } catch (err: any) {
252
250
  this.log('error getting local value for %b', key, err)
@@ -22,7 +22,7 @@ import type { CID } from 'multiformats/cid'
22
22
  import type { AbortOptions } from '@libp2p/interfaces'
23
23
  import type { Multiaddr } from '@multiformats/multiaddr'
24
24
  import type { PeerInfo } from '@libp2p/interface-peer-info'
25
- import { Components, Initializable } from '@libp2p/components'
25
+ import type { KadDHTComponents } from '../index.js'
26
26
 
27
27
  export interface ContentRoutingInit {
28
28
  network: Network
@@ -33,18 +33,19 @@ export interface ContentRoutingInit {
33
33
  lan: boolean
34
34
  }
35
35
 
36
- export class ContentRouting implements Initializable {
36
+ export class ContentRouting {
37
37
  private readonly log: Logger
38
- private components: Components = new Components()
38
+ private readonly components: KadDHTComponents
39
39
  private readonly network: Network
40
40
  private readonly peerRouting: PeerRouting
41
41
  private readonly queryManager: QueryManager
42
42
  private readonly routingTable: RoutingTable
43
43
  private readonly providers: Providers
44
44
 
45
- constructor (init: ContentRoutingInit) {
45
+ constructor (components: KadDHTComponents, init: ContentRoutingInit) {
46
46
  const { network, peerRouting, queryManager, routingTable, providers, lan } = init
47
47
 
48
+ this.components = components
48
49
  this.log = logger(`libp2p:kad-dht:${lan ? 'lan' : 'wan'}:content-routing`)
49
50
  this.network = network
50
51
  this.peerRouting = peerRouting
@@ -53,10 +54,6 @@ export class ContentRouting implements Initializable {
53
54
  this.providers = providers
54
55
  }
55
56
 
56
- init (components: Components): void {
57
- this.components = components
58
- }
59
-
60
57
  /**
61
58
  * Announce to the network that we can provide the value for a given key and
62
59
  * are contactable on the given multiaddrs
@@ -65,11 +62,11 @@ export class ContentRouting implements Initializable {
65
62
  this.log('provide %s', key)
66
63
 
67
64
  // Add peer as provider
68
- await this.providers.addProvider(key, this.components.getPeerId())
65
+ await this.providers.addProvider(key, this.components.peerId)
69
66
 
70
67
  const msg = new Message(MESSAGE_TYPE.ADD_PROVIDER, key.bytes, 0)
71
68
  msg.providerPeers = [{
72
- id: this.components.getPeerId(),
69
+ id: this.components.peerId,
73
70
  multiaddrs,
74
71
  protocols: []
75
72
  }]
@@ -144,13 +141,13 @@ export class ContentRouting implements Initializable {
144
141
  for (const peerId of provs.slice(0, toFind)) {
145
142
  providers.push({
146
143
  id: peerId,
147
- multiaddrs: ((await this.components.getPeerStore().addressBook.get(peerId)) ?? []).map(address => address.multiaddr),
144
+ multiaddrs: ((await this.components.peerStore.addressBook.get(peerId)) ?? []).map(address => address.multiaddr),
148
145
  protocols: []
149
146
  })
150
147
  }
151
148
 
152
- yield peerResponseEvent({ from: this.components.getPeerId(), messageType: MESSAGE_TYPE.GET_PROVIDERS, providers })
153
- yield providerEvent({ from: this.components.getPeerId(), providers: providers })
149
+ yield peerResponseEvent({ from: this.components.peerId, messageType: MESSAGE_TYPE.GET_PROVIDERS, providers })
150
+ yield providerEvent({ from: this.components.peerId, providers: providers })
154
151
  }
155
152
 
156
153
  // All done
@@ -9,8 +9,8 @@ import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events'
9
9
  import type { CID } from 'multiformats'
10
10
  import type { PeerId } from '@libp2p/interface-peer-id'
11
11
  import type { PeerDiscoveryEvents } from '@libp2p/interface-peer-discovery'
12
- import { Components, Initializable } from '@libp2p/components'
13
12
  import { symbol } from '@libp2p/interface-peer-discovery'
13
+ import type { KadDHTComponents } from './index.js'
14
14
 
15
15
  const log = logger('libp2p:kad-dht')
16
16
 
@@ -18,14 +18,15 @@ const log = logger('libp2p:kad-dht')
18
18
  * A DHT implementation modelled after Kademlia with S/Kademlia modifications.
19
19
  * Original implementation in go: https://github.com/libp2p/go-libp2p-kad-dht.
20
20
  */
21
- export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements DualDHT, Initializable {
22
- public wan: KadDHT
23
- public lan: KadDHT
24
- public components: Components = new Components()
21
+ export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements DualDHT {
22
+ public readonly wan: KadDHT
23
+ public readonly lan: KadDHT
24
+ public readonly components: KadDHTComponents
25
25
 
26
- constructor (wan: KadDHT, lan: KadDHT) {
26
+ constructor (components: KadDHTComponents, wan: KadDHT, lan: KadDHT) {
27
27
  super()
28
28
 
29
+ this.components = components
29
30
  this.wan = wan
30
31
  this.lan = lan
31
32
 
@@ -50,12 +51,6 @@ export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements Dua
50
51
  return '@libp2p/dual-kad-dht'
51
52
  }
52
53
 
53
- init (components: Components): void {
54
- this.components = components
55
- this.wan.init(components)
56
- this.lan.init(components)
57
- }
58
-
59
54
  /**
60
55
  * Is this DHT running.
61
56
  */
@@ -146,7 +141,7 @@ export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements Dua
146
141
 
147
142
  if (!foundValue) {
148
143
  yield queryErrorEvent({
149
- from: this.components.getPeerId(),
144
+ from: this.components.peerId,
150
145
  error: errCode(new Error('Not found'), 'ERR_NOT_FOUND')
151
146
  })
152
147
  }
package/src/index.ts CHANGED
@@ -1,6 +1,13 @@
1
1
  import { KadDHT as SingleKadDHT } from './kad-dht.js'
2
2
  import { DualKadDHT } from './dual-kad-dht.js'
3
3
  import type { Selectors, Validators } from '@libp2p/interface-dht'
4
+ import type { Registrar } from '@libp2p/interface-registrar'
5
+ import type { AddressManager } from '@libp2p/interface-address-manager'
6
+ import type { PeerStore } from '@libp2p/interface-peer-store'
7
+ import type { ConnectionManager } from '@libp2p/interface-connection-manager'
8
+ import type { Metrics } from '@libp2p/interface-metrics'
9
+ import type { PeerId } from '@libp2p/interface-peer-id'
10
+ import type { Datastore } from 'interface-datastore'
4
11
 
5
12
  export interface KadDHTInit {
6
13
  /**
@@ -57,14 +64,24 @@ export interface KadDHTInit {
57
64
  maxOutboundStreams?: number
58
65
  }
59
66
 
60
- export class KadDHT extends DualKadDHT {
61
- constructor (init?: KadDHTInit) {
62
- super(new SingleKadDHT({
67
+ export interface KadDHTComponents {
68
+ peerId: PeerId
69
+ registrar: Registrar
70
+ addressManager: AddressManager
71
+ peerStore: PeerStore
72
+ metrics?: Metrics
73
+ connectionManager: ConnectionManager
74
+ datastore: Datastore
75
+ }
76
+
77
+ class KadDHT extends DualKadDHT {
78
+ constructor (components: KadDHTComponents, init?: KadDHTInit) {
79
+ super(components, new SingleKadDHT(components, {
63
80
  protocolPrefix: '/ipfs',
64
81
  ...init,
65
82
  lan: false
66
83
  }),
67
- new SingleKadDHT({
84
+ new SingleKadDHT(components, {
68
85
  protocolPrefix: '/ipfs',
69
86
  ...init,
70
87
  clientMode: false,
@@ -72,3 +89,7 @@ export class KadDHT extends DualKadDHT {
72
89
  }))
73
90
  }
74
91
  }
92
+
93
+ export function kadDht (init?: KadDHTInit): (components: KadDHTComponents) => DualKadDHT {
94
+ return (components: KadDHTComponents) => new KadDHT(components, init)
95
+ }
package/src/kad-dht.ts CHANGED
@@ -20,8 +20,7 @@ import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
20
20
  import type { PeerId } from '@libp2p/interface-peer-id'
21
21
  import type { CID } from 'multiformats/cid'
22
22
  import type { PeerDiscoveryEvents } from '@libp2p/interface-peer-discovery'
23
- import { Components, Initializable } from '@libp2p/components'
24
- import type { KadDHTInit } from './index.js'
23
+ import type { KadDHTComponents, KadDHTInit } from './index.js'
25
24
  import { validators as recordValidators } from '@libp2p/record/validators'
26
25
  import { selectors as recordSelectors } from '@libp2p/record/selectors'
27
26
  import { symbol } from '@libp2p/interface-peer-discovery'
@@ -41,14 +40,14 @@ export interface SingleKadDHTInit extends KadDHTInit {
41
40
  * A DHT implementation modelled after Kademlia with S/Kademlia modifications.
42
41
  * Original implementation in go: https://github.com/libp2p/go-libp2p-kad-dht.
43
42
  */
44
- export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT, Initializable {
43
+ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT {
45
44
  public protocol: string
46
45
  public routingTable: RoutingTable
47
46
  public providers: Providers
48
47
  public network: Network
49
48
  public peerRouting: PeerRouting
50
49
 
51
- public components: Components = new Components()
50
+ public readonly components: KadDHTComponents
52
51
  private readonly log: Logger
53
52
  private running: boolean
54
53
  private readonly kBucketSize: number
@@ -69,7 +68,7 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT, In
69
68
  /**
70
69
  * Create a new KadDHT
71
70
  */
72
- constructor (init: SingleKadDHTInit) {
71
+ constructor (components: KadDHTComponents, init: SingleKadDHTInit) {
73
72
  super()
74
73
 
75
74
  const {
@@ -87,6 +86,7 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT, In
87
86
  } = init
88
87
 
89
88
  this.running = false
89
+ this.components = components
90
90
  this.lan = Boolean(lan)
91
91
  this.log = logger(`libp2p:kad-dht:${lan === true ? 'lan' : 'wan'}`)
92
92
  this.protocol = `${protocolPrefix ?? PROTOCOL_PREFIX}${lan === true ? LAN_PREFIX : ''}${PROTOCOL_DHT}`
@@ -94,7 +94,7 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT, In
94
94
  this.clientMode = clientMode ?? true
95
95
  this.maxInboundStreams = maxInboundStreams ?? DEFAULT_MAX_INBOUND_STREAMS
96
96
  this.maxOutboundStreams = maxOutboundStreams ?? DEFAULT_MAX_OUTBOUND_STREAMS
97
- this.routingTable = new RoutingTable({
97
+ this.routingTable = new RoutingTable(components, {
98
98
  kBucketSize,
99
99
  lan: this.lan,
100
100
  pingTimeout,
@@ -102,7 +102,7 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT, In
102
102
  protocol: this.protocol
103
103
  })
104
104
 
105
- this.providers = new Providers()
105
+ this.providers = new Providers(components)
106
106
 
107
107
  this.validators = {
108
108
  ...recordValidators,
@@ -112,25 +112,25 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT, In
112
112
  ...recordSelectors,
113
113
  ...selectors
114
114
  }
115
- this.network = new Network({
115
+ this.network = new Network(components, {
116
116
  protocol: this.protocol,
117
117
  lan: this.lan
118
118
  })
119
- this.queryManager = new QueryManager({
119
+ this.queryManager = new QueryManager(components, {
120
120
  // Number of disjoint query paths to use - This is set to `kBucketSize/2` per the S/Kademlia paper
121
121
  disjointPaths: Math.ceil(this.kBucketSize / 2),
122
122
  lan
123
123
  })
124
124
 
125
125
  // DHT components
126
- this.peerRouting = new PeerRouting({
126
+ this.peerRouting = new PeerRouting(components, {
127
127
  routingTable: this.routingTable,
128
128
  network: this.network,
129
129
  validators: this.validators,
130
130
  queryManager: this.queryManager,
131
131
  lan: this.lan
132
132
  })
133
- this.contentFetching = new ContentFetching({
133
+ this.contentFetching = new ContentFetching(components, {
134
134
  validators: this.validators,
135
135
  selectors: this.selectors,
136
136
  peerRouting: this.peerRouting,
@@ -139,7 +139,7 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT, In
139
139
  network: this.network,
140
140
  lan: this.lan
141
141
  })
142
- this.contentRouting = new ContentRouting({
142
+ this.contentRouting = new ContentRouting(components, {
143
143
  network: this.network,
144
144
  peerRouting: this.peerRouting,
145
145
  queryManager: this.queryManager,
@@ -152,18 +152,18 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT, In
152
152
  routingTable: this.routingTable,
153
153
  lan: this.lan
154
154
  })
155
- this.rpc = new RPC({
155
+ this.rpc = new RPC(components, {
156
156
  routingTable: this.routingTable,
157
157
  providers: this.providers,
158
158
  peerRouting: this.peerRouting,
159
159
  validators: this.validators,
160
160
  lan: this.lan
161
161
  })
162
- this.topologyListener = new TopologyListener({
162
+ this.topologyListener = new TopologyListener(components, {
163
163
  protocol: this.protocol,
164
164
  lan: this.lan
165
165
  })
166
- this.querySelf = new QuerySelf({
166
+ this.querySelf = new QuerySelf(components, {
167
167
  peerRouting: this.peerRouting,
168
168
  interval: querySelfInterval,
169
169
  lan: this.lan
@@ -187,7 +187,7 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT, In
187
187
  const peerId = evt.detail
188
188
 
189
189
  Promise.resolve().then(async () => {
190
- const multiaddrs = await this.components.getPeerStore().addressBook.get(peerId)
190
+ const multiaddrs = await this.components.peerStore.addressBook.get(peerId)
191
191
 
192
192
  const peerData = {
193
193
  id: peerId,
@@ -210,22 +210,6 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT, In
210
210
  return '@libp2p/kad-dht'
211
211
  }
212
212
 
213
- init (components: Components): void {
214
- this.components = components
215
-
216
- this.routingTable.init(components)
217
- this.providers.init(components)
218
- this.network.init(components)
219
- this.queryManager.init(components)
220
- this.peerRouting.init(components)
221
- this.contentFetching.init(components)
222
- this.contentRouting.init(components)
223
- this.routingTableRefresh.init(components)
224
- this.rpc.init(components)
225
- this.topologyListener.init(components)
226
- this.querySelf.init(components)
227
- }
228
-
229
213
  async onPeerConnect (peerData: PeerInfo) {
230
214
  this.log('peer %p connected with protocols %s', peerData.id, peerData.protocols)
231
215
 
@@ -265,7 +249,7 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT, In
265
249
  * If 'server' this node will respond to DHT queries, if 'client' this node will not
266
250
  */
267
251
  async setMode (mode: 'client' | 'server') {
268
- await this.components.getRegistrar().unhandle(this.protocol)
252
+ await this.components.registrar.unhandle(this.protocol)
269
253
 
270
254
  if (mode === 'client') {
271
255
  this.log('enabling client mode')
@@ -273,7 +257,7 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT, In
273
257
  } else {
274
258
  this.log('enabling server mode')
275
259
  this.clientMode = false
276
- await this.components.getRegistrar().handle(this.protocol, this.rpc.onIncomingStream.bind(this.rpc), {
260
+ await this.components.registrar.handle(this.protocol, this.rpc.onIncomingStream.bind(this.rpc), {
277
261
  maxInboundStreams: this.maxInboundStreams,
278
262
  maxOutboundStreams: this.maxOutboundStreams
279
263
  })
@@ -339,7 +323,7 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT, In
339
323
  * Announce to the network that we can provide given key's value
340
324
  */
341
325
  async * provide (key: CID, options: QueryOptions = {}) { // eslint-disable-line require-await
342
- yield * this.contentRouting.provide(key, this.components.getAddressManager().getAddresses(), options)
326
+ yield * this.contentRouting.provide(key, this.components.addressManager.getAddresses(), options)
343
327
  }
344
328
 
345
329
  /**
package/src/network.ts CHANGED
@@ -18,10 +18,10 @@ import type { Startable } from '@libp2p/interfaces/startable'
18
18
  import type { Logger } from '@libp2p/logger'
19
19
  import type { Duplex } from 'it-stream-types'
20
20
  import type { PeerInfo } from '@libp2p/interface-peer-info'
21
- import { Components, Initializable } from '@libp2p/components'
22
21
  import type { Stream } from '@libp2p/interface-connection'
23
22
  import { abortableDuplex } from 'abortable-iterator'
24
23
  import type { Uint8ArrayList } from 'uint8arraylist'
24
+ import type { KadDHTComponents } from './index.js'
25
25
 
26
26
  export interface NetworkInit {
27
27
  protocol: string
@@ -35,28 +35,25 @@ interface NetworkEvents {
35
35
  /**
36
36
  * Handle network operations for the dht
37
37
  */
38
- export class Network extends EventEmitter<NetworkEvents> implements Startable, Initializable {
38
+ export class Network extends EventEmitter<NetworkEvents> implements Startable {
39
39
  private readonly log: Logger
40
40
  private readonly protocol: string
41
41
  private running: boolean
42
- private components: Components = new Components()
42
+ private readonly components: KadDHTComponents
43
43
 
44
44
  /**
45
45
  * Create a new network
46
46
  */
47
- constructor (init: NetworkInit) {
47
+ constructor (components: KadDHTComponents, init: NetworkInit) {
48
48
  super()
49
49
 
50
50
  const { protocol, lan } = init
51
+ this.components = components
51
52
  this.log = logger(`libp2p:kad-dht:${lan ? 'lan' : 'wan'}:network`)
52
53
  this.running = false
53
54
  this.protocol = protocol
54
55
  }
55
56
 
56
- init (components: Components): void {
57
- this.components = components
58
- }
59
-
60
57
  /**
61
58
  * Start the network
62
59
  */
@@ -97,7 +94,7 @@ export class Network extends EventEmitter<NetworkEvents> implements Startable, I
97
94
  let stream: Stream | undefined
98
95
 
99
96
  try {
100
- const connection = await this.components.getConnectionManager().openConnection(to, options)
97
+ const connection = await this.components.connectionManager.openConnection(to, options)
101
98
  const stream = await connection.newStream(this.protocol, options)
102
99
 
103
100
  const response = await this._writeReadMessage(stream, msg.serialize(), options)
@@ -133,7 +130,7 @@ export class Network extends EventEmitter<NetworkEvents> implements Startable, I
133
130
  let stream: Stream | undefined
134
131
 
135
132
  try {
136
- const connection = await this.components.getConnectionManager().openConnection(to, options)
133
+ const connection = await this.components.connectionManager.openConnection(to, options)
137
134
  const stream = await connection.newStream(this.protocol, options)
138
135
 
139
136
  await this._writeMessage(stream, msg.serialize(), options)
@@ -22,7 +22,7 @@ import type { AbortOptions } from '@libp2p/interfaces'
22
22
  import type { QueryFunc } from '../query/types.js'
23
23
  import type { PeerInfo } from '@libp2p/interface-peer-info'
24
24
  import type { PeerId } from '@libp2p/interface-peer-id'
25
- import { Components, Initializable } from '@libp2p/components'
25
+ import type { KadDHTComponents } from '../index.js'
26
26
 
27
27
  export interface PeerRoutingInit {
28
28
  routingTable: RoutingTable
@@ -32,17 +32,18 @@ export interface PeerRoutingInit {
32
32
  lan: boolean
33
33
  }
34
34
 
35
- export class PeerRouting implements Initializable {
36
- private components: Components = new Components()
35
+ export class PeerRouting {
36
+ private readonly components: KadDHTComponents
37
37
  private readonly log: Logger
38
38
  private readonly routingTable: RoutingTable
39
39
  private readonly network: Network
40
40
  private readonly validators: Validators
41
41
  private readonly queryManager: QueryManager
42
42
 
43
- constructor (init: PeerRoutingInit) {
43
+ constructor (components: KadDHTComponents, init: PeerRoutingInit) {
44
44
  const { routingTable, network, validators, queryManager, lan } = init
45
45
 
46
+ this.components = components
46
47
  this.routingTable = routingTable
47
48
  this.network = network
48
49
  this.validators = validators
@@ -50,10 +51,6 @@ export class PeerRouting implements Initializable {
50
51
  this.log = logger(`libp2p:kad-dht:${lan ? 'lan' : 'wan'}:peer-routing`)
51
52
  }
52
53
 
53
- init (components: Components): void {
54
- this.components = components
55
- }
56
-
57
54
  /**
58
55
  * Look if we are connected to a peer with the given id.
59
56
  * Returns its id and addresses, if found, otherwise `undefined`.
@@ -66,7 +63,7 @@ export class PeerRouting implements Initializable {
66
63
  this.log('findPeerLocal found %p in routing table', peer)
67
64
 
68
65
  try {
69
- peerData = await this.components.getPeerStore().get(p)
66
+ peerData = await this.components.peerStore.get(p)
70
67
  } catch (err: any) {
71
68
  if (err.code !== 'ERR_NOT_FOUND') {
72
69
  throw err
@@ -76,7 +73,7 @@ export class PeerRouting implements Initializable {
76
73
 
77
74
  if (peerData == null) {
78
75
  try {
79
- peerData = await this.components.getPeerStore().get(peer)
76
+ peerData = await this.components.peerStore.get(peer)
80
77
  } catch (err: any) {
81
78
  if (err.code !== 'ERR_NOT_FOUND') {
82
79
  throw err
@@ -146,7 +143,7 @@ export class PeerRouting implements Initializable {
146
143
  if (pi != null) {
147
144
  this.log('found local')
148
145
  yield finalPeerEvent({
149
- from: this.components.getPeerId(),
146
+ from: this.components.peerId,
150
147
  peer: pi
151
148
  })
152
149
  return
@@ -160,11 +157,11 @@ export class PeerRouting implements Initializable {
160
157
 
161
158
  if (match != null) {
162
159
  try {
163
- const peer = await this.components.getPeerStore().get(id)
160
+ const peer = await this.components.peerStore.get(id)
164
161
 
165
162
  this.log('found in peerStore')
166
163
  yield finalPeerEvent({
167
- from: this.components.getPeerId(),
164
+ from: this.components.peerId,
168
165
  peer: {
169
166
  id: peer.id,
170
167
  multiaddrs: peer.addresses.map((address) => address.multiaddr),
@@ -210,7 +207,7 @@ export class PeerRouting implements Initializable {
210
207
  }
211
208
 
212
209
  if (!foundPeer) {
213
- yield queryErrorEvent({ from: this.components.getPeerId(), error: errcode(new Error('Not found'), 'ERR_NOT_FOUND') })
210
+ yield queryErrorEvent({ from: this.components.peerId, error: errcode(new Error('Not found'), 'ERR_NOT_FOUND') })
214
211
  }
215
212
  }
216
213
 
@@ -246,10 +243,10 @@ export class PeerRouting implements Initializable {
246
243
 
247
244
  for (const peer of peers.peers) {
248
245
  yield finalPeerEvent({
249
- from: this.components.getPeerId(),
246
+ from: this.components.peerId,
250
247
  peer: {
251
248
  id: peer,
252
- multiaddrs: (await (this.components.getPeerStore().addressBook.get(peer)) ?? []).map(addr => addr.multiaddr),
249
+ multiaddrs: (await (this.components.peerStore.addressBook.get(peer)) ?? []).map(addr => addr.multiaddr),
253
250
  protocols: []
254
251
  }
255
252
  })
@@ -310,8 +307,8 @@ export class PeerRouting implements Initializable {
310
307
  }
311
308
 
312
309
  try {
313
- const addresses = await this.components.getPeerStore().addressBook.get(peerId)
314
- const protocols = await this.components.getPeerStore().protoBook.get(peerId)
310
+ const addresses = await this.components.peerStore.addressBook.get(peerId)
311
+ const protocols = await this.components.peerStore.protoBook.get(peerId)
315
312
 
316
313
  output.push({
317
314
  id: peerId,