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

Commit 12b436b

Browse files
committed
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.
1 parent 08c1e0e commit 12b436b

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

src/index.js

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,44 +29,51 @@ class IPLDResolver {
2929
// Object with current list of active resolvers
3030
this.resolvers = {}
3131

32-
// API entry point
33-
this.support = {}
34-
35-
// Adds support for an IPLD format
36-
this.support.add = (codec, resolver, util) => {
37-
if (this.resolvers[codec]) {
38-
const codecName = multicodec.print[codec]
39-
throw new Error(`Resolver already exists for codec "${codecName}"`)
40-
}
41-
42-
this.resolvers[codec] = {
43-
resolver: resolver,
44-
util: util
45-
}
46-
}
47-
4832
if (options.loadFormat === undefined) {
49-
this.support.load = async (codec) => {
33+
this.loadFormat = async (codec) => {
5034
const codecName = multicodec.print[codec]
5135
throw new Error(`No resolver found for codec "${codecName}"`)
5236
}
5337
} else {
54-
this.support.load = options.loadFormat
55-
}
56-
57-
this.support.rm = (codec) => {
58-
if (this.resolvers[codec]) {
59-
delete this.resolvers[codec]
60-
}
38+
this.loadFormat = options.loadFormat
6139
}
6240

6341
// Enable all supplied formats
6442
for (const format of options.formats) {
65-
const { resolver, util } = format
66-
// IPLD Formats are using strings instead of constants for the multicodec
67-
const codecBuffer = multicodec.getCodeVarint(resolver.multicodec)
68-
const codec = multicodec.getCode(codecBuffer)
69-
this.support.add(codec, resolver, util)
43+
this.addFormat(format)
44+
}
45+
}
46+
47+
/**
48+
* Add support for an IPLD Format.
49+
*
50+
* @param {Object} format - The implementation of an IPLD Format.
51+
* @returns {void}
52+
*/
53+
addFormat (format) {
54+
// IPLD Formats are using strings instead of constants for the multicodec
55+
const codecBuffer = multicodec.getCodeVarint(format.resolver.multicodec)
56+
const codec = multicodec.getCode(codecBuffer)
57+
if (this.resolvers[codec]) {
58+
const codecName = multicodec.print[codec]
59+
throw new Error(`Resolver already exists for codec "${codecName}"`)
60+
}
61+
62+
this.resolvers[codec] = {
63+
resolver: format.resolver,
64+
util: format.util
65+
}
66+
}
67+
68+
/**
69+
* Remove support for an IPLD Format.
70+
*
71+
* @param {number} codec - The codec of the IPLD Format to remove.
72+
* @returns {void}
73+
*/
74+
removeFormat (codec) {
75+
if (this.resolvers[codec]) {
76+
delete this.resolvers[codec]
7077
}
7178
}
7279

@@ -443,7 +450,7 @@ class IPLDResolver {
443450
}
444451

445452
// If not supported, attempt to dynamically load this format
446-
const format = await this.support.load(codec)
453+
const format = await this.loadFormat(codec)
447454
this.resolvers[codec] = format
448455
return format
449456
}

0 commit comments

Comments
 (0)