# All files / src/server dag.js

100% Statements 19/19
100% Branches 6/6
100% Functions 5/5
100% Lines 18/18

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

    23x

    24x

    12x
    7x

    12x

    12x

    9x

    9x
    2x

    7x

    9x

    5x

     import { expose } from 'postmsg-rpc'
    import { pre, post } from 'prepost'
    import { isDagNode, dagNodeToJson, preDagNodeFromJson } from '../serialization/dag'
    import { cidToJson, preCidFromJson } from '../serialization/cid'
    import { isBuffer, isBufferJson, bufferFromJson, preBufferFromJson, bufferToJson } from '../serialization/buffer'
    import { convertValues } from '../converters'

    export default function (getIpfs, opts) {
      return {
        put: expose('ipfs.dag.put', pre(
          preDagNodeFromJson(0),
          (...args) => {
            // TODO: CBOR node, is this correct?
            if (args[0] && !isDagNode(args[0])) {
              args[0] = convertValues(args[0], isBufferJson, bufferFromJson)
            }

            return args
          },
          opts.pre('dag.put'),
          post(
            (...args) => getIpfs().dag.put(...args),
            cidToJson
          )
        ), opts),
        get: expose('ipfs.dag.get', pre(
          preBufferFromJson(0),
          preCidFromJson(0),
          opts.pre('dag.get'),
          post(
            (...args) => getIpfs().dag.get(...args),
            (res) => {
              if (isDagNode(res.value)) {
                res.value = dagNodeToJson(res.value)
              } else {
                res.value = convertValues(res.value, isBuffer, bufferToJson)
              }

              return res
            }
          )
        ), opts),
        tree: expose('ipfs.dag.tree', pre(
          preBufferFromJson(0),
          preCidFromJson(0),
          opts.pre('dag.tree'),
          (...args) => getIpfs().dag.tree(...args)
        ), opts)
      }
    }