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

Commit

Permalink
fix: do not emit empty buffers for non-empty files
Browse files Browse the repository at this point in the history
If a UnixFS file node has no data but does have blockSizes (e.g.
data is stored in child nodes) do not emit an empty buffer for the
containing node.
  • Loading branch information
achingbrain committed Aug 7, 2018
1 parent dfc9f20 commit ccc4ad2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/exporter/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ function streamBytes (dag, node, fileSize, offset, length) {
const file = UnixFS.unmarshal(node.data)

if (!file.data) {
if (file.blockSizes.length) {
return
}

return Buffer.alloc(0)
}

Expand Down
4 changes: 3 additions & 1 deletion test/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ module.exports = (repo) => {
}, done)
})

it('allows multihash hash algorithm to be specified for big file', (done) => {
it('allows multihash hash algorithm to be specified for big file', function (done) {
this.timeout(30000)

eachSeries(testMultihashes, (hashAlg, cb) => {
const options = { hashAlg, strategy: 'flat' }
const content = String(Math.random() + Date.now())
Expand Down
41 changes: 41 additions & 0 deletions test/exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,47 @@ module.exports = (repo) => {
}
], done)
})

it('exports file with data on leaf nodes without emitting empty buffers', function (done) {
this.timeout(30 * 1000)

pull(
pull.values([{
path: '200Bytes.txt',
content: pull.values([bigFile])
}]),
importer(ipld, {
rawLeaves: true
}),
pull.collect(collected)
)

function collected (err, files) {
expect(err).to.not.exist()
expect(files.length).to.equal(1)

pull(
exporter(files[0].multihash, ipld),
pull.collect((err, files) => {
expect(err).to.not.exist()
expect(files.length).to.equal(1)

pull(
files[0].content,
pull.collect((error, buffers) => {
expect(error).to.not.exist()

buffers.forEach(buffer => {
expect(buffer.length).to.not.equal(0)
})

done()
})
)
})
)
}
})
})
}

Expand Down

0 comments on commit ccc4ad2

Please sign in to comment.