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

Commit 1e82b8c

Browse files
committed
feat: upgrade to awesome dag-pb
1 parent a131ffd commit 1e82b8c

File tree

3 files changed

+53
-33
lines changed

3 files changed

+53
-33
lines changed

src/add-to-dagnode-transform.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ module.exports = (err, res, send, done) => {
1313

1414
map(res, (entry, next) => waterfall([
1515
(cb) => getDagNode(send, entry.Hash, cb),
16-
(node, cb) => node.size((err, size) => {
16+
(node, cb) => {
1717
if (err) {
1818
return cb(err)
1919
}
2020

2121
cb(null, {
2222
path: entry.Name,
2323
hash: entry.Hash,
24-
size: size
24+
size: node.size
2525
})
26-
})
26+
}
2727
], next), done)
2828
}

src/api/object.js

+48-27
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,17 @@ module.exports = (send) => {
4646
return callback(err)
4747
}
4848

49-
const node = new DAGNode(result.Data, result.Links.map(
50-
(l) => {
51-
return new DAGLink(l.Name, l.Size, new Buffer(bs58.decode(l.Hash)))
52-
}))
53-
54-
cache.set(multihash, node)
49+
const links = result.Links.map((l) => {
50+
return new DAGLink(l.Name, l.Size, new Buffer(bs58.decode(l.Hash)))
51+
})
5552

56-
callback(null, node)
53+
DAGNode.create(result.Data, links, (err, node) => {
54+
if (err) {
55+
return callback(err)
56+
}
57+
cache.set(multihash, node)
58+
callback(null, node)
59+
})
5760
})
5861
}),
5962

@@ -73,12 +76,19 @@ module.exports = (send) => {
7376

7477
if (Buffer.isBuffer(obj)) {
7578
if (!options.enc) {
76-
tmpObj = { Data: obj.toString(), Links: [] }
79+
tmpObj = {
80+
Data: obj.toString(),
81+
Links: []
82+
}
7783
}
7884
} else if (obj.multihash) {
7985
tmpObj = {
8086
Data: obj.data.toString(),
81-
Links: obj.links.map((l) => { return l.toJSON() })
87+
Links: obj.links.map((l) => {
88+
const link = l.toJSON()
89+
link.hash = link.multihash
90+
return link
91+
})
8292
}
8393
} else if (typeof obj === 'object') {
8494
tmpObj.Data = obj.Data.toString()
@@ -125,23 +135,26 @@ module.exports = (send) => {
125135
})
126136
return
127137
} else {
128-
node = new DAGNode(obj.Data, obj.Links)
129-
}
130-
next()
131-
132-
function next () {
133-
node.toJSON((err, nodeJSON) => {
138+
DAGNode.create(new Buffer(obj.Data), obj.Links, (err, _node) => {
134139
if (err) {
135140
return callback(err)
136141
}
137-
if (nodeJSON.Hash !== result.Hash) {
138-
return callback(new Error('Stored object was different from constructed object'))
139-
}
142+
node = _node
143+
next()
144+
})
145+
return
146+
}
147+
next()
140148

141-
cache.set(result.Hash, node)
149+
function next () {
150+
const nodeJSON = node.toJSON()
151+
if (nodeJSON.multihash !== result.Hash) {
152+
const err = new Error('multihashes do not match')
153+
return callback(err)
154+
}
142155

143-
callback(null, node)
144-
})
156+
cache.set(result.Hash, node)
157+
callback(null, node)
145158
}
146159
})
147160
}),
@@ -248,14 +261,15 @@ module.exports = (send) => {
248261
return callback(err)
249262
}
250263

251-
const node = new DAGNode()
252-
node.toJSON((err, nodeJSON) => {
264+
DAGNode.create(new Buffer(0), (err, node) => {
253265
if (err) {
254266
return callback(err)
255267
}
256268

257-
if (nodeJSON.Hash !== result.Hash) {
258-
return callback(new Error('Stored object was different from constructed object'))
269+
if (node.toJSON().multihash !== result.Hash) {
270+
console.log(node.toJSON())
271+
console.log(result)
272+
return callback(new Error('multihashes do not match'))
259273
}
260274

261275
callback(null, node)
@@ -280,7 +294,11 @@ module.exports = (send) => {
280294

281295
send({
282296
path: 'object/patch/add-link',
283-
args: [multihash, dLink.name, bs58.encode(dLink.hash).toString()]
297+
args: [
298+
multihash,
299+
dLink.name,
300+
bs58.encode(dLink.multihash).toString()
301+
]
284302
}, (err, result) => {
285303
if (err) {
286304
return callback(err)
@@ -305,7 +323,10 @@ module.exports = (send) => {
305323

306324
send({
307325
path: 'object/patch/rm-link',
308-
args: [multihash, dLink.name]
326+
args: [
327+
multihash,
328+
dLink.name
329+
]
309330
}, (err, result) => {
310331
if (err) {
311332
return callback(err)

src/get-dagnode.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@ module.exports = function (send, hash, callback) {
3434
var stream = res[1]
3535

3636
if (Buffer.isBuffer(stream)) {
37-
callback(err, new DAGNode(stream, object.Links))
37+
DAGNode.create(stream, object.Links, callback)
3838
} else {
3939
stream.pipe(bl(function (err, data) {
4040
if (err) {
4141
return callback(err)
4242
}
43-
44-
callback(err, new DAGNode(data, object.Links))
43+
DAGNode.create(data, object.Links, callback)
4544
}))
4645
}
4746
})

0 commit comments

Comments
 (0)