Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'multiaddr' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
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 }
}
publicAddress: (cb) => stunServer.once('bindingResponse', (stunMsg) => {
stunServer.close()
server.close(
cb(null, Multiaddr.fromNodeAddress(stunMsg.getAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS).value, 'tcp'))
)
}),
stunError: (cb) => stunServer.send(
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())
callback = function noop () {}
}
listener.io.emit('ss-leave')
setTimeout(() => {
listener.emit('close')
callback()
}, 100)
}
listener.getAddrs = (callback) => {
process.nextTick(() => {
callback(null, [maSelf])
})
}
listeners[multiaddr.toString()] = listener
return listener
}
})
return defer.promise
}
listener.close = () => {
listener.__connections.forEach(maConn => maConn.close())
listener.io && listener.io.emit('ss-leave')
listener.emit('close')
}
listener.getAddrs = () => {
return [WebRTCStar.maSelf]
}
WebRTCStar.listenersRefs[multiaddr.toString()] = listener
return listener
}
function cleanUrlSIO (ma) {
const maStrSplit = ma.toString().split('/')
const tcpProto = ma.protos()[1].name
const wsProto = ma.protos()[2].name
const tcpPort = ma.stringTuples()[1][1]
if (tcpProto !== 'tcp' || (wsProto !== 'ws' && wsProto !== 'wss')) {
throw new Error('invalid multiaddr: ' + ma.toString())
}
if (!multiaddr.isName(ma)) {
return 'http://' + maStrSplit[2] + ':' + maStrSplit[4]
}
if (wsProto === 'ws') {
return 'http://' + maStrSplit[2] + (tcpPort === 80 ? '' : ':' + tcpPort)
}
if (wsProto === 'wss') {
return 'https://' + maStrSplit[2] + (tcpPort === 443 ? '' : ':' + tcpPort)
}
}
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))
}