Skip to content

A minimalistic MessagePack encoder and decoder for JavaScript.

License

Notifications You must be signed in to change notification settings

JoshuaWise/tiny-msgpack

Repository files navigation

tiny-msgpack Build Status

A minimalistic MessagePack encoder and decoder for JavaScript.

  • Tiny Size (3.95 kB minified and gzipped)
  • Fast performance
  • Extension support
  • No other bells or whistles

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

Installation

npm install --save tiny-msgpack

Usage

constmsgpack=require('tiny-msgpack');constuint8array=msgpack.encode({foo: 'bar',baz: 123});constobject=msgpack.decode(uint8array);

Decoding multiple concatenated messages

for(constobjectofmsgpack.decodeEach(uint8array)){console.log(object);}

BigInts

You can encode 64-bit integers by using BigInt. Likewise, decoding a 64-bit integer will result in a BigInt. If BigInt is not supported on your platform, decoding a 64-bit integer will throw an exception.

Extensions

constmsgpack=require('tiny-msgpack');functionencodeDate(date){returnmsgpack.encode(Number(date));}functiondecodeDate(uint8array){returnnewDate(msgpack.decode(uint8array));}constcodec=newmsgpack.Codec();codec.register(0xff,Date,encodeDate,decodeDate);constuint8array=msgpack.encode({timestamp: newDate()},codec);constobject=msgpack.decode(uint8array,codec);console.log(object.timestampinstanceofDate);// => true

Browser Support

In the browser, tiny-msgpack requires the Encoding API, which is only supported by modern browsers. 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

About

A minimalistic MessagePack encoder and decoder for JavaScript.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript100.0%