@libp2p/kad-dht 11.0.8 → 12.0.0-4e0135c7d
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/README.md +78 -0
- package/dist/index.min.js +23 -23
- package/dist/src/constants.d.ts +2 -4
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +7 -9
- package/dist/src/constants.js.map +1 -1
- package/dist/src/content-fetching/index.d.ts +7 -7
- package/dist/src/content-fetching/index.d.ts.map +1 -1
- package/dist/src/content-fetching/index.js +13 -7
- package/dist/src/content-fetching/index.js.map +1 -1
- package/dist/src/content-routing/index.d.ts +5 -4
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +23 -13
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/index.d.ts +142 -28
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +87 -2
- package/dist/src/index.js.map +1 -1
- package/dist/src/kad-dht.d.ts +20 -21
- package/dist/src/kad-dht.d.ts.map +1 -1
- package/dist/src/kad-dht.js +181 -35
- package/dist/src/kad-dht.js.map +1 -1
- package/dist/src/message/dht.d.ts +35 -35
- package/dist/src/message/dht.d.ts.map +1 -1
- package/dist/src/message/dht.js +150 -130
- package/dist/src/message/dht.js.map +1 -1
- package/dist/src/message/utils.d.ts +5 -0
- package/dist/src/message/utils.d.ts.map +1 -0
- package/dist/src/message/utils.js +20 -0
- package/dist/src/message/utils.js.map +1 -0
- package/dist/src/network.d.ts +8 -8
- package/dist/src/network.d.ts.map +1 -1
- package/dist/src/network.js +30 -18
- package/dist/src/network.js.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 +48 -35
- package/dist/src/peer-routing/index.js.map +1 -1
- package/dist/src/providers.d.ts +7 -0
- package/dist/src/providers.d.ts.map +1 -1
- package/dist/src/providers.js.map +1 -1
- package/dist/src/query/events.d.ts +13 -12
- package/dist/src/query/events.d.ts.map +1 -1
- package/dist/src/query/events.js +2 -2
- package/dist/src/query/events.js.map +1 -1
- package/dist/src/query/manager.d.ts +8 -5
- package/dist/src/query/manager.d.ts.map +1 -1
- package/dist/src/query/manager.js +6 -6
- package/dist/src/query/manager.js.map +1 -1
- package/dist/src/query/query-path.d.ts +3 -3
- package/dist/src/query/query-path.d.ts.map +1 -1
- package/dist/src/query-self.d.ts +1 -1
- package/dist/src/query-self.d.ts.map +1 -1
- package/dist/src/query-self.js +2 -2
- package/dist/src/query-self.js.map +1 -1
- package/dist/src/routing-table/index.d.ts +5 -6
- package/dist/src/routing-table/index.d.ts.map +1 -1
- package/dist/src/routing-table/index.js +72 -58
- package/dist/src/routing-table/index.js.map +1 -1
- package/dist/src/routing-table/refresh.d.ts +1 -1
- package/dist/src/routing-table/refresh.d.ts.map +1 -1
- package/dist/src/routing-table/refresh.js +2 -2
- package/dist/src/routing-table/refresh.js.map +1 -1
- package/dist/src/rpc/handlers/add-provider.d.ts +2 -1
- package/dist/src/rpc/handlers/add-provider.d.ts.map +1 -1
- package/dist/src/rpc/handlers/add-provider.js +8 -6
- package/dist/src/rpc/handlers/add-provider.js.map +1 -1
- package/dist/src/rpc/handlers/find-node.d.ts +5 -3
- package/dist/src/rpc/handlers/find-node.d.ts.map +1 -1
- package/dist/src/rpc/handlers/find-node.js +22 -14
- package/dist/src/rpc/handlers/find-node.js.map +1 -1
- package/dist/src/rpc/handlers/get-providers.d.ts +5 -3
- package/dist/src/rpc/handlers/get-providers.d.ts.map +1 -1
- package/dist/src/rpc/handlers/get-providers.js +29 -16
- package/dist/src/rpc/handlers/get-providers.js.map +1 -1
- package/dist/src/rpc/handlers/get-value.d.ts +2 -1
- package/dist/src/rpc/handlers/get-value.d.ts.map +1 -1
- package/dist/src/rpc/handlers/get-value.js +16 -7
- package/dist/src/rpc/handlers/get-value.js.map +1 -1
- package/dist/src/rpc/handlers/ping.d.ts +5 -2
- package/dist/src/rpc/handlers/ping.d.ts.map +1 -1
- package/dist/src/rpc/handlers/ping.js +2 -2
- package/dist/src/rpc/handlers/ping.js.map +1 -1
- package/dist/src/rpc/handlers/put-value.d.ts +2 -1
- package/dist/src/rpc/handlers/put-value.d.ts.map +1 -1
- package/dist/src/rpc/handlers/put-value.js +8 -7
- package/dist/src/rpc/handlers/put-value.js.map +1 -1
- package/dist/src/rpc/index.d.ts +4 -3
- package/dist/src/rpc/index.d.ts.map +1 -1
- package/dist/src/rpc/index.js +11 -11
- package/dist/src/rpc/index.js.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 +2 -2
- package/dist/src/topology-listener.js.map +1 -1
- package/dist/src/utils.d.ts +5 -2
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/utils.js +32 -2
- package/dist/src/utils.js.map +1 -1
- package/package.json +13 -11
- package/src/constants.ts +7 -11
- package/src/content-fetching/index.ts +21 -14
- package/src/content-routing/index.ts +29 -18
- package/src/index.ts +148 -32
- package/src/kad-dht.ts +225 -56
- package/src/message/dht.proto +32 -32
- package/src/message/dht.ts +155 -138
- package/src/message/utils.ts +25 -0
- package/src/network.ts +41 -25
- package/src/peer-routing/index.ts +57 -42
- package/src/providers.ts +7 -0
- package/src/query/events.ts +14 -14
- package/src/query/manager.ts +14 -10
- package/src/query/query-path.ts +3 -3
- package/src/query-self.ts +3 -3
- package/src/routing-table/index.ts +86 -64
- package/src/routing-table/refresh.ts +4 -4
- package/src/rpc/handlers/add-provider.ts +10 -7
- package/src/rpc/handlers/find-node.ts +27 -18
- package/src/rpc/handlers/get-providers.ts +33 -20
- package/src/rpc/handlers/get-value.ts +18 -7
- package/src/rpc/handlers/ping.ts +7 -3
- package/src/rpc/handlers/put-value.ts +11 -9
- package/src/rpc/index.ts +14 -13
- package/src/topology-listener.ts +3 -3
- package/src/utils.ts +41 -2
- package/dist/src/dual-kad-dht.d.ts +0 -69
- package/dist/src/dual-kad-dht.d.ts.map +0 -1
- package/dist/src/dual-kad-dht.js +0 -304
- package/dist/src/dual-kad-dht.js.map +0 -1
- package/dist/src/message/index.d.ts +0 -35
- package/dist/src/message/index.d.ts.map +0 -1
- package/dist/src/message/index.js +0 -92
- package/dist/src/message/index.js.map +0 -1
- package/dist/typedoc-urls.json +0 -55
- package/src/dual-kad-dht.ts +0 -384
- package/src/message/index.ts +0 -117
package/src/index.ts
CHANGED
|
@@ -1,12 +1,94 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* This module implements the [libp2p Kademlia spec](https://github.com/libp2p/specs/blob/master/kad-dht/README.md) in TypeScript.
|
|
5
|
+
*
|
|
6
|
+
* The Kademlia DHT allow for several operations such as finding peers, searching for providers of DHT records, etc.
|
|
7
|
+
*
|
|
8
|
+
* @example Using with libp2p
|
|
9
|
+
*
|
|
10
|
+
* ```TypeScript
|
|
11
|
+
* import { kadDHT } from '@libp2p/kad-dht'
|
|
12
|
+
* import { createLibp2p } from 'libp2p'
|
|
13
|
+
* import { peerIdFromString } from '@libp2p/peer-id'
|
|
14
|
+
*
|
|
15
|
+
* const node = await createLibp2p({
|
|
16
|
+
* services: {
|
|
17
|
+
* dht: kadDHT()
|
|
18
|
+
* }
|
|
19
|
+
* })
|
|
20
|
+
*
|
|
21
|
+
* const peerId = peerIdFromString('QmFoo')
|
|
22
|
+
* const peerInfo = await libp2p.peerRouting.findPeer(peerId)
|
|
23
|
+
*
|
|
24
|
+
* console.info(peerInfo) // peer id, multiaddrs
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @example Connecting to the IPFS Amino DHT
|
|
28
|
+
*
|
|
29
|
+
* The [Amino DHT](https://blog.ipfs.tech/2023-09-amino-refactoring/) is a public-good DHT used by IPFS to fetch content, find peers, etc.
|
|
30
|
+
*
|
|
31
|
+
* If you are trying to access content on the public internet, this is the implementation you want.
|
|
32
|
+
*
|
|
33
|
+
* ```TypeScript
|
|
34
|
+
* import { kadDHT, removePrivateAddressesMapper } from '@libp2p/kad-dht'
|
|
35
|
+
* import { createLibp2p } from 'libp2p'
|
|
36
|
+
* import { peerIdFromString } from '@libp2p/peer-id'
|
|
37
|
+
*
|
|
38
|
+
* const node = await createLibp2p({
|
|
39
|
+
* services: {
|
|
40
|
+
* aminoDHT: kadDHT({
|
|
41
|
+
* protocol: '/ipfs/kad/1.0.0',
|
|
42
|
+
* addressFilter: removePrivateAddressesMapper
|
|
43
|
+
* })
|
|
44
|
+
* }
|
|
45
|
+
* })
|
|
46
|
+
*
|
|
47
|
+
* const peerId = peerIdFromString('QmFoo')
|
|
48
|
+
* const peerInfo = await libp2p.peerRouting.findPeer(peerId)
|
|
49
|
+
*
|
|
50
|
+
* console.info(peerInfo) // peer id, multiaddrs
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @example Connecting to a LAN-only DHT
|
|
54
|
+
*
|
|
55
|
+
* This DHT only works with privately dialable peers.
|
|
56
|
+
*
|
|
57
|
+
* This is for use when peers are on the local area network.
|
|
58
|
+
*
|
|
59
|
+
* ```TypeScript
|
|
60
|
+
* import { kadDHT, removePublicAddressesMapper } from '@libp2p/kad-dht'
|
|
61
|
+
* import { createLibp2p } from 'libp2p'
|
|
62
|
+
* import { peerIdFromString } from '@libp2p/peer-id'
|
|
63
|
+
*
|
|
64
|
+
* const node = await createLibp2p({
|
|
65
|
+
* services: {
|
|
66
|
+
* lanDHT: kadDHT({
|
|
67
|
+
* protocol: '/ipfs/lan/kad/1.0.0',
|
|
68
|
+
* addressFilter: removePublicAddressesMapper,
|
|
69
|
+
* clientMode: false
|
|
70
|
+
* })
|
|
71
|
+
* }
|
|
72
|
+
* })
|
|
73
|
+
*
|
|
74
|
+
* const peerId = peerIdFromString('QmFoo')
|
|
75
|
+
* const peerInfo = await libp2p.peerRouting.findPeer(peerId)
|
|
76
|
+
*
|
|
77
|
+
* console.info(peerInfo) // peer id, multiaddrs
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
|
|
81
|
+
import { KadDHT as KadDHTClass } from './kad-dht.js'
|
|
82
|
+
import { removePrivateAddressesMapper, removePublicAddressesMapper, passthroughMapper } from './utils.js'
|
|
2
83
|
import type { ProvidersInit } from './providers.js'
|
|
3
|
-
import type { Libp2pEvents,
|
|
84
|
+
import type { Libp2pEvents, ComponentLogger, TypedEventTarget, Metrics, PeerId, PeerInfo, PeerStore, RoutingOptions } from '@libp2p/interface'
|
|
4
85
|
import type { AddressManager, ConnectionManager, Registrar } from '@libp2p/interface-internal'
|
|
5
86
|
import type { Datastore } from 'interface-datastore'
|
|
6
87
|
import type { CID } from 'multiformats/cid'
|
|
7
|
-
import type {
|
|
88
|
+
import type { ProgressEvent } from 'progress-events'
|
|
8
89
|
|
|
9
90
|
export { Libp2pRecord as Record } from './record/index.js'
|
|
91
|
+
export { removePrivateAddressesMapper, removePublicAddressesMapper, passthroughMapper }
|
|
10
92
|
|
|
11
93
|
/**
|
|
12
94
|
* The types of events emitted during DHT queries
|
|
@@ -52,10 +134,6 @@ export type DHTProgressEvents =
|
|
|
52
134
|
ProgressEvent<'kad-dht:query:add-peer', AddPeerEvent> |
|
|
53
135
|
ProgressEvent<'kad-dht:query:dial-peer', DialPeerEvent>
|
|
54
136
|
|
|
55
|
-
export interface QueryOptions extends AbortOptions, ProgressOptions {
|
|
56
|
-
queryFuncTimeout?: number
|
|
57
|
-
}
|
|
58
|
-
|
|
59
137
|
/**
|
|
60
138
|
* Emitted when sending queries to remote peers
|
|
61
139
|
*/
|
|
@@ -146,44 +224,49 @@ export interface RoutingTable {
|
|
|
146
224
|
size: number
|
|
147
225
|
}
|
|
148
226
|
|
|
227
|
+
export interface PeerInfoMapper {
|
|
228
|
+
(peer: PeerInfo): PeerInfo
|
|
229
|
+
}
|
|
230
|
+
|
|
149
231
|
export interface KadDHT {
|
|
150
232
|
/**
|
|
151
233
|
* Get a value from the DHT, the final ValueEvent will be the best value
|
|
152
234
|
*/
|
|
153
|
-
get(key: Uint8Array, options?:
|
|
235
|
+
get(key: Uint8Array, options?: RoutingOptions): AsyncIterable<QueryEvent>
|
|
154
236
|
|
|
155
237
|
/**
|
|
156
238
|
* Find providers of a given CID
|
|
157
239
|
*/
|
|
158
|
-
findProviders(key: CID, options?:
|
|
240
|
+
findProviders(key: CID, options?: RoutingOptions): AsyncIterable<QueryEvent>
|
|
159
241
|
|
|
160
242
|
/**
|
|
161
243
|
* Find a peer on the DHT
|
|
162
244
|
*/
|
|
163
|
-
findPeer(id: PeerId, options?:
|
|
245
|
+
findPeer(id: PeerId, options?: RoutingOptions): AsyncIterable<QueryEvent>
|
|
164
246
|
|
|
165
247
|
/**
|
|
166
248
|
* Find the closest peers to the passed key
|
|
167
249
|
*/
|
|
168
|
-
getClosestPeers(key: Uint8Array, options?:
|
|
250
|
+
getClosestPeers(key: Uint8Array, options?: RoutingOptions): AsyncIterable<QueryEvent>
|
|
169
251
|
|
|
170
252
|
/**
|
|
171
253
|
* Store provider records for the passed CID on the DHT pointing to us
|
|
172
254
|
*/
|
|
173
|
-
provide(key: CID, options?:
|
|
255
|
+
provide(key: CID, options?: RoutingOptions): AsyncIterable<QueryEvent>
|
|
174
256
|
|
|
175
257
|
/**
|
|
176
258
|
* Store the passed value under the passed key on the DHT
|
|
177
259
|
*/
|
|
178
|
-
put(key: Uint8Array, value: Uint8Array, options?:
|
|
260
|
+
put(key: Uint8Array, value: Uint8Array, options?: RoutingOptions): AsyncIterable<QueryEvent>
|
|
179
261
|
|
|
180
262
|
/**
|
|
181
263
|
* Returns the mode this node is in
|
|
182
264
|
*/
|
|
183
|
-
getMode():
|
|
265
|
+
getMode(): 'client' | 'server'
|
|
184
266
|
|
|
185
267
|
/**
|
|
186
|
-
* If 'server' this node will respond to DHT queries, if 'client' this node
|
|
268
|
+
* If 'server' this node will respond to DHT queries, if 'client' this node
|
|
269
|
+
* will not.
|
|
187
270
|
*/
|
|
188
271
|
setMode(mode: 'client' | 'server'): Promise<void>
|
|
189
272
|
|
|
@@ -197,11 +280,6 @@ export interface SingleKadDHT extends KadDHT {
|
|
|
197
280
|
routingTable: RoutingTable
|
|
198
281
|
}
|
|
199
282
|
|
|
200
|
-
export interface DualKadDHT extends KadDHT {
|
|
201
|
-
wan: SingleKadDHT
|
|
202
|
-
lan: SingleKadDHT
|
|
203
|
-
}
|
|
204
|
-
|
|
205
283
|
/**
|
|
206
284
|
* A selector function takes a DHT key and a list of records and returns the
|
|
207
285
|
* index of the best record in the list
|
|
@@ -226,12 +304,17 @@ export type Validators = Record<string, ValidateFn>
|
|
|
226
304
|
|
|
227
305
|
export interface KadDHTInit {
|
|
228
306
|
/**
|
|
229
|
-
* How many peers to store in each kBucket
|
|
307
|
+
* How many peers to store in each kBucket
|
|
308
|
+
*
|
|
309
|
+
* @default 20
|
|
230
310
|
*/
|
|
231
311
|
kBucketSize?: number
|
|
232
312
|
|
|
233
313
|
/**
|
|
234
|
-
*
|
|
314
|
+
* If true, only ever be a DHT client. If false, be a DHT client until told
|
|
315
|
+
* to be a DHT server via `setMode`.
|
|
316
|
+
*
|
|
317
|
+
* @default false
|
|
235
318
|
*/
|
|
236
319
|
clientMode?: boolean
|
|
237
320
|
|
|
@@ -254,7 +337,9 @@ export interface KadDHTInit {
|
|
|
254
337
|
/**
|
|
255
338
|
* During startup we run the self-query at a shorter interval to ensure
|
|
256
339
|
* the containing node can respond to queries quickly. Set this interval
|
|
257
|
-
* here in ms
|
|
340
|
+
* here in ms.
|
|
341
|
+
*
|
|
342
|
+
* @default 1000
|
|
258
343
|
*/
|
|
259
344
|
initialQuerySelfInterval?: number
|
|
260
345
|
|
|
@@ -262,34 +347,55 @@ export interface KadDHTInit {
|
|
|
262
347
|
* After startup by default all queries will be paused until the initial
|
|
263
348
|
* self-query has run and there are some peers in the routing table.
|
|
264
349
|
*
|
|
265
|
-
* Pass true here to disable this behaviour.
|
|
350
|
+
* Pass true here to disable this behaviour.
|
|
351
|
+
*
|
|
352
|
+
* @default false
|
|
266
353
|
*/
|
|
267
354
|
allowQueryWithZeroPeers?: boolean
|
|
268
355
|
|
|
269
356
|
/**
|
|
270
|
-
*
|
|
357
|
+
* The network protocol to use
|
|
358
|
+
*
|
|
359
|
+
* @default "/ipfs/kad/1.0.0"
|
|
271
360
|
*/
|
|
272
|
-
|
|
361
|
+
protocol?: string
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* The logging prefix to use
|
|
365
|
+
*
|
|
366
|
+
* @default "libp2p:kad-dht"
|
|
367
|
+
*/
|
|
368
|
+
logPrefix?: string
|
|
273
369
|
|
|
274
370
|
/**
|
|
275
371
|
* How long to wait in ms when pinging DHT peers to decide if they
|
|
276
|
-
* should be evicted from the routing table or not
|
|
372
|
+
* should be evicted from the routing table or not.
|
|
373
|
+
*
|
|
374
|
+
* @default 10000
|
|
277
375
|
*/
|
|
278
376
|
pingTimeout?: number
|
|
279
377
|
|
|
280
378
|
/**
|
|
281
379
|
* How many peers to ping in parallel when deciding if they should
|
|
282
|
-
* be evicted from the routing table or not
|
|
380
|
+
* be evicted from the routing table or not
|
|
381
|
+
*
|
|
382
|
+
* @default 10
|
|
283
383
|
*/
|
|
284
384
|
pingConcurrency?: number
|
|
285
385
|
|
|
286
386
|
/**
|
|
287
|
-
* How many parallel incoming streams to allow on the DHT protocol per
|
|
387
|
+
* How many parallel incoming streams to allow on the DHT protocol per
|
|
388
|
+
* connection
|
|
389
|
+
*
|
|
390
|
+
* @default 32
|
|
288
391
|
*/
|
|
289
392
|
maxInboundStreams?: number
|
|
290
393
|
|
|
291
394
|
/**
|
|
292
|
-
* How many parallel outgoing streams to allow on the DHT protocol per
|
|
395
|
+
* How many parallel outgoing streams to allow on the DHT protocol per
|
|
396
|
+
* connection
|
|
397
|
+
*
|
|
398
|
+
* @default 64
|
|
293
399
|
*/
|
|
294
400
|
maxOutboundStreams?: number
|
|
295
401
|
|
|
@@ -297,6 +403,12 @@ export interface KadDHTInit {
|
|
|
297
403
|
* Initialization options for the Providers component
|
|
298
404
|
*/
|
|
299
405
|
providers?: ProvidersInit
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* For every incoming and outgoing PeerInfo, override address configuration
|
|
409
|
+
* with this filter.
|
|
410
|
+
*/
|
|
411
|
+
peerInfoMapper?(peer: PeerInfo): PeerInfo
|
|
300
412
|
}
|
|
301
413
|
|
|
302
414
|
export interface KadDHTComponents {
|
|
@@ -311,6 +423,10 @@ export interface KadDHTComponents {
|
|
|
311
423
|
logger: ComponentLogger
|
|
312
424
|
}
|
|
313
425
|
|
|
314
|
-
|
|
315
|
-
|
|
426
|
+
/**
|
|
427
|
+
* Creates a custom DHT implementation, please ensure you pass a `protocol`
|
|
428
|
+
* string as an option.
|
|
429
|
+
*/
|
|
430
|
+
export function kadDHT (init: KadDHTInit): (components: KadDHTComponents) => KadDHT {
|
|
431
|
+
return (components: KadDHTComponents) => new KadDHTClass(components, init)
|
|
316
432
|
}
|