-
Notifications
You must be signed in to change notification settings - Fork 5
/
walk-path.ts
39 lines (30 loc) · 1.13 KB
/
walk-path.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { CodeError } from '@libp2p/interface'
import { walkPath as exporterWalk, type ExporterOptions, type ReadableStorage, type ObjectNode, type UnixFSEntry } from 'ipfs-unixfs-exporter'
import type { CID } from 'multiformats/cid'
export interface PathWalkerOptions extends ExporterOptions {
}
export interface PathWalkerResponse {
ipfsRoots: CID[]
terminalElement: UnixFSEntry
}
export interface PathWalkerFn {
(blockstore: ReadableStorage, path: string, options?: PathWalkerOptions): Promise<PathWalkerResponse>
}
export async function walkPath (blockstore: ReadableStorage, path: string, options?: PathWalkerOptions): Promise<PathWalkerResponse> {
const ipfsRoots: CID[] = []
let terminalElement: UnixFSEntry | undefined
for await (const entry of exporterWalk(path, blockstore, options)) {
ipfsRoots.push(entry.cid)
terminalElement = entry
}
if (terminalElement == null) {
throw new CodeError('No terminal element found', 'ERR_NO_TERMINAL_ELEMENT')
}
return {
ipfsRoots,
terminalElement
}
}
export function isObjectNode (node: UnixFSEntry): node is ObjectNode {
return node.type === 'object'
}