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

Commit ee6cfd3

Browse files
hackergrrldaviddias
authored andcommitted
Make ipfs.files.add return wrapped path+node.
1 parent dfdb998 commit ee6cfd3

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/add-to-dagnode-transform.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
const async = require('async')
44
const getDagNode = require('./get-dagnode')
55

6-
// transform { Hash: '...' } objects into DAGNodes async
6+
// transform { Hash: '...' } objects into { path: 'string', node: DAGNode }
77
module.exports = function (err, res, send, done) {
88
if (err) return done(err)
99
async.map(res, function map (entry, next) {
10-
getDagNode(send, entry.Hash, next)
10+
getDagNode(send, entry.Hash, function (err, node) {
11+
var obj = {
12+
path: entry.Name,
13+
node: node
14+
}
15+
next(null, obj)
16+
})
1117
}, function (err, res) {
1218
done(err, res)
1319
})

src/get-dagnode.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,36 @@
11
'use strict'
22

33
const DAGNode = require('ipfs-merkle-dag').DAGNode
4+
const bl = require('bl')
5+
const async = require('async')
46

57
module.exports = function (send, hash, cb) {
6-
send('object/get', hash, null, null, function (err, result) {
7-
if (err) return cb(err)
8-
const node = new DAGNode(result.Data, result.Links)
9-
cb(err, node)
10-
})
8+
9+
// Retrieve the object and its data in parallel, then produce a DAGNode
10+
// instance using this information.
11+
async.parallel([
12+
function get (done) {
13+
send('object/get', hash, null, null, done)
14+
},
15+
16+
function data (done) {
17+
// WORKAROUND: request the object's data separately, since raw bits in JSON
18+
// are interpreted as UTF-8 and corrupt the data.
19+
// See https://github.com/ipfs/go-ipfs/issues/1582 for more details.
20+
send('object/data', hash, null, null, done)
21+
}],
22+
23+
function done (err, res) {
24+
if (err) return cb(err)
25+
26+
var object = res[0]
27+
var stream = res[1]
28+
29+
stream.pipe(bl(function (err, data) {
30+
if (err) return cb(err)
31+
32+
cb(err, new DAGNode(data, object.Links))
33+
}))
34+
})
35+
1136
}

0 commit comments

Comments
 (0)