Skip to content

Commit e670eac

Browse files
committed
fix: switch to reusing walkPath from ipfs-unixfs-exporter
1 parent 3c40080 commit e670eac

File tree

1 file changed

+28
-41
lines changed

1 file changed

+28
-41
lines changed

packages/unixfs/src/commands/utils/resolve.ts

+28-41
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { logger } from '@libp2p/logger'
2-
import { exporter } from 'ipfs-unixfs-exporter'
3-
import findShardCid from 'ipfs-unixfs-exporter/dist/src/utils/find-cid-in-shard.js'
4-
import { DoesNotExistError, InvalidParametersError } from '../../errors.js'
2+
import { walkPath } from 'ipfs-unixfs-exporter'
3+
import { InvalidParametersError } from '../../errors.js'
54
import { addLink } from './add-link.js'
65
import { cidToDirectory } from './cid-to-directory.js'
76
import { cidToPBLink } from './cid-to-pblink.js'
8-
import type { PBNode } from '@ipld/dag-pb/interface'
97
import type { AbortOptions } from '@libp2p/interface'
108
import type { Blockstore } from 'interface-blockstore'
119
import type { CID } from 'multiformats/cid'
@@ -34,65 +32,54 @@ export interface ResolveResult {
3432
segments?: Segment[]
3533
}
3634

37-
const findLinkCid = (node: PBNode, name: string): CID | undefined => {
38-
const link = node.Links.find(link => link.Name === name)
39-
40-
return link?.Hash
41-
}
42-
4335
export async function resolve (cid: CID, path: string | undefined, blockstore: Blockstore, options: AbortOptions): Promise<ResolveResult> {
4436
if (path == null || path === '') {
4537
return { cid }
4638
}
4739

4840
log('resolve "%s" under %c', path, cid)
4941

50-
const parts = path.split('/').filter(Boolean)
5142
const segments: Segment[] = [{
5243
name: '',
5344
cid,
5445
size: 0n
5546
}]
5647

57-
for (let i = 0; i < parts.length; i++) {
58-
const part = parts[i]
59-
const result = await exporter(cid, blockstore, options)
60-
61-
log('resolving "%s"', part, result)
48+
const parts = path.split('/').filter(Boolean)
6249

63-
if (result.type === 'file') {
64-
if (i < parts.length - 1) {
65-
throw new InvalidParametersError('Path was invalid')
66-
}
50+
const combinedPath = `/ipfs/${cid.toString()}/${path}`
6751

68-
cid = result.cid
69-
} else if (result.type === 'directory') {
70-
let dirCid: CID | undefined
52+
const pathElems = walkPath(combinedPath, blockstore, options)
53+
let i = 0
54+
let lastCid = cid
7155

72-
if (result.unixfs?.type === 'hamt-sharded-directory') {
73-
// special case - unixfs v1 hamt shards
74-
dirCid = await findShardCid(result.node, part, blockstore)
75-
} else {
76-
dirCid = findLinkCid(result.node, part)
77-
}
56+
// TODO: Do we need to catch errors during enumeration and wrap them?
57+
// For example do we need a DoesNotExistError if the path doesn't exist?
58+
// Should the other errors be handled along with any caught errors rather than separately?
59+
for await (const e of pathElems) {
60+
const name = parts[i]
61+
i++
62+
log('resolving "%s"', name, e)
7863

79-
if (dirCid == null) {
80-
throw new DoesNotExistError('Could not find path in directory')
64+
if (e.type === 'file') {
65+
if (i < parts.length - 1) {
66+
throw new InvalidParametersError('Path was invalid')
8167
}
68+
}
69+
lastCid = e.cid
8270

83-
cid = dirCid
71+
segments.push({
72+
name: name,
73+
cid: e.cid,
74+
size: e.size
75+
})
76+
}
8477

85-
segments.push({
86-
name: part,
87-
cid,
88-
size: result.size
89-
})
90-
} else {
91-
throw new InvalidParametersError('Could not resolve path')
92-
}
78+
if (i < parts.length - 1) {
79+
throw new InvalidParametersError('Path was invalid')
9380
}
9481

95-
log('resolved %s to %c', path, cid)
82+
log('resolved %s to %c', path, lastCid)
9683

9784
return {
9885
cid,

0 commit comments

Comments
 (0)