From e64af85d6ef02d99521689ed8b60e0c3702efbc5 Mon Sep 17 00:00:00 2001 From: Joel Gustafson Date: Wed, 7 Dec 2022 05:08:59 -0500 Subject: [PATCH] feat: allow passing ProvidersInit in KadDHT constructor (#404) The `Providers` component takes a `ProvidersInit` object, but there's not actually a way to provide it with one from the `KadDHT` constructor. This means users aren't able to override the defaults for how long provider records are valid for and how frequently to clean up expired provider records. Co-authored-by: Alex Potsides --- src/index.ts | 6 ++++++ src/kad-dht.ts | 5 +++-- test/kad-dht.spec.ts | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 94394cbf..eb77c35e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import { KadDHT as SingleKadDHT } from './kad-dht.js' import { DualKadDHT } from './dual-kad-dht.js' +import type { ProvidersInit } from './providers.js' import type { Selectors, Validators } from '@libp2p/interface-dht' import type { Registrar } from '@libp2p/interface-registrar' import type { AddressManager } from '@libp2p/interface-address-manager' @@ -62,6 +63,11 @@ export interface KadDHTInit { * How many parallel outgoing streams to allow on the DHT protocol per-connection */ maxOutboundStreams?: number + + /** + * Initialization options for the Providers component + */ + providers?: ProvidersInit } export interface KadDHTComponents { diff --git a/src/kad-dht.ts b/src/kad-dht.ts index 098144bb..3108bc80 100644 --- a/src/kad-dht.ts +++ b/src/kad-dht.ts @@ -82,7 +82,8 @@ export class KadDHT extends EventEmitter implements DHT { pingTimeout, pingConcurrency, maxInboundStreams, - maxOutboundStreams + maxOutboundStreams, + providers: providersInit } = init this.running = false @@ -102,7 +103,7 @@ export class KadDHT extends EventEmitter implements DHT { protocol: this.protocol }) - this.providers = new Providers(components) + this.providers = new Providers(components, providersInit ?? {}) this.validators = { ...recordValidators, diff --git a/test/kad-dht.spec.ts b/test/kad-dht.spec.ts index 6b4299af..13e89294 100644 --- a/test/kad-dht.spec.ts +++ b/test/kad-dht.spec.ts @@ -91,6 +91,20 @@ describe('KadDHT', () => { expect(dht).to.have.property('getMode') expect(dht).to.have.property('setMode') }) + + it('forward providers init options to providers component', async () => { + const dht = await tdht.spawn({ + kBucketSize: 5, + providers: { + cleanupInterval: 60, + provideValidity: 60 * 10 + } + }) + expect(dht.lan.providers).to.have.property('cleanupInterval', 60) + expect(dht.lan.providers).to.have.property('provideValidity', 60 * 10) + expect(dht.wan.providers).to.have.property('cleanupInterval', 60) + expect(dht.wan.providers).to.have.property('provideValidity', 60 * 10) + }) }) describe('start and stop', () => {