From ccc4ad2e45c66fa704e9263823279e18667f841f Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 7 Aug 2018 16:32:38 +0100 Subject: [PATCH] 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. --- src/exporter/file.js | 4 ++++ test/builder.js | 4 +++- test/exporter.js | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/exporter/file.js b/src/exporter/file.js index 5491f545..71418c88 100644 --- a/src/exporter/file.js +++ b/src/exporter/file.js @@ -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) } diff --git a/test/builder.js b/test/builder.js index f5d7f976..d05994af 100644 --- a/test/builder.js +++ b/test/builder.js @@ -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()) diff --git a/test/exporter.js b/test/exporter.js index e587f188..2d02cc3b 100644 --- a/test/exporter.js +++ b/test/exporter.js @@ -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() + }) + ) + }) + ) + } + }) }) }