Skip to content
This repository was archived by the owner on Aug 23, 2019. It is now read-only.

Commit 5fb9c8a

Browse files
fix: correctly handle multiplex stream muxers (#179)
1 parent 44ed367 commit 5fb9c8a

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,4 @@
8484
"Sid Harder <[email protected]>",
8585
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <[email protected]>"
8686
]
87-
}
87+
}

src/dial.js

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -132,44 +132,45 @@ module.exports = function dial (swarm) {
132132
// - add the muxedConn to the list of muxedConns
133133
// - add incomming new streams to connHandler
134134

135-
nextMuxer(muxers.shift())
135+
const ms = new multistream.Dialer()
136+
ms.handle(conn, (err) => {
137+
if (err) {
138+
return callback(new Error('multistream not supported'))
139+
}
140+
141+
nextMuxer(muxers.shift())
142+
})
136143

137144
function nextMuxer (key) {
138-
const ms = new multistream.Dialer()
139-
ms.handle(conn, (err) => {
145+
log('selecting %s', key)
146+
ms.select(key, (err, conn) => {
140147
if (err) {
141-
return callback(new Error('multistream not supported'))
142-
}
143-
log('selecting %s', key)
144-
ms.select(key, (err, conn) => {
145-
if (err) {
146-
if (muxers.length === 0) {
147-
cb(new Error('could not upgrade to stream muxing'))
148-
} else {
149-
nextMuxer(muxers.shift())
150-
}
151-
return
148+
if (muxers.length === 0) {
149+
cb(new Error('could not upgrade to stream muxing'))
150+
} else {
151+
nextMuxer(muxers.shift())
152152
}
153+
return
154+
}
153155

154-
const muxedConn = swarm.muxers[key].dialer(conn)
155-
swarm.muxedConns[b58Id] = {}
156-
swarm.muxedConns[b58Id].muxer = muxedConn
157-
// should not be needed anymore - swarm.muxedConns[b58Id].conn = conn
158-
159-
swarm.emit('peer-mux-established', pi)
156+
const muxedConn = swarm.muxers[key].dialer(conn)
157+
swarm.muxedConns[b58Id] = {}
158+
swarm.muxedConns[b58Id].muxer = muxedConn
159+
// should not be needed anymore - swarm.muxedConns[b58Id].conn = conn
160160

161-
muxedConn.once('close', () => {
162-
delete swarm.muxedConns[pi.id.toB58String()]
163-
swarm.emit('peer-mux-closed', pi)
164-
})
161+
swarm.emit('peer-mux-established', pi)
165162

166-
// For incoming streams, in case identify is on
167-
muxedConn.on('stream', (conn) => {
168-
protocolMuxer(swarm.protocols, conn)
169-
})
163+
muxedConn.once('close', () => {
164+
delete swarm.muxedConns[pi.id.toB58String()]
165+
swarm.emit('peer-mux-closed', pi)
166+
})
170167

171-
cb(null, muxedConn)
168+
// For incoming streams, in case identify is on
169+
muxedConn.on('stream', (conn) => {
170+
protocolMuxer(swarm.protocols, conn)
172171
})
172+
173+
cb(null, muxedConn)
173174
})
174175
}
175176
}

0 commit comments

Comments
 (0)