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

Commit ad93dfc

Browse files
committed
doc: adding listener documentation
1 parent 10da18e commit ad93dfc

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/listener.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ module.exports = (swarm, options, connHandler) => {
2525
listener.stopHandler = new Stop(swarm)
2626
listener.hopHandler = new Hop(swarm, options.hop)
2727

28+
/**
29+
* Add swarm handler and listen for incoming connections
30+
*
31+
* @param {Multiaddr} ma
32+
* @param {Function} callback
33+
* @return {void}
34+
*/
2835
listener.listen = (ma, callback) => {
2936
callback = callback || (() => {})
3037

@@ -65,12 +72,33 @@ module.exports = (swarm, options, connHandler) => {
6572
callback()
6673
}
6774

75+
/**
76+
* Remove swarm listener
77+
*
78+
* @param {Function} cb
79+
* @return {void}
80+
*/
6881
listener.close = (cb) => {
6982
swarm.unhandle(multicodec.stop)
7083
setImmediate(() => listener.emit('close'))
7184
cb()
7285
}
7386

87+
/**
88+
* Get fixed up multiaddrs
89+
*
90+
* NOTE: This method will grab the peers multiaddrs and expand them such that:
91+
*
92+
* a) If it's an existing /p2p-circuit address for a specific relay i.e.
93+
* `/ip4/0.0.0.0/tcp/0/ipfs/QmRelay/p2p-circuit` this method will expand the
94+
* address to `/ip4/0.0.0.0/tcp/0/ipfs/QmRelay/p2p-circuit/ipfs/QmPeer` where
95+
* `QmPeer` is this peers id
96+
* b) If it's not a /p2p-circuit address, it will encapsulate the address as a /p2p-circuit
97+
* addr such that dials a relay uses that address to dial this peer
98+
*
99+
* @param {Function} callback
100+
* @return {void}
101+
*/
74102
listener.getAddrs = (callback) => {
75103
let addrs = swarm._peerInfo.multiaddrs.toArray()
76104

@@ -94,7 +122,7 @@ module.exports = (swarm, options, connHandler) => {
94122
}
95123

96124
if (!mafmt.Circuit.matches(addr)) {
97-
if (addr.getPeerId() !== null) {
125+
if (addr.getPeerId()) {
98126
// by default we're reachable over any relay
99127
listenAddrs.push(multiaddr(`/p2p-circuit`).encapsulate(addr))
100128
} else {

0 commit comments

Comments
 (0)