Skip to content

Commit 88bf273

Browse files
mikealhacdias
authored andcommitted
fix: name table not hex encoded
1 parent e86a89d commit 88bf273

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

src/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
'use strict'
1313

1414
const varint = require('varint')
15+
const intTable = require('./int-table')
1516
const codecNameToCodeVarint = require('./varint-table')
16-
const codeToCodecName = require('./name-table')
1717
const util = require('./util')
1818

1919
exports = module.exports
@@ -57,10 +57,10 @@ exports.rmPrefix = (data) => {
5757
* @returns {string}
5858
*/
5959
exports.getCodec = (prefixedData) => {
60-
const code = util.varintBufferDecode(prefixedData)
61-
const codecName = codeToCodecName[code.toString('hex')]
60+
const code = varint.decode(prefixedData)
61+
const codecName = intTable.get(code)
6262
if (codecName === undefined) {
63-
throw new Error('Code `0x' + code.toString('hex') + '` not found')
63+
throw new Error(`Code ${code} not found`)
6464
}
6565
return codecName
6666
}
@@ -71,7 +71,7 @@ exports.getCodec = (prefixedData) => {
7171
* @returns {string}
7272
*/
7373
exports.getName = (codec) => {
74-
return codeToCodecName[codec.toString(16)]
74+
return intTable.get(codec)
7575
}
7676

7777
/**

src/int-table.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict'
2+
const baseTable = require('./base-table.json')
3+
4+
// map for hexString -> codecName
5+
const nameTable = new Map()
6+
7+
for (const encodingName in baseTable) {
8+
const code = baseTable[encodingName]
9+
nameTable.set(code, encodingName)
10+
}
11+
12+
module.exports = Object.freeze(nameTable)

test/multicodec.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ describe('multicodec', () => {
9999
expect(() => {
100100
multicodec.getCodec(prefixedBuf)
101101
}).to.throw(
102-
'Code `0xffee` not found'
102+
'Code 65518 not found'
103103
)
104104
})
105105
})

0 commit comments

Comments
 (0)