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

Commit

Permalink
feat: make listen take an array of addrs (#46)
Browse files Browse the repository at this point in the history
* feat: make listen take an array of addrs

* fix: make error more user friendly
  • Loading branch information
jacobheun authored Apr 19, 2019
1 parent 06ed59d commit 1dc5baa
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
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>)`

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 @@ -16,6 +16,18 @@ class AbortError extends Error {
}
}

class AllListenersFailedError extends Error {
constructor () {
super('All listeners failed to listen on any addresses, please verify the addresses you provided are correct')
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

0 comments on commit 1dc5baa

Please sign in to comment.