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

Commit 06f39b3

Browse files
committed
refactor: update files and add jsdocs to improve readability
refactor: initial refactor of dial.js refactor: add more jsdocs to dial and clean up some code refactor: make get-peer-info more readable fix: jsdocs in dial docs: update some jsdocs refactor: make dial.js a bit easier to consume fix: fix linting docs: add more jsdocs and comments refactor: clean up dial methods and encryption order
1 parent 97bb973 commit 06f39b3

12 files changed

+816
-400
lines changed

src/connection.js

+135-101
Original file line numberDiff line numberDiff line change
@@ -12,121 +12,155 @@ const Circuit = require('libp2p-circuit')
1212

1313
const plaintext = require('./plaintext')
1414

15-
module.exports = function connection (swtch) {
16-
return {
17-
addUpgrade () {},
18-
19-
addStreamMuxer (muxer) {
20-
// for dialing
21-
swtch.muxers[muxer.multicodec] = muxer
22-
23-
// for listening
24-
swtch.handle(muxer.multicodec, (protocol, conn) => {
25-
const muxedConn = muxer.listener(conn)
26-
27-
muxedConn.on('stream', swtch.protocolMuxer(null))
28-
29-
// If identify is enabled
30-
// 1. overload getPeerInfo
31-
// 2. call getPeerInfo
32-
// 3. add this conn to the pool
33-
if (swtch.identify) {
34-
// overload peerInfo to use Identify instead
35-
conn.getPeerInfo = (cb) => {
36-
const conn = muxedConn.newStream()
37-
const ms = new multistream.Dialer()
38-
cb = once(cb)
39-
40-
waterfall([
41-
(cb) => ms.handle(conn, cb),
42-
(cb) => ms.select(identify.multicodec, cb),
43-
(conn, cb) => identify.dialer(conn, cb),
44-
(peerInfo, observedAddrs, cb) => {
45-
observedAddrs.forEach((oa) => {
46-
swtch._peerInfo.multiaddrs.addSafe(oa)
47-
})
48-
cb(null, peerInfo)
49-
}
50-
], (err, pi) => {
51-
if (pi) {
52-
conn.setPeerInfo(pi)
53-
}
54-
cb(err, pi)
55-
})
56-
}
15+
/**
16+
* Contains methods for binding handlers to the Switch
17+
* in order to better manage its connections.
18+
*/
19+
class ConnectionManager {
20+
constructor (_switch) {
21+
this.switch = _switch
22+
}
5723

58-
conn.getPeerInfo((err, peerInfo) => {
59-
if (err) {
60-
return log('Identify not successful')
24+
/**
25+
* Adds a listener for the given `muxer` and creates a handler for it
26+
* leveraging the Switch.protocolMuxer handler factory
27+
*
28+
* @param {Muxer} muxer
29+
* @returns {void}
30+
*/
31+
addStreamMuxer (muxer) {
32+
// for dialing
33+
this.switch.muxers[muxer.multicodec] = muxer
34+
35+
// for listening
36+
this.switch.handle(muxer.multicodec, (protocol, conn) => {
37+
const muxedConn = muxer.listener(conn)
38+
39+
muxedConn.on('stream', this.switch.protocolMuxer(null))
40+
41+
// If identify is enabled
42+
// 1. overload getPeerInfo
43+
// 2. call getPeerInfo
44+
// 3. add this conn to the pool
45+
if (this.switch.identify) {
46+
// overload peerInfo to use Identify instead
47+
conn.getPeerInfo = (callback) => {
48+
const conn = muxedConn.newStream()
49+
const ms = new multistream.Dialer()
50+
callback = once(callback)
51+
52+
waterfall([
53+
(cb) => ms.handle(conn, cb),
54+
(cb) => ms.select(identify.multicodec, cb),
55+
(conn, cb) => identify.dialer(conn, cb),
56+
(peerInfo, observedAddrs, cb) => {
57+
observedAddrs.forEach((oa) => {
58+
this.switch._peerInfo.multiaddrs.addSafe(oa)
59+
})
60+
cb(null, peerInfo)
6161
}
62-
const b58Str = peerInfo.id.toB58String()
63-
64-
swtch.muxedConns[b58Str] = { muxer: muxedConn }
65-
66-
if (peerInfo.multiaddrs.size > 0) {
67-
// with incomming conn and through identify, going to pick one
68-
// of the available multiaddrs from the other peer as the one
69-
// I'm connected to as we really can't be sure at the moment
70-
// TODO add this consideration to the connection abstraction!
71-
peerInfo.connect(peerInfo.multiaddrs.toArray()[0])
72-
} else {
73-
// for the case of websockets in the browser, where peers have
74-
// no addr, use just their IPFS id
75-
peerInfo.connect(`/ipfs/${b58Str}`)
62+
], (err, peerInfo) => {
63+
if (peerInfo) {
64+
conn.setPeerInfo(peerInfo)
7665
}
77-
peerInfo = swtch._peerBook.put(peerInfo)
66+
callback(err, peerInfo)
67+
})
68+
}
7869

79-
muxedConn.on('close', () => {
80-
delete swtch.muxedConns[b58Str]
81-
peerInfo.disconnect()
82-
peerInfo = swtch._peerBook.put(peerInfo)
83-
setImmediate(() => swtch.emit('peer-mux-closed', peerInfo))
84-
})
70+
conn.getPeerInfo((err, peerInfo) => {
71+
if (err) {
72+
return log('Identify not successful')
73+
}
74+
const b58Str = peerInfo.id.toB58String()
75+
76+
this.switch.muxedConns[b58Str] = { muxer: muxedConn }
77+
78+
if (peerInfo.multiaddrs.size > 0) {
79+
// with incomming conn and through identify, going to pick one
80+
// of the available multiaddrs from the other peer as the one
81+
// I'm connected to as we really can't be sure at the moment
82+
// TODO add this consideration to the connection abstraction!
83+
peerInfo.connect(peerInfo.multiaddrs.toArray()[0])
84+
} else {
85+
// for the case of websockets in the browser, where peers have
86+
// no addr, use just their IPFS id
87+
peerInfo.connect(`/ipfs/${b58Str}`)
88+
}
89+
peerInfo = this.switch._peerBook.put(peerInfo)
8590

86-
setImmediate(() => swtch.emit('peer-mux-established', peerInfo))
91+
muxedConn.on('close', () => {
92+
delete this.switch.muxedConns[b58Str]
93+
peerInfo.disconnect()
94+
peerInfo = this.switch._peerBook.put(peerInfo)
95+
setImmediate(() => this.switch.emit('peer-mux-closed', peerInfo))
8796
})
88-
}
8997

90-
return conn
91-
})
92-
},
98+
setImmediate(() => this.switch.emit('peer-mux-established', peerInfo))
99+
})
100+
}
93101

94-
reuse () {
95-
swtch.identify = true
96-
swtch.handle(identify.multicodec, (protocol, conn) => {
97-
identify.listener(conn, swtch._peerInfo)
98-
})
99-
},
102+
return conn
103+
})
104+
}
100105

101-
enableCircuitRelay (config) {
102-
config = config || {}
106+
/**
107+
* Adds the `encrypt` handler for the given `tag` and also sets the
108+
* Switch's crypto to past `encrypt` function
109+
*
110+
* @param {String} tag
111+
* @param {function(PeerID, Connection, PeerId, Callback)} encrypt
112+
* @returns {void}
113+
*/
114+
crypto (tag, encrypt) {
115+
if (!tag && !encrypt) {
116+
tag = plaintext.tag
117+
encrypt = plaintext.encrypt
118+
}
103119

104-
if (config.enabled) {
105-
if (!config.hop) {
106-
Object.assign(config, { hop: { enabled: false, active: false } })
107-
}
120+
this.switch.unhandle(this.switch.crypto.tag)
121+
this.switch.handle(tag, (protocol, conn) => {
122+
const myId = this.switch._peerInfo.id
123+
const secure = encrypt(myId, conn, undefined, () => {
124+
this.switch.protocolMuxer(null)(secure)
125+
})
126+
})
108127

109-
// TODO: (dryajov) should we enable circuit listener and
110-
// dialer by default?
111-
swtch.transport.add(Circuit.tag, new Circuit(swtch, config))
112-
}
113-
},
128+
this.switch.crypto = {tag, encrypt}
129+
}
114130

115-
crypto (tag, encrypt) {
116-
if (!tag && !encrypt) {
117-
tag = plaintext.tag
118-
encrypt = plaintext.encrypt
131+
/**
132+
* If config.enabled is true, a Circuit relay will be added to the
133+
* available Switch transports.
134+
*
135+
* @param {any} config
136+
* @returns {void}
137+
*/
138+
enableCircuitRelay (config) {
139+
config = config || {}
140+
141+
if (config.enabled) {
142+
if (!config.hop) {
143+
Object.assign(config, { hop: { enabled: false, active: false } })
119144
}
120145

121-
swtch.unhandle(swtch.crypto.tag)
122-
swtch.handle(tag, (protocol, conn) => {
123-
const myId = swtch._peerInfo.id
124-
const secure = encrypt(myId, conn, undefined, () => {
125-
swtch.protocolMuxer(null)(secure)
126-
})
127-
})
128-
129-
swtch.crypto = {tag, encrypt}
146+
// TODO: (dryajov) should we enable circuit listener and
147+
// dialer by default?
148+
this.switch.transport.add(Circuit.tag, new Circuit(this.switch, config))
130149
}
131150
}
151+
152+
/**
153+
* Sets identify to true on the Switch and performs handshakes
154+
* for libp2p-identify leveraging the Switch's muxer.
155+
*
156+
* @returns {void}
157+
*/
158+
reuse () {
159+
this.switch.identify = true
160+
this.switch.handle(identify.multicodec, (protocol, conn) => {
161+
identify.listener(conn, this.switch._peerInfo)
162+
})
163+
}
132164
}
165+
166+
module.exports = ConnectionManager

0 commit comments

Comments
 (0)