All files / server dht.js

33.33% Statements 6/18
0% Branches 0/6
0% Functions 0/12
37.5% Lines 6/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 631x 1x 1x 1x 1x     39x                                                                                                              
import { expose } from 'postmsg-rpc'
import { pre, post } from 'prepost'
import { preCidFromJson, preArrayOfCidFromJson } from '../serialization/cid'
import { prePeerIdFromJson, peerInfoToJson, isPeerInfo } from '../serialization/peer'
import { preBufferFromJson, preArrayOfBufferFromJson } from '../serialization/buffer'
 
export default function (getIpfs, opts) {
  return {
    put: expose('ipfs.dht.put', pre(
      opts.pre('dht.put'),
      (...args) => getIpfs().dht.put(...args)
    ), opts),
    get: expose('ipfs.dht.get', pre(
      opts.pre('dht.get'),
      (...args) => getIpfs().dht.get(...args)
    ), opts),
    findprovs: expose('ipfs.dht.findprovs', pre(
      preBufferFromJson(0),
      opts.pre('dht.findprovs'),
      post(
        (...args) => getIpfs().dht.findprovs(...args),
        (res) => res.map((item) => isPeerInfo(item) ? peerInfoToJson(item) : item)
      )
    ), opts),
    findpeer: expose('ipfs.dht.findpeer', pre(
      prePeerIdFromJson(0),
      opts.pre('dht.findpeer'),
      post(
        (...args) => getIpfs().dht.findpeer(...args),
        (res) => isPeerInfo(res) ? peerInfoToJson(res) : res
      )
    ), opts),
    provide: expose('ipfs.dht.provide', pre(
      preBufferFromJson(0),
      preArrayOfBufferFromJson(0),
      preCidFromJson(0),
      preArrayOfCidFromJson(0),
      opts.pre('dht.provide'),
      post(
        (...args) => getIpfs().dht.provide(...args),
        // js-ipfs returns undefined
        // js-ipfs-api -> go-ipfs returns the request stream, with no data
        //
        // https://ipfs.io/docs/api/#api-v0-dht-provide
        // ^ Docs say some response should be sent, but nothing is returned in
        // current implementations.
        //
        // Returning null here so structured clone doesn't error trying to clone
        // a stream.
        () => null
      )
    ), opts),
    query: expose('ipfs.dht.query', pre(
      prePeerIdFromJson(0),
      opts.pre('dht.query'),
      post(
        (...args) => getIpfs().dht.query(...args),
        (res) => res.map((item) => isPeerInfo(item) ? peerInfoToJson(item) : item)
      )
    ), opts)
  }
}