msgpack/msgpack-javascript

How can we convert the msgpack-encoded str back to the origin info

meloalright opened this issue · 2 comments

const encoded = encode({ name: 'abcde' });
const str = new util.TextDecoder().decode(encoded);

console.log(str) // ��name�abcde

// Then the str has been send to another service using rpc
// How can we convert the msgpack-encoded str back to the origin info

Hey @meloalright ! You should first decode it using the function msgpack.decode ( not TextDecoder's decode ):

const { encode, decode } = require('@msgpack/msgpack');
const encoded = encode({ name: 'abcde' });
const decoded = decode(encoded);

console.log(decoded.name); // `decoded` is an object
// the output will be 'abcde'

Here's the definition of decode:

decode(buffer: ArrayLike<number> | BufferSource, options?: DecodeOptions): unknown

Easy question..
Thanks