Skip to content
This repository was archived by the owner on Aug 23, 2019. It is now read-only.

Commit 920b25e

Browse files
committed
fix: cleanup, moving setup code outside of dialer
1 parent d57be3d commit 920b25e

File tree

5 files changed

+63
-76
lines changed

5 files changed

+63
-76
lines changed

src/circuit/dialer.js

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
require('safe-buffer')
44

55
const Connection = require('interface-connection').Connection
6-
const mafmt = require('mafmt')
76
const isFunction = require('lodash.isfunction')
87
const multiaddr = require('multiaddr')
98
const constants = require('./constants')
@@ -31,46 +30,6 @@ class Dialer {
3130
this.relayPeers = new Map()
3231
this.options = options
3332
this.utils = utilsFactory(swarm)
34-
35-
// get all the relay addresses for this swarm
36-
const relays = this.filter(swarm._peerInfo.multiaddrs.toArray())
37-
38-
// if no explicit relays, add a default relay addr
39-
if (relays.length === 0) {
40-
this.swarm
41-
._peerInfo
42-
.multiaddrs
43-
.add(`/p2p-circuit/ipfs/${this.swarm._peerInfo.id.toB58String()}`)
44-
}
45-
46-
this.swarm.on('peer-mux-established', this.dialRelay.bind(this))
47-
this.swarm.on('peer-mux-closed', (peerInfo) => {
48-
this.relayPeers.delete(peerInfo.id.toB58String())
49-
})
50-
51-
this.dialSwarmRelays(relays)
52-
}
53-
54-
/**
55-
* Dial the relays in the Addresses.Swarm config
56-
*
57-
* @param {Array} relays
58-
* @return {void}
59-
*/
60-
dialSwarmRelays (relays) {
61-
// if we have relay addresses in swarm config, then dial those relays
62-
this.swarm.on('listening', () => {
63-
relays.forEach((relay) => {
64-
let relaySegments = relay
65-
.toString()
66-
.split('/p2p-circuit')
67-
.filter(segment => segment.length)
68-
69-
relaySegments.forEach((relaySegment) => {
70-
this.dialRelay(this.utils.peerInfoFromMa(multiaddr(relaySegment)))
71-
})
72-
})
73-
})
7433
}
7534

7635
/**
@@ -222,24 +181,6 @@ class Dialer {
222181
cb(null, relayConn)
223182
}))
224183
}
225-
226-
/**
227-
* Filter check for all multiaddresses
228-
* that this transport can dial on
229-
*
230-
* @param {any} multiaddrs
231-
* @returns {Array<multiaddr>}
232-
*
233-
* @memberOf Dialer
234-
*/
235-
filter (multiaddrs) {
236-
if (!Array.isArray(multiaddrs)) {
237-
multiaddrs = [multiaddrs]
238-
}
239-
return multiaddrs.filter((ma) => {
240-
return mafmt.Circuit.matches(ma)
241-
})
242-
}
243184
}
244185

245186
module.exports = Dialer

