Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
fix(dial): proper error handling on dial (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire authored and daviddias committed Mar 27, 2017
1 parent 0edc487 commit 4d4f295
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"lodash.isfunction": "^3.0.8",
"mafmt": "^2.1.6",
"multiaddr": "^2.2.2",
"once": "^1.4.0",
"stream-to-pull-stream": "^1.7.2"
},
"contributors": [
Expand All @@ -60,4 +61,4 @@
"Richard Littauer <richard.littauer@gmail.com>",
"Stephen Whitmore <stephen.whitmore@gmail.com>"
]
}
}
13 changes: 11 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const mafmt = require('mafmt')
const includes = require('lodash.includes')
const isFunction = require('lodash.isfunction')
const Connection = require('interface-connection').Connection
const once = require('once')
const debug = require('debug')
const log = debug('libp2p:tcp:dial')

Expand All @@ -22,16 +23,24 @@ module.exports = class TCP {
cb = () => {}
}

cb = once(cb)
const cOpts = ma.toOptions()
log('Connecting to %s %s', cOpts.port, cOpts.host)

const rawSocket = net.connect(cOpts, cb)

const rawSocket = net.connect(cOpts)
rawSocket.once('timeout', () => {
log('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 conn = new Connection(socket)
Expand Down
7 changes: 7 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
const conn = tcp.dial(ma)
const connWrap1 = new Connection(conn)
Expand Down

0 comments on commit 4d4f295

Please sign in to comment.