|
78 | 78 | * |
79 | 79 | * console.info(peerInfo) // peer id, multiaddrs |
80 | 80 | * ``` |
| 81 | + * |
| 82 | + * @example Connecting to both a LAN-only DHT and the IPFS Amino DHT |
| 83 | + * |
| 84 | + * When using multiple DHTs, you should specify distinct datastore, metrics and |
| 85 | + * log prefixes to ensure that data is kept separate for each instance. |
| 86 | + * |
| 87 | + * ```TypeScript |
| 88 | + * import { kadDHT, removePublicAddressesMapper, removePrivateAddressesMapper } from '@libp2p/kad-dht' |
| 89 | + * import { createLibp2p } from 'libp2p' |
| 90 | + * import { peerIdFromString } from '@libp2p/peer-id' |
| 91 | + * |
| 92 | + * const node = await createLibp2p({ |
| 93 | + * services: { |
| 94 | + * lanDHT: kadDHT({ |
| 95 | + * protocol: '/ipfs/lan/kad/1.0.0', |
| 96 | + * peerInfoMapper: removePublicAddressesMapper, |
| 97 | + * clientMode: false, |
| 98 | + * logPrefix: 'libp2p:dht-lan', |
| 99 | + * datastorePrefix: '/dht-lan', |
| 100 | + * metricsPrefix: 'libp2p_dht_lan' |
| 101 | + * }), |
| 102 | + * aminoDHT: kadDHT({ |
| 103 | + * protocol: '/ipfs/kad/1.0.0', |
| 104 | + * peerInfoMapper: removePrivateAddressesMapper, |
| 105 | + * logPrefix: 'libp2p:dht-amino', |
| 106 | + * datastorePrefix: '/dht-amino', |
| 107 | + * metricsPrefix: 'libp2p_dht_amino' |
| 108 | + * }) |
| 109 | + * } |
| 110 | + * }) |
| 111 | + * |
| 112 | + * const peerId = peerIdFromString('QmFoo') |
| 113 | + * const peerInfo = await node.peerRouting.findPeer(peerId) |
| 114 | + * |
| 115 | + * console.info(peerInfo) // peer id, multiaddrs |
| 116 | + * ``` |
81 | 117 | */ |
82 | 118 |
|
83 | 119 | import { KadDHT as KadDHTClass } from './kad-dht.js' |
84 | 120 | import { MessageType } from './message/dht.js' |
85 | 121 | import { removePrivateAddressesMapper, removePublicAddressesMapper, passthroughMapper } from './utils.js' |
86 | | -import type { ProvidersInit } from './providers.js' |
87 | 122 | import type { Libp2pEvents, ComponentLogger, TypedEventTarget, Metrics, PeerId, PeerInfo, PeerStore, RoutingOptions, PrivateKey } from '@libp2p/interface' |
88 | 123 | import type { AddressManager, ConnectionManager, Registrar } from '@libp2p/interface-internal' |
89 | 124 | import type { AdaptiveTimeoutInit } from '@libp2p/utils/src/adaptive-timeout.js' |
@@ -251,6 +286,12 @@ export interface KadDHT { |
251 | 286 | */ |
252 | 287 | provide(key: CID, options?: RoutingOptions): AsyncIterable<QueryEvent> |
253 | 288 |
|
| 289 | + /** |
| 290 | + * Provider records must be re-published every 24 hours - pass a previously |
| 291 | + * provided CID here to not re-publish a record for it any more |
| 292 | + */ |
| 293 | + cancelReprovide(key: CID): Promise<void> |
| 294 | + |
254 | 295 | /** |
255 | 296 | * Store the passed value under the passed key on the DHT |
256 | 297 | */ |
@@ -299,7 +340,61 @@ export type Selectors = Record<string, SelectFn> |
299 | 340 | */ |
300 | 341 | export type Validators = Record<string, ValidateFn> |
301 | 342 |
|
302 | | -export type { ProvidersInit } |
| 343 | +export interface ProvidersInit { |
| 344 | + /** |
| 345 | + * @default 256 |
| 346 | + */ |
| 347 | + cacheSize?: number |
| 348 | + /** |
| 349 | + * How often invalid records are cleaned. (in seconds) |
| 350 | + * |
| 351 | + * @default 5400 |
| 352 | + */ |
| 353 | + cleanupInterval?: number |
| 354 | + /** |
| 355 | + * How long is a provider valid for. (in seconds) |
| 356 | + * |
| 357 | + * @default 86400 |
| 358 | + */ |
| 359 | + provideValidity?: number |
| 360 | +} |
| 361 | + |
| 362 | +export interface ReProvideInit { |
| 363 | + /** |
| 364 | + * How many re-provide operations to run simultaneously |
| 365 | + * |
| 366 | + * @default 10 |
| 367 | + */ |
| 368 | + concurrency?: number |
| 369 | + |
| 370 | + /** |
| 371 | + * How long to let the re-provide queue grow |
| 372 | + * |
| 373 | + * @default 16384 |
| 374 | + */ |
| 375 | + maxQueueSize?: number |
| 376 | + |
| 377 | + /** |
| 378 | + * How long before the record expiry to re-provide in ms |
| 379 | + * |
| 380 | + * @default 7200000 |
| 381 | + */ |
| 382 | + threshold?: number |
| 383 | + |
| 384 | + /** |
| 385 | + * How often to check which records need reproviding in ms |
| 386 | + * |
| 387 | + * @default 3600000 |
| 388 | + */ |
| 389 | + interval?: number |
| 390 | + |
| 391 | + /** |
| 392 | + * How long provider records are valid for in ms |
| 393 | + * |
| 394 | + * @default 86400000 |
| 395 | + */ |
| 396 | + validity?: number |
| 397 | +} |
303 | 398 |
|
304 | 399 | export interface KadDHTInit { |
305 | 400 | /** |
@@ -396,6 +491,20 @@ export interface KadDHTInit { |
396 | 491 | */ |
397 | 492 | logPrefix?: string |
398 | 493 |
|
| 494 | + /** |
| 495 | + * The datastore prefix to use |
| 496 | + * |
| 497 | + * @default "/dht" |
| 498 | + */ |
| 499 | + datastorePrefix?: string |
| 500 | + |
| 501 | + /** |
| 502 | + * The metrics prefix to use |
| 503 | + * |
| 504 | + * @default "libp2p_kad_dht" |
| 505 | + */ |
| 506 | + metricsPrefix?: string |
| 507 | + |
399 | 508 | /** |
400 | 509 | * Settings for how long to wait in ms when pinging DHT peers to decide if |
401 | 510 | * they should be evicted from the routing table or not. |
@@ -459,6 +568,11 @@ export interface KadDHTInit { |
459 | 568 | */ |
460 | 569 | providers?: ProvidersInit |
461 | 570 |
|
| 571 | + /** |
| 572 | + * Initialization options for the Reprovider component |
| 573 | + */ |
| 574 | + reprovide?: ReProvideInit |
| 575 | + |
462 | 576 | /** |
463 | 577 | * For every incoming and outgoing PeerInfo, override address configuration |
464 | 578 | * with this filter. |
|
0 commit comments