From 12b436bb15366640cc901dc4b853e8ed574778b7 Mon Sep 17 00:00:00 2001 From: Volker Mische Date: Thu, 13 Dec 2018 00:17:01 +0100 Subject: [PATCH] feat: implementation of the new `addFormat/removeFormat()` functions 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. --- src/index.js | 67 +++++++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/src/index.js b/src/index.js index 67f5218..0b9f39f 100644 --- a/src/index.js +++ b/src/index.js @@ -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] } } @@ -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 }