diff --git a/src/connection/README.md b/src/connection/README.md index fab29d2..491d74c 100644 --- a/src/connection/README.md +++ b/src/connection/README.md @@ -110,7 +110,7 @@ const conn = new Connection({ 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. -`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. `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. diff --git a/src/connection/connection.js b/src/connection/connection.js index 8352e80..74f2a26 100644 --- a/src/connection/connection.js +++ b/src/connection/connection.js @@ -18,7 +18,7 @@ class Connection { * Creates an instance of Connection. * @param {object} properties properties of the connection. * @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.remotePeer remote peer-id. * @param {function} properties.newStream new stream muxer function. @@ -34,7 +34,6 @@ class Connection { */ constructor ({ localAddr, remoteAddr, localPeer, remotePeer, newStream, close, getStreams, stat }) { 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(remotePeer), 'remotePeer must be an instance of peer-id') assert(typeof newStream === 'function', 'new stream must be a function') diff --git a/test/connection/index.spec.js b/test/connection/index.spec.js new file mode 100644 index 0000000..5e32b35 --- /dev/null +++ b/test/connection/index.spec.js @@ -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 + }) + }) +})