Skip to content

fix: export basic file from dir or shard #440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const contentExporters: Record<string, UnixfsV1Resolver> = {

// @ts-expect-error types are wrong
const unixFsResolver: Resolver = async (cid, name, path, toResolve, resolve, depth, blockstore, options) => {
if (isBasicExporterOptions(options)) {
if (isBasicExporterOptions(options) && toResolve.length === 0) {
const basic: UnixFSBasicEntry = {
cid,
name,
Expand Down
33 changes: 33 additions & 0 deletions packages/ipfs-unixfs-exporter/test/exporter-sharded.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,37 @@ describe('exporter sharded', function () {
await expect(exporter(dirFile.cid, block)).to.eventually.be.rejected()
}
})

it('exports basic file from sharded directory', async () => {
const files: Record<string, { content: Uint8Array, cid?: CID }> = {}

// needs to result in a block that is larger than SHARD_SPLIT_THRESHOLD bytes
for (let i = 0; i < 100; i++) {
files[`file-${Math.random()}.txt`] = {
content: uint8ArrayConcat(await all(randomBytes(100)))
}
}

const imported = await all(importer(Object.keys(files).map(path => ({
path,
content: asAsyncIterable(files[path].content)
})), block, {
wrapWithDirectory: true,
shardSplitThresholdBytes: SHARD_SPLIT_THRESHOLD,
rawLeaves: false
}))

const file = imported[0]
const dir = imported[imported.length - 1]

const basicfile = await exporter(`/ipfs/${dir.cid}/${file.path}`, block, {
extended: false
})

expect(basicfile).to.have.property('name', file.path)
expect(basicfile).to.have.property('path', `${dir.cid}/${file.path}`)
expect(basicfile).to.have.deep.property('cid', file.cid)
expect(basicfile).to.not.have.property('unixfs')
expect(basicfile).to.not.have.property('content')
})
})
29 changes: 29 additions & 0 deletions packages/ipfs-unixfs-exporter/test/exporter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1708,4 +1708,33 @@ describe('exporter', () => {
expect(basicDir).to.not.have.property('unixfs')
expect(basicDir).to.not.have.property('content')
})

it('exports basic file from directory', async () => {
const files: Record<string, { content: Uint8Array, cid?: CID }> = {
'file.txt': {
content: uint8ArrayConcat(await all(randomBytes(100)))
}
}

const imported = await all(importer(Object.keys(files).map(path => ({
path,
content: asAsyncIterable(files[path].content)
})), block, {
wrapWithDirectory: true,
rawLeaves: false
}))

const file = imported[0]
const dir = imported[imported.length - 1]

const basicfile = await exporter(`/ipfs/${dir.cid}/${file.path}`, block, {
extended: false
})

expect(basicfile).to.have.property('name', file.path)
expect(basicfile).to.have.property('path', `${dir.cid}/${file.path}`)
expect(basicfile).to.have.deep.property('cid', file.cid)
expect(basicfile).to.not.have.property('unixfs')
expect(basicfile).to.not.have.property('content')
})
})
Loading