|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | 3 | const DAGNode = require('ipfs-merkle-dag').DAGNode |
| 4 | +const bl = require('bl') |
| 5 | +const async = require('async') |
4 | 6 |
|
5 | 7 | 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 | + |
11 | 36 | } |
0 commit comments