# All files / src/serialization peer.js

70.91% Statements 39/55
38.89% Branches 7/18
72.22% Functions 13/18
75.61% Lines 31/41

    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
    36x

    36x

    36x
    58x

    36x
    36x

    36x

    36x

    1x
    1x

    1x

    1x

    36x
    36x
    36x
    36x

    36x

    1x
    2x

    1x
    46x
    2x

    2x

    1x
    46x
    2x

    2x

     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))
          })

          Eif (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) => {
        Iif (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) => {
        Iif (isPeerIdJson(args[index])) {
          args[index] = peerIdFromJson(args[index])
        }
        return args
      }
    }

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