Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

feat: expose getFormat function #292

Merged
merged 1 commit into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ the new strategy in [Issue #260](https://github.com/ipld/js-ipld/issues/260)
- [`.removeMany(cids, options)`](#removemanycids-options)
- [`.tree(cid, [path], [options])`](#treecid-path-options)
- [`.addFormat(ipldFormatImplementation)`](#addformatipldformatimplementation)
- [`.getFormat(codec)`](#getformatcodec)
- [`.removeFormat(codec)`](#removeformatcodec)
- [Properties](#properties)
- [`defaultOptions`](#defaultoptions)
Expand Down Expand Up @@ -284,6 +285,13 @@ Returns an async iterator of all the paths (as Strings) you could resolve into.

Returns the IPLD instance. This way you can chain `addFormat()` calls.

### `.getFormat(codec)`

> Return the implementation for an IPLD Format

- `codec` (`multicodec`, required): the codec of the IPLD Format to return the implementation from.

If the implementation is not present in the current list of resolvers, the `loadFormat` function passed as an option to the constructor of this module will be invoked and it's output added to the list of available resolvers.

### `.removeFormat(codec)`

Expand Down
17 changes: 7 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class IPLDResolver {
const generator = async function * () {
// End iteration if there isn't a CID to follow anymore
while (cid !== null) {
const format = await this._getFormat(cid.codec)
const format = await this.getFormat(cid.codec)

// get block
// use local resolver
Expand Down Expand Up @@ -133,7 +133,7 @@ class IPLDResolver {
*/
async get (cid, options) {
const block = await this.bs.get(cid, options)
const format = await this._getFormat(block.cid.codec)
const format = await this.getFormat(block.cid.codec)
const node = format.util.deserialize(block.data)

return node
Expand Down Expand Up @@ -182,7 +182,7 @@ class IPLDResolver {
throw new Error('`format` parameter must be number (multicodec)')
}

const formatImpl = await this._getFormat(format)
const formatImpl = await this.getFormat(format)
const defaultOptions = {
hashAlg: formatImpl.defaultHashAlg,
cidVersion: 1,
Expand Down Expand Up @@ -239,7 +239,7 @@ class IPLDResolver {
// when we hit the first iteration. This way the constructor can be
// a synchronous function.
if (options === undefined) {
formatImpl = await this._getFormat(format)
formatImpl = await this.getFormat(format)
const defaultOptions = {
hashAlg: formatImpl.defaultHashAlg,
cidVersion: 1,
Expand Down Expand Up @@ -318,7 +318,7 @@ class IPLDResolver {
// If a path is a link then follow it and return its CID
const maybeRecurse = async (block, treePath) => {
// A treepath we might want to follow recursively
const format = await this._getFormat(block.cid.codec)
const format = await this.getFormat(block.cid.codec)
const result = format.resolver.resolve(block.data, treePath)
// Something to follow recusively, hence push it into the queue
if (CID.isCID(result.value)) {
Expand Down Expand Up @@ -347,7 +347,7 @@ class IPLDResolver {
// There aren't any paths left, get them from the given CID
if (treePaths.length === 0 && queue.length > 0) {
({ cid, basePath } = queue.shift())
const format = await this._getFormat(cid.codec)
const format = await this.getFormat(cid.codec)
block = await this.bs.get(cid, options)

const paths = format.resolver.tree(block.data)
Expand Down Expand Up @@ -381,10 +381,7 @@ class IPLDResolver {
return extendIterator(generator())
}

/* */
/* internals */
/* */
async _getFormat (codec) {
async getFormat (codec) {
// TODO vmx 2019-01-24: Once all CIDs support accessing the codec code
// instead of the name, remove this part
if (typeof codec === 'string') {
Expand Down