fix(dial): proper error handling on dial (#77)

This commit is contained in:
Friedel Ziegelmayer
2017-03-27 17:18:55 +02:00
committed by David Dias
parent 0edc487b23
commit 4d4f295dd5
3 changed files with 20 additions and 3 deletions

View File

@ -48,6 +48,7 @@
"lodash.isfunction": "^3.0.8", "lodash.isfunction": "^3.0.8",
"mafmt": "^2.1.6", "mafmt": "^2.1.6",
"multiaddr": "^2.2.2", "multiaddr": "^2.2.2",
"once": "^1.4.0",
"stream-to-pull-stream": "^1.7.2" "stream-to-pull-stream": "^1.7.2"
}, },
"contributors": [ "contributors": [

View File

@ -6,6 +6,7 @@ const mafmt = require('mafmt')
const includes = require('lodash.includes') const includes = require('lodash.includes')
const isFunction = require('lodash.isfunction') const isFunction = require('lodash.isfunction')
const Connection = require('interface-connection').Connection const Connection = require('interface-connection').Connection
const once = require('once')
const debug = require('debug') const debug = require('debug')
const log = debug('libp2p:tcp:dial') const log = debug('libp2p:tcp:dial')
@ -22,16 +23,24 @@ module.exports = class TCP {
cb = () => {} cb = () => {}
} }
cb = once(cb)
const cOpts = ma.toOptions() const cOpts = ma.toOptions()
log('Connecting to %s %s', cOpts.port, cOpts.host) log('Connecting to %s %s', cOpts.port, cOpts.host)
const rawSocket = net.connect(cOpts, cb) const rawSocket = net.connect(cOpts)
rawSocket.once('timeout', () => { rawSocket.once('timeout', () => {
log('timeout') log('timeout')
rawSocket.emit('error', new Error('Timeout')) rawSocket.emit('error', new Error('Timeout'))
}) })
rawSocket.once('error', cb)
rawSocket.once('connect', () => {
rawSocket.removeListener('error', cb)
cb()
})
const socket = toPull.duplex(rawSocket) const socket = toPull.duplex(rawSocket)
const conn = new Connection(socket) const conn = new Connection(socket)

View File

@ -471,6 +471,13 @@ describe('Connection wrap', () => {
) )
}) })
it('dial error', (done) => {
tcp.dial(multiaddr('/ip4/999.0.0.1/tcp/1234'), (err) => {
expect(err).to.exist()
done()
})
})
it('matryoshka wrap', (done) => { it('matryoshka wrap', (done) => {
const conn = tcp.dial(ma) const conn = tcp.dial(ma)
const connWrap1 = new Connection(conn) const connWrap1 = new Connection(conn)