diff --git a/src/add-to-dagnode-transform.js b/src/add-to-dagnode-transform.js index 5566de542..17798162d 100644 --- a/src/add-to-dagnode-transform.js +++ b/src/add-to-dagnode-transform.js @@ -13,7 +13,7 @@ module.exports = (err, res, send, done) => { map(res, (entry, next) => waterfall([ (cb) => getDagNode(send, entry.Hash, cb), - (node, cb) => node.size((err, size) => { + (node, cb) => { if (err) { return cb(err) } @@ -21,8 +21,8 @@ module.exports = (err, res, send, done) => { cb(null, { path: entry.Name, hash: entry.Hash, - size: size + size: node.size }) - }) + } ], next), done) } diff --git a/src/api/object.js b/src/api/object.js index 1d77bcd74..ad983ad52 100644 --- a/src/api/object.js +++ b/src/api/object.js @@ -46,14 +46,17 @@ module.exports = (send) => { return callback(err) } - const node = new DAGNode(result.Data, result.Links.map( - (l) => { - return new DAGLink(l.Name, l.Size, new Buffer(bs58.decode(l.Hash))) - })) - - cache.set(multihash, node) + const links = result.Links.map((l) => { + return new DAGLink(l.Name, l.Size, new Buffer(bs58.decode(l.Hash))) + }) - callback(null, node) + DAGNode.create(result.Data, links, (err, node) => { + if (err) { + return callback(err) + } + cache.set(multihash, node) + callback(null, node) + }) }) }), @@ -73,12 +76,19 @@ module.exports = (send) => { if (Buffer.isBuffer(obj)) { if (!options.enc) { - tmpObj = { Data: obj.toString(), Links: [] } + tmpObj = { + Data: obj.toString(), + Links: [] + } } } else if (obj.multihash) { tmpObj = { Data: obj.data.toString(), - Links: obj.links.map((l) => { return l.toJSON() }) + Links: obj.links.map((l) => { + const link = l.toJSON() + link.hash = link.multihash + return link + }) } } else if (typeof obj === 'object') { tmpObj.Data = obj.Data.toString() @@ -125,23 +135,26 @@ module.exports = (send) => { }) return } else { - node = new DAGNode(obj.Data, obj.Links) - } - next() - - function next () { - node.toJSON((err, nodeJSON) => { + DAGNode.create(new Buffer(obj.Data), obj.Links, (err, _node) => { if (err) { return callback(err) } - if (nodeJSON.Hash !== result.Hash) { - return callback(new Error('Stored object was different from constructed object')) - } + node = _node + next() + }) + return + } + next() - cache.set(result.Hash, node) + function next () { + const nodeJSON = node.toJSON() + if (nodeJSON.multihash !== result.Hash) { + const err = new Error('multihashes do not match') + return callback(err) + } - callback(null, node) - }) + cache.set(result.Hash, node) + callback(null, node) } }) }), @@ -248,14 +261,15 @@ module.exports = (send) => { return callback(err) } - const node = new DAGNode() - node.toJSON((err, nodeJSON) => { + DAGNode.create(new Buffer(0), (err, node) => { if (err) { return callback(err) } - if (nodeJSON.Hash !== result.Hash) { - return callback(new Error('Stored object was different from constructed object')) + if (node.toJSON().multihash !== result.Hash) { + console.log(node.toJSON()) + console.log(result) + return callback(new Error('multihashes do not match')) } callback(null, node) @@ -280,7 +294,11 @@ module.exports = (send) => { send({ path: 'object/patch/add-link', - args: [multihash, dLink.name, bs58.encode(dLink.hash).toString()] + args: [ + multihash, + dLink.name, + bs58.encode(dLink.multihash).toString() + ] }, (err, result) => { if (err) { return callback(err) @@ -305,7 +323,10 @@ module.exports = (send) => { send({ path: 'object/patch/rm-link', - args: [multihash, dLink.name] + args: [ + multihash, + dLink.name + ] }, (err, result) => { if (err) { return callback(err) diff --git a/src/get-dagnode.js b/src/get-dagnode.js index 3c0896ecf..75cd0886c 100644 --- a/src/get-dagnode.js +++ b/src/get-dagnode.js @@ -34,14 +34,13 @@ module.exports = function (send, hash, callback) { var stream = res[1] if (Buffer.isBuffer(stream)) { - callback(err, new DAGNode(stream, object.Links)) + DAGNode.create(stream, object.Links, callback) } else { stream.pipe(bl(function (err, data) { if (err) { return callback(err) } - - callback(err, new DAGNode(data, object.Links)) + DAGNode.create(data, object.Links, callback) })) } })