mirror of
https://github.com/fluencelabs/js-libp2p-interfaces
synced 2025-04-25 12:52:35 +00:00
fix(connection): dont require remoteAddr on creation (#20)
This commit is contained in:
parent
32ee3e18e2
commit
5967834a2f
@ -110,7 +110,7 @@ const conn = new Connection({
|
|||||||
Creates a new Connection instance.
|
Creates a new Connection instance.
|
||||||
|
|
||||||
`localAddr` is the optional [multiaddr](https://github.com/multiformats/multiaddr) address used by the local peer to reach the remote.
|
`localAddr` is the optional [multiaddr](https://github.com/multiformats/multiaddr) address used by the local peer to reach the remote.
|
||||||
`remoteAddr` is the [multiaddr](https://github.com/multiformats/multiaddr) address used to communicate with the remote peer.
|
`remoteAddr` is the optional [multiaddr](https://github.com/multiformats/multiaddr) address used to communicate with the remote peer.
|
||||||
`localPeer` is the [PeerId](https://github.com/libp2p/js-peer-id) of the local peer.
|
`localPeer` is the [PeerId](https://github.com/libp2p/js-peer-id) of the local peer.
|
||||||
`remotePeer` is the [PeerId](https://github.com/libp2p/js-peer-id) of the remote peer.
|
`remotePeer` is the [PeerId](https://github.com/libp2p/js-peer-id) of the remote peer.
|
||||||
`newStream` is the `function` responsible for getting a new muxed+multistream-selected stream.
|
`newStream` is the `function` responsible for getting a new muxed+multistream-selected stream.
|
||||||
|
@ -18,7 +18,7 @@ class Connection {
|
|||||||
* Creates an instance of Connection.
|
* Creates an instance of Connection.
|
||||||
* @param {object} properties properties of the connection.
|
* @param {object} properties properties of the connection.
|
||||||
* @param {multiaddr} [properties.localAddr] local multiaddr of the connection if known.
|
* @param {multiaddr} [properties.localAddr] local multiaddr of the connection if known.
|
||||||
* @param {multiaddr} properties.remoteAddr remote multiaddr of the connection.
|
* @param {multiaddr} [properties.remoteAddr] remote multiaddr of the connection.
|
||||||
* @param {PeerId} properties.localPeer local peer-id.
|
* @param {PeerId} properties.localPeer local peer-id.
|
||||||
* @param {PeerId} properties.remotePeer remote peer-id.
|
* @param {PeerId} properties.remotePeer remote peer-id.
|
||||||
* @param {function} properties.newStream new stream muxer function.
|
* @param {function} properties.newStream new stream muxer function.
|
||||||
@ -34,7 +34,6 @@ class Connection {
|
|||||||
*/
|
*/
|
||||||
constructor ({ localAddr, remoteAddr, localPeer, remotePeer, newStream, close, getStreams, stat }) {
|
constructor ({ localAddr, remoteAddr, localPeer, remotePeer, newStream, close, getStreams, stat }) {
|
||||||
localAddr && assert(multiaddr.isMultiaddr(localAddr), 'localAddr must be an instance of multiaddr')
|
localAddr && assert(multiaddr.isMultiaddr(localAddr), 'localAddr must be an instance of multiaddr')
|
||||||
assert(multiaddr.isMultiaddr(remoteAddr), 'remoteAddr must be an instance of multiaddr')
|
|
||||||
assert(PeerId.isPeerId(localPeer), 'localPeer must be an instance of peer-id')
|
assert(PeerId.isPeerId(localPeer), 'localPeer must be an instance of peer-id')
|
||||||
assert(PeerId.isPeerId(remotePeer), 'remotePeer must be an instance of peer-id')
|
assert(PeerId.isPeerId(remotePeer), 'remotePeer must be an instance of peer-id')
|
||||||
assert(typeof newStream === 'function', 'new stream must be a function')
|
assert(typeof newStream === 'function', 'new stream must be a function')
|
||||||
|
48
test/connection/index.spec.js
Normal file
48
test/connection/index.spec.js
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
/* eslint-env mocha */
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const { Connection } = require('../../src/connection')
|
||||||
|
const peers = require('../../src/utils/peers')
|
||||||
|
const PeerId = require('peer-id')
|
||||||
|
const pair = require('it-pair')
|
||||||
|
|
||||||
|
describe('connection tests', () => {
|
||||||
|
it('should not require local or remote addrs', async () => {
|
||||||
|
const [localPeer, remotePeer] = await Promise.all([
|
||||||
|
PeerId.createFromJSON(peers[0]),
|
||||||
|
PeerId.createFromJSON(peers[1])
|
||||||
|
])
|
||||||
|
const openStreams = []
|
||||||
|
let streamId = 0
|
||||||
|
|
||||||
|
return new Connection({
|
||||||
|
localPeer,
|
||||||
|
remotePeer,
|
||||||
|
stat: {
|
||||||
|
timeline: {
|
||||||
|
open: Date.now() - 10,
|
||||||
|
upgraded: Date.now()
|
||||||
|
},
|
||||||
|
direction: 'outbound',
|
||||||
|
encryption: '/secio/1.0.0',
|
||||||
|
multiplexer: '/mplex/6.7.0'
|
||||||
|
},
|
||||||
|
newStream: (protocols) => {
|
||||||
|
const id = streamId++
|
||||||
|
const stream = pair()
|
||||||
|
|
||||||
|
stream.close = () => stream.sink([])
|
||||||
|
stream.id = id
|
||||||
|
|
||||||
|
openStreams.push(stream)
|
||||||
|
|
||||||
|
return {
|
||||||
|
stream,
|
||||||
|
protocol: protocols[0]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
close: () => {},
|
||||||
|
getStreams: () => openStreams
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
Loading…
x
Reference in New Issue
Block a user