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

Commit cd570b7

Browse files
committed
chore: handle offsets when data is on internal and leaf nodes
1 parent bbb403c commit cd570b7

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"bs58": "^4.0.1",
6060
"cids": "~0.5.3",
6161
"deep-extend": "~0.6.0",
62-
"ipfs-unixfs": "~0.1.14",
62+
"ipfs-unixfs": "~0.1.15",
6363
"ipld": "~0.17.2",
6464
"ipld-dag-pb": "~0.14.4",
6565
"left-pad": "^1.3.0",

src/exporter/file.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ function streamBytes (dag, node, fileSize, offset, length) {
139139
function extractDataFromBlock (block, streamPosition, begin, end) {
140140
const blockLength = block.length
141141

142+
if (begin >= streamPosition + blockLength) {
143+
// If begin is after the start of the block, return an empty block
144+
// This can happen when internal nodes contain data
145+
return Buffer.alloc(0)
146+
}
147+
142148
if (end - streamPosition < blockLength) {
143149
// If the end byte is in the current block, truncate the block to the end byte
144150
block = block.slice(0, end - streamPosition)

test/exporter.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,27 @@ module.exports = (repo) => {
713713
}
714714
], done)
715715
})
716+
717+
it('exports file with data on internal and leaf nodes with an offset that only fetches data from leaf nodes', function (done) {
718+
waterfall([
719+
(cb) => createAndPersistNode(ipld, 'raw', [0x04, 0x05, 0x06, 0x07], [], cb),
720+
(leaf, cb) => createAndPersistNode(ipld, 'file', [0x00, 0x01, 0x02, 0x03], [
721+
leaf
722+
], cb),
723+
(file, cb) => {
724+
pull(
725+
exporter(file.multihash, ipld, {
726+
offset: 4
727+
}),
728+
pull.asyncMap((file, cb) => readFile(file, cb)),
729+
pull.through(buffer => {
730+
expect(buffer).to.deep.equal(Buffer.from([0x04, 0x05, 0x06, 0x07]))
731+
}),
732+
pull.collect(cb)
733+
)
734+
}
735+
], done)
736+
})
716737
})
717738
}
718739

0 commit comments

Comments
 (0)