msgpack-lite
August 26, 2026 ยท View on GitHub
Fast Pure JavaScript MessagePack Encoder and Decoder
Online demo: http://kawanet.github.io/msgpack-lite/
Encoding and Decoding MessagePack
const msgpack = require("msgpack-lite");
// encode from JS Object to MessagePack (Buffer)
const buffer = msgpack.encode({"foo": "bar"});
// decode from MessagePack (Buffer) to JS Object
const data = msgpack.decode(buffer); // => {"foo": "bar"}
// if encode/decode receives an invalid argument an error is thrown
Writing to MessagePack Stream
const fs = require("fs");
const msgpack = require("msgpack-lite");
const writeStream = fs.createWriteStream("test.msp");
const encodeStream = msgpack.createEncodeStream();
encodeStream.pipe(writeStream);
// send multiple objects to stream
encodeStream.write({foo: "bar"});
encodeStream.write({baz: "qux"});
// call this once you're done writing to the stream.
encodeStream.end();
Reading from MessagePack Stream
const fs = require("fs");
const msgpack = require("msgpack-lite");
const readStream = fs.createReadStream("test.msp");
const decodeStream = msgpack.createDecodeStream();
// show multiple objects decoded from stream
readStream.pipe(decodeStream).on("data", console.warn);
Decoding MessagePack Bytes Array
const msgpack = require("msgpack-lite");
// decode() accepts Buffer instance per default
msgpack.decode(Buffer.from([0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72]));
// decode() also accepts Array instance
msgpack.decode([0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72]);
// decode() accepts raw Uint8Array instance as well
msgpack.decode(new Uint8Array([0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72]));
Command Line Interface
A CLI tool bin/msgpack converts data stream from JSON to MessagePack and vice versa.
$ echo '{"foo": "bar"}' | ./bin/msgpack -Jm | od -tx1
0000000 81 a3 66 6f 6f a3 62 61 72
$ echo '{"foo": "bar"}' | ./bin/msgpack -Jm | ./bin/msgpack -Mj
{"foo":"bar"}
Installation
$ npm install --save msgpack-lite
Tests
Run tests on node.js:
$ make test
Browser Build
Browser version msgpack.min.js is also available. 50KB minified, 14KB gziped.
<!--[if lte IE 9]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.1.10/es5-shim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.min.js"></script>
<![endif]-->
<script src="https://cdn.jsdelivr.net/npm/msgpack-lite/dist/msgpack.min.js"></script>
<script>
// encode from JS Object to MessagePack (Uint8Array)
var buffer = msgpack.encode({foo: "bar"});
// decode from MessagePack (Uint8Array) to JS Object
var array = new Uint8Array([0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72]);
var data = msgpack.decode(array);
</script>
MessagePack Mapping Table
The following table shows how JavaScript objects (value) will be mapped to MessagePack formats and vice versa.
| Source Value | MessagePack Format | Value Decoded |
|---|---|---|
| null, undefined | nil format family | null |
| Boolean (true, false) | bool format family | Boolean (true, false) |
| Number (32bit int) | int format family | Number (int or double) |
| Number (64bit double) | float format family | Number (double) |
| String | str format family | String |
| Buffer | bin format family | Buffer |
| Array | array format family | Array |
| Map | map format family | Map (if usemap=true) |
| Object (plain object) | map format family | Object (or Map if usemap=true) |
| Object (see below) | ext format family | Object (see below) |
Note that both null and undefined are mapped to nil 0xC1 type.
This means undefined value will be upgraded to null in other words.
Extension Types
The MessagePack specification allows 128 application-specific extension types. The library uses the following types to make round-trip conversion possible for JavaScript native objects.
| Type | Object | Type | Object |
|---|---|---|---|
| 0x00 | 0x10 | ||
| 0x01 | EvalError | 0x11 | Int8Array |
| 0x02 | RangeError | 0x12 | Uint8Array |
| 0x03 | ReferenceError | 0x13 | Int16Array |
| 0x04 | SyntaxError | 0x14 | Uint16Array |
| 0x05 | TypeError | 0x15 | Int32Array |
| 0x06 | URIError | 0x16 | Uint32Array |
| 0x07 | 0x17 | Float32Array | |
| 0x08 | 0x18 | Float64Array | |
| 0x09 | 0x19 | Uint8ClampedArray | |
| 0x0A | RegExp | 0x1A | ArrayBuffer |
| 0x0B | Boolean | 0x1B | Buffer |
| 0x0C | String | 0x1C | |
| 0x0D | Date | 0x1D | DataView |
| 0x0E | Error | 0x1E | |
| 0x0F | Number | 0x1F |
Other extension types are mapped to built-in ExtBuffer object.
Custom Extension Types (Codecs)
Register a custom extension type number to serialize/deserialize your own class instances.
const msgpack = require("msgpack-lite");
const codec = msgpack.createCodec();
codec.addExtPacker(0x3F, MyVector, myVectorPacker);
codec.addExtUnpacker(0x3F, myVectorUnpacker);
const data = new MyVector(1, 2);
const encoded = msgpack.encode(data, {codec: codec});
const decoded = msgpack.decode(encoded, {codec: codec});
function MyVector(x, y) {
this.x = x;
this.y = y;
}
function myVectorPacker(vector) {
const array = [vector.x, vector.y];
return msgpack.encode(array); // return Buffer serialized
}
function myVectorUnpacker(buffer) {
const array = msgpack.decode(buffer);
return new MyVector(array[0], array[1]); // return Object deserialized
}
The first argument of addExtPacker and addExtUnpacker should be an integer within the range of 0 and 127 (0x0 and 0x7F). myVectorPacker is a function that accepts an instance of MyVector, and should return a buffer representing that instance. myVectorUnpacker is the opposite: it accepts a buffer and should return an instance of MyVector.
If you pass an array of functions to addExtPacker or addExtUnpacker, the value to be encoded/decoded will pass through each one in order. This allows you to do things like this:
codec.addExtPacker(0x00, Date, [Number, msgpack.encode]);
You can also pass the codec option to msgpack.Decoder(options), msgpack.Encoder(options), msgpack.createEncodeStream(options), and msgpack.createDecodeStream(options).
If you wish to modify the default built-in codec, you can access it at msgpack.codec.preset.
Custom Codec Options
msgpack.createCodec() function accepts some options.
It does NOT have the preset extension types defined when no options given.
const codec = msgpack.createCodec();
preset: It has the preset extension types described above.
const codec = msgpack.createCodec({preset: true});
safe: It runs a validation of the value before writing it into buffer. This is the default behavior for some old browsers which do not support ArrayBuffer object.
const codec = msgpack.createCodec({safe: true});
useraw: It uses raw formats instead of bin and str.
const codec = msgpack.createCodec({useraw: true});
int64: It decodes msgpack's int64/uint64 formats with int64-buffer object.
const codec = msgpack.createCodec({int64: true});
binarraybuffer: It ties msgpack's bin format with ArrayBuffer object, instead of Buffer object.
const codec = msgpack.createCodec({binarraybuffer: true, preset: true});
uint8array: It returns Uint8Array object when encoding, instead of Buffer object.
const codec = msgpack.createCodec({uint8array: true});
usemap: Uses the global JavaScript Map type, if available, to unpack
MessagePack map elements.
const codec = msgpack.createCodec({usemap: true});
Compatibility Mode
The compatibility mode respects for msgpack's old spec. Set true to useraw.
// default mode handles both str and bin formats individually
msgpack.encode("Aa"); // => <Buffer a2 41 61> (str format)
msgpack.encode(Buffer.from([0x41, 0x61])); // => <Buffer c4 02 41 61> (bin format)
msgpack.decode(Buffer.from([0xa2, 0x41, 0x61])); // => 'Aa' (String)
msgpack.decode(Buffer.from([0xc4, 0x02, 0x41, 0x61])); // => <Buffer 41 61> (Buffer)
// compatibility mode handles only raw format both for String and Buffer
const options = {codec: msgpack.createCodec({useraw: true})};
msgpack.encode("Aa", options); // => <Buffer a2 41 61> (raw format)
msgpack.encode(Buffer.from([0x41, 0x61]), options); // => <Buffer a2 41 61> (raw format)
msgpack.decode(Buffer.from([0xa2, 0x41, 0x61]), options); // => <Buffer 41 61> (Buffer)
msgpack.decode(Buffer.from([0xa2, 0x41, 0x61]), options).toString(); // => 'Aa' (String)
Repository
See Also
License
The MIT License (MIT)
Copyright (c) 2015-2026 Yusuke Kawasaki
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.