All files / server dag.js

84.62% Statements 22/26
68.75% Branches 11/16
100% Functions 5/5
84% Lines 21/25

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 611x 1x 1x 1x 1x 1x     39x     28x   16x 11x     16x               16x       16x                 9x   9x 2x 7x 3x   4x     9x               5x        
import { expose } from 'postmsg-rpc'
import { pre, post } from 'prepost'
import { isDagNode, dagNodeToJson, preDagNodeFromJson } from '../serialization/dag'
import { isCidJson, cidToJson, cidFromJson, preCidFromJson } from '../serialization/cid'
import { isBuffer, isBufferJson, bufferFromJson, preBufferFromJson, bufferToJson } from '../serialization/buffer'
import convertValues from '../serialization/utils/convert-values'
 
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)
        }
 
        Iif (args[1] && args[1].cid) {
          if (isBufferJson(args[1].cid)) {
            args[1].cid = bufferFromJson(args[1].cid)
          } else if (isCidJson(args[1].cid)) {
            args[1].cid = cidFromJson(args[1].cid)
          }
        }
 
        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 if (isBuffer(res.value)) {
            res.value = bufferToJson(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)
  }
}