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

Commit 5967834

Browse files
authored
fix(connection): dont require remoteAddr on creation (#20)
1 parent 32ee3e1 commit 5967834

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

src/connection/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ const conn = new Connection({
110110
Creates a new Connection instance.
111111

112112
`localAddr` is the optional [multiaddr](https://github.com/multiformats/multiaddr) address used by the local peer to reach the remote.
113-
`remoteAddr` is the [multiaddr](https://github.com/multiformats/multiaddr) address used to communicate with the remote peer.
113+
`remoteAddr` is the optional [multiaddr](https://github.com/multiformats/multiaddr) address used to communicate with the remote peer.
114114
`localPeer` is the [PeerId](https://github.com/libp2p/js-peer-id) of the local peer.
115115
`remotePeer` is the [PeerId](https://github.com/libp2p/js-peer-id) of the remote peer.
116116
`newStream` is the `function` responsible for getting a new muxed+multistream-selected stream.

src/connection/connection.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Connection {
1818
* Creates an instance of Connection.
1919
* @param {object} properties properties of the connection.
2020
* @param {multiaddr} [properties.localAddr] local multiaddr of the connection if known.
21-
* @param {multiaddr} properties.remoteAddr remote multiaddr of the connection.
21+
* @param {multiaddr} [properties.remoteAddr] remote multiaddr of the connection.
2222
* @param {PeerId} properties.localPeer local peer-id.
2323
* @param {PeerId} properties.remotePeer remote peer-id.
2424
* @param {function} properties.newStream new stream muxer function.
@@ -34,7 +34,6 @@ class Connection {
3434
*/
3535
constructor ({ localAddr, remoteAddr, localPeer, remotePeer, newStream, close, getStreams, stat }) {
3636
localAddr && assert(multiaddr.isMultiaddr(localAddr), 'localAddr must be an instance of multiaddr')
37-
assert(multiaddr.isMultiaddr(remoteAddr), 'remoteAddr must be an instance of multiaddr')
3837
assert(PeerId.isPeerId(localPeer), 'localPeer must be an instance of peer-id')
3938
assert(PeerId.isPeerId(remotePeer), 'remotePeer must be an instance of peer-id')
4039
assert(typeof newStream === 'function', 'new stream must be a function')

test/connection/index.spec.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const { Connection } = require('../../src/connection')
5+
const peers = require('../../src/utils/peers')
6+
const PeerId = require('peer-id')
7+
const pair = require('it-pair')
8+
9+
describe('connection tests', () => {
10+
it('should not require local or remote addrs', async () => {
11+
const [localPeer, remotePeer] = await Promise.all([
12+
PeerId.createFromJSON(peers[0]),
13+
PeerId.createFromJSON(peers[1])
14+
])
15+
const openStreams = []
16+
let streamId = 0
17+
18+
return new Connection({
19+
localPeer,
20+
remotePeer,
21+
stat: {
22+
timeline: {
23+
open: Date.now() - 10,
24+
upgraded: Date.now()
25+
},
26+
direction: 'outbound',
27+
encryption: '/secio/1.0.0',
28+
multiplexer: '/mplex/6.7.0'
29+
},
30+
newStream: (protocols) => {
31+
const id = streamId++
32+
const stream = pair()
33+
34+
stream.close = () => stream.sink([])
35+
stream.id = id
36+
37+
openStreams.push(stream)
38+
39+
return {
40+
stream,
41+
protocol: protocols[0]
42+
}
43+
},
44+
close: () => {},
45+
getStreams: () => openStreams
46+
})
47+
})
48+
})

0 commit comments

Comments
 (0)