All files / client swarm.js

35% Statements 7/20
0% Branches 0/4
0% Functions 0/6
35% Lines 7/20

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 63 641x 1x 1x 1x 1x 1x     39x                                                                                                              
import { caller } from 'postmsg-rpc'
import callbackify from 'callbackify'
import { pre, post } from 'prepost'
import { peerInfoFromJson, peerIdFromJson, isPeerInfoJson, isPeerIdJson } from '../serialization/peer'
import { preMultiaddrToJson, multiaddrFromJson } from '../serialization/multiaddr'
import { preBufferToJson } from '../serialization/buffer'
 
export default function (opts) {
  return {
    peers: callbackify.variadic(
      post(
        caller('ipfs.swarm.peers', opts),
        (res) => Promise.all(
          res.map((item) => {
            item.addr = multiaddrFromJson(item.addr)
 
            // https://github.com/ipfs/js-ipfs/issues/1248
            if (isPeerInfoJson(item.peer)) {
              return peerInfoFromJson(item.peer)
                .then((peerInfo) => {
                  item.peer = peerInfo
                  return item
                })
            } else if (isPeerIdJson(item.peer)) {
              return peerIdFromJson(item.peer)
                .then((peerId) => {
                  item.peer = peerId
                  return item
                })
            }
 
            return Promise.resolve(item)
          })
        )
      )
    ),
    addrs: callbackify(
      post(
        caller('ipfs.swarm.addrs', opts),
        (res) => Promise.all(res.map(peerInfoFromJson))
      )
    ),
    localAddrs: callbackify(
      post(
        caller('ipfs.swarm.localAddrs', opts),
        (res) => res.map(multiaddrFromJson)
      )),
    connect: callbackify.variadic(
      pre(
        preBufferToJson(0),
        preMultiaddrToJson(0),
        caller('ipfs.swarm.connect', opts)
      )
    ),
    disconnect: callbackify.variadic(
      pre(
        preBufferToJson(0),
        preMultiaddrToJson(0),
        caller('ipfs.swarm.disconnect', opts)
      )
    )
  }
}