swarm: Fix self.handles data structure

This commit is contained in:
dignifiedquire 2015-07-31 17:56:36 +02:00
parent 85e312dc66
commit 13659ecb40
2 changed files with 16 additions and 4 deletions

View File

@ -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])
})
}

View File

@ -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 () {