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

Commit 4027108

Browse files
committed
fix: remove direct access to codec lookup table
Use `getCodeVarint()` instead of directly accessing the lookup table of the multicodec module. This leads to nicer error messages if the given codec is not found. Prior to this commit: buffer.js:183 throw new TypeError(kFromErrorMsg); ^ TypeError: First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object. at Function.Buffer.from (buffer.js:183:11) at CID.get buffer [as buffer] (/home/vmx/src/pl/js-cid/src/index.js:123:18) at CID.toBaseEncodedString (/home/vmx/src/pl/js-cid/src/index.js:184:44) at Object.<anonymous> (/home/vmx/src/pl/js-cid/keccak.js:16:19) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Function.Module.runMain (module.js:693:10) With this commit: /home/vmx/src/pl/js-multicodec/src/index.js:73 throw new Error('Codec `' + codecName + '` not found') ^ Error: Codec `mycodec` not found at Object.exports.getCodeVarint (/home/vmx/src/pl/js-multicodec/src/index.js:73:11) at CID.get buffer [as buffer] (/home/vmx/src/pl/js-cid/src/index.js:121:38) at CID.toBaseEncodedString (/home/vmx/src/pl/js-cid/src/index.js:186:44) at Object.<anonymous> (/home/vmx/src/pl/js-cid/keccak.js:16:19) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Function.Module.runMain (module.js:693:10)
1 parent 4d7bcf8 commit 4027108

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
},
3737
"dependencies": {
3838
"multibase": "~0.4.0",
39-
"multicodec": "~0.2.6",
39+
"multicodec": "~0.2.7",
4040
"multihashes": "~0.4.13"
4141
},
4242
"devDependencies": {

src/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const mh = require('multihashes')
44
const multibase = require('multibase')
55
const multicodec = require('multicodec')
66
const codecs = require('multicodec/src/base-table')
7-
const codecVarints = require('multicodec/src/varint-table')
87
const multihash = require('multihashes')
98
const CIDUtil = require('./cid-util')
109

@@ -120,7 +119,7 @@ class CID {
120119
case 1:
121120
return Buffer.concat([
122121
Buffer.from('01', 'hex'),
123-
Buffer.from(codecVarints[this.codec]),
122+
multicodec.getCodeVarint(this.codec),
124123
this.multihash
125124
])
126125
default:
@@ -137,7 +136,7 @@ class CID {
137136
get prefix () {
138137
return Buffer.concat([
139138
Buffer.from(`0${this.version}`, 'hex'),
140-
codecVarints[this.codec],
139+
multicodec.getCodeVarint(this.codec),
141140
multihash.prefix(this.multihash)
142141
])
143142
}

test/index.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ describe('CID', () => {
157157
const str = buffer.toString('hex')
158158
expect(str).to.equals('01711220ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad')
159159
})
160+
161+
it('throws error on unknown codec when base encoding it', () => {
162+
expect(() => {
163+
new CID(1, 'this-codec-doesnt-exist', hash).toBaseEncodedString()
164+
}).to.throw(
165+
'Codec `this-codec-doesnt-exist` not found'
166+
)
167+
})
160168
})
161169

162170
describe('utilities', () => {

0 commit comments

Comments
 (0)