Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit e64af85

Browse files
joeltgachingbrain
andauthored
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 <[email protected]>
1 parent e27747a commit e64af85

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { KadDHT as SingleKadDHT } from './kad-dht.js'
22
import { DualKadDHT } from './dual-kad-dht.js'
3+
import type { ProvidersInit } from './providers.js'
34
import type { Selectors, Validators } from '@libp2p/interface-dht'
45
import type { Registrar } from '@libp2p/interface-registrar'
56
import type { AddressManager } from '@libp2p/interface-address-manager'
@@ -62,6 +63,11 @@ export interface KadDHTInit {
6263
* How many parallel outgoing streams to allow on the DHT protocol per-connection
6364
*/
6465
maxOutboundStreams?: number
66+
67+
/**
68+
* Initialization options for the Providers component
69+
*/
70+
providers?: ProvidersInit
6571
}
6672

6773
export interface KadDHTComponents {

src/kad-dht.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT {
8282
pingTimeout,
8383
pingConcurrency,
8484
maxInboundStreams,
85-
maxOutboundStreams
85+
maxOutboundStreams,
86+
providers: providersInit
8687
} = init
8788

8889
this.running = false
@@ -102,7 +103,7 @@ export class KadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT {
102103
protocol: this.protocol
103104
})
104105

105-
this.providers = new Providers(components)
106+
this.providers = new Providers(components, providersInit ?? {})
106107

107108
this.validators = {
108109
...recordValidators,

test/kad-dht.spec.ts

+14
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@ describe('KadDHT', () => {
9191
expect(dht).to.have.property('getMode')
9292
expect(dht).to.have.property('setMode')
9393
})
94+
95+
it('forward providers init options to providers component', async () => {
96+
const dht = await tdht.spawn({
97+
kBucketSize: 5,
98+
providers: {
99+
cleanupInterval: 60,
100+
provideValidity: 60 * 10
101+
}
102+
})
103+
expect(dht.lan.providers).to.have.property('cleanupInterval', 60)
104+
expect(dht.lan.providers).to.have.property('provideValidity', 60 * 10)
105+
expect(dht.wan.providers).to.have.property('cleanupInterval', 60)
106+
expect(dht.wan.providers).to.have.property('provideValidity', 60 * 10)
107+
})
94108
})
95109

96110
describe('start and stop', () => {

0 commit comments

Comments
 (0)