src/circuit/relay.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ class Relay extends EE {
3333
*/
3434
constructor (options) {
3535
super()
36-
this.config = Object.assign({Active: false}, options)
36+
this.config = Object.assign({active: false}, options)
3737
this.swarm = null
38-
this.active = this.config.Active
38+
this.active = this.config.active
3939
}
4040

4141
_writeErr (streamHandler, errCode, cb) {
@@ -112,11 +112,7 @@ class Relay extends EE {
112112
this.swarm.handle(multicodec.hop, (proto, conn) => {
113113
const streamHandler = new StreamHandler(conn, 1000 * 60)
114114
waterfall([
115-
(cb) => {
116-
this._readDstAddr(streamHandler, (err, dstMa) => {
117-
cb(err, dstMa)
118-
})
119-
},
115+
(cb) => this._readDstAddr(streamHandler, cb),
120116
(dstMa, cb) => {
121117
this._readSrcAddr(streamHandler, (err, srcMa) => {
122118
cb(err, dstMa, srcMa)
@@ -128,7 +124,7 @@ class Relay extends EE {
128124
return
129125
}
130126

131-
return this.circuit(streamHandler.rest(), dstMa, srcMa, (err) => {
127+
return this._circuit(streamHandler.rest(), dstMa, srcMa, (err) => {
132128
if (err) {
133129
log.err(err)
134130
setImmediate(() => this.emit('circuit:error', err))
@@ -151,7 +147,7 @@ class Relay extends EE {
151147
*
152148
* @return {void}
153149
*/
154-
circuit (conn, dstAddr, srcAddr, cb) {
150+
_circuit (conn, dstAddr, srcAddr, cb) {
155151
this._dialPeer(dstAddr, (err, dstConn) => {
156152
let streamHandler = null
157153

src/dialer.js

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
'use strict'
22

3-
const debug = require('debug')
4-
const constants = require('./circuit/constants')
3+
const mafmt = require('mafmt')
4+
const multiaddr = require('multiaddr')
55

6+
const constants = require('./circuit/constants')
67
const OnionDialer = require('./circuit/onion-dialer')
8+
const utilsFactory = require('./circuit/utils')
79

10+
const debug = require('debug')
811
const log = debug('libp2p:circuit:transportdialer')
912
log.err = debug('libp2p:circuit:error:transportdialer')
1013

@@ -23,9 +26,50 @@ class Dialer {
2326

2427
this.swarm = swarm
2528
this.dialer = null
29+
this.utils = utilsFactory(swarm)
2630

27-
// TODO: add flag for other types of dialers, ie telescope
31+
// get all the relay addresses for this swarm
32+
const relays = this.filter(swarm._peerInfo.multiaddrs.toArray())
33+
34+
// if no explicit relays, add a default relay addr
35+
if (relays.length === 0) {
36+
this.swarm
37+
._peerInfo
38+
.multiaddrs
39+
.add(`/p2p-circuit/ipfs/${this.swarm._peerInfo.id.toB58String()}`)
40+
}
41+
42+
// TODO: add flag for other types of dealers, ie telescope
2843
this.dialer = new OnionDialer(swarm, options)
44+
45+
this.swarm.on('peer-mux-established', this.dialer.dialRelay.bind(this.dialer))
46+
this.swarm.on('peer-mux-closed', (peerInfo) => {
47+
this.dialer.relayPeers.delete(peerInfo.id.toB58String())
48+
})
49+
50+
this._dialSwarmRelays(relays)
51+
}
52+
53+
/**
54+
* Dial the relays in the Addresses.Swarm config
55+
*
56+
* @param {Array} relays
57+
* @return {void}
58+
*/
59+
_dialSwarmRelays (relays) {
60+
// if we have relay addresses in swarm config, then dial those relays
61+
this.swarm.on('listening', () => {
62+
relays.forEach((relay) => {
63+
let relaySegments = relay
64+
.toString()
65+
.split('/p2p-circuit')
66+
.filter(segment => segment.length)
67+
68+
relaySegments.forEach((relaySegment) => {
69+
this.dialer.dialRelay(this.utils.peerInfoFromMa(multiaddr(relaySegment)))
70+
})
71+
})
72+
})
2973
}
3074

3175
get priority () {
@@ -76,7 +120,12 @@ class Dialer {
76120
* @memberOf Dialer
77121
*/
78122
filter (multiaddrs) {
79-
return this.dialer.filter(multiaddrs)
123+
if (!Array.isArray(multiaddrs)) {
124+
multiaddrs = [multiaddrs]
125+
}
126+
return multiaddrs.filter((ma) => {
127+
return mafmt.Circuit.matches(ma)
128+
})
80129
}
81130
}
82131

src/listener.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ module.exports = (swarm, options, handler) => {
9494

9595
// get all the explicit relay addrs excluding self
9696
let p2pAddrs = addrs.filter((addr) => {
97-
return mafmt.Circuit.matches(addr) && !addr.toString().includes(swarm._peerInfo.id.toB58String())
97+
return mafmt.Circuit.matches(addr) &&
98+
!addr.toString().includes(swarm._peerInfo.id.toB58String())
9899
})
99100

100101
// use the explicit relays instead of any relay

test/relay.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('relay', function () {
6262
relay._readSrcAddr.callThrough()
6363
relay._writeErr.callThrough()
6464
relay.mount(swarm) // mount the swarm
65-
relay.circuit.callsArg(3, null, toConn)
65+
relay._circuit.callsArg(3, null, toConn)
6666

6767
dialer.relayConns = new Map()
6868
dialer.negotiateRelay.callThrough()
@@ -74,7 +74,7 @@ describe('relay', function () {
7474
relay.mount.reset()
7575
relay.emit.reset()
7676
relay.on.reset()
77-
relay.circuit.reset()
77+
relay._circuit.reset()
7878
relay._readDstAddr.reset()
7979
relay._readSrcAddr.reset()
8080
relay._readSrcAddr.reset()
@@ -84,7 +84,7 @@ describe('relay', function () {
8484
it(`handle a valid circuit request`, function (done) {
8585
relay.active = true
8686
relay.on('circuit:success', () => {
87-
expect(relay.circuit.calledWith(sinon.match.any, dstMa)).to.be.ok
87+
expect(relay._circuit.calledWith(sinon.match.any, dstMa)).to.be.ok
8888
done()
8989
})
9090

0 commit comments

Comments
 (0)