# All files / server swarm.js

30% Statements 6/20
0% Branches 0/4
0% Functions 0/9
30% Lines 6/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
    531x
    1x
    1x
    1x
    1x

    1x

     import { expose } from 'postmsg-rpc'
    import { pre, post } from 'prepost'
    import { peerInfoToJson, peerIdToJson, isPeerInfo, isPeerId } from '../serialization/peer'
    import { preMultiaddrFromJson, multiaddrToJson } from '../serialization/multiaddr'
    import { preBufferFromJson } from '../serialization/buffer'

    export default function (getIpfs, opts) {
      return {
        peers: expose('ipfs.swarm.peers', pre(
          opts.pre('swarm.peers'),
          post(
            (...args) => getIpfs().swarm.peers(...args),
            (res) => res.map((item) => {
              item.addr = multiaddrToJson(item.addr)
              // https://github.com/ipfs/js-ipfs/issues/1248
              if (isPeerInfo(item.peer)) {
                item.peer = peerInfoToJson(item.peer)
              } else if (isPeerId(item.peer)) {
                item.peer = peerIdToJson(item.peer)
              }
              return item
            })
          )
        ), opts),
        addrs: expose('ipfs.swarm.addrs', pre(
          opts.pre('swarm.addrs'),
          post(
            () => getIpfs().swarm.addrs(),
            (res) => res.map(peerInfoToJson)
          )
        ), opts),
        localAddrs: expose('ipfs.swarm.localAddrs', pre(
          opts.pre('swarm.localAddrs'),
          post(
            () => getIpfs().swarm.localAddrs(),
            (res) => res.map(multiaddrToJson)
          )
        ), opts),
        connect: expose('ipfs.swarm.connect', pre(
          preBufferFromJson(0),
          preMultiaddrFromJson(0),
          opts.pre('swarm.connect'),
          (...args) => getIpfs().swarm.connect(...args)
        ), opts),
        disconnect: expose('ipfs.swarm.disconnect', pre(
          preBufferFromJson(0),
          preMultiaddrFromJson(0),
          opts.pre('swarm.disconnect'),
          (...args) => getIpfs().swarm.disconnect(...args)
        ), opts)
      }
    }