Skip to content

Commit b453bd4

Browse files
committed
Merge pull request libp2p#46 from diasdavid/feat/improve-identify
Freeze handling conns till identify is finished on the incomming multiplexed streams
2 parents 02dd32e + 0143ab6 commit b453bd4

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

src/index.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,25 @@ function Swarm (peerInfo) {
152152
var peerIdForConn
153153

154154
muxedConn.on('stream', (conn) => {
155-
if (peerIdForConn) {
156-
conn.peerId = peerIdForConn
155+
function gotId () {
156+
if (peerIdForConn) {
157+
conn.peerId = peerIdForConn
158+
connHandler(conn)
159+
} else {
160+
setTimeout(gotId, 100)
161+
}
162+
}
163+
164+
if (this.identify) {
165+
return gotId()
157166
}
167+
158168
connHandler(conn)
159169
})
160170

161171
// if identify is enabled, attempt to do it for muxer reuse
162172
if (this.identify) {
173+
console.log('exec identify')
163174
identify.exec(conn, muxedConn, peerInfo, (err, pi) => {
164175
if (err) {
165176
return console.log('Identify exec failed', err)

test/09-swarm-with-muxing.node.js

+1-15
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ describe('high level API - with everything mixed all together!', function () {
190190

191191
it('dial from tcp+ws to tcp+ws', (done) => {
192192
swarmC.handle('/mamao/1.0.0', (conn) => {
193+
expect(conn.peerId).to.exist
193194
conn.pipe(conn)
194195
})
195196

@@ -204,21 +205,6 @@ describe('high level API - with everything mixed all together!', function () {
204205
})
205206
})
206207

207-
it('again, so that identify had time', (done) => {
208-
swarmC.handle('/mamao/1.0.0', (conn) => {
209-
expect(conn.peerId).to.exist
210-
conn.pipe(conn)
211-
})
212-
213-
swarmA.dial(peerC, '/mamao/1.0.0', (err, conn) => {
214-
expect(err).to.not.exist
215-
expect(conn.peerId).to.exist
216-
conn.end()
217-
conn.on('data', () => {}) // let it flow.. let it flooooow
218-
conn.on('end', done)
219-
})
220-
})
221-
222208
it('close a muxer emits event', (done) => {
223209
swarmC.close(() => {})
224210
swarmA.once('peer-mux-closed', (peerInfo) => {

0 commit comments

Comments
 (0)