Msgpack implemented in pure ECMAScript.
These files are written using ES6 modules. I consume them using rollup.
For example:
import { encode, decode } from "./msgpack";
// Encode a value to Uint8Array holding msgpack
let encoded = encode([1,2,3]);
// Decode back to a value
let decoded = decode(encoded);
Msgpack supports up to 128 custom, application defined, types. To register a new type you need to provide a constructor function and encode/decode functions.
For example, here is how you would serialize RegExp
instances.
import { register, encode, decode } from "./msgpack";
register(1, RegExp, encodeRegExp, decodeRegExp);
function encodeRegExp(reg) {
// this needs to return a Uint8Array that will be stored. The format can be
// anything, but here we're reusing msgpack for convienience.
return encode([reg.source,reg.flags]);
}
function decodeRegExp(buf) {
let opts = decode(buf);
return new RegExp(opts[0], opts[1]);
}
Run tests with rollup and node.
rollup test.js | node