@libp2p/kad-dht 7.0.1 → 7.0.2

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 (86) hide show
  1. package/dist/index.min.js +9 -9
  2. package/dist/src/content-fetching/index.d.ts +5 -5
  3. package/dist/src/content-fetching/index.d.ts.map +1 -1
  4. package/dist/src/content-fetching/index.js +2 -2
  5. package/dist/src/content-fetching/index.js.map +1 -1
  6. package/dist/src/content-routing/index.d.ts +3 -3
  7. package/dist/src/content-routing/index.d.ts.map +1 -1
  8. package/dist/src/content-routing/index.js +1 -1
  9. package/dist/src/content-routing/index.js.map +1 -1
  10. package/dist/src/dual-kad-dht.d.ts +9 -9
  11. package/dist/src/dual-kad-dht.d.ts.map +1 -1
  12. package/dist/src/kad-dht.d.ts +9 -9
  13. package/dist/src/kad-dht.d.ts.map +1 -1
  14. package/dist/src/kad-dht.js +1 -1
  15. package/dist/src/kad-dht.js.map +1 -1
  16. package/dist/src/message/dht.d.ts.map +1 -1
  17. package/dist/src/message/dht.js.map +1 -1
  18. package/dist/src/message/index.d.ts.map +1 -1
  19. package/dist/src/network.d.ts +3 -2
  20. package/dist/src/network.d.ts.map +1 -1
  21. package/dist/src/network.js.map +1 -1
  22. package/dist/src/peer-list/index.d.ts.map +1 -1
  23. package/dist/src/peer-list/peer-distance-list.d.ts.map +1 -1
  24. package/dist/src/peer-routing/index.d.ts +6 -6
  25. package/dist/src/peer-routing/index.d.ts.map +1 -1
  26. package/dist/src/peer-routing/index.js +2 -2
  27. package/dist/src/peer-routing/index.js.map +1 -1
  28. package/dist/src/providers.d.ts.map +1 -1
  29. package/dist/src/providers.js +3 -3
  30. package/dist/src/providers.js.map +1 -1
  31. package/dist/src/query/manager.d.ts +2 -2
  32. package/dist/src/query/manager.d.ts.map +1 -1
  33. package/dist/src/query/query-path.d.ts.map +1 -1
  34. package/dist/src/query/query-path.js.map +1 -1
  35. package/dist/src/query-self.d.ts.map +1 -1
  36. package/dist/src/routing-table/index.d.ts.map +1 -1
  37. package/dist/src/routing-table/index.js +1 -1
  38. package/dist/src/routing-table/index.js.map +1 -1
  39. package/dist/src/routing-table/refresh.d.ts +3 -2
  40. package/dist/src/routing-table/refresh.d.ts.map +1 -1
  41. package/dist/src/routing-table/refresh.js.map +1 -1
  42. package/dist/src/rpc/handlers/add-provider.d.ts +1 -1
  43. package/dist/src/rpc/handlers/add-provider.d.ts.map +1 -1
  44. package/dist/src/rpc/handlers/find-node.d.ts.map +1 -1
  45. package/dist/src/rpc/handlers/get-providers.d.ts +2 -1
  46. package/dist/src/rpc/handlers/get-providers.d.ts.map +1 -1
  47. package/dist/src/rpc/handlers/get-providers.js.map +1 -1
  48. package/dist/src/rpc/handlers/get-value.d.ts +1 -1
  49. package/dist/src/rpc/handlers/get-value.d.ts.map +1 -1
  50. package/dist/src/rpc/handlers/get-value.js +1 -1
  51. package/dist/src/rpc/handlers/ping.d.ts.map +1 -1
  52. package/dist/src/rpc/handlers/put-value.d.ts.map +1 -1
  53. package/dist/src/rpc/index.d.ts.map +1 -1
  54. package/dist/src/topology-listener.d.ts +1 -1
  55. package/dist/src/topology-listener.d.ts.map +1 -1
  56. package/dist/src/topology-listener.js +1 -1
  57. package/dist/src/topology-listener.js.map +1 -1
  58. package/dist/src/utils.d.ts.map +1 -1
  59. package/dist/src/utils.js +1 -1
  60. package/dist/src/utils.js.map +1 -1
  61. package/package.json +3 -5
  62. package/src/content-fetching/index.ts +9 -9
  63. package/src/content-routing/index.ts +4 -4
  64. package/src/dual-kad-dht.ts +14 -14
  65. package/src/kad-dht.ts +16 -16
  66. package/src/message/dht.ts +2 -2
  67. package/src/message/index.ts +5 -5
  68. package/src/network.ts +8 -7
  69. package/src/peer-list/index.ts +5 -5
  70. package/src/peer-list/peer-distance-list.ts +4 -4
  71. package/src/peer-routing/index.ts +10 -10
  72. package/src/providers.ts +14 -14
  73. package/src/query/manager.ts +5 -5
  74. package/src/query/query-path.ts +4 -4
  75. package/src/query-self.ts +4 -4
  76. package/src/routing-table/index.ts +10 -10
  77. package/src/routing-table/refresh.ts +11 -10
  78. package/src/rpc/handlers/add-provider.ts +1 -1
  79. package/src/rpc/handlers/find-node.ts +1 -1
  80. package/src/rpc/handlers/get-providers.ts +4 -3
  81. package/src/rpc/handlers/get-value.ts +3 -3
  82. package/src/rpc/handlers/ping.ts +1 -1
  83. package/src/rpc/handlers/put-value.ts +1 -1
  84. package/src/rpc/index.ts +2 -2
  85. package/src/topology-listener.ts +3 -3
  86. package/src/utils.ts +12 -12
