diff --git a/src/index.js b/src/index.js index 483c2f8b..6f71ee3a 100644 --- a/src/index.js +++ b/src/index.js @@ -36,10 +36,14 @@ class KadDHT { * @param {number} options.kBucketSize k-bucket size (default 20) * @param {Datastore} options.datastore datastore (default MemoryDatastore) * @param {boolean} options.enabledDiscovery enable dht discovery (default true) + * @param {object} options.validators validators object with namespace as keys and function(key, record, callback) + * @param {object} options.selectors selectors object with namespace as keys and function(key, records) */ constructor (sw, options) { assert(sw, 'libp2p-kad-dht requires a instance of Switch') options = options || {} + options.validators = options.validators + options.selectors = options.selectors /** * Local reference to the libp2p-switch instance @@ -83,8 +87,15 @@ class KadDHT { */ this.providers = new Providers(this.datastore, this.peerInfo.id) - this.validators = { pk: libp2pRecord.validator.validators.pk } - this.selectors = { pk: libp2pRecord.selection.selectors.pk } + this.validators = { + pk: libp2pRecord.validator.validators.pk, + ...options.validators + } + + this.selectors = { + pk: libp2pRecord.selection.selectors.pk, + ...options.selectors + } this.network = new Network(this) diff --git a/test/kad-dht.spec.js b/test/kad-dht.spec.js index cdb4108d..b75626c5 100644 --- a/test/kad-dht.spec.js +++ b/test/kad-dht.spec.js @@ -139,6 +139,29 @@ describe('KadDHT', () => { expect(dht).to.have.property('routingTable') }) + it('create with validators and selectors', () => { + const sw = new Switch(peerInfos[0], new PeerBook()) + sw.transport.add('tcp', new TCP()) + sw.connection.addStreamMuxer(Mplex) + sw.connection.reuse() + const dht = new KadDHT(sw, { + validators: { + ipns: { + func: (key, record, cb) => cb() + } + }, + selectors: { + ipns: (key, records) => 0 + } + }) + + expect(dht).to.have.property('peerInfo').eql(peerInfos[0]) + expect(dht).to.have.property('switch').eql(sw) + expect(dht).to.have.property('routingTable') + expect(dht.validators).to.have.property('ipns') + expect(dht.selectors).to.have.property('ipns') + }) + it('should be able to start and stop', function (done) { const sw = new Switch(peerInfos[0], new PeerBook()) sw.transport.add('tcp', new TCP()) @@ -280,6 +303,39 @@ describe('KadDHT', () => { }) }) + it('put - get using key from provided validator and selector', function (done) { + this.timeout(10 * 1000) + const tdht = new TestDHT() + + tdht.spawn(2, { + validators: { + ipns: { + func: (key, record, cb) => cb() + } + }, + selectors: { + ipns: (key, records) => 0 + } + }, (err, dhts) => { + expect(err).to.not.exist() + const dhtA = dhts[0] + const dhtB = dhts[1] + + waterfall([ + (cb) => connect(dhtA, dhtB, cb), + (cb) => dhtA.put(Buffer.from('/ipns/hello'), Buffer.from('world'), cb), + (cb) => dhtB.get(Buffer.from('/ipns/hello'), { maxTimeout: 1000 }, cb), + (res, cb) => { + expect(res).to.eql(Buffer.from('world')) + cb() + } + ], (err) => { + expect(err).to.not.exist() + tdht.teardown(done) + }) + }) + }) + it('put - get should fail if unrecognized key prefix in get', function (done) { this.timeout(10 * 1000) const tdht = new TestDHT()