3
3
const Swarm = require ( 'libp2p-swarm' )
4
4
const PeerId = require ( 'peer-id' )
5
5
const PeerInfo = require ( 'peer-info' )
6
+ const mafmt = require ( 'mafmt' )
6
7
const PeerBook = require ( 'peer-book' )
7
8
const multiaddr = require ( 'multiaddr' )
8
9
const EventEmitter = require ( 'events' ) . EventEmitter
@@ -25,7 +26,7 @@ class Node extends EventEmitter {
25
26
this . peerBook = _peerBook || new PeerBook ( )
26
27
this . isOnline = false
27
28
28
- this . swarm = new Swarm ( this . peerInfo )
29
+ this . swarm = new Swarm ( this . peerInfo , this . peerBook )
29
30
30
31
// Attach stream multiplexers
31
32
if ( this . modules . connection . muxer ) {
@@ -38,16 +39,15 @@ class Node extends EventEmitter {
38
39
// If muxer exists, we can use Identify
39
40
this . swarm . connection . reuse ( )
40
41
41
- // Received incommind dial and muxer upgrade happened, reuse this
42
- // muxed connection
42
+ // Received incommind dial and muxer upgrade happened,
43
+ // reuse this muxed connection
43
44
this . swarm . on ( 'peer-mux-established' , ( peerInfo ) => {
44
45
this . emit ( 'peer:connect' , peerInfo )
45
46
this . peerBook . put ( peerInfo )
46
47
} )
47
48
48
49
this . swarm . on ( 'peer-mux-closed' , ( peerInfo ) => {
49
50
this . emit ( 'peer:disconnect' , peerInfo )
50
- this . peerBook . removeByB58String ( peerInfo . id . toB58String ( ) )
51
51
} )
52
52
}
53
53
@@ -91,7 +91,19 @@ class Node extends EventEmitter {
91
91
let transports = this . modules . transport
92
92
93
93
transports = Array . isArray ( transports ) ? transports : [ transports ]
94
- const multiaddrs = this . peerInfo . multiaddrs
94
+
95
+ // so that we can have webrtc-star addrs without adding manually the id
96
+ const maOld = [ ]
97
+ const maNew = [ ]
98
+ this . peerInfo . multiaddrs . forEach ( ( ma ) => {
99
+ if ( ! mafmt . IPFS . matches ( ma ) ) {
100
+ maOld . push ( ma )
101
+ maNew . push ( ma . encapsulate ( '/ipfs/' + this . peerInfo . id . toB58String ( ) ) )
102
+ }
103
+ } )
104
+ this . peerInfo . multiaddrs . replace ( maOld , maNew )
105
+
106
+ const multiaddrs = this . peerInfo . multiaddrs . toArray ( )
95
107
96
108
transports . forEach ( ( transport ) => {
97
109
if ( transport . filter ( multiaddrs ) . length > 0 ) {
@@ -152,13 +164,19 @@ class Node extends EventEmitter {
152
164
153
165
dial ( peer , protocol , callback ) {
154
166
assert ( this . isOn ( ) , OFFLINE_ERROR_MESSAGE )
155
- const peerInfo = this . _getPeerInfo ( peer )
156
167
157
168
if ( typeof protocol === 'function' ) {
158
169
callback = protocol
159
170
protocol = undefined
160
171
}
161
172
173
+ let peerInfo
174
+ try {
175
+ peerInfo = this . _getPeerInfo ( peer )
176
+ } catch ( err ) {
177
+ return callback ( err )
178
+ }
179
+
162
180
this . swarm . dial ( peerInfo , protocol , ( err , conn ) => {
163
181
if ( err ) {
164
182
return callback ( err )
@@ -172,7 +190,6 @@ class Node extends EventEmitter {
172
190
assert ( this . isOn ( ) , OFFLINE_ERROR_MESSAGE )
173
191
const peerInfo = this . _getPeerInfo ( peer )
174
192
175
- this . peerBook . removeByB58String ( peerInfo . id . toB58String ( ) )
176
193
this . swarm . hangUp ( peerInfo , callback )
177
194
}
178
195
@@ -198,7 +215,7 @@ class Node extends EventEmitter {
198
215
} catch ( err ) {
199
216
p = new PeerInfo ( PeerId . createFromB58String ( peerIdB58Str ) )
200
217
}
201
- p . multiaddr . add ( peer )
218
+ p . multiaddrs . add ( peer )
202
219
} else if ( PeerId . isPeerId ( peer ) ) {
203
220
const peerIdB58Str = peer . toB58String ( )
204
221
try {
0 commit comments