@libp2p/kad-dht 7.0.1 → 7.0.3

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 (88) hide show
  1. package/dist/index.min.js +19 -19
  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 +4 -4
  17. package/dist/src/message/dht.d.ts.map +1 -1
  18. package/dist/src/message/dht.js +40 -46
  19. package/dist/src/message/dht.js.map +1 -1
  20. package/dist/src/message/index.d.ts.map +1 -1
  21. package/dist/src/network.d.ts +3 -2
  22. package/dist/src/network.d.ts.map +1 -1
  23. package/dist/src/network.js.map +1 -1
  24. package/dist/src/peer-list/index.d.ts.map +1 -1
  25. package/dist/src/peer-list/peer-distance-list.d.ts.map +1 -1
  26. package/dist/src/peer-routing/index.d.ts +6 -6
  27. package/dist/src/peer-routing/index.d.ts.map +1 -1
  28. package/dist/src/peer-routing/index.js +2 -2
  29. package/dist/src/peer-routing/index.js.map +1 -1
  30. package/dist/src/providers.d.ts.map +1 -1
  31. package/dist/src/providers.js +3 -3
  32. package/dist/src/providers.js.map +1 -1
  33. package/dist/src/query/manager.d.ts +2 -2
  34. package/dist/src/query/manager.d.ts.map +1 -1
  35. package/dist/src/query/query-path.d.ts.map +1 -1
  36. package/dist/src/query/query-path.js.map +1 -1
  37. package/dist/src/query-self.d.ts.map +1 -1
  38. package/dist/src/routing-table/index.d.ts.map +1 -1
  39. package/dist/src/routing-table/index.js +1 -1
  40. package/dist/src/routing-table/index.js.map +1 -1
  41. package/dist/src/routing-table/refresh.d.ts +3 -2
  42. package/dist/src/routing-table/refresh.d.ts.map +1 -1
  43. package/dist/src/routing-table/refresh.js.map +1 -1
  44. package/dist/src/rpc/handlers/add-provider.d.ts +1 -1
  45. package/dist/src/rpc/handlers/add-provider.d.ts.map +1 -1
  46. package/dist/src/rpc/handlers/find-node.d.ts.map +1 -1
  47. package/dist/src/rpc/handlers/get-providers.d.ts +2 -1
  48. package/dist/src/rpc/handlers/get-providers.d.ts.map +1 -1
  49. package/dist/src/rpc/handlers/get-providers.js.map +1 -1
  50. package/dist/src/rpc/handlers/get-value.d.ts +1 -1
  51. package/dist/src/rpc/handlers/get-value.d.ts.map +1 -1
  52. package/dist/src/rpc/handlers/get-value.js +1 -1
  53. package/dist/src/rpc/handlers/ping.d.ts.map +1 -1
  54. package/dist/src/rpc/handlers/put-value.d.ts.map +1 -1
  55. package/dist/src/rpc/index.d.ts.map +1 -1
  56. package/dist/src/topology-listener.d.ts +1 -1
  57. package/dist/src/topology-listener.d.ts.map +1 -1
  58. package/dist/src/topology-listener.js +1 -1
  59. package/dist/src/topology-listener.js.map +1 -1
  60. package/dist/src/utils.d.ts.map +1 -1
  61. package/dist/src/utils.js +1 -1
  62. package/dist/src/utils.js.map +1 -1
  63. package/package.json +6 -8
  64. package/src/content-fetching/index.ts +9 -9
  65. package/src/content-routing/index.ts +4 -4
  66. package/src/dual-kad-dht.ts +14 -14
  67. package/src/kad-dht.ts +16 -16
  68. package/src/message/dht.ts +46 -49
  69. package/src/message/index.ts +5 -5
  70. package/src/network.ts +8 -7
  71. package/src/peer-list/index.ts +5 -5
  72. package/src/peer-list/peer-distance-list.ts +4 -4
  73. package/src/peer-routing/index.ts +10 -10
  74. package/src/providers.ts +14 -14
  75. package/src/query/manager.ts +5 -5
  76. package/src/query/query-path.ts +4 -4
  77. package/src/query-self.ts +4 -4
  78. package/src/routing-table/index.ts +10 -10
  79. package/src/routing-table/refresh.ts +11 -10
  80. package/src/rpc/handlers/add-provider.ts +1 -1
  81. package/src/rpc/handlers/find-node.ts +1 -1
  82. package/src/rpc/handlers/get-providers.ts +4 -3
  83. package/src/rpc/handlers/get-value.ts +3 -3
  84. package/src/rpc/handlers/ping.ts +1 -1
  85. package/src/rpc/handlers/put-value.ts +1 -1
  86. package/src/rpc/index.ts +2 -2
  87. package/src/topology-listener.ts +3 -3
  88. package/src/utils.ts +12 -12
