Skip to content

Commit

Permalink
fix: name table not hex encoded
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeal authored and hacdias committed Dec 22, 2019
1 parent e86a89d commit 88bf273
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
'use strict'

const varint = require('varint')
const intTable = require('./int-table')
const codecNameToCodeVarint = require('./varint-table')
const codeToCodecName = require('./name-table')
const util = require('./util')

exports = module.exports
Expand Down Expand Up @@ -57,10 +57,10 @@ exports.rmPrefix = (data) => {
* @returns {string}
*/
exports.getCodec = (prefixedData) => {
const code = util.varintBufferDecode(prefixedData)
const codecName = codeToCodecName[code.toString('hex')]
const code = varint.decode(prefixedData)
const codecName = intTable.get(code)
if (codecName === undefined) {
throw new Error('Code `0x' + code.toString('hex') + '` not found')
throw new Error(`Code ${code} not found`)
}
return codecName
}
Expand All @@ -71,7 +71,7 @@ exports.getCodec = (prefixedData) => {
* @returns {string}
*/
exports.getName = (codec) => {
return codeToCodecName[codec.toString(16)]
return intTable.get(codec)
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/int-table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'
const baseTable = require('./base-table.json')

// map for hexString -> codecName
const nameTable = new Map()

for (const encodingName in baseTable) {
const code = baseTable[encodingName]
nameTable.set(code, encodingName)
}

module.exports = Object.freeze(nameTable)
2 changes: 1 addition & 1 deletion test/multicodec.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('multicodec', () => {
expect(() => {
multicodec.getCodec(prefixedBuf)
}).to.throw(
'Code `0xffee` not found'
'Code 65518 not found'
)
})
})

0 comments on commit 88bf273

Please sign in to comment.