From 6ef929d56bb079875baec6a99d1e51276d8a04d6 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Tue, 21 Aug 2018 15:39:39 +0100 Subject: [PATCH] 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 https://github.com/ipfs/js-ipfs/issues/1518 License: MIT Signed-off-by: Alan Shaw --- src/builder/reduce.js | 3 ++- test/importer.js | 53 +++++++++++++++++++++++++++++++------------ 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/builder/reduce.js b/src/builder/reduce.js index 45d612e5..2ba62bc4 100644 --- a/src/builder/reduce.js +++ b/src/builder/reduce.js @@ -27,7 +27,8 @@ module.exports = function reduce (file, ipld, options) { return waterfall([ (cb) => ipld.get(leaf.cid, cb), (result, cb) => { - const data = result.value.data + // If result.value is a buffer, this is a raw leaf otherwise it's a dag-pb node + const data = Buffer.isBuffer(result.value) ? result.value : result.value.data const fileNode = new UnixFS('file', data) DAGNode.create(fileNode.marshal(), [], options.hashAlg, (error, node) => { diff --git a/test/importer.js b/test/importer.js index 5fe4b9c4..44601645 100644 --- a/test/importer.js +++ b/test/importer.js @@ -2,6 +2,7 @@ 'use strict' const importer = require('./../src').importer +const exporter = require('./../src').exporter const extend = require('deep-extend') const chai = require('chai') @@ -542,10 +543,23 @@ module.exports = (repo) => { path = path[path.length - 1] === '/' ? path : path + '/' return { path: path + name + '.txt', - content: Buffer.alloc(262144 + 5).fill(1) + content: Buffer.alloc(size).fill(1) } } + const inputFiles = [ + createInputFile('/foo', 10), + createInputFile('/foo', 60), + createInputFile('/foo/bar', 78), + createInputFile('/foo/baz', 200), + // Bigger than maxChunkSize + createInputFile('/foo', 262144 + 45), + createInputFile('/foo/bar', 262144 + 134), + createInputFile('/foo/bar', 262144 + 79), + createInputFile('/foo/bar', 262144 + 876), + createInputFile('/foo/bar', 262144 + 21) + ] + const options = { cidVersion: 1, // Ensures we use DirSharded for the data below @@ -560,23 +574,34 @@ module.exports = (repo) => { each(files, (file, cb) => { const cid = new CID(file.multihash).toV1() - ipld.get(cid, cb) + const inputFile = inputFiles.find(f => f.path === file.path) + + // Just check the intermediate directory can be retrieved + if (!inputFile) { + return ipld.get(cid, cb) + } + + // Check the imported content is correct + pull( + exporter(cid, ipld), + pull.collect((err, nodes) => { + expect(err).to.not.exist() + pull( + nodes[0].content, + pull.collect((err, chunks) => { + expect(err).to.not.exist() + expect(Buffer.concat(chunks)).to.deep.equal(inputFile.content) + cb() + }) + ) + }) + ) }, done) } pull( - pull.values([ - createInputFile('/foo', 10), - createInputFile('/foo', 60), - createInputFile('/foo/bar', 78), - createInputFile('/foo/baz', 200), - // Bigger than maxChunkSize - createInputFile('/foo', 262144 + 45), - createInputFile('/foo/bar', 262144 + 134), - createInputFile('/foo/bar', 262144 + 79), - createInputFile('/foo/bar', 262144 + 876), - createInputFile('/foo/bar', 262144 + 21) - ]), + // Pass a copy of inputFiles, since the importer mutates them + pull.values(inputFiles.map(f => Object.assign({}, f))), importer(ipld, options), pull.collect(onCollected) )