revert: "feat: make listen take an array of addrs (#46)" (#51)

This reverts commit 1dc5baab0b3efdf5ec07be646b8692700eb4c91c.
This commit is contained in:
dirkmc 2019-04-29 23:04:17 +08:00 committed by Jacob Heun
parent e4cc841ebd
commit 030195e315
4 changed files with 11 additions and 48 deletions

View File

@ -103,7 +103,7 @@ A valid transport (one that follows the interface defined) must implement the fo
- event: 'close' - event: 'close'
- event: 'connection' - event: 'connection'
- event: 'error' - event: 'error'
- `<Promise> listener.listen(Array<multiaddr>)` - `<Promise> listener.listen(multiaddr)`
- `listener.getAddrs()` - `listener.getAddrs()`
- `<Promise> listener.close([options])` - `<Promise> listener.close([options])`
@ -173,11 +173,11 @@ The listener object created may emit the following events:
### Start a listener ### Start a listener
- `JavaScript` - `await listener.listen(Array<multiaddr>)` - `JavaScript` - `await listener.listen(multiaddr)`
This method puts the listener in `listening` mode, waiting for incoming connections. This method puts the listener in `listening` mode, waiting for incoming connections.
`Array<multiaddr>` is an array of the addresses that the listener should bind to. `multiaddr` is the address that the listener should bind to.
### Get listener addrs ### Get listener addrs

View File

@ -34,17 +34,17 @@
}, },
"homepage": "https://github.com/libp2p/interface-transport", "homepage": "https://github.com/libp2p/interface-transport",
"devDependencies": { "devDependencies": {
"aegir": "^18.2.2" "aegir": "^17.0.1",
"dirty-chai": "^2.0.1"
}, },
"dependencies": { "dependencies": {
"abort-controller": "^3.0.0", "abort-controller": "^3.0.0",
"async-iterator-to-pull-stream": "^1.3.0", "async-iterator-to-pull-stream": "^1.3.0",
"chai": "^4.2.0", "chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"interface-connection": "~0.3.3", "interface-connection": "~0.3.3",
"it-goodbye": "^2.0.0", "it-goodbye": "^2.0.0",
"it-pipe": "^1.0.0", "it-pipe": "^1.0.0",
"multiaddr": "^6.0.6", "multiaddr": "^5.0.2",
"pull-stream": "^3.6.9", "pull-stream": "^3.6.9",
"streaming-iterables": "^4.0.2" "streaming-iterables": "^4.0.2"
}, },

View File

@ -16,18 +16,6 @@ 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 = { module.exports = {
AbortError, AbortError
AllListenersFailedError
} }

View File

@ -8,7 +8,6 @@ const expect = chai.expect
chai.use(dirtyChai) chai.use(dirtyChai)
const pipe = require('it-pipe') const pipe = require('it-pipe')
const { collect } = require('streaming-iterables')
module.exports = (common) => { module.exports = (common) => {
describe('listen', () => { describe('listen', () => {
@ -23,31 +22,7 @@ module.exports = (common) => {
it('simple', async () => { it('simple', async () => {
const listener = transport.createListener((conn) => {}) 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() await listener.close()
}) })
@ -60,7 +35,7 @@ module.exports = (common) => {
const listener = transport.createListener((conn) => pipe(conn, conn)) const listener = transport.createListener((conn) => pipe(conn, conn))
// Listen // Listen
await listener.listen([addrs[0]]) await listener.listen(addrs[0])
// Create two connections to the listener // Create two connections to the listener
const socket1 = await transport.dial(addrs[0]) const socket1 = await transport.dial(addrs[0])
@ -91,7 +66,7 @@ module.exports = (common) => {
}) })
;(async () => { ;(async () => {
await listener.listen([addrs[0]]) await listener.listen(addrs[0])
await transport.dial(addrs[0]) await transport.dial(addrs[0])
})() })()
}) })
@ -120,7 +95,7 @@ module.exports = (common) => {
listener.on('close', done) listener.on('close', done)
;(async () => { ;(async () => {
await listener.listen([addrs[0]]) await listener.listen(addrs[0])
await listener.close() await listener.close()
})() })()
}) })