mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-05-01 05:22:14 +00:00
swarm: Fix self.handles data structure
This commit is contained in:
parent
85e312dc66
commit
13659ecb40
@ -20,7 +20,7 @@ function Swarm () {
|
||||
|
||||
self.port = parseInt(process.env.IPFS_SWARM_PORT, 10) || 4001
|
||||
self.connections = {} // {peerIdB58: {conn: <>, socket: <>}
|
||||
self.handles = []
|
||||
self.handles = {}
|
||||
|
||||
// set the listener
|
||||
|
||||
@ -140,7 +140,7 @@ function Swarm () {
|
||||
if (self.handles[protocol]) {
|
||||
return handlerFunc(new Error('Handle for protocol already exists', protocol))
|
||||
}
|
||||
self.handles.push({ protocol: protocol, func: handlerFunc })
|
||||
self.handles[protocol] = handlerFunc
|
||||
log.info('Registered handler for protocol:', protocol)
|
||||
}
|
||||
|
||||
@ -165,8 +165,8 @@ function Swarm () {
|
||||
errorUp(self, stream)
|
||||
var msH = new Select()
|
||||
msH.handle(stream)
|
||||
self.handles.forEach(function (handle) {
|
||||
msH.addHandler(handle.protocol, handle.func)
|
||||
Object.keys(self.handles).forEach(function (protocol) {
|
||||
msH.addHandler(protocol, self.handles[protocol])
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -73,6 +73,18 @@ experiment('BASICS', function () {
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
experiment('Swarm.registerHandler', function () {
|
||||
test('throws when registering a protcol handler twice', function (done) {
|
||||
var swarm = new Swarm()
|
||||
swarm.registerHandler('/sparkles/1.1.1', function () {})
|
||||
swarm.registerHandler('/sparkles/1.1.1', function (err) {
|
||||
expect(err).to.be.an.instanceOf(Error)
|
||||
expect(err.message).to.be.equal('Handle for protocol already exists')
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
experiment('BASE', function () {
|
||||
|
Loading…
x
Reference in New Issue
Block a user