/tiny-msgpack

A minimalistic MessagePack encoder and decoder for JavaScript.

Primary LanguageJavaScriptMIT LicenseMIT

tiny-msgpack Build Status

A minimalistic MessagePack encoder and decoder for JavaScript.

  • Tiny Size (2.63 kB minified and gzipped)
  • Fast performance (Slightly faster than msgpack-lite)
  • Extension support
  • No other bells or whistles

By default, msgpack can encode numbers, strings, booleans, nulls, arrays, objects, and binary data (Uint8Array). However, additional types can be registered by using extensions.

Installation

npm install --save tiny-msgpack

Usage

var msgpack = require('tiny-msgpack');

var uint8array = msgpack.encode({foo: 'bar', baz: 123});
var object = msgpack.decode(uint8array);

Extensions

var msgpack = require('tiny-msgpack');
var codec = new msgpack.Codec;

function encodeDate(date) {
  return msgpack.encode(+date);
}
function decodeDate(uint8array) {
  return new Date(msgpack.decode(uint8array));
}
codec.register(0x42, Date, encodeDate, decodeDate);

var uint8array = msgpack.encode({timestamp: new Date}, codec);
var object = msgpack.decode(uint8array, codec);
console.log(object.timestamp instanceof Date); // => true

Browser Support

In the browser, tiny-msgpack requires the Encoding API, which is currently only implemented by Chrome and Firefox. However, if you polyfill it, this package is supported by the following browsers:

  • Chrome 9+
  • Firefox 15+
  • Safari 5.1+
  • Opera 12.1+
  • Internet Explorer 10+

Zero copy

In the MessagePack format, binary data is encoded as... binary data! To maximize performance, tiny-msgpack does not copy binary data when encoding or decoding it. So after decoding, the contents of a returned Uint8Array can be affected by modifying the input Uint8Array (the same can happen with encoding).

License

MIT