# All files / serialization peer.js

27.27% Statements 15/55
0% Branches 0/18
0% Functions 0/18
36.59% Lines 15/41

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
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    841x
    1x
    1x

    1x

    1x

    1x
    1x

    1x

    1x

    1x

    1x

    1x
    1x

    1x

    1x

     import PeerInfo from 'peer-info'
    import PeerId from 'peer-id'
    import { multiaddrToJson, multiaddrFromJson } from './multiaddr'

    export const peerInfoFromJson = (obj) => {
      return peerIdFromJson(obj.id)
        .then((peerId) => {
          const peerInfo = new PeerInfo(peerId)

          obj.multiaddrs.forEach((multiaddr) => {
            peerInfo.multiaddrs.add(multiaddrFromJson(multiaddr))
          })

          if (obj._connectedMultiaddr) {
            peerInfo.connect(multiaddrFromJson(obj._connectedMultiaddr))
          }

          return peerInfo
        })
    }

    export const peerInfoToJson = (peerInfo) => ({
      __ipfsPostMsgProxyType: 'PeerInfo',
      id: peerIdToJson(peerInfo.id),
      multiaddrs: peerInfo.multiaddrs.toArray().map(multiaddrToJson),
      _connectedMultiaddr: peerInfo._connectedMultiaddr
        ? multiaddrToJson(peerInfo._connectedMultiaddr)
        : null
    })

    export const isPeerInfo = PeerInfo.isPeerInfo
    export const isPeerInfoJson = (obj) => obj && obj.__ipfsPostMsgProxyType === 'PeerInfo'

    export const prePeerInfoFromJson = (index) => {
      return (...args) => {
        if (isPeerInfoJson(args[index])) {
          args[index] = peerInfoFromJson(args[index])
        }
        return args
      }
    }

    export const prePeerInfoToJson = (index) => {
      return (...args) => {
        if (isPeerInfo(args[index])) {
          args[index] = peerInfoToJson(args[index])
        }
        return args
      }
    }

    export const peerIdFromJson = (obj) => new Promise((resolve, reject) => {
      PeerId.createFromJSON(obj, (err, peerId) => {
        if (err) return reject(err)
        resolve(peerId)
      })
    })

    export const peerIdToJson = (peerId) => Object.assign(
      { __ipfsPostMsgProxyType: 'PeerId' },
      peerId.toJSON()
    )

    export const isPeerId = PeerId.isPeerId
    export const isPeerIdJson = (obj) => obj && obj.__ipfsPostMsgProxyType === 'PeerId'

    export const prePeerIdFromJson = (index) => {
      return (...args) => {
        if (isPeerIdJson(args[index])) {
          args[index] = peerIdFromJson(args[index])
        }
        return args
      }
    }

    export const prePeerIdToJson = (index) => {
      return (...args) => {
        if (isPeerId(args[index])) {
          args[index] = peerIdToJson(args[index])
        }
        return args
      }
    }