mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-24 18:12:14 +00:00
* docs: fix spelling in api * fix: dont create peerstore twice * feat: add support for dial coalescing * doc(fix): add setPeerValue to API TOC * docs: add more jsdocs to dialer * chore: remove old comment * fix: ensure connections are closed * fix: registrar.getConnections returns first open conn * fix: directly set the closed status * chore: remove unneeded log * refactor: peerStore.put takes an options object
67 lines
1.9 KiB
JavaScript
67 lines
1.9 KiB
JavaScript
'use strict'
|
|
|
|
const EventEmitter = require('events')
|
|
const multiaddr = require('multiaddr')
|
|
|
|
const debug = require('debug')
|
|
const log = debug('libp2p:circuit:listener')
|
|
log.err = debug('libp2p:circuit:error:listener')
|
|
|
|
/**
|
|
* @param {*} circuit
|
|
* @returns {Listener} a transport listener
|
|
*/
|
|
module.exports = (circuit) => {
|
|
const listener = new EventEmitter()
|
|
const listeningAddrs = new Map()
|
|
|
|
/**
|
|
* Add swarm handler and listen for incoming connections
|
|
*
|
|
* @param {Multiaddr} addr
|
|
* @return {void}
|
|
*/
|
|
listener.listen = async (addr) => {
|
|
const [addrString] = String(addr).split('/p2p-circuit').slice(-1)
|
|
|
|
const relayConn = await circuit._dialer.connectToPeer(multiaddr(addrString))
|
|
const relayedAddr = relayConn.remoteAddr.encapsulate('/p2p-circuit')
|
|
|
|
listeningAddrs.set(relayConn.remotePeer.toB58String(), relayedAddr)
|
|
listener.emit('listening')
|
|
}
|
|
|
|
/**
|
|
* TODO: Remove the peers from our topology
|
|
*
|
|
* @return {void}
|
|
*/
|
|
listener.close = () => {}
|
|
|
|
/**
|
|
* Get fixed up multiaddrs
|
|
*
|
|
* NOTE: This method will grab the peers multiaddrs and expand them such that:
|
|
*
|
|
* a) If it's an existing /p2p-circuit address for a specific relay i.e.
|
|
* `/ip4/0.0.0.0/tcp/0/ipfs/QmRelay/p2p-circuit` this method will expand the
|
|
* address to `/ip4/0.0.0.0/tcp/0/ipfs/QmRelay/p2p-circuit/ipfs/QmPeer` where
|
|
* `QmPeer` is this peers id
|
|
* b) If it's not a /p2p-circuit address, it will encapsulate the address as a /p2p-circuit
|
|
* addr, such when dialing over a relay with this address, it will create the circuit using
|
|
* the encapsulated transport address. This is useful when for example, a peer should only
|
|
* be dialed over TCP rather than any other transport
|
|
*
|
|
* @return {Multiaddr[]}
|
|
*/
|
|
listener.getAddrs = () => {
|
|
const addrs = []
|
|
for (const addr of listeningAddrs.values()) {
|
|
addrs.push(addr)
|
|
}
|
|
return addrs
|
|
}
|
|
|
|
return listener
|
|
}
|