# All files / client dag.js

36.84% Statements 7/19
0% Branches 0/8
0% Functions 0/3
41.18% Lines 7/17

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

     import { caller } from 'postmsg-rpc'
    import callbackify from 'callbackify'
    import { pre, post } from 'prepost'
    import { isDagNodeJson, preDagNodeToJson, dagNodeFromJson } from '../serialization/dag'
    import { cidFromJson, preCidToJson } from '../serialization/cid'
    import { isBuffer, isBufferJson, preBufferToJson, bufferToJson, bufferFromJson } from '../serialization/buffer'
    import convertValues from '../serialization/utils/convert-values'

    export default function (opts) {
      return {
        put: callbackify.variadic(
          pre(
            preDagNodeToJson(0),
            (...args) => {
              if (args[0] && !isDagNodeJson(args[0])) {
                args[0] = convertValues(args[0], isBuffer, bufferToJson)
              }

              return args
            },
            post(
              caller('ipfs.dag.put', opts),
              cidFromJson
            )
          )
        ),
        get: callbackify.variadic(
          pre(
            preBufferToJson(0),
            preCidToJson(0),
            post(
              caller('ipfs.dag.get', opts),
              (res) => {
                if (isDagNodeJson(res.value)) {
                  return dagNodeFromJson(res.value).then((value) => ({ value }))
                }

                // TODO: CBOR node, is this correct?
                if (res.value) {
                  res.value = convertValues(res.value, isBufferJson, bufferFromJson)
                }

                return res
              }
            )
          )
        ),
        tree: callbackify.variadic(
          pre(
            preBufferToJson(0),
            preCidToJson(0),
            caller('ipfs.dag.tree', opts)
          )
        )
      }
    }