Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should list peers this node is connected to with verbose option', async () => {
const peers = await ipfsA.swarm.peers({ verbose: true })
expect(peers).to.have.length.above(0)
const peer = peers[0]
expect(peer).to.have.a.property('addr')
expect(multiaddr.isMultiaddr(peer.addr)).to.equal(true)
expect(peer).to.have.a.property('peer')
expect(peer).to.have.a.property('latency')
expect(peer.latency).to.match(/n\/a|[0-9]+[mµ]?s/) // n/a or 3ms or 3µs or 3s
/* TODO: These assertions must be uncommented as soon as
https://github.com/ipfs/js-ipfs/issues/2601 gets resolved */
// expect(peer).to.have.a.property('muxer')
// expect(peer).to.have.a.property('streams')
})
function isMultiaddr (input) {
if (!input) return false
if (isString(input) || input instanceof Buffer) {
try {
new Multiaddr(input) // eslint-disable-line no-new
return true
} catch (e) {
return false
}
}
if (Multiaddr.isMultiaddr(input)) return true
return false
}
function ipfsClient (hostOrMultiaddr, port, userOptions) {
// convert all three params to objects that we can merge.
let options = {}
if (!hostOrMultiaddr) {
// autoconfigure host and port in browser
if (typeof self !== 'undefined') {
options = urlToOptions(self.location)
}
} else if (multiaddr.isMultiaddr(hostOrMultiaddr)) {
options = maToOptions(hostOrMultiaddr)
} else if (typeof hostOrMultiaddr === 'object') {
options = hostOrMultiaddr
} else if (typeof hostOrMultiaddr === 'string') {
if (hostOrMultiaddr[0] === '/') {
// throws if multiaddr is malformed or can't be converted to a nodeAddress
options = maToOptions(multiaddr(hostOrMultiaddr))
} else {
// hostOrMultiaddr is domain or ip address as a string
options.host = hostOrMultiaddr
}
}
if (port && typeof port !== 'object') {
port = { port: port }
}
add(pi, lazy) {
let p = isInPool(this.cache, pi)
if (p.length > 1) throw new Error("Multiple peers found!")
if (p.length) return p.pop()
if (PeerInfo.isPeerInfo(pi) && pi.id.toB58String())
return this.push(new Peer.Lp2pPeer(pi), lazy)
if (ip2multi.isIp(pi))
return this.push(new Peer.ZeroPeer(ip2multi(pi)), lazy)
if (multiaddr.isMultiaddr(pi)) {
if (pi.toString().indexOf("ipfs") != -1) {
const id = Id.createFromB58String(pi.toString().split("ipfs/").pop())
const _pi = new PeerInfo(id)
_pi.multiaddrs.addSafe(pi)
return this.push(new Peer.Lp2pPeer(_pi), lazy)
} else {
return this.push(new Peer.ZeroPeer(pi.toString()), lazy)
}
}
if (typeof pi == "string") {
if (pi.match(/\/.+\/.+\/.+\/.+\//) || pi.match(/\/.+\/.+\/.+\/.+/))
return this.push(new Peer.ZeroPeer(pi), lazy)
if (pi.match(/\/.+\/.+\/.+\/.+\/.+\/.+\//) || pi.match(/\/.+\/.+\/.+\/.+\/.+\/.+/)) {
const id = Id.createFromB58String(pi.split("ipfs/").pop())
function getB58Str (peer) {
let b58Str
if (typeof peer === 'string') {
if (peer.startsWith('/')) {
b58Str = Multiaddr(peer).getPeerId()
} else {
b58Str = peer
}
} else if (Buffer.isBuffer(peer)) {
b58Str = bs58.encode(peer).toString()
} else if (PeerId.isPeerId(peer)) {
b58Str = peer.toB58String()
} else if (PeerInfo.isPeerInfo(peer)) {
b58Str = peer.id.toB58String()
} else if (Multiaddr.isMultiaddr(peer)) {
b58Str = peer.getPeerId()
} else {
throw new Error('not valid PeerId or PeerInfo, or B58Str')
}
return b58Str
}
addrs.map(addr => multiaddr.isMultiaddr(addr) ? addr : multiaddr(addr)).forEach(addr => this.pi.multiaddrs.add(addr))
}
function getPeerIdType(pi) {
if (Peer.isPeerInfo(pi))
return "libp2p"
if (ip2multi.isIp(pi))
return "zero"
if (multiaddr.isMultiaddr(pi)) {
if (pi.toString().indexOf("ipfs") != -1)
return "libp2p"
return "zero"
}
if (typeof pi == "string") {
if (pi.match(/\/.+\/.+\/.+\/.+\//) || pi.match(/\/.+\/.+\/.+\/.+/))
return "zero"
if (pi.match(/\/.+\/.+\/.+\/.+\/.+\/.+\//) || pi.match(/\/.+\/.+\/.+\/.+\/.+\/.+/))
return "libp2p"
}
return false
}
export const isMultiaddr = (obj) => obj && Multiaddr.isMultiaddr(obj)
export const isMultiaddrJson = (obj) => obj && obj.__ipfsPostMsgProxyType === 'Multiaddr'
constructor ({ localAddr, remoteAddr, localPeer, remotePeer, newStream, close, getStreams, stat }) {
localAddr && assert(multiaddr.isMultiaddr(localAddr), 'localAddr must be an instance of multiaddr')
assert(multiaddr.isMultiaddr(remoteAddr), 'remoteAddr must be an instance of multiaddr')
assert(PeerId.isPeerId(localPeer), 'localPeer must be an instance of peer-id')
assert(PeerId.isPeerId(remotePeer), 'remotePeer must be an instance of peer-id')
assert(typeof newStream === 'function', 'new stream must be a function')
assert(typeof close === 'function', 'close must be a function')
assert(typeof getStreams === 'function', 'getStreams must be a function')
assert(stat, 'connection metadata object must be provided')
assert(stat.direction === 'inbound' || stat.direction === 'outbound', 'direction must be "inbound" or "outbound"')
assert(stat.timeline, 'connection timeline object must be provided in the stat object')
assert(stat.timeline.open, 'connection open timestamp must be provided')
assert(stat.timeline.upgraded, 'connection upgraded timestamp must be provided')
/**
* Connection identifier.
*/
this.id = (parseInt(Math.random() * 1e9)).toString(36) + Date.now()