@@ -1,9 +1,12 @@
1
1
  /* eslint-disable import/export */
2
+ /* eslint-disable complexity */
2
3
  /* eslint-disable @typescript-eslint/no-namespace */
4
+ /* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
5
+ /* eslint-disable @typescript-eslint/no-empty-interface */
3
6
 
4
7
  import { encodeMessage, decodeMessage, message, enumeration } from 'protons-runtime'
5
- import type { Uint8ArrayList } from 'uint8arraylist'
6
8
  import type { Codec } from 'protons-runtime'
9
+ import type { Uint8ArrayList } from 'uint8arraylist'
7
10
 
8
11
  export interface Record {
9
12
  key?: Uint8Array
@@ -18,38 +21,38 @@ export namespace Record {
18
21
 
19
22
  export const codec = (): Codec<Record> => {
20
23
  if (_codec == null) {
21
- _codec = message<Record>((obj, writer, opts = {}) => {
24
+ _codec = message<Record>((obj, w, opts = {}) => {
22
25
  if (opts.lengthDelimited !== false) {
23
- writer.fork()
26
+ w.fork()
24
27
  }
25
28
 
26
29
  if (obj.key != null) {
27
- writer.uint32(10)
28
- writer.bytes(obj.key)
30
+ w.uint32(10)
31
+ w.bytes(obj.key)
29
32
  }
30
33
 
31
34
  if (obj.value != null) {
32
- writer.uint32(18)
33
- writer.bytes(obj.value)
35
+ w.uint32(18)
36
+ w.bytes(obj.value)
34
37
  }
35
38
 
36
39
  if (obj.author != null) {
37
- writer.uint32(26)
38
- writer.bytes(obj.author)
40
+ w.uint32(26)
41
+ w.bytes(obj.author)
39
42
  }
40
43
 
41
44
  if (obj.signature != null) {
42
- writer.uint32(34)
43
- writer.bytes(obj.signature)
45
+ w.uint32(34)
46
+ w.bytes(obj.signature)
44
47
  }
45
48
 
46
49
  if (obj.timeReceived != null) {
47
- writer.uint32(42)
48
- writer.string(obj.timeReceived)
50
+ w.uint32(42)
51
+ w.string(obj.timeReceived)
49
52
  }
50
53
 
51
54
  if (opts.lengthDelimited !== false) {
52
- writer.ldelim()
55
+ w.ldelim()
53
56
  }
54
57
  }, (reader, length) => {
55
58
  const obj: any = {}
@@ -88,7 +91,7 @@ export namespace Record {
88
91
  return _codec
89
92
  }
90
93
 
91
- export const encode = (obj: Record): Uint8Array => {
94
+ export const encode = (obj: Partial<Record>): Uint8Array => {
92
95
  return encodeMessage(obj, Record.codec())
93
96
  }
94
97
 
@@ -126,7 +129,7 @@ export namespace Message {
126
129
  }
127
130
 
128
131
  export namespace MessageType {
129
- export const codec = () => {
132
+ export const codec = (): Codec<MessageType> => {
130
133
  return enumeration<MessageType>(__MessageTypeValues)
131
134
  }
132
135
  }
@@ -146,7 +149,7 @@ export namespace Message {
146
149
  }
147
150
 
148
151
  export namespace ConnectionType {
149
- export const codec = () => {
152
+ export const codec = (): Codec<ConnectionType> => {
150
153
  return enumeration<ConnectionType>(__ConnectionTypeValues)
151
154
  }
152
155
  }
@@ -162,32 +165,30 @@ export namespace Message {
162
165
 
163
166
  export const codec = (): Codec<Peer> => {
164
167
  if (_codec == null) {
165
- _codec = message<Peer>((obj, writer, opts = {}) => {
168
+ _codec = message<Peer>((obj, w, opts = {}) => {
166
169
  if (opts.lengthDelimited !== false) {
167
- writer.fork()
170
+ w.fork()
168
171
  }
169
172
 
170
173
  if (obj.id != null) {
171
- writer.uint32(10)
172
- writer.bytes(obj.id)
174
+ w.uint32(10)
175
+ w.bytes(obj.id)
173
176
  }
174
177
 
175
178
  if (obj.addrs != null) {
176
179
  for (const value of obj.addrs) {
177
- writer.uint32(18)
178
- writer.bytes(value)
180
+ w.uint32(18)
181
+ w.bytes(value)
179
182
  }
180
- } else {
181
- throw new Error('Protocol error: required field "addrs" was not found in object')
182
183
  }
183
184
 
184
185
  if (obj.connection != null) {
185
- writer.uint32(24)
186
- Message.ConnectionType.codec().encode(obj.connection, writer)
186
+ w.uint32(24)
187
+ Message.ConnectionType.codec().encode(obj.connection, w)
187
188
  }
188
189
 
189
190
  if (opts.lengthDelimited !== false) {
190
- writer.ldelim()
191
+ w.ldelim()
191
192
  }
192
193
  }, (reader, length) => {
193
194
  const obj: any = {
@@ -222,7 +223,7 @@ export namespace Message {
222
223
  return _codec
223
224
  }
224
225
 
225
- export const encode = (obj: Peer): Uint8Array => {
226
+ export const encode = (obj: Partial<Peer>): Uint8Array => {
226
227
  return encodeMessage(obj, Peer.codec())
227
228
  }
228
229
 
@@ -235,51 +236,47 @@ export namespace Message {
235
236
 
236
237
  export const codec = (): Codec<Message> => {
237
238
  if (_codec == null) {
238
- _codec = message<Message>((obj, writer, opts = {}) => {
239
+ _codec = message<Message>((obj, w, opts = {}) => {
239
240
  if (opts.lengthDelimited !== false) {
240
- writer.fork()
241
+ w.fork()
241
242
  }
242
243
 
243
244
  if (obj.type != null) {
244
- writer.uint32(8)
245
- Message.MessageType.codec().encode(obj.type, writer)
245
+ w.uint32(8)
246
+ Message.MessageType.codec().encode(obj.type, w)
246
247
  }
247
248
 
248
249
  if (obj.clusterLevelRaw != null) {
249
- writer.uint32(80)
250
- writer.int32(obj.clusterLevelRaw)
250
+ w.uint32(80)
251
+ w.int32(obj.clusterLevelRaw)
251
252
  }
252
253
 
253
254
  if (obj.key != null) {
254
- writer.uint32(18)
255
- writer.bytes(obj.key)
255
+ w.uint32(18)
256
+ w.bytes(obj.key)
256
257
  }
257
258
 
258
259
  if (obj.record != null) {
259
- writer.uint32(26)
260
- writer.bytes(obj.record)
260
+ w.uint32(26)
261
+ w.bytes(obj.record)
261
262
  }
262
263
 
263
264
  if (obj.closerPeers != null) {
264
265
  for (const value of obj.closerPeers) {
265
- writer.uint32(66)
266
- Message.Peer.codec().encode(value, writer)
266
+ w.uint32(66)
267
+ Message.Peer.codec().encode(value, w)
267
268
  }
268
- } else {
269
- throw new Error('Protocol error: required field "closerPeers" was not found in object')
270
269
  }
271
270
 
272
271
  if (obj.providerPeers != null) {
273
272
  for (const value of obj.providerPeers) {
274
- writer.uint32(74)
275
- Message.Peer.codec().encode(value, writer)
273
+ w.uint32(74)
274
+ Message.Peer.codec().encode(value, w)
276
275
  }
277
- } else {
278
- throw new Error('Protocol error: required field "providerPeers" was not found in object')
279
276
  }
280
277
 
281
278
  if (opts.lengthDelimited !== false) {
282
- writer.ldelim()
279
+ w.ldelim()
283
280
  }
284
281
  }, (reader, length) => {
285
282
  const obj: any = {
@@ -324,7 +321,7 @@ export namespace Message {
324
321
  return _codec
325
322
  }
326
323
 
327
- export const encode = (obj: Message): Uint8Array => {
324
+ export const encode = (obj: Partial<Message>): Uint8Array => {
328
325
  return encodeMessage(obj, Message.codec())
329
326
  }
330
327
 
@@ -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[] = []
package/src/providers.ts CHANGED
@@ -66,14 +66,14 @@ export class Providers implements Startable {
66
66
  this.started = false
67
67
  }
68
68
 
69
- isStarted () {
69
+ isStarted (): boolean {
70
70
  return this.started
71
71
  }
72
72
 
73
73
  /**
74
74
  * Start the provider cleanup service
75
75
  */
76
- async start () {
76
+ async start (): Promise<void> {
77
77
  if (this.started) {
78
78
  return
79
79
  }
@@ -93,7 +93,7 @@ export class Providers implements Startable {
93
93
  /**
94
94
  * Release any resources.
95
95
  */
96
- async stop () {
96
+ async stop (): Promise<void> {
97
97
  this.started = false
98
98
 
99
99
  if (this.cleaner != null) {
@@ -105,8 +105,8 @@ export class Providers implements Startable {
105
105
  /**
106
106
  * Check all providers if they are still valid, and if not delete them
107
107
  */
108
- async _cleanup () {
109
- return await this.syncQueue.add(async () => {
108
+ async _cleanup (): Promise<void> {
109
+ await this.syncQueue.add(async () => {
110
110
  const start = Date.now()
111
111
 
112
112
  let count = 0
@@ -174,7 +174,7 @@ export class Providers implements Startable {
174
174
  /**
175
175
  * Get the currently known provider peer ids for a given CID
176
176
  */
177
- async _getProvidersMap (cid: CID) {
177
+ async _getProvidersMap (cid: CID): Promise<Map<string, Date>> {
178
178
  const cacheKey = makeProviderKey(cid)
179
179
  let provs: Map<string, Date> = this.cache.get(cacheKey)
180
180
 
@@ -189,8 +189,8 @@ export class Providers implements Startable {
189
189
  /**
190
190
  * Add a new provider for the given CID
191
191
  */
192
- async addProvider (cid: CID, provider: PeerId) {
193
- return await this.syncQueue.add(async () => {
192
+ async addProvider (cid: CID, provider: PeerId): Promise<void> {
193
+ await this.syncQueue.add(async () => {
194
194
  log('%p provides %s', provider, cid)
195
195
  const provs = await this._getProvidersMap(cid)
196
196
 
@@ -228,7 +228,7 @@ export class Providers implements Startable {
228
228
  /**
229
229
  * Encode the given key its matching datastore key
230
230
  */
231
- function makeProviderKey (cid: CID | string) {
231
+ function makeProviderKey (cid: CID | string): string {
232
232
  const cidStr = typeof cid === 'string' ? cid : uint8ArrayToString(cid.multihash.bytes, 'base32')
233
233
 
234
234
  return `${PROVIDER_KEY_PREFIX}/${cidStr}`
@@ -237,7 +237,7 @@ function makeProviderKey (cid: CID | string) {
237
237
  /**
238
238
  * Write a provider into the given store
239
239
  */
240
- async function writeProviderEntry (store: Datastore, cid: CID, peer: PeerId, time: Date) { // eslint-disable-line require-await
240
+ async function writeProviderEntry (store: Datastore, cid: CID, peer: PeerId, time: Date): Promise<void> {
241
241
  const dsKey = [
242
242
  makeProviderKey(cid),
243
243
  '/',
@@ -247,13 +247,13 @@ async function writeProviderEntry (store: Datastore, cid: CID, peer: PeerId, tim
247
247
  const key = new Key(dsKey)
248
248
  const buffer = Uint8Array.from(varint.encode(time.getTime()))
249
249
 
250
- return await store.put(key, buffer)
250
+ await store.put(key, buffer)
251
251
  }
252
252
 
253
253
  /**
254
254
  * Parse the CID and provider peer id from the key
255
255
  */
256
- function parseProviderKey (key: Key) {
256
+ function parseProviderKey (key: Key): { cid: string, peerId: string } {
257
257
  const parts = key.toString().split('/')
258
258
 
259
259
  if (parts.length !== 5) {
@@ -269,7 +269,7 @@ function parseProviderKey (key: Key) {
269
269
  /**
270
270
  * Load providers for the given CID from the store
271
271
  */
272
- async function loadProviders (store: Datastore, cid: CID) {
272
+ async function loadProviders (store: Datastore, cid: CID): Promise<Map<string, Date>> {
273
273
  const providers = new Map<string, Date>()
274
274
  const query = store.query({ prefix: makeProviderKey(cid) })
275
275
 
@@ -281,6 +281,6 @@ async function loadProviders (store: Datastore, cid: CID) {
281
281
  return providers
282
282
  }
283
283
 
284
- function readTime (buf: Uint8Array) {
284
+ function readTime (buf: Uint8Array): Date {
285
285
  return new Date(varint.decode(buf))
286
286
  }
@@ -12,7 +12,7 @@ import { logger } from '@libp2p/logger'
12
12
  import type { PeerId } from '@libp2p/interface-peer-id'
13
13
  import type { Startable } from '@libp2p/interfaces/startable'
14
14
  import type { QueryFunc } from './types.js'
15
- import type { QueryOptions } from '@libp2p/interface-dht'
15
+ import type { QueryEvent, QueryOptions } from '@libp2p/interface-dht'
16
16
  import { PeerSet } from '@libp2p/peer-collections'
17
17
  import type { Metric, Metrics } from '@libp2p/interface-metrics'
18
18
 
@@ -59,14 +59,14 @@ export class QueryManager implements Startable {
59
59
  this.queries = 0
60
60
  }
61
61
 
62
- isStarted () {
62
+ isStarted (): boolean {
63
63
  return this.running
64
64
  }
65
65
 
66
66
  /**
67
67
  * Starts the query manager
68
68
  */
69
- async start () {
69
+ async start (): Promise<void> {
70
70
  this.running = true
71
71
 
72
72
  if (this.components.metrics != null && this.metrics == null) {
@@ -80,7 +80,7 @@ export class QueryManager implements Startable {
80
80
  /**
81
81
  * Stops all queries
82
82
  */
83
- async stop () {
83
+ async stop (): Promise<void> {
84
84
  this.running = false
85
85
 
86
86
  for (const controller of this.controllers) {
@@ -90,7 +90,7 @@ export class QueryManager implements Startable {
90
90
  this.controllers.clear()
91
91
  }
92
92
 
93
- async * run (key: Uint8Array, peers: PeerId[], queryFunc: QueryFunc, options: QueryOptions = {}) {
93
+ async * run (key: Uint8Array, peers: PeerId[], queryFunc: QueryFunc, options: QueryOptions = {}): AsyncGenerator<QueryEvent> {
94
94
  if (!this.running) {
95
95
  throw new Error('QueryManager not started')
96
96
  }
@@ -83,7 +83,7 @@ export interface QueryPathOptions {
83
83
  * Walks a path through the DHT, calling the passed query function for
84
84
  * every peer encountered that we have not seen before
85
85
  */
86
- export async function * queryPath (options: QueryPathOptions) {
86
+ export async function * queryPath (options: QueryPathOptions): AsyncGenerator<QueryEvent, void, undefined> {
87
87
  const { key, startingPeer, ourPeerId, signal, query, alpha, pathIndex, numPaths, cleanUp, queryFuncTimeout, log, peersSeen } = options
88
88
  // Only ALPHA node/value lookups are allowed at any given time for each process
89
89
  // https://github.com/libp2p/specs/tree/master/kad-dht#alpha-concurrency-parameter-%CE%B1
@@ -98,7 +98,7 @@ export async function * queryPath (options: QueryPathOptions) {
98
98
  * Adds the passed peer to the query queue if it's not us and no
99
99
  * other path has passed through this peer
100
100
  */
101
- function queryPeer (peer: PeerId, peerKadId: Uint8Array) {
101
+ function queryPeer (peer: PeerId, peerKadId: Uint8Array): void {
102
102
  if (peer == null) {
103
103
  return
104
104
  }
@@ -196,12 +196,12 @@ export async function * queryPath (options: QueryPathOptions) {
196
196
  yield * toGenerator(queue, signal, cleanUp, log)
197
197
  }
198
198
 
199
- async function * toGenerator (queue: Queue, signal: AbortSignal, cleanUp: EventEmitter<CleanUpEvents>, log: Logger) {
199
+ async function * toGenerator (queue: Queue, signal: AbortSignal, cleanUp: EventEmitter<CleanUpEvents>, log: Logger): AsyncGenerator<QueryEvent, void, undefined> {
200
200
  let deferred = defer()
201
201
  let running = true
202
202
  const results: QueryEvent[] = []
203
203
 
204
- const cleanup = () => {
204
+ const cleanup = (): void => {
205
205
  if (!running) {
206
206
  return
207
207
  }