All files index.js

100% Statements 73/73
100% Branches 33/33
100% Functions 12/12
100% Lines 73/73
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 1701x 1x     54x       37x       1x 1x       1x         3x       6x 6x 6x 6x 6x                     4x     4x 4x         4x   4x 2x 2x 2x     4x 1x   4x 4x       8x 8x 8x 28x   28x 3x 25x   5x     20x 7x   13x 13x 13x 12x 12x   1x 1x               7x                           4x 4x 4x       21x 21x 3x     21x 11x 11x 11x 19x 19x   11x   21x       14x 14x 11x 12x 12x     14x 14x 14x 5x       5x                       2x 1x 1x 1x   2x 2x      
const CID = require('cids')
const multihashes = require('multihashes')
 
function isValidCID (link) {
  return (typeof link === 'string' || Buffer.isBuffer(link)) && !link.options
}
 
function isObject (obj) {
  return typeof obj === 'object' && obj !== null
}
 
function clearObject (myObject) {
  for (var member in myObject) {
    delete myObject[member]
  }
}
 
module.exports = class Graph {
  /**
   * @param {Object} ipfsDag an instance of [ipfs.dag](https://github.com/ipfs/interface-ipfs-core/tree/master/API/dag#dag-api)
   */
  constructor (ipfsDag) {
    this._dag = ipfsDag
  }
 
  async _loadCID (node, link) {
    const cid = new CID(link)
    node.options = {}
    node.options.format = cid.codec
    node.options.hashAlg = multihashes.decode(cid.multihash).name
    node['/'] = (await this._dag.get(cid)).value
  }
 
  /**
   * sets a value on a root object given its path
   * @param {Object} node
   * @param {String} path
   * @param {*} value
   * @return {Promise}
   */
  async set (node, path, value) {
    value = {
      '/': value
    }
    path = path.split('/')
    const last = path.pop()
    let {
      value: foundVal,
      remainderPath: remainder,
      parent
    } = await this._get(node, path)
    // if the found value is a litaral attach an object to the parent object
    if (!isObject(foundVal)) {
      const pos = path.length - remainder.length - 1
      const name = path.slice(pos, pos + 1)[0]
      foundVal = parent[name] = {}
    }
    // extend the path for the left over path names
    for (const name of remainder) {
      foundVal = foundVal[name] = {}
    }
    foundVal[last] = value
    return node
  }
 
  async _get (node, path) {
    let parent = node
    path = path.slice(0)
    while (1) {
      const link = node['/']
      // if there is a link, traverse throught it
      if (isValidCID(link)) {
        await this._loadCID(node, link)
      } else if (link) {
        // link is a POJO
        node = link
      } else {
        // traverse through POJOs
        if (!path.length) {
          break
        }
        const name = path.shift()
        const edge = node[name]
        if (edge) {
          parent = node
          node = edge
        } else {
          path.unshift(name)
          return {
            value: node,
            remainderPath: path,
            parent: parent
          }
        }
      }
    }
    return {
      value: node,
      remainderPath: [],
      parent: parent
    }
  }
 
  /**
   * traverses an object's path and returns the resulting value in a Promise
   * @param {Object} node
   * @param {String} path
   * @return {Promise}
   */
  async get (node, path) {
    path = path.split('/')
    const {value} = await this._get(node, path)
    return value
  }
 
  async tree (node, levels = 1) {
    const link = node['/']
    if (isValidCID(link)) {
      await this._loadCID(node, link)
    }
 
    if (levels && isObject(node)) {
      levels--
      const promises = []
      for (const name in node) {
        const edge = node[name]
        promises.push(this.tree(edge, levels))
      }
      await Promise.all(promises)
    }
    return node
  }
 
  async _flush (node, opts) {
    const awaiting = []
    if (isObject(node)) {
      for (const name in node) {
        const edge = node[name]
        awaiting.push(this._flush(edge))
      }
    }
    await Promise.all(awaiting)
    const link = node['/']
    if (link && !isValidCID(link)) {
      return this._dag.put(link, opts || node.options || {
        format: 'dag-cbor',
        hashAlg: 'sha2-256'
      }).then(cid => {
        node['/'] = cid.toBaseEncodedString()
      })
    }
  }
 
  /**
   * flush an object to ipfs returning the resulting CID in a promise
   * @param {Object} node
   * @param {Object} opts - encoding options for [`dag.put`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/dag#dagput)
   * @return {Promise}
   */
  async flush (node, opts) {
    if (!node['/']) {
      const oldRoot = Object.assign({}, node)
      clearObject(node)
      node['/'] = oldRoot
    }
    await this._flush(node, opts)
    return node
  }
}