mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-05-01 21:42: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.port = parseInt(process.env.IPFS_SWARM_PORT, 10) || 4001
|
||||||
self.connections = {} // {peerIdB58: {conn: <>, socket: <>}
|
self.connections = {} // {peerIdB58: {conn: <>, socket: <>}
|
||||||
self.handles = []
|
self.handles = {}
|
||||||
|
|
||||||
// set the listener
|
// set the listener
|
||||||
|
|
||||||
@ -140,7 +140,7 @@ function Swarm () {
|
|||||||
if (self.handles[protocol]) {
|
if (self.handles[protocol]) {
|
||||||
return handlerFunc(new Error('Handle for protocol already exists', 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)
|
log.info('Registered handler for protocol:', protocol)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,8 +165,8 @@ function Swarm () {
|
|||||||
errorUp(self, stream)
|
errorUp(self, stream)
|
||||||
var msH = new Select()
|
var msH = new Select()
|
||||||
msH.handle(stream)
|
msH.handle(stream)
|
||||||
self.handles.forEach(function (handle) {
|
Object.keys(self.handles).forEach(function (protocol) {
|
||||||
msH.addHandler(handle.protocol, handle.func)
|
msH.addHandler(protocol, self.handles[protocol])
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,6 +73,18 @@ experiment('BASICS', function () {
|
|||||||
done()
|
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 () {
|
experiment('BASE', function () {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user