Skip to content
This repository has been archived by the owner on Mar 11, 2020. It is now read-only.

Commit

Permalink
chore: remove peerInfo from constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Jul 22, 2019
1 parent 33bf777 commit 60277b1
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/connection.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

const { Duplex } = require('stream')

const PeerInfo = require('peer-info')
const multiaddr = require('multiaddr')
const multistream = require('multistream-select')
Expand All @@ -21,17 +19,13 @@ const defaultMaxNumberOfAttemptsForHasMultiplexer = 5
* This is an abstract duplex connection between two nodes and
* each transport must pipe its socket through this.
*/
class Connection extends Duplex {
class Connection {
/**
* Creates an instance of Connection.
* @param {PeerInfo} peerInfo remote peer PeerInfo
* @param {multiaddr} remoteMa remote peer multiaddr
* @param {boolean} [isInitiator=true] peer initiated the connection
*/
constructor (peerInfo, remoteMa, isInitiator = true) {
super()

assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of PeerInfo')
constructor (remoteMa, isInitiator = true) {
assert(multiaddr.isMultiaddr(remoteMa), 'remoteMa must be an instance of multiaddr')
assert(typeof isInitiator === 'boolean', 'isInitiator must be a boolean')

Expand All @@ -40,11 +34,6 @@ class Connection extends Duplex {
*/
this.id = (~~(Math.random() * 1e9)).toString(36) + Date.now()

/**
* Remote peer infos
*/
this.peerInfo = peerInfo

/**
* Status of the connection
*/
Expand All @@ -71,6 +60,11 @@ class Connection extends Duplex {
*/
this.role = isInitiator ? ROLE.INITIATOR : ROLE.RESPONDER

/**
* Remote peer infos
*/
this.peerInfo = undefined

/**
* Reference of the multiplexer being used
*/
Expand Down Expand Up @@ -114,6 +108,16 @@ class Connection extends Duplex {
this.endpoints.local = ma
}

/**
* Set remote peer info.
* @param {PeerInfo} peerInfo remote peer PeerInfo
*/
setPeerInfo (peerInfo) {
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of PeerInfo')

this.peerInfo = peerInfo
}

/**
* Create a new stream from this connection
* @param {string} protocol intended protocol for the stream
Expand Down

0 comments on commit 60277b1

Please sign in to comment.