msgpack / msgpack-javascript

@msgpack/msgpack - MessagePack for JavaScript / msgpack.org[JavaScript/TypeScript/ECMA-262]
https://msgpack.org/
ISC License
1.29k stars 161 forks source link

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

Closed meloalright closed 2 years ago

meloalright commented 2 years ago
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
cc00010 commented 2 years ago

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
meloalright commented 2 years ago

Easy question.. Thanks