Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
feat: upgrade to awesome dag-pb
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Nov 24, 2016
1 parent a131ffd commit 1e82b8c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 33 deletions.
6 changes: 3 additions & 3 deletions src/add-to-dagnode-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ 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)
}

cb(null, {
path: entry.Name,
hash: entry.Hash,
size: size
size: node.size
})
})
}
], next), done)
}
75 changes: 48 additions & 27 deletions src/api/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
}),

Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
})
}),
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions src/get-dagnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}))
}
})
Expand Down

0 comments on commit 1e82b8c

Please sign in to comment.