All files dagOverlay.js

100% Statements 15/15
100% Branches 3/3
100% Functions 6/6
100% Lines 13/13
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 351x 1x 1x   1x   6x       19x       17x 17x 14x 14x 14x   17x       77x                    
const CID = require('cids')
const multihashes = require('multihashes')
const Buffer = require('safe-buffer').Buffer
 
module.exports = class DAGoverlay {
  constructor (dag) {
    this._dag = dag
  }
 
  put (val, options) {
    return this._dag.put(val, options).then(link => link.buffer)
  }
 
  get (link, node, dropOptions = false) {
    const cid = new CID(link)
    if (!dropOptions) {
      node.options = {}
      node.options.format = cid.codec
      node.options.hashAlg = multihashes.decode(cid.multihash).name
    }
    return this._dag.get(cid).then(node => node.value)
  }
 
  isValidLink (link) {
    return Buffer.isBuffer(link)
    // try {
    //   const cid = new CID(link)
    //   CID.isCID(cid)
    //   return cid
    // } catch (e) {
    //   return false
    // }
  }
}