@libp2p/kad-dht 7.0.0 → 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.
- package/dist/index.min.js +19 -19
- package/dist/src/content-fetching/index.d.ts +5 -5
- package/dist/src/content-fetching/index.d.ts.map +1 -1
- package/dist/src/content-fetching/index.js +6 -6
- package/dist/src/content-fetching/index.js.map +1 -1
- package/dist/src/content-routing/index.d.ts +3 -3
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +1 -1
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/dual-kad-dht.d.ts +9 -9
- package/dist/src/dual-kad-dht.d.ts.map +1 -1
- package/dist/src/dual-kad-dht.js +6 -6
- package/dist/src/dual-kad-dht.js.map +1 -1
- package/dist/src/kad-dht.d.ts +9 -9
- package/dist/src/kad-dht.d.ts.map +1 -1
- package/dist/src/kad-dht.js +1 -1
- package/dist/src/kad-dht.js.map +1 -1
- package/dist/src/message/dht.d.ts.map +1 -1
- package/dist/src/message/dht.js.map +1 -1
- package/dist/src/message/index.d.ts.map +1 -1
- package/dist/src/network.d.ts +3 -2
- package/dist/src/network.d.ts.map +1 -1
- package/dist/src/network.js +2 -2
- package/dist/src/network.js.map +1 -1
- package/dist/src/peer-list/index.d.ts.map +1 -1
- package/dist/src/peer-list/peer-distance-list.d.ts.map +1 -1
- package/dist/src/peer-routing/index.d.ts +6 -6
- package/dist/src/peer-routing/index.d.ts.map +1 -1
- package/dist/src/peer-routing/index.js +9 -9
- package/dist/src/peer-routing/index.js.map +1 -1
- package/dist/src/providers.d.ts.map +1 -1
- package/dist/src/providers.js +8 -3
- package/dist/src/providers.js.map +1 -1
- package/dist/src/query/manager.d.ts +2 -2
- package/dist/src/query/manager.d.ts.map +1 -1
- package/dist/src/query/query-path.d.ts.map +1 -1
- package/dist/src/query/query-path.js +2 -2
- package/dist/src/query/query-path.js.map +1 -1
- package/dist/src/query-self.d.ts.map +1 -1
- package/dist/src/routing-table/index.d.ts +1 -1
- package/dist/src/routing-table/index.d.ts.map +1 -1
- package/dist/src/routing-table/index.js +1 -1
- package/dist/src/routing-table/index.js.map +1 -1
- package/dist/src/routing-table/refresh.d.ts +3 -2
- package/dist/src/routing-table/refresh.d.ts.map +1 -1
- package/dist/src/routing-table/refresh.js.map +1 -1
- package/dist/src/rpc/handlers/add-provider.d.ts +1 -1
- package/dist/src/rpc/handlers/add-provider.d.ts.map +1 -1
- package/dist/src/rpc/handlers/add-provider.js +3 -3
- package/dist/src/rpc/handlers/add-provider.js.map +1 -1
- package/dist/src/rpc/handlers/find-node.d.ts.map +1 -1
- package/dist/src/rpc/handlers/get-providers.d.ts +2 -1
- package/dist/src/rpc/handlers/get-providers.d.ts.map +1 -1
- package/dist/src/rpc/handlers/get-providers.js +2 -2
- package/dist/src/rpc/handlers/get-providers.js.map +1 -1
- package/dist/src/rpc/handlers/get-value.d.ts +1 -1
- package/dist/src/rpc/handlers/get-value.d.ts.map +1 -1
- package/dist/src/rpc/handlers/get-value.js +5 -5
- package/dist/src/rpc/handlers/get-value.js.map +1 -1
- package/dist/src/rpc/handlers/ping.d.ts.map +1 -1
- package/dist/src/rpc/handlers/put-value.d.ts.map +1 -1
- package/dist/src/rpc/handlers/put-value.js +2 -2
- package/dist/src/rpc/handlers/put-value.js.map +1 -1
- package/dist/src/rpc/index.d.ts.map +1 -1
- package/dist/src/topology-listener.d.ts +1 -1
- package/dist/src/topology-listener.d.ts.map +1 -1
- package/dist/src/topology-listener.js +1 -1
- package/dist/src/topology-listener.js.map +1 -1
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/utils.js +1 -1
- package/dist/src/utils.js.map +1 -1
- package/dist/typedoc-urls.json +1 -0
- package/package.json +4 -7
- package/src/content-fetching/index.ts +13 -13
- package/src/content-routing/index.ts +4 -4
- package/src/dual-kad-dht.ts +20 -20
- package/src/kad-dht.ts +16 -16
- package/src/message/dht.ts +2 -2
- package/src/message/index.ts +5 -5
- package/src/network.ts +10 -9
- package/src/peer-list/index.ts +5 -5
- package/src/peer-list/peer-distance-list.ts +4 -4
- package/src/peer-routing/index.ts +17 -17
- package/src/providers.ts +19 -14
- package/src/query/manager.ts +5 -5
- package/src/query/query-path.ts +6 -6
- package/src/query-self.ts +4 -4
- package/src/routing-table/index.ts +11 -11
- package/src/routing-table/refresh.ts +11 -10
- package/src/rpc/handlers/add-provider.ts +4 -4
- package/src/rpc/handlers/find-node.ts +1 -1
- package/src/rpc/handlers/get-providers.ts +6 -5
- package/src/rpc/handlers/get-value.ts +7 -7
- package/src/rpc/handlers/ping.ts +1 -1
- package/src/rpc/handlers/put-value.ts +3 -3
- package/src/rpc/index.ts +2 -2
- package/src/topology-listener.ts +3 -3
- package/src/utils.ts +12 -12
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 = {})
|
|
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 = {})
|
|
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 = {})
|
|
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 = {})
|
|
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
|
-
|
|
353
|
+
async refreshRoutingTable (): Promise<void> {
|
|
354
|
+
this.routingTableRefresh.refreshTable(true)
|
|
355
355
|
}
|
|
356
356
|
}
|
package/src/message/dht.ts
CHANGED
|
@@ -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
|
}
|
package/src/message/index.ts
CHANGED
|
@@ -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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { CodeError } from '@libp2p/interfaces/errors'
|
|
2
2
|
import { pipe } from 'it-pipe'
|
|
3
3
|
import * as lp from 'it-length-prefixed'
|
|
4
4
|
import drain from 'it-drain'
|
|
@@ -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
|
}
|
|
@@ -183,7 +184,7 @@ export class Network extends EventEmitter<NetworkEvents> implements Startable {
|
|
|
183
184
|
return buf
|
|
184
185
|
}
|
|
185
186
|
|
|
186
|
-
throw
|
|
187
|
+
throw new CodeError('No message received', 'ERR_NO_MESSAGE_RECEIVED')
|
|
187
188
|
}
|
|
188
189
|
)
|
|
189
190
|
|
package/src/peer-list/index.ts
CHANGED
|
@@ -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
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { CodeError } from '@libp2p/interfaces/errors'
|
|
2
2
|
import { verifyRecord } from '@libp2p/record/validators'
|
|
3
3
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
4
4
|
import { Message, MESSAGE_TYPE } from '../message/index.js'
|
|
@@ -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 = {}) {
|
|
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)) {
|
|
@@ -116,24 +116,24 @@ export class PeerRouting {
|
|
|
116
116
|
|
|
117
117
|
// compare hashes of the pub key
|
|
118
118
|
if (!recPeer.equals(peer)) {
|
|
119
|
-
throw
|
|
119
|
+
throw new CodeError('public key does not match id', 'ERR_PUBLIC_KEY_DOES_NOT_MATCH_ID')
|
|
120
120
|
}
|
|
121
121
|
|
|
122
122
|
if (recPeer.publicKey == null) {
|
|
123
|
-
throw
|
|
123
|
+
throw new CodeError('public key missing', 'ERR_PUBLIC_KEY_MISSING')
|
|
124
124
|
}
|
|
125
125
|
|
|
126
126
|
yield valueEvent({ from: peer, value: recPeer.publicKey })
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
-
throw
|
|
130
|
+
throw new CodeError(`Node not responding with its public key: ${peer.toString()}`, 'ERR_INVALID_RECORD')
|
|
131
131
|
}
|
|
132
132
|
|
|
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
|
|
@@ -207,7 +207,7 @@ export class PeerRouting {
|
|
|
207
207
|
}
|
|
208
208
|
|
|
209
209
|
if (!foundPeer) {
|
|
210
|
-
yield queryErrorEvent({ from: this.components.peerId, error:
|
|
210
|
+
yield queryErrorEvent({ from: this.components.peerId, error: new CodeError('Not found', 'ERR_NOT_FOUND') })
|
|
211
211
|
}
|
|
212
212
|
}
|
|
213
213
|
|
|
@@ -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) {
|
|
@@ -270,7 +270,7 @@ export class PeerRouting {
|
|
|
270
270
|
const errMsg = 'invalid record received, discarded'
|
|
271
271
|
this.log(errMsg)
|
|
272
272
|
|
|
273
|
-
yield queryErrorEvent({ from: event.from, error:
|
|
273
|
+
yield queryErrorEvent({ from: event.from, error: new CodeError(errMsg, 'ERR_INVALID_RECORD') })
|
|
274
274
|
continue
|
|
275
275
|
}
|
|
276
276
|
}
|
|
@@ -284,9 +284,9 @@ 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
|
-
throw
|
|
289
|
+
throw new CodeError('invalid record received', 'ERR_INVALID_RECORD')
|
|
290
290
|
}
|
|
291
291
|
|
|
292
292
|
await verifyRecord(this.validators, new Libp2pRecord(record.key, record.value, record.timeReceived))
|
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
|
|
@@ -216,6 +216,11 @@ export class Providers implements Startable {
|
|
|
216
216
|
return [...provs.keys()].map(peerIdStr => {
|
|
217
217
|
return peerIdFromString(peerIdStr)
|
|
218
218
|
})
|
|
219
|
+
}, {
|
|
220
|
+
// no timeout is specified for this queue so it will not
|
|
221
|
+
// throw, but this is required to get the right return
|
|
222
|
+
// type since p-queue@7.3.4
|
|
223
|
+
throwOnTimeout: true
|
|
219
224
|
})
|
|
220
225
|
}
|
|
221
226
|
}
|
|
@@ -223,7 +228,7 @@ export class Providers implements Startable {
|
|
|
223
228
|
/**
|
|
224
229
|
* Encode the given key its matching datastore key
|
|
225
230
|
*/
|
|
226
|
-
function makeProviderKey (cid: CID | string) {
|
|
231
|
+
function makeProviderKey (cid: CID | string): string {
|
|
227
232
|
const cidStr = typeof cid === 'string' ? cid : uint8ArrayToString(cid.multihash.bytes, 'base32')
|
|
228
233
|
|
|
229
234
|
return `${PROVIDER_KEY_PREFIX}/${cidStr}`
|
|
@@ -232,7 +237,7 @@ function makeProviderKey (cid: CID | string) {
|
|
|
232
237
|
/**
|
|
233
238
|
* Write a provider into the given store
|
|
234
239
|
*/
|
|
235
|
-
async function writeProviderEntry (store: Datastore, cid: CID, peer: PeerId, time: Date) {
|
|
240
|
+
async function writeProviderEntry (store: Datastore, cid: CID, peer: PeerId, time: Date): Promise<void> {
|
|
236
241
|
const dsKey = [
|
|
237
242
|
makeProviderKey(cid),
|
|
238
243
|
'/',
|
|
@@ -242,13 +247,13 @@ async function writeProviderEntry (store: Datastore, cid: CID, peer: PeerId, tim
|
|
|
242
247
|
const key = new Key(dsKey)
|
|
243
248
|
const buffer = Uint8Array.from(varint.encode(time.getTime()))
|
|
244
249
|
|
|
245
|
-
|
|
250
|
+
await store.put(key, buffer)
|
|
246
251
|
}
|
|
247
252
|
|
|
248
253
|
/**
|
|
249
254
|
* Parse the CID and provider peer id from the key
|
|
250
255
|
*/
|
|
251
|
-
function parseProviderKey (key: Key) {
|
|
256
|
+
function parseProviderKey (key: Key): { cid: string, peerId: string } {
|
|
252
257
|
const parts = key.toString().split('/')
|
|
253
258
|
|
|
254
259
|
if (parts.length !== 5) {
|
|
@@ -264,7 +269,7 @@ function parseProviderKey (key: Key) {
|
|
|
264
269
|
/**
|
|
265
270
|
* Load providers for the given CID from the store
|
|
266
271
|
*/
|
|
267
|
-
async function loadProviders (store: Datastore, cid: CID) {
|
|
272
|
+
async function loadProviders (store: Datastore, cid: CID): Promise<Map<string, Date>> {
|
|
268
273
|
const providers = new Map<string, Date>()
|
|
269
274
|
const query = store.query({ prefix: makeProviderKey(cid) })
|
|
270
275
|
|
|
@@ -276,6 +281,6 @@ async function loadProviders (store: Datastore, cid: CID) {
|
|
|
276
281
|
return providers
|
|
277
282
|
}
|
|
278
283
|
|
|
279
|
-
function readTime (buf: Uint8Array) {
|
|
284
|
+
function readTime (buf: Uint8Array): Date {
|
|
280
285
|
return new Date(varint.decode(buf))
|
|
281
286
|
}
|
package/src/query/manager.ts
CHANGED
|
@@ -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
|
}
|