Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"dependencies": {
"async": "^2.6.1",
"base32.js": "~0.1.0",
"chai-checkmark": "^1.0.1",
"cids": "~0.5.7",
"debug": "^4.1.1",
"err-code": "^1.1.2",
Expand Down
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { EventEmitter } = require('events')
const libp2pRecord = require('libp2p-record')
const MemoryStore = require('interface-datastore').MemoryDatastore
const waterfall = require('async/waterfall')
Expand Down Expand Up @@ -27,7 +28,7 @@ const assert = require('assert')
*
* Original implementation in go: https://github.com/libp2p/go-libp2p-kad-dht.
*/
class KadDHT {
class KadDHT extends EventEmitter {
/**
* Create a new KadDHT.
*
Expand All @@ -40,6 +41,7 @@ class KadDHT {
* @param {object} options.selectors selectors object with namespace as keys and function(key, records)
*/
constructor (sw, options) {
super()
assert(sw, 'libp2p-kad-dht requires a instance of Switch')
options = options || {}
options.validators = options.validators || {}
Expand Down Expand Up @@ -608,6 +610,10 @@ class KadDHT {
], callback)
})
}

_peerDiscovered (peerInfo) {
this.emit('peer', peerInfo)
}
}

module.exports = KadDHT
2 changes: 2 additions & 0 deletions src/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ class Network {
return this._log.error('Failed to add to the routing table', err)
}

this.dht._peerDiscovered(peer)

this._log('added to the routing table: %s', peer.id.toB58String())
})
})
Expand Down
1 change: 1 addition & 0 deletions src/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ function execQuery (next, query, path, callback) {
return cb()
}
closer = query.dht.peerBook.put(closer)
query.dht._peerDiscovered(closer)
addPeerToQuery(closer.id, query.dht, path, cb)
}, callback)
} else {
Expand Down
26 changes: 26 additions & 0 deletions test/kad-dht.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-checkmark'))
const expect = chai.expect
const sinon = require('sinon')
const series = require('async/series')
Expand Down Expand Up @@ -256,6 +257,31 @@ describe('KadDHT', () => {
})
})

it('should emit a peer event when a peer is connected', function (done) {
this.timeout(10 * 1000)
const tdht = new TestDHT()

tdht.spawn(2, (err, dhts) => {
expect(err).to.not.exist()
const dhtA = dhts[0]
const dhtB = dhts[1]

dhtA.on('peer', (peerInfo) => {
expect(peerInfo).to.exist().mark()
})

dhtB.on('peer', (peerInfo) => {
expect(peerInfo).to.exist().mark()
})

connect(dhtA, dhtB, (err) => {
expect(err).to.not.exist()
})
})

expect(2).checks(done)
})

it('put - get', function (done) {
this.timeout(10 * 1000)
const tdht = new TestDHT()
Expand Down