-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathresolve-object-path.ts
62 lines (56 loc) · 1.44 KB
/
resolve-object-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
import { CID } from 'multiformats/cid'
import { NoPropError } from '../errors.js'
import type { ResolveResult } from '../index.js'
export function resolveObjectPath (object: any, block: Uint8Array, cid: CID, name: string, path: string, toResolve: string[], depth: number): ResolveResult {
let subObject = object
let subPath = path
while (toResolve.length > 0) {
const prop = toResolve[0]
if (prop in subObject) {
// remove the bit of the path we have resolved
toResolve.shift()
subPath = `${subPath}/${prop}`
const subObjectCid = CID.asCID(subObject[prop])
if (subObjectCid != null) {
return {
entry: {
type: 'object',
name,
path,
cid,
node: block,
depth,
size: BigInt(block.length),
content: async function * () {
yield object
}
},
next: {
cid: subObjectCid,
name: prop,
path: subPath,
toResolve
}
}
}
subObject = subObject[prop]
} else {
// cannot resolve further
throw new NoPropError(`No property named ${prop} found in node ${cid}`)
}
}
return {
entry: {
type: 'object',
name,
path,
cid,
node: block,
depth,
size: BigInt(block.length),
content: async function * () {
yield object
}
}
}
}