-
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.
The json codec is included in the multiformats module so no extra deps are needed. Similar to #397
- Loading branch information
1 parent
6fd12b9
commit 81e85c8
Showing
6 changed files
with
120 additions
and
119 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,12 @@ | ||
import * as dagCbor from '@ipld/dag-cbor' | ||
import errCode from 'err-code' | ||
import { CID } from 'multiformats/cid' | ||
import { resolveObjectPath } from '../utils/resolve-object-path.js' | ||
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 = dagCbor.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 cbor node ${cid}`), 'ERR_NO_PROP') | ||
} | ||
} | ||
|
||
return { | ||
entry: { | ||
type: 'object', | ||
name, | ||
path, | ||
cid, | ||
node: block, | ||
depth, | ||
size: BigInt(block.length), | ||
content: async function * () { | ||
yield object | ||
} | ||
} | ||
} | ||
return resolveObjectPath(object, block, cid, name, path, toResolve, depth) | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,12 @@ | ||
import * as dagJson from '@ipld/dag-json' | ||
import errCode from 'err-code' | ||
import { CID } from 'multiformats/cid' | ||
import { resolveObjectPath } from '../utils/resolve-object-path.js' | ||
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 | ||
} | ||
} | ||
} | ||
return resolveObjectPath(object, block, cid, name, path, toResolve, depth) | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as json from 'multiformats/codecs/json' | ||
import { resolveObjectPath } from '../utils/resolve-object-path.js' | ||
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 = json.decode<any>(block) | ||
|
||
return resolveObjectPath(object, block, cid, name, path, toResolve, depth) | ||
} | ||
|
||
export default resolve |
62 changes: 62 additions & 0 deletions
62
packages/ipfs-unixfs-exporter/src/utils/resolve-object-path.ts
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,62 @@ | ||
import errCode from 'err-code' | ||
import { CID } from 'multiformats/cid' | ||
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 errCode(new Error(`No property named ${prop} found in node ${cid}`), 'ERR_NO_PROP') | ||
} | ||
} | ||
|
||
return { | ||
entry: { | ||
type: 'object', | ||
name, | ||
path, | ||
cid, | ||
node: block, | ||
depth, | ||
size: BigInt(block.length), | ||
content: async function * () { | ||
yield object | ||
} | ||
} | ||
} | ||
} |
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