msgpack-javascript
JavaScript ES6 implementation of the MessagePack protocol.
Design Goals
- Isomorphic
- Single codebase to serve both Browser and Node environments
- Future Ready
- Source and tests written in ES6
Installation
npm i msgpack-javascript
Basic Usage
Import
;
Pack
let packer = ; packer;packer;packer;packer;packer;packer;packer;packer;packer;packer;packer;packer;packer;
Unpack
let unpacker = packer actual = {}; unpacker = packer;actualnil = unpacker;actualboolean = unpacker;actualfixnum = unpacker;actualbyte = unpacker;actualshort = unpacker;actualint = unpacker;actuallong = unpacker;actualfloat = unpacker;actualdouble = unpacker;actualstring = unpacker;actualarray = unpacker;actualbinary = unpacker;actualmap = unpacker;
Reflect
Packer.packValue allows packing of any value that may be identified as one of the standard value types (excluding Extended).
let packer = ; packer; // packs nilpacker; // packs boolpacker; // packs pos fixintpacker; // packs uint8packer; // packs uint16packer; // packs int32packer; // packs uint64packer; // packs float32packer; // packs float64packer; // packs fixed stringpacker; // packs fixed arraypacker; // packs bin8packer; // packs fixed mappacker; // throws error
Unpacker.unpackValue unpacks the next value by evaluating the prefix of the next byte to be read.
Type Mapping
Source Format => JavaScript Type
Source Format | JavaScript Type |
---|---|
pos fixint, neg fixint, int 8/16/32 and uint 8/16/32 | Number |
int 64 and uint 64 | Long |
nil | undefined |
false and true | Boolean |
float 32/64 | Number |
fixstr and str 8/16/32 | String |
bin 8/16/32 | Array (of bytes) |
fixarray and array 16/32 | Array |
fixmap map 16/32 | Map |
fixext and ext 8/16/32 | Extended |
JavaScript Type => Output Format
JavaScript Type | Output Format |
---|---|
Number, Long | pos fixint, neg fixint, int 8/16/32/64, uint 8/16/32/64, float 32/64 |
undefined, null | nil |
Boolean | false and true |
String | fixstr and str 8/16/32 |
Array (of bytes) | bin 8/16/32 |
Array | fixarray and array 16/32 |
Map | fixmap map 16/32 |
64-bit Integers
Support for 64-bit integers is realised via npm package pg-long.