1
1
'use strict'
2
2
3
- const { Duplex } = require ( 'stream' )
4
-
5
3
const PeerInfo = require ( 'peer-info' )
6
4
const multiaddr = require ( 'multiaddr' )
7
5
const multistream = require ( 'multistream-select' )
@@ -21,17 +19,13 @@ const defaultMaxNumberOfAttemptsForHasMultiplexer = 5
21
19
* This is an abstract duplex connection between two nodes and
22
20
* each transport must pipe its socket through this.
23
21
*/
24
- class Connection extends Duplex {
22
+ class Connection {
25
23
/**
26
24
* Creates an instance of Connection.
27
- * @param {PeerInfo } peerInfo remote peer PeerInfo
28
25
* @param {multiaddr } remoteMa remote peer multiaddr
29
26
* @param {boolean } [isInitiator=true] peer initiated the connection
30
27
*/
31
- constructor ( peerInfo , remoteMa , isInitiator = true ) {
32
- super ( )
33
-
34
- assert ( PeerInfo . isPeerInfo ( peerInfo ) , 'peerInfo must be an instance of PeerInfo' )
28
+ constructor ( remoteMa , isInitiator = true ) {
35
29
assert ( multiaddr . isMultiaddr ( remoteMa ) , 'remoteMa must be an instance of multiaddr' )
36
30
assert ( typeof isInitiator === 'boolean' , 'isInitiator must be a boolean' )
37
31
@@ -40,11 +34,6 @@ class Connection extends Duplex {
40
34
*/
41
35
this . id = ( ~ ~ ( Math . random ( ) * 1e9 ) ) . toString ( 36 ) + Date . now ( )
42
36
43
- /**
44
- * Remote peer infos
45
- */
46
- this . peerInfo = peerInfo
47
-
48
37
/**
49
38
* Status of the connection
50
39
*/
@@ -71,6 +60,11 @@ class Connection extends Duplex {
71
60
*/
72
61
this . role = isInitiator ? ROLE . INITIATOR : ROLE . RESPONDER
73
62
63
+ /**
64
+ * Remote peer infos
65
+ */
66
+ this . peerInfo = undefined
67
+
74
68
/**
75
69
* Reference of the multiplexer being used
76
70
*/
@@ -114,6 +108,16 @@ class Connection extends Duplex {
114
108
this . endpoints . local = ma
115
109
}
116
110
111
+ /**
112
+ * Set remote peer info.
113
+ * @param {PeerInfo } peerInfo remote peer PeerInfo
114
+ */
115
+ setPeerInfo ( peerInfo ) {
116
+ assert ( PeerInfo . isPeerInfo ( peerInfo ) , 'peerInfo must be an instance of PeerInfo' )
117
+
118
+ this . peerInfo = peerInfo
119
+ }
120
+
117
121
/**
118
122
* Create a new stream from this connection
119
123
* @param {string } protocol intended protocol for the stream
0 commit comments