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

Commit 6ef929d

Browse files
committed
fix: import with CID version 1
Using CID version 1 causes the raw leaves option to become `true` unless specified. The builder reducer was not expecting a raw leaf to be returned by ipld.get so was creating file nodes with empty data. fixes ipfs/js-ipfs#1518 License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent c5cb38b commit 6ef929d

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

src/builder/reduce.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ module.exports = function reduce (file, ipld, options) {
2727
return waterfall([
2828
(cb) => ipld.get(leaf.cid, cb),
2929
(result, cb) => {
30-
const data = result.value.data
30+
// If result.value is a buffer, this is a raw leaf otherwise it's a dag-pb node
31+
const data = Buffer.isBuffer(result.value) ? result.value : result.value.data
3132
const fileNode = new UnixFS('file', data)
3233

3334
DAGNode.create(fileNode.marshal(), [], options.hashAlg, (error, node) => {

test/importer.js

+39-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
'use strict'
33

44
const importer = require('./../src').importer
5+
const exporter = require('./../src').exporter
56

67
const extend = require('deep-extend')
78
const chai = require('chai')
@@ -542,10 +543,23 @@ module.exports = (repo) => {
542543
path = path[path.length - 1] === '/' ? path : path + '/'
543544
return {
544545
path: path + name + '.txt',
545-
content: Buffer.alloc(262144 + 5).fill(1)
546+
content: Buffer.alloc(size).fill(1)
546547
}
547548
}
548549

550+
const inputFiles = [
551+
createInputFile('/foo', 10),
552+
createInputFile('/foo', 60),
553+
createInputFile('/foo/bar', 78),
554+
createInputFile('/foo/baz', 200),
555+
// Bigger than maxChunkSize
556+
createInputFile('/foo', 262144 + 45),
557+
createInputFile('/foo/bar', 262144 + 134),
558+
createInputFile('/foo/bar', 262144 + 79),
559+
createInputFile('/foo/bar', 262144 + 876),
560+
createInputFile('/foo/bar', 262144 + 21)
561+
]
562+
549563
const options = {
550564
cidVersion: 1,
551565
// Ensures we use DirSharded for the data below
@@ -560,23 +574,34 @@ module.exports = (repo) => {
560574

561575
each(files, (file, cb) => {
562576
const cid = new CID(file.multihash).toV1()
563-
ipld.get(cid, cb)
577+
const inputFile = inputFiles.find(f => f.path === file.path)
578+
579+
// Just check the intermediate directory can be retrieved
580+
if (!inputFile) {
581+
return ipld.get(cid, cb)
582+
}
583+
584+
// Check the imported content is correct
585+
pull(
586+
exporter(cid, ipld),
587+
pull.collect((err, nodes) => {
588+
expect(err).to.not.exist()
589+
pull(
590+
nodes[0].content,
591+
pull.collect((err, chunks) => {
592+
expect(err).to.not.exist()
593+
expect(Buffer.concat(chunks)).to.deep.equal(inputFile.content)
594+
cb()
595+
})
596+
)
597+
})
598+
)
564599
}, done)
565600
}
566601

567602
pull(
568-
pull.values([
569-
createInputFile('/foo', 10),
570-
createInputFile('/foo', 60),
571-
createInputFile('/foo/bar', 78),
572-
createInputFile('/foo/baz', 200),
573-
// Bigger than maxChunkSize
574-
createInputFile('/foo', 262144 + 45),
575-
createInputFile('/foo/bar', 262144 + 134),
576-
createInputFile('/foo/bar', 262144 + 79),
577-
createInputFile('/foo/bar', 262144 + 876),
578-
createInputFile('/foo/bar', 262144 + 21)
579-
]),
603+
// Pass a copy of inputFiles, since the importer mutates them
604+
pull.values(inputFiles.map(f => Object.assign({}, f))),
580605
importer(ipld, options),
581606
pull.collect(onCollected)
582607
)

0 commit comments

Comments
 (0)