Skip to content

Commit 4c0b883

Browse files
committed
fix: export basic file from dir or shard
Fixes a bug whereby when exporting a deep path to a file within a directory or shard, the returned entry had the CID of the containing folder rather than the target file.
1 parent e3fbc96 commit 4c0b883

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const contentExporters: Record<string, UnixfsV1Resolver> = {
3131

3232
// @ts-expect-error types are wrong
3333
const unixFsResolver: Resolver = async (cid, name, path, toResolve, resolve, depth, blockstore, options) => {
34-
if (isBasicExporterOptions(options)) {
34+
if (isBasicExporterOptions(options) && toResolve.length === 0) {
3535
const basic: UnixFSBasicEntry = {
3636
cid,
3737
name,

packages/ipfs-unixfs-exporter/test/exporter-sharded.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,4 +414,37 @@ describe('exporter sharded', function () {
414414
await expect(exporter(dirFile.cid, block)).to.eventually.be.rejected()
415415
}
416416
})
417+
418+
it('exports basic file from sharded directory', async () => {
419+
const files: Record<string, { content: Uint8Array, cid?: CID }> = {}
420+
421+
// needs to result in a block that is larger than SHARD_SPLIT_THRESHOLD bytes
422+
for (let i = 0; i < 100; i++) {
423+
files[`file-${Math.random()}.txt`] = {
424+
content: uint8ArrayConcat(await all(randomBytes(100)))
425+
}
426+
}
427+
428+
const imported = await all(importer(Object.keys(files).map(path => ({
429+
path,
430+
content: asAsyncIterable(files[path].content)
431+
})), block, {
432+
wrapWithDirectory: true,
433+
shardSplitThresholdBytes: SHARD_SPLIT_THRESHOLD,
434+
rawLeaves: false
435+
}))
436+
437+
const file = imported[0]
438+
const dir = imported[imported.length - 1]
439+
440+
const basicfile = await exporter(`/ipfs/${dir.cid}/${file.path}`, block, {
441+
extended: false
442+
})
443+
444+
expect(basicfile).to.have.property('name', file.path)
445+
expect(basicfile).to.have.property('path', `${dir.cid}/${file.path}`)
446+
expect(basicfile).to.have.deep.property('cid', file.cid)
447+
expect(basicfile).to.not.have.property('unixfs')
448+
expect(basicfile).to.not.have.property('content')
449+
})
417450
})

packages/ipfs-unixfs-exporter/test/exporter.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,4 +1708,33 @@ describe('exporter', () => {
17081708
expect(basicDir).to.not.have.property('unixfs')
17091709
expect(basicDir).to.not.have.property('content')
17101710
})
1711+
1712+
it('exports basic file from directory', async () => {
1713+
const files: Record<string, { content: Uint8Array, cid?: CID }> = {
1714+
'file.txt': {
1715+
content: uint8ArrayConcat(await all(randomBytes(100)))
1716+
}
1717+
}
1718+
1719+
const imported = await all(importer(Object.keys(files).map(path => ({
1720+
path,
1721+
content: asAsyncIterable(files[path].content)
1722+
})), block, {
1723+
wrapWithDirectory: true,
1724+
rawLeaves: false
1725+
}))
1726+
1727+
const file = imported[0]
1728+
const dir = imported[imported.length - 1]
1729+
1730+
const basicfile = await exporter(`/ipfs/${dir.cid}/${file.path}`, block, {
1731+
extended: false
1732+
})
1733+
1734+
expect(basicfile).to.have.property('name', file.path)
1735+
expect(basicfile).to.have.property('path', `${dir.cid}/${file.path}`)
1736+
expect(basicfile).to.have.deep.property('cid', file.cid)
1737+
expect(basicfile).to.not.have.property('unixfs')
1738+
expect(basicfile).to.not.have.property('content')
1739+
})
17111740
})

0 commit comments

Comments
 (0)