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

Commit

Permalink
feat: implementation of the new addFormat/removeFormat() functions
Browse files Browse the repository at this point in the history
BREAKING CHANGE: They replace the `support.add()` and `support.rm()` functions.

The API docs for it:

`.addFormat(ipldFormatImplementation)`:

> Add support for an IPLD Format

 - `ipldFormatImplementation` (`IPLD Format`, required): the implementation of an IPLD Format.

`.removeFormat(codec)`:

> Remove support for an IPLD Format

 - `codec` (`multicodec`, required): the codec of the IPLD Format to remove.
  • Loading branch information
vmx committed Mar 21, 2019
1 parent 08c1e0e commit 12b436b
Showing 1 changed file with 37 additions and 30 deletions.
67 changes: 37 additions & 30 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,51 @@ class IPLDResolver {
// Object with current list of active resolvers
this.resolvers = {}

// API entry point
this.support = {}

// Adds support for an IPLD format
this.support.add = (codec, resolver, util) => {
if (this.resolvers[codec]) {
const codecName = multicodec.print[codec]
throw new Error(`Resolver already exists for codec "${codecName}"`)
}

this.resolvers[codec] = {
resolver: resolver,
util: util
}
}

if (options.loadFormat === undefined) {
this.support.load = async (codec) => {
this.loadFormat = async (codec) => {
const codecName = multicodec.print[codec]
throw new Error(`No resolver found for codec "${codecName}"`)
}
} else {
this.support.load = options.loadFormat
}

this.support.rm = (codec) => {
if (this.resolvers[codec]) {
delete this.resolvers[codec]
}
this.loadFormat = options.loadFormat
}

// Enable all supplied formats
for (const format of options.formats) {
const { resolver, util } = format
// IPLD Formats are using strings instead of constants for the multicodec
const codecBuffer = multicodec.getCodeVarint(resolver.multicodec)
const codec = multicodec.getCode(codecBuffer)
this.support.add(codec, resolver, util)
this.addFormat(format)
}
}

/**
* Add support for an IPLD Format.
*
* @param {Object} format - The implementation of an IPLD Format.
* @returns {void}
*/
addFormat (format) {
// IPLD Formats are using strings instead of constants for the multicodec
const codecBuffer = multicodec.getCodeVarint(format.resolver.multicodec)
const codec = multicodec.getCode(codecBuffer)
if (this.resolvers[codec]) {
const codecName = multicodec.print[codec]
throw new Error(`Resolver already exists for codec "${codecName}"`)
}

this.resolvers[codec] = {
resolver: format.resolver,
util: format.util
}
}

/**
* Remove support for an IPLD Format.
*
* @param {number} codec - The codec of the IPLD Format to remove.
* @returns {void}
*/
removeFormat (codec) {
if (this.resolvers[codec]) {
delete this.resolvers[codec]
}
}

Expand Down Expand Up @@ -443,7 +450,7 @@ class IPLDResolver {
}

// If not supported, attempt to dynamically load this format
const format = await this.support.load(codec)
const format = await this.loadFormat(codec)
this.resolvers[codec] = format
return format
}
Expand Down

0 comments on commit 12b436b

Please sign in to comment.