Skip to content
This repository has been archived by the owner on Mar 11, 2020. It is now read-only.

feat: make listen take an array of addrs #46

Merged
merged 2 commits into from
Apr 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ A valid transport (one that follows the interface defined) must implement the fo
- event: 'close'
- event: 'connection'
- event: 'error'
- `<Promise> listener.listen(multiaddr)`
- `<Promise> listener.listen(Array<multiaddr>)`
- `listener.getAddrs()`
- `<Promise> listener.close([options])`

Expand Down Expand Up @@ -168,11 +168,11 @@ The listener object created may emit the following events:

### Start a listener

- `JavaScript` - `await listener.listen(multiaddr)`
- `JavaScript` - `await listener.listen(Array<multiaddr>)`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if it fails to bind to one or more of the addresses?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this would eventually be configurable, libp2p/js-libp2p-websocket-star#61 (comment), so you could specify exactly what happens.

The default behavior we decided would be good is to only error if all addresses fail. This is similar to how websocket-star-multi behaves today.

I think a subsequent improvement would be to add the configuration option. We could add them now, but I wanted to try and block the async changes as little as possible.


This method puts the listener in `listening` mode, waiting for incoming connections.

`multiaddr` is the address that the listener should bind to.
`Array<multiaddr>` is an array of the addresses that the listener should bind to.

### Get listener addrs

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@
},
"homepage": "https://github.com/libp2p/interface-transport",
"devDependencies": {
"aegir": "^17.0.1",
"dirty-chai": "^2.0.1"
"aegir": "^18.2.2"
},
"dependencies": {
"abort-controller": "^3.0.0",
"async-iterator-to-pull-stream": "^1.3.0",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"interface-connection": "~0.3.3",
"it-goodbye": "^2.0.0",
"it-pipe": "^1.0.0",
"multiaddr": "^5.0.2",
"multiaddr": "^6.0.6",
"pull-stream": "^3.6.9",
"streaming-iterables": "^4.0.2"
},
Expand Down
14 changes: 13 additions & 1 deletion src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ class AbortError extends Error {
}
}

class AllListenersFailedError extends Error {
constructor () {
super('AllListenersFailedError')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the message right? Shouldn't this be more human readable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the error more verbose to try and provide an actionable error for users.

this.code = AllListenersFailedError.code
}

static get code () {
return 'ERR_ALL_LISTENERS_FAILED'
}
}

module.exports = {
AbortError
AbortError,
AllListenersFailedError
}
33 changes: 29 additions & 4 deletions src/listen-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const expect = chai.expect
chai.use(dirtyChai)

const pipe = require('it-pipe')
const { collect } = require('streaming-iterables')

module.exports = (common) => {
describe('listen', () => {
Expand All @@ -22,7 +23,31 @@ module.exports = (common) => {

it('simple', async () => {
const listener = transport.createListener((conn) => {})
await listener.listen(addrs[0])
await listener.listen([addrs[0]])
await listener.close()
})

it('listen on multiple addresses', async () => {
// create an echo listener
const listener = transport.createListener((conn) => pipe(conn, conn))
await listener.listen(addrs.slice(0, 2))

// Connect on both addresses
const [socket1, socket2] = await Promise.all([
transport.dial(addrs[0]),
transport.dial(addrs[1])
])

const data = Buffer.from('hi there')
const results = await pipe(
[data], // [data] -> socket1
socket1, // socket1 -> server (echo) -> socket1 -> socket2
socket2, // socket2 -> server (echo) -> socket2 -> collect
collect
)

expect(results).to.eql([data])

await listener.close()
})

Expand All @@ -35,7 +60,7 @@ module.exports = (common) => {
const listener = transport.createListener((conn) => pipe(conn, conn))

// Listen
await listener.listen(addrs[0])
await listener.listen([addrs[0]])

// Create two connections to the listener
const socket1 = await transport.dial(addrs[0])
Expand Down Expand Up @@ -66,7 +91,7 @@ module.exports = (common) => {
})

;(async () => {
await listener.listen(addrs[0])
await listener.listen([addrs[0]])
await transport.dial(addrs[0])
})()
})
Expand Down Expand Up @@ -95,7 +120,7 @@ module.exports = (common) => {
listener.on('close', done)

;(async () => {
await listener.listen(addrs[0])
await listener.listen([addrs[0]])
await listener.close()
})()
})
Expand Down