|
1 | 1 | 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' |
5 | 4 | import { addLink } from './add-link.js'
|
6 | 5 | import { cidToDirectory } from './cid-to-directory.js'
|
7 | 6 | import { cidToPBLink } from './cid-to-pblink.js'
|
8 |
| -import type { PBNode } from '@ipld/dag-pb/interface' |
9 | 7 | import type { AbortOptions } from '@libp2p/interface'
|
10 | 8 | import type { Blockstore } from 'interface-blockstore'
|
11 | 9 | import type { CID } from 'multiformats/cid'
|
@@ -34,65 +32,54 @@ export interface ResolveResult {
|
34 | 32 | segments?: Segment[]
|
35 | 33 | }
|
36 | 34 |
|
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 |
| - |
43 | 35 | export async function resolve (cid: CID, path: string | undefined, blockstore: Blockstore, options: AbortOptions): Promise<ResolveResult> {
|
44 | 36 | if (path == null || path === '') {
|
45 | 37 | return { cid }
|
46 | 38 | }
|
47 | 39 |
|
48 | 40 | log('resolve "%s" under %c', path, cid)
|
49 | 41 |
|
50 |
| - const parts = path.split('/').filter(Boolean) |
51 | 42 | const segments: Segment[] = [{
|
52 | 43 | name: '',
|
53 | 44 | cid,
|
54 | 45 | size: 0n
|
55 | 46 | }]
|
56 | 47 |
|
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) |
62 | 49 |
|
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}` |
67 | 51 |
|
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 |
71 | 55 |
|
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) |
78 | 63 |
|
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') |
81 | 67 | }
|
| 68 | + } |
| 69 | + lastCid = e.cid |
82 | 70 |
|
83 |
| - cid = dirCid |
| 71 | + segments.push({ |
| 72 | + name: name, |
| 73 | + cid: e.cid, |
| 74 | + size: e.size |
| 75 | + }) |
| 76 | + } |
84 | 77 |
|
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') |
93 | 80 | }
|
94 | 81 |
|
95 |
| - log('resolved %s to %c', path, cid) |
| 82 | + log('resolved %s to %c', path, lastCid) |
96 | 83 |
|
97 | 84 | return {
|
98 | 85 | cid,
|
|
0 commit comments