# All files / src/client swarm.js

100% Statements 14/14
100% Branches 0/0
100% Functions 5/5
100% Lines 14/14

    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
    521x
    1x
    1x
    1x
    1x
    1x

    23x

    6x
    21x

    21x
    21x
    21x

    2x

    2x

     import { caller } from 'postmsg-rpc'
    import callbackify from 'callbackify'
    import { pre, post } from 'prepost'
    import { peerInfoFromJson } 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) => (
                peerInfoFromJson(item.peer)
                  .then((peerInfo) => {
                    item.addr = multiaddrFromJson(item.addr)
                    item.peer = peerInfo
                    return 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)
          )
        )
      }
    }