@@ -16,7 +16,7 @@ import {
16
16
  } from '../constants.js'
17
17
  import { createPutRecord, convertBuffer, bufferToRecordKey } from '../utils.js'
18
18
  import { logger } from '@libp2p/logger'
19
- import type { Validators, Selectors, ValueEvent, QueryOptions } from '@libp2p/interface-dht'
19
+ import type { Validators, Selectors, ValueEvent, QueryOptions, QueryEvent } from '@libp2p/interface-dht'
20
20
  import type { PeerRouting } from '../peer-routing/index.js'
21
21
  import type { QueryManager } from '../query/manager.js'
22
22
  import type { RoutingTable } from '../routing-table/index.js'
@@ -59,7 +59,7 @@ export class ContentFetching {
59
59
  this.network = network
60
60
  }
61
61
 
62
- async putLocal (key: Uint8Array, rec: Uint8Array) { // eslint-disable-line require-await
62
+ async putLocal (key: Uint8Array, rec: Uint8Array): Promise<void> {
63
63
  const dsKey = bufferToRecordKey(key)
64
64
  await this.components.datastore.put(dsKey, rec)
65
65
  }
@@ -68,7 +68,7 @@ export class ContentFetching {
68
68
  * Attempt to retrieve the value for the given key from
69
69
  * the local datastore
70
70
  */
71
- async getLocal (key: Uint8Array) {
71
+ async getLocal (key: Uint8Array): Promise<Libp2pRecord> {
72
72
  this.log('getLocal %b', key)
73
73
 
74
74
  const dsKey = bufferToRecordKey(key)
@@ -88,9 +88,9 @@ export class ContentFetching {
88
88
  /**
89
89
  * Send the best record found to any peers that have an out of date record
90
90
  */
91
- async * sendCorrectionRecord (key: Uint8Array, vals: ValueEvent[], best: Uint8Array, options: AbortOptions = {}) {
91
+ async * sendCorrectionRecord (key: Uint8Array, vals: ValueEvent[], best: Uint8Array, options: AbortOptions = {}): AsyncGenerator<QueryEvent> {
92
92
  this.log('sendCorrection for %b', key)
93
- const fixupRec = await createPutRecord(key, best)
93
+ const fixupRec = createPutRecord(key, best)
94
94
 
95
95
  for (const { value, from } of vals) {
96
96
  // no need to do anything
@@ -136,11 +136,11 @@ export class ContentFetching {
136
136
  /**
137
137
  * Store the given key/value pair in the DHT
138
138
  */
139
- async * put (key: Uint8Array, value: Uint8Array, options: AbortOptions = {}) {
139
+ async * put (key: Uint8Array, value: Uint8Array, options: AbortOptions = {}): AsyncGenerator<unknown, void, undefined> {
140
140
  this.log('put key %b value %b', key, value)
141
141
 
142
142
  // create record in the dht format
143
- const record = await createPutRecord(key, value)
143
+ const record = createPutRecord(key, value)
144
144
 
145
145
  // store the record locally
146
146
  const dsKey = bufferToRecordKey(key)
@@ -192,7 +192,7 @@ export class ContentFetching {
192
192
  /**
193
193
  * Get the value to the given key
194
194
  */
195
- async * get (key: Uint8Array, options: QueryOptions = {}) {
195
+ async * get (key: Uint8Array, options: QueryOptions = {}): AsyncGenerator<QueryEvent | ValueEvent> {
196
196
  this.log('get %b', key)
197
197
 
198
198
  const vals: ValueEvent[] = []
@@ -236,7 +236,7 @@ export class ContentFetching {
236
236
  /**
237
237
  * Get the `n` values to the given key without sorting
238
238
  */
239
- async * getMany (key: Uint8Array, options: QueryOptions = {}) {
239
+ async * getMany (key: Uint8Array, options: QueryOptions = {}): AsyncGenerator<QueryEvent> {
240
240
  this.log('getMany values for %b', key)
241
241
 
242
242
  try {
@@ -10,7 +10,7 @@ import {
10
10
  providerEvent
11
11
  } from '../query/events.js'
12
12
  import { logger } from '@libp2p/logger'
13
- import type { QueryEvent, QueryOptions } from '@libp2p/interface-dht'
13
+ import type { PeerResponseEvent, ProviderEvent, QueryEvent, QueryOptions } from '@libp2p/interface-dht'
14
14
  import type { PeerRouting } from '../peer-routing/index.js'
15
15
  import type { QueryManager } from '../query/manager.js'
16
16
  import type { RoutingTable } from '../routing-table/index.js'
@@ -58,7 +58,7 @@ export class ContentRouting {
58
58
  * Announce to the network that we can provide the value for a given key and
59
59
  * are contactable on the given multiaddrs
60
60
  */
61
- async * provide (key: CID, multiaddrs: Multiaddr[], options: AbortOptions = {}) {
61
+ async * provide (key: CID, multiaddrs: Multiaddr[], options: AbortOptions = {}): AsyncGenerator<QueryEvent, void, undefined> {
62
62
  this.log('provide %s', key)
63
63
 
64
64
  // Add peer as provider
@@ -124,7 +124,7 @@ export class ContentRouting {
124
124
  /**
125
125
  * Search the dht for up to `K` providers of the given CID.
126
126
  */
127
- async * findProviders (key: CID, options: QueryOptions) {
127
+ async * findProviders (key: CID, options: QueryOptions): AsyncGenerator<PeerResponseEvent | ProviderEvent | QueryEvent> {
128
128
  const toFind = this.routingTable.kBucketSize
129
129
  const target = key.multihash.bytes
130
130
  const id = await convertBuffer(target)
@@ -147,7 +147,7 @@ export class ContentRouting {
147
147
  }
148
148
 
149
149
  yield peerResponseEvent({ from: this.components.peerId, messageType: MESSAGE_TYPE.GET_PROVIDERS, providers })
150
- yield providerEvent({ from: this.components.peerId, providers: providers })
150
+ yield providerEvent({ from: this.components.peerId, providers })
151
151
  }
152
152
 
153
153
  // All done
@@ -3,7 +3,7 @@ import { CodeError } from '@libp2p/interfaces/errors'
3
3
  import merge from 'it-merge'
4
4
  import { queryErrorEvent } from './query/events.js'
5
5
  import type { KadDHT } from './kad-dht.js'
6
- import type { DualDHT, QueryOptions } from '@libp2p/interface-dht'
6
+ import type { DualDHT, QueryEvent, QueryOptions } from '@libp2p/interface-dht'
7
7
  import type { AbortOptions } from '@libp2p/interfaces'
8
8
  import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events'
9
9
  import type { CID } from 'multiformats'
@@ -47,35 +47,35 @@ export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements Dua
47
47
  return true
48
48
  }
49
49
 
50
- get [Symbol.toStringTag] () {
50
+ get [Symbol.toStringTag] (): '@libp2p/dual-kad-dht' {
51
51
  return '@libp2p/dual-kad-dht'
52
52
  }
53
53
 
54
54
  /**
55
55
  * Is this DHT running.
56
56
  */
57
- isStarted () {
57
+ isStarted (): boolean {
58
58
  return this.wan.isStarted() && this.lan.isStarted()
59
59
  }
60
60
 
61
61
  /**
62
62
  * If 'server' this node will respond to DHT queries, if 'client' this node will not
63
63
  */
64
- async getMode () {
64
+ async getMode (): Promise<'client' | 'server'> {
65
65
  return await this.wan.getMode()
66
66
  }
67
67
 
68
68
  /**
69
69
  * If 'server' this node will respond to DHT queries, if 'client' this node will not
70
70
  */
71
- async setMode (mode: 'client' | 'server') {
71
+ async setMode (mode: 'client' | 'server'): Promise<void> {
72
72
  await this.wan.setMode(mode)
73
73
  }
74
74
 
75
75
  /**
76
76
  * Start listening to incoming connections.
77
77
  */
78
- async start () {
78
+ async start (): Promise<void> {
79
79
  await Promise.all([
80
80
  this.lan.start(),
81
81
  this.wan.start()
@@ -86,7 +86,7 @@ export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements Dua
86
86
  * Stop accepting incoming connections and sending outgoing
87
87
  * messages.
88
88
  */
89
- async stop () {
89
+ async stop (): Promise<void> {
90
90
  await Promise.all([
91
91
  this.lan.stop(),
92
92
  this.wan.stop()
@@ -96,7 +96,7 @@ export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements Dua
96
96
  /**
97
97
  * Store the given key/value pair in the DHT
98
98
  */
99
- async * put (key: Uint8Array, value: Uint8Array, options: QueryOptions = {}) {
99
+ async * put (key: Uint8Array, value: Uint8Array, options: QueryOptions = {}): AsyncGenerator<QueryEvent> {
100
100
  for await (const event of merge(
101
101
  this.lan.put(key, value, options),
102
102
  this.wan.put(key, value, options)
@@ -108,7 +108,7 @@ export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements Dua
108
108
  /**
109
109
  * Get the value that corresponds to the passed key
110
110
  */
111
- async * get (key: Uint8Array, options: QueryOptions = {}) { // eslint-disable-line require-await
111
+ async * get (key: Uint8Array, options: QueryOptions = {}): AsyncGenerator<QueryEvent> {
112
112
  let queriedPeers = false
113
113
  let foundValue = false
114
114
 
@@ -152,7 +152,7 @@ export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements Dua
152
152
  /**
153
153
  * Announce to the network that we can provide given key's value
154
154
  */
155
- async * provide (key: CID, options: AbortOptions = {}) { // eslint-disable-line require-await
155
+ async * provide (key: CID, options: AbortOptions = {}): AsyncGenerator<QueryEvent> {
156
156
  let sent = 0
157
157
  let success = 0
158
158
  const errors = []
@@ -194,7 +194,7 @@ export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements Dua
194
194
  /**
195
195
  * Search the dht for up to `K` providers of the given CID
196
196
  */
197
- async * findProviders (key: CID, options: QueryOptions = {}) {
197
+ async * findProviders (key: CID, options: QueryOptions = {}): AsyncGenerator<QueryEvent, void, undefined> {
198
198
  yield * merge(
199
199
  this.lan.findProviders(key, options),
200
200
  this.wan.findProviders(key, options)
@@ -206,7 +206,7 @@ export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements Dua
206
206
  /**
207
207
  * Search for a peer with the given ID
208
208
  */
209
- async * findPeer (id: PeerId, options: QueryOptions = {}) {
209
+ async * findPeer (id: PeerId, options: QueryOptions = {}): AsyncGenerator<QueryEvent> {
210
210
  let queriedPeers = false
211
211
 
212
212
  for await (const event of merge(
@@ -228,14 +228,14 @@ export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements Dua
228
228
  /**
229
229
  * Kademlia 'node lookup' operation
230
230
  */
231
- async * getClosestPeers (key: Uint8Array, options: QueryOptions = {}) {
231
+ async * getClosestPeers (key: Uint8Array, options: QueryOptions = {}): AsyncGenerator<QueryEvent, void, undefined> {
232
232
  yield * merge(
233
233
  this.lan.getClosestPeers(key, options),
234
234
  this.wan.getClosestPeers(key, options)
235
235
  )
236
236
  }
237
237
 
238
- async refreshRoutingTable () {
238
+ async refreshRoutingTable (): Promise<void> {
239
239
  await Promise.all([
240
240
  this.lan.refreshRoutingTable(),
241
241
  this.wan.refreshRoutingTable()
package/src/kad-dht.ts CHANGED
@@ -14,7 +14,7 @@ import {
14
14
  removePublicAddresses
15
15
  } from './utils.js'
16
16
  import { Logger, logger } from '@libp2p/logger'
17
- import type { QueryOptions, Validators, Selectors, DHT } from '@libp2p/interface-dht'
17
+ import type { QueryOptions, Validators, Selectors, DHT, QueryEvent } from '@libp2p/interface-dht'
18
18
  import type { PeerInfo } from '@libp2p/interface-peer-info'
19
19
  import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
20
20
  import type { PeerId } from '@libp2p/interface-peer-id'
@@ -207,11 +207,11 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT {
207
207
  return true
208
208
  }
209
209
 
210
- get [Symbol.toStringTag] () {
210
+ get [Symbol.toStringTag] (): '@libp2p/kad-dht' {
211
211
  return '@libp2p/kad-dht'
212
212
  }
213
213
 
214
- async onPeerConnect (peerData: PeerInfo) {
214
+ async onPeerConnect (peerData: PeerInfo): Promise<void> {
215
215
  this.log('peer %p connected with protocols %s', peerData.id, peerData.protocols)
216
216
 
217
217
  if (this.lan) {
@@ -235,21 +235,21 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT {
235
235
  /**
236
236
  * Is this DHT running.
237
237
  */
238
- isStarted () {
238
+ isStarted (): boolean {
239
239
  return this.running
240
240
  }
241
241
 
242
242
  /**
243
243
  * If 'server' this node will respond to DHT queries, if 'client' this node will not
244
244
  */
245
- async getMode () {
245
+ async getMode (): Promise<'client' | 'server'> {
246
246
  return this.clientMode ? 'client' : 'server'
247
247
  }
248
248
 
249
249
  /**
250
250
  * If 'server' this node will respond to DHT queries, if 'client' this node will not
251
251
  */
252
- async setMode (mode: 'client' | 'server') {
252
+ async setMode (mode: 'client' | 'server'): Promise<void> {
253
253
  await this.components.registrar.unhandle(this.protocol)
254
254
 
255
255
  if (mode === 'client') {
@@ -268,7 +268,7 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT {
268
268
  /**
269
269
  * Start listening to incoming connections.
270
270
  */
271
- async start () {
271
+ async start (): Promise<void> {
272
272
  this.running = true
273
273
 
274
274
  // Only respond to queries when not in client mode
@@ -290,7 +290,7 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT {
290
290
  * Stop accepting incoming connections and sending outgoing
291
291
  * messages.
292
292
  */
293
- async stop () {
293
+ async stop (): Promise<void> {
294
294
  this.running = false
295
295
 
296
296
  await Promise.all([
@@ -307,14 +307,14 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT {
307
307
  /**
308
308
  * Store the given key/value pair in the DHT
309
309
  */
310
- async * put (key: Uint8Array, value: Uint8Array, options: QueryOptions = {}) { // eslint-disable-line require-await
310
+ async * put (key: Uint8Array, value: Uint8Array, options: QueryOptions = {}): AsyncGenerator<any, void, undefined> {
311
311
  yield * this.contentFetching.put(key, value, options)
312
312
  }
313
313
 
314
314
  /**
315
315
  * Get the value that corresponds to the passed key
316
316
  */
317
- async * get (key: Uint8Array, options: QueryOptions = {}) { // eslint-disable-line require-await
317
+ async * get (key: Uint8Array, options: QueryOptions = {}): AsyncGenerator<QueryEvent, void, undefined> {
318
318
  yield * this.contentFetching.get(key, options)
319
319
  }
320
320
 
@@ -323,14 +323,14 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT {
323
323
  /**
324
324
  * Announce to the network that we can provide given key's value
325
325
  */
326
- async * provide (key: CID, options: QueryOptions = {}) { // eslint-disable-line require-await
326
+ async * provide (key: CID, options: QueryOptions = {}): AsyncGenerator<QueryEvent, void, undefined> {
327
327
  yield * this.contentRouting.provide(key, this.components.addressManager.getAddresses(), options)
328
328
  }
329
329
 
330
330
  /**
331
331
  * Search the dht for providers of the given CID
332
332
  */
333
- async * findProviders (key: CID, options: QueryOptions = {}) {
333
+ async * findProviders (key: CID, options: QueryOptions = {}): AsyncGenerator<QueryEvent, any, unknown> {
334
334
  yield * this.contentRouting.findProviders(key, options)
335
335
  }
336
336
 
@@ -339,18 +339,18 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT {
339
339
  /**
340
340
  * Search for a peer with the given ID
341
341
  */
342
- async * findPeer (id: PeerId, options: QueryOptions = {}) { // eslint-disable-line require-await
342
+ async * findPeer (id: PeerId, options: QueryOptions = {}): AsyncGenerator<QueryEvent, any, unknown> {
343
343
  yield * this.peerRouting.findPeer(id, options)
344
344
  }
345
345
 
346
346
  /**
347
347
  * Kademlia 'node lookup' operation
348
348
  */
349
- async * getClosestPeers (key: Uint8Array, options: QueryOptions = {}) {
349
+ async * getClosestPeers (key: Uint8Array, options: QueryOptions = {}): AsyncGenerator<QueryEvent, any, unknown> {
350
350
  yield * this.peerRouting.getClosestPeers(key, options)
351
351
  }
352
352
 
353
- async refreshRoutingTable () {
354
- await this.routingTableRefresh.refreshTable(true)
353
+ async refreshRoutingTable (): Promise<void> {
354
+ this.routingTableRefresh.refreshTable(true)
355
355
  }
356
356
  }
@@ -126,7 +126,7 @@ export namespace Message {
126
126
  }
127
127
 
128
128
  export namespace MessageType {
129
- export const codec = () => {
129
+ export const codec = (): Codec<MessageType> => {
130
130
  return enumeration<MessageType>(__MessageTypeValues)
131
131
  }
132
132
  }
@@ -146,7 +146,7 @@ export namespace Message {
146
146
  }
147
147
 
148
148
  export namespace ConnectionType {
149
- export const codec = () => {
149
+ export const codec = (): Codec<ConnectionType> => {
150
150
  return enumeration<ConnectionType>(__ConnectionTypeValues)
151
151
  }
152
152
  }
@@ -42,7 +42,7 @@ export class Message {
42
42
  /**
43
43
  * @type {number}
44
44
  */
45
- get clusterLevel () {
45
+ get clusterLevel (): number {
46
46
  const level = this.clusterLevelRaw - 1
47
47
  if (level < 0) {
48
48
  return 0
@@ -58,7 +58,7 @@ export class Message {
58
58
  /**
59
59
  * Encode into protobuf
60
60
  */
61
- serialize () {
61
+ serialize (): Uint8Array {
62
62
  return PBMessage.encode({
63
63
  key: this.key,
64
64
  type: this.type,
@@ -72,7 +72,7 @@ export class Message {
72
72
  /**
73
73
  * Decode from protobuf
74
74
  */
75
- static deserialize (raw: Uint8ArrayList | Uint8Array) {
75
+ static deserialize (raw: Uint8ArrayList | Uint8Array): Message {
76
76
  const dec = PBMessage.decode(raw)
77
77
 
78
78
  const msg = new Message(dec.type ?? PBMessage.MessageType.PUT_VALUE, dec.key ?? Uint8Array.from([]), dec.clusterLevelRaw ?? 0)
@@ -87,7 +87,7 @@ export class Message {
87
87
  }
88
88
  }
89
89
 
90
- function toPbPeer (peer: PeerInfo) {
90
+ function toPbPeer (peer: PeerInfo): PBPeer {
91
91
  const output: PBPeer = {
92
92
  id: peer.id.toBytes(),
93
93
  addrs: (peer.multiaddrs ?? []).map((m) => m.bytes),
@@ -97,7 +97,7 @@ function toPbPeer (peer: PeerInfo) {
97
97
  return output
98
98
  }
99
99
 
100
- function fromPbPeer (peer: PBMessage.Peer) {
100
+ function fromPbPeer (peer: PBMessage.Peer): PeerInfo {
101
101
  if (peer.id == null) {
102
102
  throw new Error('Invalid peer in message')
103
103
  }
package/src/network.ts CHANGED
@@ -22,6 +22,7 @@ import type { Stream } from '@libp2p/interface-connection'
22
22
  import { abortableDuplex } from 'abortable-iterator'
23
23
  import type { Uint8ArrayList } from 'uint8arraylist'
24
24
  import type { KadDHTComponents } from './index.js'
25
+ import type { QueryEvent } from '@libp2p/interface-dht'
25
26
 
26
27
  export interface NetworkInit {
27
28
  protocol: string
@@ -57,7 +58,7 @@ export class Network extends EventEmitter<NetworkEvents> implements Startable {
57
58
  /**
58
59
  * Start the network
59
60
  */
60
- async start () {
61
+ async start (): Promise<void> {
61
62
  if (this.running) {
62
63
  return
63
64
  }
@@ -68,21 +69,21 @@ export class Network extends EventEmitter<NetworkEvents> implements Startable {
68
69
  /**
69
70
  * Stop all network activity
70
71
  */
71
- async stop () {
72
+ async stop (): Promise<void> {
72
73
  this.running = false
73
74
  }
74
75
 
75
76
  /**
76
77
  * Is the network online?
77
78
  */
78
- isStarted () {
79
+ isStarted (): boolean {
79
80
  return this.running
80
81
  }
81
82
 
82
83
  /**
83
84
  * Send a request and record RTT for latency measurements
84
85
  */
85
- async * sendRequest (to: PeerId, msg: Message, options: AbortOptions = {}) {
86
+ async * sendRequest (to: PeerId, msg: Message, options: AbortOptions = {}): AsyncGenerator<QueryEvent> {
86
87
  if (!this.running) {
87
88
  return
88
89
  }
@@ -118,7 +119,7 @@ export class Network extends EventEmitter<NetworkEvents> implements Startable {
118
119
  /**
119
120
  * Sends a message without expecting an answer
120
121
  */
121
- async * sendMessage (to: PeerId, msg: Message, options: AbortOptions = {}) {
122
+ async * sendMessage (to: PeerId, msg: Message, options: AbortOptions = {}): AsyncGenerator<QueryEvent> {
122
123
  if (!this.running) {
123
124
  return
124
125
  }
@@ -148,7 +149,7 @@ export class Network extends EventEmitter<NetworkEvents> implements Startable {
148
149
  /**
149
150
  * Write a message to the given stream
150
151
  */
151
- async _writeMessage (stream: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>, msg: Uint8Array | Uint8ArrayList, options: AbortOptions) {
152
+ async _writeMessage (stream: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>, msg: Uint8Array | Uint8ArrayList, options: AbortOptions): Promise<void> {
152
153
  if (options.signal != null) {
153
154
  stream = abortableDuplex(stream, options.signal)
154
155
  }
@@ -166,7 +167,7 @@ export class Network extends EventEmitter<NetworkEvents> implements Startable {
166
167
  * If no response is received after the specified timeout
167
168
  * this will error out.
168
169
  */
169
- async _writeReadMessage (stream: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>, msg: Uint8Array | Uint8ArrayList, options: AbortOptions) {
170
+ async _writeReadMessage (stream: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>, msg: Uint8Array | Uint8ArrayList, options: AbortOptions): Promise<Message> {
170
171
  if (options.signal != null) {
171
172
  stream = abortableDuplex(stream, options.signal)
172
173
  }
@@ -13,7 +13,7 @@ export class PeerList {
13
13
  /**
14
14
  * Add a new peer. Returns `true` if it was a new one
15
15
  */
16
- push (peerId: PeerId) {
16
+ push (peerId: PeerId): boolean {
17
17
  if (!this.has(peerId)) {
18
18
  this.list.push(peerId)
19
19
 
@@ -26,7 +26,7 @@ export class PeerList {
26
26
  /**
27
27
  * Check if this PeerInfo is already in here
28
28
  */
29
- has (peerId: PeerId) {
29
+ has (peerId: PeerId): boolean {
30
30
  const match = this.list.find((i) => i.equals(peerId))
31
31
  return Boolean(match)
32
32
  }
@@ -34,21 +34,21 @@ export class PeerList {
34
34
  /**
35
35
  * Get the list as an array
36
36
  */
37
- toArray () {
37
+ toArray (): PeerId[] {
38
38
  return this.list.slice()
39
39
  }
40
40
 
41
41
  /**
42
42
  * Remove the last element
43
43
  */
44
- pop () {
44
+ pop (): PeerId | undefined {
45
45
  return this.list.pop()
46
46
  }
47
47
 
48
48
  /**
49
49
  * The length of the list
50
50
  */
51
- get length () {
51
+ get length (): number {
52
52
  return this.list.length
53
53
  }
54
54
  }
@@ -33,21 +33,21 @@ export class PeerDistanceList {
33
33
  /**
34
34
  * The length of the list
35
35
  */
36
- get length () {
36
+ get length (): number {
37
37
  return this.peerDistances.length
38
38
  }
39
39
 
40
40
  /**
41
41
  * The peerIds in the list, in order of distance from the origin key
42
42
  */
43
- get peers () {
43
+ get peers (): PeerId[] {
44
44
  return this.peerDistances.map(pd => pd.peerId)
45
45
  }
46
46
 
47
47
  /**
48
48
  * Add a peerId to the list.
49
49
  */
50
- async add (peerId: PeerId) {
50
+ async add (peerId: PeerId): Promise<void> {
51
51
  if (this.peerDistances.find(pd => pd.peerId.equals(peerId)) != null) {
52
52
  return
53
53
  }
@@ -67,7 +67,7 @@ export class PeerDistanceList {
67
67
  * Indicates whether any of the peerIds passed as a parameter are closer
68
68
  * to the origin key than the furthest peerId in the PeerDistanceList.
69
69
  */
70
- async anyCloser (peerIds: PeerId[]) {
70
+ async anyCloser (peerIds: PeerId[]): Promise<boolean> {
71
71
  if (peerIds.length === 0) {
72
72
  return false
73
73
  }
@@ -13,7 +13,7 @@ import { Libp2pRecord } from '@libp2p/record'
13
13
  import { logger } from '@libp2p/logger'
14
14
  import { keys } from '@libp2p/crypto'
15
15
  import { peerIdFromKeys } from '@libp2p/peer-id'
16
- import type { DHTRecord, QueryOptions, Validators } from '@libp2p/interface-dht'
16
+ import type { DHTRecord, DialingPeerEvent, FinalPeerEvent, QueryEvent, QueryOptions, Validators } from '@libp2p/interface-dht'
17
17
  import type { RoutingTable } from '../routing-table/index.js'
18
18
  import type { QueryManager } from '../query/manager.js'
19
19
  import type { Network } from '../network.js'
@@ -97,7 +97,7 @@ export class PeerRouting {
97
97
  /**
98
98
  * Get a value via rpc call for the given parameters
99
99
  */
100
- async * _getValueSingle (peer: PeerId, key: Uint8Array, options: AbortOptions = {}) { // eslint-disable-line require-await
100
+ async * _getValueSingle (peer: PeerId, key: Uint8Array, options: AbortOptions = {}): AsyncGenerator<QueryEvent> {
101
101
  const msg = new Message(MESSAGE_TYPE.GET_VALUE, key, 0)
102
102
  yield * this.network.sendRequest(peer, msg, options)
103
103
  }
@@ -105,7 +105,7 @@ export class PeerRouting {
105
105
  /**
106
106
  * Get the public key directly from a node
107
107
  */
108
- async * getPublicKeyFromNode (peer: PeerId, options: AbortOptions = {}) {
108
+ async * getPublicKeyFromNode (peer: PeerId, options: AbortOptions = {}): AsyncGenerator<QueryEvent> {
109
109
  const pkKey = utils.keyForPublicKey(peer)
110
110
 
111
111
  for await (const event of this._getValueSingle(peer, pkKey, options)) {
@@ -133,7 +133,7 @@ export class PeerRouting {
133
133
  /**
134
134
  * Search for a peer with the given ID
135
135
  */
136
- async * findPeer (id: PeerId, options: QueryOptions = {}) {
136
+ async * findPeer (id: PeerId, options: QueryOptions = {}): AsyncGenerator<FinalPeerEvent | QueryEvent> {
137
137
  this.log('findPeer %p', id)
138
138
 
139
139
  // Try to find locally
@@ -215,14 +215,14 @@ export class PeerRouting {
215
215
  * Kademlia 'node lookup' operation on a key, which could be a the
216
216
  * bytes from a multihash or a peer ID
217
217
  */
218
- async * getClosestPeers (key: Uint8Array, options: QueryOptions = {}) {
218
+ async * getClosestPeers (key: Uint8Array, options: QueryOptions = {}): AsyncGenerator<DialingPeerEvent | QueryEvent> {
219
219
  this.log('getClosestPeers to %b', key)
220
220
  const id = await utils.convertBuffer(key)
221
221
  const tablePeers = this.routingTable.closestPeers(id)
222
222
  const self = this // eslint-disable-line @typescript-eslint/no-this-alias
223
223
 
224
224
  const peers = new PeerDistanceList(id, this.routingTable.kBucketSize)
225
- await Promise.all(tablePeers.map(async peer => await peers.add(peer)))
225
+ await Promise.all(tablePeers.map(async peer => { await peers.add(peer) }))
226
226
 
227
227
  const getCloserPeersQuery: QueryFunc = async function * ({ peer, signal }) {
228
228
  self.log('closerPeersSingle %s from %p', uint8ArrayToString(key, 'base32'), peer)
@@ -235,7 +235,7 @@ export class PeerRouting {
235
235
  yield event
236
236
 
237
237
  if (event.name === 'PEER_RESPONSE') {
238
- await Promise.all(event.closer.map(async peerData => await peers.add(peerData.id)))
238
+ await Promise.all(event.closer.map(async peerData => { await peers.add(peerData.id) }))
239
239
  }
240
240
  }
241
241
 
@@ -259,7 +259,7 @@ export class PeerRouting {
259
259
  *
260
260
  * Note: The peerStore is updated with new addresses found for the given peer.
261
261
  */
262
- async * getValueOrPeers (peer: PeerId, key: Uint8Array, options: AbortOptions = {}) {
262
+ async * getValueOrPeers (peer: PeerId, key: Uint8Array, options: AbortOptions = {}): AsyncGenerator<DialingPeerEvent | QueryEvent> {
263
263
  for await (const event of this._getValueSingle(peer, key, options)) {
264
264
  if (event.name === 'PEER_RESPONSE') {
265
265
  if (event.record != null) {
@@ -284,7 +284,7 @@ export class PeerRouting {
284
284
  * Verify a record, fetching missing public keys from the network.
285
285
  * Throws an error if the record is invalid.
286
286
  */
287
- async _verifyRecordOnline (record: DHTRecord) {
287
+ async _verifyRecordOnline (record: DHTRecord): Promise<void> {
288
288
  if (record.timeReceived == null) {
289
289
  throw new CodeError('invalid record received', 'ERR_INVALID_RECORD')
290
290
  }
@@ -296,7 +296,7 @@ export class PeerRouting {
296
296
  * Get the nearest peers to the given query, but if closer
297
297
  * than self
298
298
  */
299
- async getCloserPeersOffline (key: Uint8Array, closerThan: PeerId) {
299
+ async getCloserPeersOffline (key: Uint8Array, closerThan: PeerId): Promise<PeerInfo[]> {
300
300
  const id = await utils.convertBuffer(key)
301
301
  const ids = this.routingTable.closestPeers(id)
302
302
  const output: PeerInfo[] = []