-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwalk-path.ts
63 lines (52 loc) · 2.28 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { DoesNotExistError } from '@helia/unixfs/errors'
import { type Logger } from '@libp2p/interface'
import { type Blockstore } from 'interface-blockstore'
import { walkPath as exporterWalk, type ExporterOptions, type ReadableStorage, type ObjectNode, type UnixFSEntry } from 'ipfs-unixfs-exporter'
import { type FetchHandlerFunctionArg } from '../types.js'
import { badGatewayResponse, notFoundResponse } from './responses.js'
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 DoesNotExistError('No terminal element found')
}
return {
ipfsRoots,
terminalElement
}
}
export function isObjectNode (node: UnixFSEntry): node is ObjectNode {
return node.type === 'object'
}
/**
* Attempts to walk the path in the blockstore, returning ipfsRoots needed to resolve the path, and the terminal element.
* If the signal is aborted, the function will throw an AbortError
* If a terminal element is not found, a notFoundResponse is returned
* If another unknown error occurs, a badGatewayResponse is returned
*/
export async function handlePathWalking ({ cid, path, resource, options, blockstore, log }: Omit<FetchHandlerFunctionArg, 'session'> & { blockstore: Blockstore, log: Logger }): Promise<PathWalkerResponse | Response> {
try {
return await walkPath(blockstore, `${cid.toString()}/${path}`, options)
} catch (err: any) {
options?.signal?.throwIfAborted()
if (['ERR_NO_PROP', 'ERR_NO_TERMINAL_ELEMENT', 'ERR_NOT_FOUND'].includes(err.code)) {
return notFoundResponse(resource)
}
log.error('error walking path %s', path, err)
return badGatewayResponse(resource, 'Error walking path')
}
}