All files / client dag.js

84.62% Statements 22/26
66.67% Branches 12/18
100% Functions 3/3
83.33% Lines 20/24

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 57 58 59 60 61 62 63 64 65 661x 1x 1x 1x 1x 1x 1x     39x       28x 16x 11x     16x               16x                             9x 2x     7x 3x 4x 4x     7x                            
import { caller } from 'postmsg-rpc'
import callbackify from 'callbackify'
import { pre, post } from 'prepost'
import { isDagNodeJson, preDagNodeToJson, dagNodeFromJson } from '../serialization/dag'
import { isCid, cidFromJson, cidToJson, 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)
          }
 
          Iif (args[1] && args[1].cid) {
            if (isBuffer(args[1].cid)) {
              args[1].cid = bufferToJson(args[1].cid)
            } else if (isCid(args[1].cid)) {
              args[1].cid = cidToJson(args[1].cid)
            }
          }
 
          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 }))
            }
 
            if (isBufferJson(res.value)) {
              res.value = bufferFromJson(res.value)
            } else Eif (res.value) { // TODO: CBOR node, is this correct?
              res.value = convertValues(res.value, isBufferJson, bufferFromJson)
            }
 
            return res
          }
        )
      )
    ),
    tree: callbackify.variadic(
      pre(
        preBufferToJson(0),
        preCidToJson(0),
        caller('ipfs.dag.tree', opts)
      )
    )
  }
}