How to use dht-rpc - 1 common examples

To help you get started, we’ve selected a few dht-rpc examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github hyperswarm / dht / index.js View on Github external
const from = {
      port: value.port || query.node.port,
      host: query.node.host
    }
    if (!(from.port > 0 && from.port < 65536)) return cb(new Error('Invalid port'))

    const localRecord = value.localAddress
    const remoteRecord = peers.encode([from])

    const remoteCache = query.target.toString('hex')
    const localCache = localRecord &&
      remoteCache + '@local.' + localRecord.slice(0, 2).toString('hex')

    const localSuffix = localRecord && localRecord.slice(2)

    if (query.type === DHT.QUERY) {
      const local = localCache ? filter(this._peers.get(localCache, 64), localSuffix) : []
      const remote = filter(this._peers.get(remoteCache, 128 - local.length), remoteRecord)
      this.emit('lookup', query.target, from)

      return cb(null, {
        peers: remote.length ? Buffer.concat(remote) : null,
        localPeers: local.length ? Buffer.concat(local) : null
      })
    }
    if (value.unannounce) {
      this._peers.remove(remoteCache, remoteRecord)
      if (localRecord) this._peers.remove(localCache, localSuffix)
      this.emit('unannounce', query.target, from)
    } else {
      this._peers.add(remoteCache, remoteRecord)
      if (localRecord) this._peers.add(localCache, localSuffix)

dht-rpc

Make RPC calls over a Kademlia based DHT

MIT
Latest version published 2 months ago

Package Health Score

73 / 100
Full package analysis

Popular dht-rpc functions

Similar packages