Skip to content

Commit f97f60a

Browse files
committed
feat: support multiple roots (#93)
It's never been clear why js-ipfs doesn't support multiple roots when go-ipfs does. This PR removes the check for multiple roots so if you import five files without a leading directory in their paths or with the `wrapWithDirectory` option, you'll get five CIDs back instead of an error. BREAKING CHANGE: Importing files that would result in multiple roots no longer throws an error
1 parent 6de9252 commit f97f60a

File tree

2 files changed

+29
-35
lines changed

2 files changed

+29
-35
lines changed

packages/ipfs-unixfs-importer/src/tree-builder.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ const DirFlat = require('./dir-flat')
44
const flatToShard = require('./flat-to-shard')
55
const Dir = require('./dir')
66
const toPathComponents = require('./utils/to-path-components')
7-
const errCode = require('err-code')
8-
const first = require('it-first')
97

108
async function addToTree (elem, tree, options) {
119
const pathElems = toPathComponents(elem.path || '')
@@ -51,6 +49,18 @@ async function addToTree (elem, tree, options) {
5149
return tree
5250
}
5351

52+
async function * flushAndYield (tree, block) {
53+
if (!(tree instanceof Dir)) {
54+
if (tree && tree.unixfs && tree.unixfs.isDirectory()) {
55+
yield tree
56+
}
57+
58+
return
59+
}
60+
61+
yield * tree.flush(tree.path, block)
62+
}
63+
5464
async function * treeBuilder (source, block, options) {
5565
let tree = new DirFlat({
5666
root: true,
@@ -72,29 +82,17 @@ async function * treeBuilder (source, block, options) {
7282
}
7383
}
7484

75-
if (!options.wrapWithDirectory) {
76-
if (tree.childCount() > 1) {
77-
throw errCode(new Error('detected more than one root'), 'ERR_MORE_THAN_ONE_ROOT')
78-
}
79-
80-
const unwrapped = await first(tree.eachChildSeries())
81-
82-
if (!unwrapped) {
83-
return
84-
}
85-
86-
tree = unwrapped.child
87-
}
85+
if (options.wrapWithDirectory) {
86+
yield * flushAndYield(tree, block)
87+
} else {
88+
for await (const unwrapped of tree.eachChildSeries()) {
89+
if (!unwrapped) {
90+
continue
91+
}
8892

89-
if (!(tree instanceof Dir)) {
90-
if (tree && tree.unixfs && tree.unixfs.isDirectory()) {
91-
yield tree
93+
yield * flushAndYield(unwrapped.child, block)
9294
}
93-
94-
return
9595
}
96-
97-
yield * tree.flush(tree.path, block)
9896
}
9997

10098
module.exports = treeBuilder

packages/ipfs-unixfs-importer/test/importer.spec.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -376,20 +376,16 @@ strategies.forEach((strategy) => {
376376
expect(files[0].cid.toBaseEncodedString()).to.eql('QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH')
377377
})
378378

379-
it('fails on more than one root', async () => {
380-
try {
381-
await all(importer([{
382-
path: 'beep/200Bytes.txt',
383-
content: smallFile
384-
}, {
385-
path: 'boop/200Bytes.txt',
386-
content: bigFile
387-
}], block, options))
379+
it('supports more than one root', async () => {
380+
const files = await all(importer([{
381+
path: '200Bytes.txt',
382+
content: smallFile
383+
}, {
384+
path: '200Bytes.txt',
385+
content: bigFile
386+
}], block, options))
388387

389-
throw new Error('No error was thrown')
390-
} catch (err) {
391-
expect(err.code).to.equal('ERR_MORE_THAN_ONE_ROOT')
392-
}
388+
expect(files).to.have.lengthOf(2)
393389
})
394390

395391
it('accepts strings as content', async () => {

0 commit comments

Comments
 (0)