Skip to content

Commit b0e61ed

Browse files
committed
chore: dedupe load
1 parent 4e59372 commit b0e61ed

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/directory.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,23 @@ const directoryContent: UnixfsV1Resolver = (cid, node, unixfs, path, resolve, de
2323
const linkName = link.Name ?? ''
2424
const linkPath = `${path}/${linkName}`
2525

26+
const load = async (options = {}) => {
27+
const result = await resolve(link.Hash, linkName, linkPath, [], depth + 1, blockstore, options)
28+
return result.entry
29+
}
30+
2631
if (isBasicExporterOptions(options)) {
2732
const basic: UnixfsV1BasicContent = {
2833
cid: link.Hash,
2934
name: linkName,
3035
path: linkPath,
31-
resolve: async (options = {}) => {
32-
const result = await resolve(link.Hash, linkName, linkPath, [], depth + 1, blockstore, options)
33-
return result.entry
34-
}
36+
resolve: load
3537
}
3638

3739
return basic
3840
}
3941

40-
const result = await resolve(link.Hash, linkName, linkPath, [], depth + 1, blockstore, options)
41-
return result.entry
42+
return load(options)
4243
}
4344
}),
4445
source => parallel(source, {

packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/hamt-sharded-directory.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { pipe } from 'it-pipe'
66
import { CustomProgressEvent } from 'progress-events'
77
import { NotUnixFSError } from '../../../errors.js'
88
import { isBasicExporterOptions } from '../../../utils/is-basic-exporter-options.ts'
9-
import type { ExporterOptions, Resolve, UnixfsV1DirectoryContent, UnixfsV1Resolver, ReadableStorage, ExportWalk, BasicExporterOptions, UnixfsV1BasicContent } from '../../../index.js'
9+
import type { ExporterOptions, Resolve, UnixfsV1DirectoryContent, UnixfsV1Resolver, ReadableStorage, ExportWalk, BasicExporterOptions } from '../../../index.js'
1010
import type { PBNode } from '@ipld/dag-pb'
1111

1212
const hamtShardedDirectoryContent: UnixfsV1Resolver = (cid, node, unixfs, path, resolve, depth, blockstore) => {
@@ -49,24 +49,27 @@ async function * listDirectory (node: PBNode, path: string, resolve: Resolve, de
4949

5050
if (name != null && name !== '') {
5151
const linkPath = `${path}/${name}`
52+
const load = async (options = {}) => {
53+
const result = await resolve(link.Hash, name, linkPath, [], depth + 1, blockstore, options)
54+
return result.entry
55+
}
5256

5357
if (isBasicExporterOptions(options)) {
54-
const basic: UnixfsV1BasicContent = {
55-
cid: link.Hash,
56-
name,
57-
path: linkPath,
58-
resolve: async (options = {}) => {
59-
const result = await resolve(link.Hash, name, linkPath, [], depth + 1, blockstore, options)
60-
return result.entry
61-
}
58+
return {
59+
entries: [{
60+
cid: link.Hash,
61+
name,
62+
path: linkPath,
63+
resolve: load
64+
}]
6265
}
63-
64-
return { entries: [basic] }
6566
}
6667

67-
const result = await resolve(link.Hash, name, linkPath, [], depth + 1, blockstore, options)
68-
69-
return { entries: result.entry == null ? [] : [result.entry] }
68+
return {
69+
entries: [
70+
await load()
71+
].filter(Boolean)
72+
}
7073
} else {
7174
// descend into subshard
7275
const block = await blockstore.get(link.Hash, options)
@@ -76,7 +79,9 @@ async function * listDirectory (node: PBNode, path: string, resolve: Resolve, de
7679
cid: link.Hash
7780
}))
7881

79-
return { entries: listDirectory(node, path, resolve, depth, blockstore, options) }
82+
return {
83+
entries: listDirectory(node, path, resolve, depth, blockstore, options)
84+
}
8085
}
8186
}
8287
}),

0 commit comments

Comments
 (0)