diff --git a/src/constants.js b/src/constants.js deleted file mode 100644 index fc1cf19..0000000 --- a/src/constants.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict' - -/** @typedef {import('./generated-types').ConstantCodeMap} ConstantCodeMap */ - -const { baseTable } = require('./base-table') - -const constants = /** @type {ConstantCodeMap} */({}) - -for (const [name, code] of Object.entries(baseTable)) { - const constant = name.toUpperCase().replace(/-/g, '_') - constants[constant] = code -} - -module.exports = Object.freeze(constants) diff --git a/src/base-table.js b/src/generated-table.js similarity index 100% rename from src/base-table.js rename to src/generated-table.js diff --git a/src/index.js b/src/index.js index cf20cb6..e520994 100644 --- a/src/index.js +++ b/src/index.js @@ -15,10 +15,9 @@ /** @typedef {import('./generated-types').CodecCode} CodecCode */ const varint = require('varint') -const intTable = require('./int-table') -const codecNameToCodeVarint = require('./varint-table') -const util = require('./util') const uint8ArrayConcat = require('uint8arrays/concat') +const util = require('./util') +const { nameToVarint, constantToCode, nameToCode, codeToName } = require('./maps') /** * Prefix a buffer with a multicodec-packed. @@ -33,8 +32,8 @@ function addPrefix (multicodecStrOrCode, data) { if (multicodecStrOrCode instanceof Uint8Array) { prefix = util.varintUint8ArrayEncode(multicodecStrOrCode) } else { - if (codecNameToCodeVarint[multicodecStrOrCode]) { - prefix = codecNameToCodeVarint[multicodecStrOrCode] + if (nameToVarint[multicodecStrOrCode]) { + prefix = nameToVarint[multicodecStrOrCode] } else { throw new Error('multicodec not recognized') } @@ -61,7 +60,7 @@ function rmPrefix (data) { */ function getCodec (prefixedData) { const code = varint.decode(prefixedData) - const codecName = intTable.get(code) + const codecName = codeToName[code] if (codecName === undefined) { throw new Error(`Code ${code} not found`) } @@ -75,7 +74,7 @@ function getCodec (prefixedData) { * @returns {CodecName|undefined} */ function getName (codec) { - return intTable.get(codec) + return codeToName[codec] } /** @@ -85,11 +84,11 @@ function getName (codec) { * @returns {CodecCode} */ function getNumber (name) { - const code = codecNameToCodeVarint[name] + const code = nameToCode[name] if (code === undefined) { throw new Error('Codec `' + name + '` not found') } - return varint.decode(code) + return code } /** @@ -109,7 +108,7 @@ function getCode (prefixedData) { * @returns {Uint8Array} */ function getCodeVarint (codecName) { - const code = codecNameToCodeVarint[codecName] + const code = nameToVarint[codecName] if (code === undefined) { throw new Error('Codec `' + codecName + '` not found') } @@ -126,9 +125,6 @@ function getVarint (code) { return varint.encode(code) } -// Make the constants top-level constants -const constants = require('./constants') - module.exports = { addPrefix, rmPrefix, @@ -138,5 +134,12 @@ module.exports = { getCode, getCodeVarint, getVarint, - ...constants + + // Make the constants top-level constants + ...constantToCode, + + // Export the maps + nameToVarint, + nameToCode, + codeToName } diff --git a/src/int-table.js b/src/int-table.js deleted file mode 100644 index 1ca9178..0000000 --- a/src/int-table.js +++ /dev/null @@ -1,20 +0,0 @@ -'use strict' - -/** @typedef {import('./generated-types').CodecName} CodecName */ -/** @typedef {import('./generated-types').CodecCode} CodecCode */ - -const { baseTable } = require('./base-table') - -/** - * @type {Map} - */ -const nameTable = new Map() - -for (const encodingName in baseTable) { - const code = baseTable[encodingName] - if (!nameTable.has(code)) { - nameTable.set(code, /** @type {CodecName} */(encodingName)) - } -} - -module.exports = Object.freeze(nameTable) diff --git a/src/maps.js b/src/maps.js new file mode 100644 index 0000000..d54ca88 --- /dev/null +++ b/src/maps.js @@ -0,0 +1,31 @@ +'use strict' + +/** @typedef {import('./generated-types').ConstantCodeMap} ConstantCodeMap */ +/** @typedef {import('./generated-types').NameUint8ArrayMap} NameUint8ArrayMap */ +/** @typedef {import('./generated-types').CodeNameMap} CodeNameMap */ + +const { baseTable } = require('./generated-table') +const varintEncode = require('./util').varintEncode + +const nameToVarint = /** @type {NameUint8ArrayMap} */ ({}) +const constantToCode = /** @type {ConstantCodeMap} */({}) +const codeToName = /** @type {CodeNameMap} */({}) + +for (const name in baseTable) { + const code = baseTable[name] + nameToVarint[name] = varintEncode(code) + + const constant = name.toUpperCase().replace(/-/g, '_') + constantToCode[constant] = code + + if (!codeToName[code]) { + codeToName[code] = name + } +} + +module.exports = { + nameToVarint: Object.freeze(nameToVarint), + constantToCode: Object.freeze(constantToCode), + nameToCode: Object.freeze(baseTable), + codeToName: Object.freeze(codeToName) +} diff --git a/src/varint-table.js b/src/varint-table.js deleted file mode 100644 index 01e611e..0000000 --- a/src/varint-table.js +++ /dev/null @@ -1,15 +0,0 @@ -'use strict' - -/** @typedef {import('./generated-types').NameUint8ArrayMap} NameUint8ArrayMap */ - -const { baseTable } = require('./base-table') -const varintEncode = require('./util').varintEncode - -const varintTable = /** @type {NameUint8ArrayMap} */ ({}) - -for (const encodingName in baseTable) { - const code = baseTable[encodingName] - varintTable[encodingName] = varintEncode(code) -} - -module.exports = Object.freeze(varintTable) diff --git a/test/multicodec.spec.js b/test/multicodec.spec.js index 4e2876f..4b9b1a7 100644 --- a/test/multicodec.spec.js +++ b/test/multicodec.spec.js @@ -7,7 +7,7 @@ const { expect } = require('aegir/utils/chai') const multicodec = require('../src') const uint8ArrayFromString = require('uint8arrays/from-string') -const { baseTable } = require('../src/base-table') +const { nameToCode } = require('../src/maps') describe('multicodec', () => { it('add prefix through multicodec (string)', () => { @@ -61,8 +61,8 @@ describe('multicodec', () => { }) it('returns all codec numbers from names', () => { - for (const name in baseTable) { - expect(multicodec.getNumber(/** @type {CodecName} */(name))).to.eql(baseTable[name]) + for (const name in nameToCode) { + expect(multicodec.getNumber(/** @type {CodecName} */(name))).to.eql(nameToCode[name]) } }) diff --git a/tools/update-table.js b/tools/update-table.js index 0fa9ae9..fae88ad 100755 --- a/tools/update-table.js +++ b/tools/update-table.js @@ -65,7 +65,7 @@ module.exports = { baseTable } ` fs.writeFileSync(path.join(__dirname, '../src/generated-types.ts'), typesTemplate) - fs.writeFileSync(path.join(__dirname, '../src/base-table.js'), tableTemplate) + fs.writeFileSync(path.join(__dirname, '../src/generated-table.js'), tableTemplate) } run()