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

Commit ba0a537

Browse files
authored
feat: emit event on peer connected (#66)
1 parent b2a9fc5 commit ba0a537

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"dependencies": {
4040
"async": "^2.6.1",
4141
"base32.js": "~0.1.0",
42+
"chai-checkmark": "^1.0.1",
4243
"cids": "~0.5.7",
4344
"debug": "^4.1.1",
4445
"err-code": "^1.1.2",

src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const { EventEmitter } = require('events')
34
const libp2pRecord = require('libp2p-record')
45
const MemoryStore = require('interface-datastore').MemoryDatastore
56
const waterfall = require('async/waterfall')
@@ -27,7 +28,7 @@ const assert = require('assert')
2728
*
2829
* Original implementation in go: https://github.com/libp2p/go-libp2p-kad-dht.
2930
*/
30-
class KadDHT {
31+
class KadDHT extends EventEmitter {
3132
/**
3233
* Create a new KadDHT.
3334
*
@@ -40,6 +41,7 @@ class KadDHT {
4041
* @param {object} options.selectors selectors object with namespace as keys and function(key, records)
4142
*/
4243
constructor (sw, options) {
44+
super()
4345
assert(sw, 'libp2p-kad-dht requires a instance of Switch')
4446
options = options || {}
4547
options.validators = options.validators || {}
@@ -608,6 +610,10 @@ class KadDHT {
608610
], callback)
609611
})
610612
}
613+
614+
_peerDiscovered (peerInfo) {
615+
this.emit('peer', peerInfo)
616+
}
611617
}
612618

613619
module.exports = KadDHT

src/network.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ class Network {
122122
return this._log.error('Failed to add to the routing table', err)
123123
}
124124

125+
this.dht._peerDiscovered(peer)
126+
125127
this._log('added to the routing table: %s', peer.id.toB58String())
126128
})
127129
})

src/query.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ function execQuery (next, query, path, callback) {
206206
return cb()
207207
}
208208
closer = query.dht.peerBook.put(closer)
209+
query.dht._peerDiscovered(closer)
209210
addPeerToQuery(closer.id, query.dht, path, cb)
210211
}, callback)
211212
} else {

test/kad-dht.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const chai = require('chai')
55
chai.use(require('dirty-chai'))
6+
chai.use(require('chai-checkmark'))
67
const expect = chai.expect
78
const sinon = require('sinon')
89
const series = require('async/series')
@@ -256,6 +257,31 @@ describe('KadDHT', () => {
256257
})
257258
})
258259

260+
it('should emit a peer event when a peer is connected', function (done) {
261+
this.timeout(10 * 1000)
262+
const tdht = new TestDHT()
263+
264+
tdht.spawn(2, (err, dhts) => {
265+
expect(err).to.not.exist()
266+
const dhtA = dhts[0]
267+
const dhtB = dhts[1]
268+
269+
dhtA.on('peer', (peerInfo) => {
270+
expect(peerInfo).to.exist().mark()
271+
})
272+
273+
dhtB.on('peer', (peerInfo) => {
274+
expect(peerInfo).to.exist().mark()
275+
})
276+
277+
connect(dhtA, dhtB, (err) => {
278+
expect(err).to.not.exist()
279+
})
280+
})
281+
282+
expect(2).checks(done)
283+
})
284+
259285
it('put - get', function (done) {
260286
this.timeout(10 * 1000)
261287
const tdht = new TestDHT()

0 commit comments

Comments
 (0)