Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'ipfs-geoip' 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.
peers.forEach((peer, i) => {
peer.ipfs = this.props.ipfs
peer.location = {
formatted: ''
}
let id = peer.peer.toB58String()
let location = this.state.locations[id]
if (!location) {
this.state.locations[id] = {}
const addr = peer.addr.toString()
getLocation(this.props.ipfs, [addr], (err, res) => {
if (err) return console.error(err)
// If we've unmounted, abort
if (!this.mounted) return
res = res || {}
peer.location = res
let locations = this.state.locations
locations[id] = res
peers[i] = peer
this.setState({
peers,
locations,
nonce: this.state.nonce++
})
})
}
this.geoipLookupPromises[ipv4Addr] = this.queue.add(async () => {
try {
const data = await geoip.lookup(getIpfs(), ipv4Addr)
await this.geoipCache.set(ipv4Addr, data)
} catch (e) {
// mark this one as failed so we don't retry again
this.failedAddrs.set(ipv4Addr, true)
} finally {
delete this.geoipLookupPromises[ipv4Addr]
}
})
}
this.props.ipfs.id((err, peer) => {
if (err || !peer) return console.error(err)
if (!this.mounted) return
this.setState({
node: {
peer,
location: {}
}
})
getLocation(this.props.ipfs, peer.addresses, (err, location) => {
if (err || !location) return console.error(err)
if (!this.mounted) return
this.setState({
node: {
peer,
location
}
})
})
})
}
_handleId (raw) {
this.statsCache.node = raw
this.statsCache.node.location = 'Unknown'
lookupPretty(this.ipfs, raw.addresses, (err, location) => {
if (err) { return }
this.statsCache.node.location = location && location.formatted
this.emit('change', this.statsCache)
})
}
var getLocation = module.exports = function (ipfs, multiaddrs, cb) {
if (multiaddrs.length === 0) return cb(null, null)
var address = multiaddrs[0].split('/')[2]
if (isLocal(address)) return getLocation(ipfs, multiaddrs.slice(1), cb)
geoip.lookup(ipfs, address, function (err, res) {
if (err) {
throw err
}
if (!res.country_name && multiaddrs.length > 1) {
return getLocation(ipfs, multiaddrs.slice(1), cb)
}
var location = 'Earth'
if (res.country_name) location = res.country_name + ', ' + location
if (res.region_code) location = res.region_code + ', ' + location
if (res.city) location = res.city + ', ' + location
res.formatted = location
cb(null, res)
})
raw.forEach((rawPeer) => {
let peer = {
id: rawPeer.peer.toB58String(),
addr: rawPeer.addr.toString(),
location: {
formatted: 'Unknown'
}
}
if (!this.locationsCache[peer.id]) {
lookupPretty(this.ipfs, [peer.addr], (err, result) => {
if (err) { return }
this.locationsCache[peer.id] = result
})
} else {
peer.location = this.locationsCache[peer.id]
}
peers.push(peer)
})
.then((peer) => {
lookupPretty(ipfs, peer.addresses, (err, location) => {
if (err) throw err
statsCache.location = location && location.formatted
mb.window.webContents.send('stats', statsCache)
})
})
.catch((err) => {