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

fix: handle listen error #224

Merged
merged 1 commit into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ export class TCPListener extends EventEmitter<ListenerEvents> implements Listene

return await new Promise<void>((resolve, reject) => {
const options = multiaddrToNetConfig(listeningAddr)
this.server.listen(options, (err?: any) => {
if (err != null) {
return reject(err)
}
this.server.on('error', (err) => {
reject(err)
})
this.server.listen(options, () => {
log('Listening on %s', this.server.address())
resolve()
})
Expand Down
16 changes: 16 additions & 0 deletions test/listen-dial.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ describe('listen', () => {
await listener.listen(mh)
})

it('errors when listening on busy port', async () => {
const mh = multiaddr('/ip4/127.0.0.1/tcp/0')
listener = transport.createListener({
upgrader
})
await listener.listen(mh)

const listener2 = transport.createListener({
upgrader
})

const mh2 = listener.getAddrs()[0]
await expect(listener2.listen(mh2)).to.eventually.be.rejected()
.with.property('code', 'EADDRINUSE')
})

it('listen on IPv6 addr', async () => {
if (isCI != null) {
return
Expand Down