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

Commit ccc4ad2

Browse files
committed
fix: do not emit empty buffers for non-empty files
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.
1 parent dfc9f20 commit ccc4ad2

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

src/exporter/file.js

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ function streamBytes (dag, node, fileSize, offset, length) {
7171
const file = UnixFS.unmarshal(node.data)
7272

7373
if (!file.data) {
74+
if (file.blockSizes.length) {
75+
return
76+
}
77+
7478
return Buffer.alloc(0)
7579
}
7680

test/builder.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ module.exports = (repo) => {
6262
}, done)
6363
})
6464

65-
it('allows multihash hash algorithm to be specified for big file', (done) => {
65+
it('allows multihash hash algorithm to be specified for big file', function (done) {
66+
this.timeout(30000)
67+
6668
eachSeries(testMultihashes, (hashAlg, cb) => {
6769
const options = { hashAlg, strategy: 'flat' }
6870
const content = String(Math.random() + Date.now())

test/exporter.js

+41
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,47 @@ module.exports = (repo) => {
763763
}
764764
], done)
765765
})
766+
767+
it('exports file with data on leaf nodes without emitting empty buffers', function (done) {
768+
this.timeout(30 * 1000)
769+
770+
pull(
771+
pull.values([{
772+
path: '200Bytes.txt',
773+
content: pull.values([bigFile])
774+
}]),
775+
importer(ipld, {
776+
rawLeaves: true
777+
}),
778+
pull.collect(collected)
779+
)
780+
781+
function collected (err, files) {
782+
expect(err).to.not.exist()
783+
expect(files.length).to.equal(1)
784+
785+
pull(
786+
exporter(files[0].multihash, ipld),
787+
pull.collect((err, files) => {
788+
expect(err).to.not.exist()
789+
expect(files.length).to.equal(1)
790+
791+
pull(
792+
files[0].content,
793+
pull.collect((error, buffers) => {
794+
expect(error).to.not.exist()
795+
796+
buffers.forEach(buffer => {
797+
expect(buffer.length).to.not.equal(0)
798+
})
799+
800+
done()
801+
})
802+
)
803+
})
804+
)
805+
}
806+
})
766807
})
767808
}
768809

0 commit comments

Comments
 (0)