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

Commit c9cb891

Browse files
authored
Merge pull request #220 from ipfs/return-cids-from-builder
fix: return cids from builder
2 parents abafb48 + 0d3d3d8 commit c9cb891

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

src/builder/builder.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ const defaultOptions = {
2626

2727
module.exports = function builder (createChunker, ipld, createReducer, _options) {
2828
const options = extend({}, defaultOptions, _options)
29-
options.cidVersion = options.cidVersion || 0
29+
options.cidVersion = options.cidVersion || options.cidVersion
30+
options.hashAlg = options.hashAlg || defaultOptions.hashAlg
31+
32+
if (options.hashAlg !== 'sha2-256') {
33+
options.cidVersion = 1
34+
}
3035

3136
return function (source) {
3237
return function (items, cb) {
@@ -71,8 +76,10 @@ module.exports = function builder (createChunker, ipld, createReducer, _options)
7176
return cb(null, node)
7277
}
7378

79+
node.cid = new CID(options.cidVersion, 'dag-pb', node.multihash)
80+
7481
ipld.put(node, {
75-
cid: new CID(options.cidVersion, 'dag-pb', node.multihash)
82+
cid: node.cid
7683
}, (err) => cb(err, node))
7784
}
7885
], (err, node) => {
@@ -81,7 +88,7 @@ module.exports = function builder (createChunker, ipld, createReducer, _options)
8188
}
8289
callback(null, {
8390
path: item.path,
84-
multihash: node.multihash,
91+
multihash: node.cid.buffer,
8592
size: node.size
8693
})
8794
})
@@ -155,7 +162,7 @@ module.exports = function builder (createChunker, ipld, createReducer, _options)
155162
pull.map((leaf) => {
156163
return {
157164
path: file.path,
158-
multihash: leaf.multihash,
165+
multihash: leaf.cid.buffer,
159166
size: leaf.size,
160167
leafSize: leaf.leafSize,
161168
name: '',

test/builder.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ module.exports = (repo) => {
4040
const node = nodes[0]
4141
expect(node).to.exist()
4242

43+
const cid = new CID(node.multihash)
44+
4345
// Verify multihash has been encoded using hashAlg
44-
expect(mh.decode(node.multihash).name).to.equal(hashAlg)
46+
expect(mh.decode(cid.multihash).name).to.equal(hashAlg)
4547

4648
// Fetch using hashAlg encoded multihash
47-
ipld.get(new CID(node.multihash), (err, res) => {
49+
ipld.get(cid, (err, res) => {
4850
if (err) return cb(err)
4951
const content = UnixFS.unmarshal(res.value.data).data
5052
expect(content.equals(inputFile.content)).to.be.true()
@@ -77,7 +79,8 @@ module.exports = (repo) => {
7779

7880
try {
7981
expect(node).to.exist()
80-
expect(mh.decode(node.multihash).name).to.equal(hashAlg)
82+
const cid = new CID(node.multihash)
83+
expect(mh.decode(cid.multihash).name).to.equal(hashAlg)
8184
} catch (err) {
8285
return cb(err)
8386
}
@@ -92,5 +95,41 @@ module.exports = (repo) => {
9295
)
9396
}, done)
9497
})
98+
99+
it('allows multihash hash algorithm to be specified for a directory', (done) => {
100+
eachSeries(testMultihashes, (hashAlg, cb) => {
101+
const options = { hashAlg, strategy: 'flat' }
102+
const inputFile = {
103+
path: `${String(Math.random() + Date.now())}-dir`,
104+
content: null
105+
}
106+
107+
const onCollected = (err, nodes) => {
108+
if (err) return cb(err)
109+
110+
const node = nodes[0]
111+
112+
expect(node).to.exist()
113+
114+
const cid = new CID(node.multihash)
115+
116+
expect(mh.decode(cid.multihash).name).to.equal(hashAlg)
117+
118+
// Fetch using hashAlg encoded multihash
119+
ipld.get(cid, (err, res) => {
120+
if (err) return cb(err)
121+
const meta = UnixFS.unmarshal(res.value.data)
122+
expect(meta.type).to.equal('directory')
123+
cb()
124+
})
125+
}
126+
127+
pull(
128+
pull.values([Object.assign({}, inputFile)]),
129+
createBuilder(FixedSizeChunker, ipld, options),
130+
pull.collect(onCollected)
131+
)
132+
}, done)
133+
})
95134
})
96135
}

0 commit comments

Comments
 (0)