The decoder has quite a bit more state to it than the encoder and this is usually true of data formats. When dealing with raw bytes, trying to parse the information out of it is usually more difficult than writing the data out as that raw format.
Let's take a look at the helper methods that we will use to decode the data types we support:
import { CONSTANTS } from './helper.js';export const decodeString = function(buf) { if(buf[0] !== CONSTANTS.string) { return false; } const len = buf.readUInt32BE(1); return buf.slice(5, 5 + len).toString('utf8');}export const decodeNumber = function(buf) { return buf.readInt32BE(1);}
The decodeString method showcases how we could handle errors in the case of incorrectly formatted ...