-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathdag-cbor.js
75 lines (66 loc) · 1.57 KB
/
dag-cbor.js
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
64
65
66
67
68
69
70
71
72
73
74
75
'use strict'
const { CID } = require('multiformats/cid')
const errCode = require('err-code')
const dagCbor = require('@ipld/dag-cbor')
/**
* @typedef {import('../types').Resolver} Resolver
*/
/**
* @type {Resolver}
*/
const resolve = async (cid, name, path, toResolve, resolve, depth, blockstore, options) => {
const block = await blockstore.get(cid)
const object = dagCbor.decode(block)
let subObject = object
let subPath = path
while (toResolve.length) {
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) {
return {
entry: {
type: 'object',
name,
path,
cid,
node: block,
depth,
size: block.length,
content: async function * () {
yield object
}
},
next: {
cid: subObjectCid,
name: prop,
path: subPath,
toResolve
}
}
}
subObject = subObject[prop]
} else {
// cannot resolve further
throw errCode(new Error(`No property named ${prop} found in cbor node ${cid}`), 'ERR_NO_PROP')
}
}
return {
entry: {
type: 'object',
name,
path,
cid,
node: block,
depth,
size: block.length,
content: async function * () {
yield object
}
}
}
}
module.exports = resolve