# All files / src/serialization dag.js

98.11% Statements 52/53
85% Branches 17/20
100% Functions 19/19
100% Lines 37/37

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

    1x
    62x
    62x
    62x
    62x
    62x

    62x

    64x
    104x

    1x
    57x
    43x
    12x

    12x
    12x

    31x

    1x
    57x
    43x
    12x

    43x

    16x

    16x

    4x
    4x

    1x
    46x
    4x
    4x

    4x

    1x
    46x
    4x
    4x

    4x

     import { DAGNode, DAGLink } from 'ipld-dag-pb'
    import { bufferFromJson, bufferToJson } from './buffer'

    export const dagNodeFromJson = (obj) => {
      return new Promise((resolve, reject) => {
        const links = obj.links.map(dagLinkFromJson)
        DAGNode.create(bufferFromJson(obj.data), links, (err, dagNode) => {
          Iif (err) return reject(err)
          resolve(dagNode)
        })
      })
    }

    export const dagNodeToJson = (dagNode) => ({
      __ipfsPostMsgProxyType: 'DAGNode',
      links: dagNode.links.map(dagLinkToJson),
      data: bufferToJson(dagNode.data)
    })

    export const isDagNode = (obj) => obj && obj.constructor && obj.constructor.name === 'DAGNode'
    export const isDagNodeJson = (obj) => obj && obj.__ipfsPostMsgProxyType === 'DAGNode'

    export const preDagNodeFromJson = (index) => {
      return (...args) => {
        if (isDagNodeJson(args[index])) {
          return dagNodeFromJson(args[index])
            .then((dagNode) => {
              args[index] = dagNode
              return args
            })
        }
        return args
      }
    }

    export const preDagNodeToJson = (index) => {
      return (...args) => {
        if (isDagNode(args[index])) {
          args[index] = dagNodeToJson(args[index])
        }
        return args
      }
    }

    export const dagLinkFromJson = (obj) => new DAGLink(obj.name, obj.size, obj.multihash)

    export const dagLinkToJson = (link) => Object.assign(
      { __ipfsPostMsgProxyType: 'DAGLink' },
      link.toJSON()
    )

    export const isDagLink = (obj) => obj && obj.constructor && obj.constructor.name === 'DAGLink'
    export const isDagLinkJson = (obj) => obj && obj.__ipfsPostMsgProxyType === 'DAGLink'

    export const preDagLinkFromJson = (index) => {
      return (...args) => {
        Eif (isDagLinkJson(args[index])) {
          args[index] = dagLinkFromJson(args[index])
        }
        return args
      }
    }

    export const preDagLinkToJson = (index) => {
      return (...args) => {
        Eif (isDagLink(args[index])) {
          args[index] = dagLinkToJson(args[index])
        }
        return args
      }
    }