mirror of
https://github.com/fluencelabs/js-libp2p-interfaces
synced 2025-04-24 22:52:35 +00:00
feat: make listen take an array of addrs (#46)
* feat: make listen take an array of addrs * fix: make error more user friendly
This commit is contained in:
parent
06ed59dc39
commit
1dc5baab0b
@ -98,7 +98,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(multiaddr)`
|
- `<Promise> listener.listen(Array<multiaddr>)`
|
||||||
- `listener.getAddrs()`
|
- `listener.getAddrs()`
|
||||||
- `<Promise> listener.close([options])`
|
- `<Promise> listener.close([options])`
|
||||||
|
|
||||||
@ -168,11 +168,11 @@ The listener object created may emit the following events:
|
|||||||
|
|
||||||
### Start a listener
|
### 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.
|
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
|
### Get listener addrs
|
||||||
|
|
||||||
|
@ -34,17 +34,17 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/libp2p/interface-transport",
|
"homepage": "https://github.com/libp2p/interface-transport",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"aegir": "^17.0.1",
|
"aegir": "^18.2.2"
|
||||||
"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": "^5.0.2",
|
"multiaddr": "^6.0.6",
|
||||||
"pull-stream": "^3.6.9",
|
"pull-stream": "^3.6.9",
|
||||||
"streaming-iterables": "^4.0.2"
|
"streaming-iterables": "^4.0.2"
|
||||||
},
|
},
|
||||||
|
@ -16,6 +16,18 @@ class AbortError extends Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
class AllListenersFailedError extends Error {
|
||||||
AbortError
|
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,
|
||||||
|
AllListenersFailedError
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ 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', () => {
|
||||||
@ -22,7 +23,31 @@ 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()
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -35,7 +60,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])
|
||||||
@ -66,7 +91,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])
|
||||||
})()
|
})()
|
||||||
})
|
})
|
||||||
@ -95,7 +120,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()
|
||||||
})()
|
})()
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user