multiformats / js-cid

CID implementation in JavaScript
MIT License
97 stars 39 forks source link

Remove direct access to codec lookup table #50

Closed vmx closed 6 years ago

vmx commented 6 years ago

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)