-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basically the same as `dag-cbor`...allows the exporter to `walkPath` and export `dag-json` encoded data.
- Loading branch information
Alan Shaw
authored
Jan 16, 2024
1 parent
54de7bc
commit 1b40e3e
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import * as dagJson from '@ipld/dag-json' | ||
import errCode from 'err-code' | ||
import { CID } from 'multiformats/cid' | ||
import type { Resolver } from '../index.js' | ||
|
||
const resolve: Resolver = async (cid, name, path, toResolve, resolve, depth, blockstore, options) => { | ||
const block = await blockstore.get(cid, options) | ||
const object = dagJson.decode<any>(block) | ||
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 errCode(new Error(`No property named ${prop} found in dag-json node ${cid}`), 'ERR_NO_PROP') | ||
} | ||
} | ||
|
||
return { | ||
entry: { | ||
type: 'object', | ||
name, | ||
path, | ||
cid, | ||
node: block, | ||
depth, | ||
size: BigInt(block.length), | ||
content: async function * () { | ||
yield object | ||
} | ||
} | ||
} | ||
} | ||
|
||
export default resolve |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters