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

fix: return cids from builder #220

Merged
merged 1 commit into from
Jul 24, 2018
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
15 changes: 11 additions & 4 deletions src/builder/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ const defaultOptions = {

module.exports = function builder (createChunker, ipld, createReducer, _options) {
const options = extend({}, defaultOptions, _options)
options.cidVersion = options.cidVersion || 0
options.cidVersion = options.cidVersion || options.cidVersion
options.hashAlg = options.hashAlg || defaultOptions.hashAlg

if (options.hashAlg !== 'sha2-256') {
options.cidVersion = 1
}

return function (source) {
return function (items, cb) {
Expand Down Expand Up @@ -71,8 +76,10 @@ module.exports = function builder (createChunker, ipld, createReducer, _options)
return cb(null, node)
}

node.cid = new CID(options.cidVersion, 'dag-pb', node.multihash)

ipld.put(node, {
cid: new CID(options.cidVersion, 'dag-pb', node.multihash)
cid: node.cid
}, (err) => cb(err, node))
}
], (err, node) => {
Expand All @@ -81,7 +88,7 @@ module.exports = function builder (createChunker, ipld, createReducer, _options)
}
callback(null, {
path: item.path,
multihash: node.multihash,
multihash: node.cid.buffer,
size: node.size
})
})
Expand Down Expand Up @@ -155,7 +162,7 @@ module.exports = function builder (createChunker, ipld, createReducer, _options)
pull.map((leaf) => {
return {
path: file.path,
multihash: leaf.multihash,
multihash: leaf.cid.buffer,
size: leaf.size,
leafSize: leaf.leafSize,
name: '',
Expand Down
45 changes: 42 additions & 3 deletions test/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ module.exports = (repo) => {
const node = nodes[0]
expect(node).to.exist()

const cid = new CID(node.multihash)

// Verify multihash has been encoded using hashAlg
expect(mh.decode(node.multihash).name).to.equal(hashAlg)
expect(mh.decode(cid.multihash).name).to.equal(hashAlg)

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

try {
expect(node).to.exist()
expect(mh.decode(node.multihash).name).to.equal(hashAlg)
const cid = new CID(node.multihash)
expect(mh.decode(cid.multihash).name).to.equal(hashAlg)
} catch (err) {
return cb(err)
}
Expand All @@ -92,5 +95,41 @@ module.exports = (repo) => {
)
}, done)
})

it('allows multihash hash algorithm to be specified for a directory', (done) => {
eachSeries(testMultihashes, (hashAlg, cb) => {
const options = { hashAlg, strategy: 'flat' }
const inputFile = {
path: `${String(Math.random() + Date.now())}-dir`,
content: null
}

const onCollected = (err, nodes) => {
if (err) return cb(err)

const node = nodes[0]

expect(node).to.exist()

const cid = new CID(node.multihash)

expect(mh.decode(cid.multihash).name).to.equal(hashAlg)

// Fetch using hashAlg encoded multihash
ipld.get(cid, (err, res) => {
if (err) return cb(err)
const meta = UnixFS.unmarshal(res.value.data)
expect(meta.type).to.equal('directory')
cb()
})
}

pull(
pull.values([Object.assign({}, inputFile)]),
createBuilder(FixedSizeChunker, ipld, options),
pull.collect(onCollected)
)
}, done)
})
})
}