Skip to content

fix: only traverse relevant part of HAMT #448

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

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
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
18 changes: 13 additions & 5 deletions packages/unixfs/src/commands/utils/resolve.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { logger } from '@libp2p/logger'
import { exporter } from 'ipfs-unixfs-exporter'
import findShardCid from 'ipfs-unixfs-exporter/dist/src/utils/find-cid-in-shard.js'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't right. We could copy-paste or export the code if needed, but wonder if we could use more of the resolver code from ipfs-unixfs-exporter as-is

Copy link
Member

@achingbrain achingbrain Feb 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW you can't do deep imports of files like this any more, it's prevented by the exports map in the module's package.json which defines what's importable from a module and what isn't.

import { DoesNotExistError, InvalidParametersError } from '../../errors.js'
import { addLink } from './add-link.js'
import { cidToDirectory } from './cid-to-directory.js'
import { cidToPBLink } from './cid-to-pblink.js'
import type { AbortOptions } from '@libp2p/interface'
import type { Blockstore } from 'interface-blockstore'
import type { CID } from 'multiformats/cid'
import type { PBNode } from '@ipld/dag-pb/dist/src/interface.js'

const log = logger('helia:unixfs:components:utils:resolve')

Expand All @@ -32,6 +34,12 @@ export interface ResolveResult {
segments?: Segment[]
}

const findLinkCid = (node: PBNode, name: string): CID | undefined => {
const link = node.Links.find(link => link.Name === name)

return link?.Hash
}

export async function resolve (cid: CID, path: string | undefined, blockstore: Blockstore, options: AbortOptions): Promise<ResolveResult> {
if (path == null || path === '') {
return { cid }
Expand Down Expand Up @@ -61,11 +69,11 @@ export async function resolve (cid: CID, path: string | undefined, blockstore: B
} else if (result.type === 'directory') {
let dirCid: CID | undefined

for await (const entry of result.content()) {
if (entry.name === part) {
dirCid = entry.cid
break
}
if (result.unixfs?.type === 'hamt-sharded-directory') {
// special case - unixfs v1 hamt shards
dirCid = await findShardCid(result.node, part, blockstore)
} else {
dirCid = findLinkCid(result.node, part)
Copy link
Author

@aschmahmann aschmahmann Feb 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is straight-up copy-pasted from https://github.com/ipfs/js-ipfs-unixfs/blob/4749d9a7c1eddd86b8fc42c3fa47f88c7b1b75ae/packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/index.ts#L59 which makes me wonder why we can't just use walkPath from ipfs-unixfs-exporter directly and pass a path

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using ipfs-unixfs-exporter instead of copy-pasting the code is a better idea.

}

if (dirCid == null) {
Expand Down