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

feat: upgrade to awesome dag-pb #444

Merged
merged 2 commits into from
Nov 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"flatmap": "0.0.3",
"glob": "^7.1.1",
"ipfs-block": "^0.5.0",
"ipld-dag-pb": "^0.8.0",
"ipld-dag-pb": "^0.9.0",
"is-ipfs": "^0.2.1",
"isstream": "^0.1.2",
"multiaddr": "^2.0.3",
Expand Down Expand Up @@ -61,7 +61,7 @@
"eslint-plugin-react": "^6.7.1",
"gulp": "^3.9.1",
"hapi": "^15.2.0",
"interface-ipfs-core": "^0.20.1",
"interface-ipfs-core": "^0.21.0",
"ipfsd-ctl": "^0.17.0",
"pre-commit": "^1.1.3",
"socket.io": "^1.5.1",
Expand Down
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