Skip to content

Commit

Permalink
feat: export tables and dedup code
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Henrique Dias <[email protected]>
  • Loading branch information
hacdias committed Dec 17, 2020
1 parent c3af948 commit d26166b
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 67 deletions.
14 changes: 0 additions & 14 deletions src/constants.js

This file was deleted.

File renamed without changes.
31 changes: 17 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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')
}
Expand All @@ -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`)
}
Expand All @@ -75,7 +74,7 @@ function getCodec (prefixedData) {
* @returns {CodecName|undefined}
*/
function getName (codec) {
return intTable.get(codec)
return codeToName[codec]
}

/**
Expand All @@ -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
}

/**
Expand All @@ -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')
}
Expand All @@ -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,
Expand All @@ -138,5 +134,12 @@ module.exports = {
getCode,
getCodeVarint,
getVarint,
...constants

// Make the constants top-level constants
...constantToCode,

// Export the maps
nameToVarint,
nameToCode,
codeToName
}
20 changes: 0 additions & 20 deletions src/int-table.js

This file was deleted.

31 changes: 31 additions & 0 deletions src/maps.js
Original file line number Diff line number Diff line change
@@ -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)
}
15 changes: 0 additions & 15 deletions src/varint-table.js

This file was deleted.

6 changes: 3 additions & 3 deletions test/multicodec.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)', () => {
Expand Down Expand Up @@ -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])
}
})

Expand Down
2 changes: 1 addition & 1 deletion tools/update-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit d26166b

Please sign in to comment.