# All files / serialization dag.js

33.96% Statements 18/53
0% Branches 0/20
21.05% Functions 4/19
48.65% Lines 18/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

    1x

    1x
    1x

    1x
    2x

    1x
    2x

    1x

    1x

    1x
    1x

    1x
    2x

    1x
    2x

     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) => {
          if (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) => {
        if (isDagLinkJson(args[index])) {
          args[index] = dagLinkFromJson(args[index])
        }
        return args
      }
    }

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