What is msgpack5?
The msgpack5 npm package is a JavaScript implementation of the MessagePack serialization format. MessagePack is an efficient binary serialization format that enables data exchange between different languages and environments. The msgpack5 package allows you to encode and decode data in the MessagePack format, making it useful for applications that require efficient data serialization and deserialization.
What are msgpack5's main functionalities?
Encoding Data
This feature allows you to encode JavaScript objects into the MessagePack binary format. The code sample demonstrates encoding a simple object with a key-value pair.
const msgpack = require('msgpack5')();
const encoded = msgpack.encode({ foo: 'bar' });
console.log(encoded);
Decoding Data
This feature allows you to decode MessagePack binary data back into JavaScript objects. The code sample shows encoding an object and then decoding it back to its original form.
const msgpack = require('msgpack5')();
const encoded = msgpack.encode({ foo: 'bar' });
const decoded = msgpack.decode(encoded);
console.log(decoded);
Custom Types
This feature allows you to register custom types for encoding and decoding. The code sample demonstrates how to register the Date type and encode/decode it using MessagePack.
const msgpack = require('msgpack5')();
msgpack.register(0x42, Date, (date) => +date, (timestamp) => new Date(timestamp));
const encoded = msgpack.encode(new Date());
const decoded = msgpack.decode(encoded);
console.log(decoded);
Other packages similar to msgpack5
msgpack-lite
msgpack-lite is another JavaScript implementation of the MessagePack format. It is designed to be lightweight and fast, with a focus on performance. Compared to msgpack5, msgpack-lite may offer better performance but might lack some of the additional features and flexibility provided by msgpack5.
notepack.io
notepack.io is a high-performance MessagePack implementation for JavaScript. It is optimized for speed and efficiency, making it suitable for applications that require fast serialization and deserialization. Compared to msgpack5, notepack.io is more performance-oriented but may not have as many customization options.
msgpack
msgpack is the official JavaScript implementation of the MessagePack format. It is maintained by the MessagePack organization and aims to provide a standard and reliable implementation. Compared to msgpack5, the official msgpack package may offer better compatibility with the MessagePack specification but might not have the same level of community support and additional features.
msgpack5
A msgpack v5 implementation for node.js, with extension point support.
Install
npm install msgpack5 --save
Usage
var msgpack = require('msgpack5')()
, a = new MyType(2, 'a')
, encode = msgpack.encode
, decode = msgpack.decode
msgpack.register(0x42, MyType, mytipeEncode, mytipeDecode)
console.log(encode({ 'hello': 'world' }).toString('hex'))
console.log(decode(encode({ 'hello': 'world' })))
console.log(encode(a).toString('hex'))
console.log(decode(encode(a)) instanceof MyType)
console.log(decode(encode(a)))
function MyType(size, value) {
this.value = value
this.size = size
}
function mytipeEncode(obj) {
var buf = new Buffer(obj.size)
buf.fill(obj.value)
return buf
}
function mytipeDecode(data) {
var result = new MyType(data.length, data.toString('utf8', 0, 1))
, i
for (i = 0; i < data.length; i++) {
if (data.readUInt8(0) != data.readUInt8(i)) {
throw new Error('should all be the same')
}
}
return result
}
API
API
msgpack()
Creates a new instance on which you can register new types for being
encoded.
encode(object)
Encodes object
in msgpack, returns a bl.
decode(buf)
Decodes buf from in msgpack. buf
can be a Buffer
or a bl instance.
registerEncoder(check(obj), encode(obj))
Register a new custom object type for being automatically encoded.
The arguments are:
check
, a function that will be called to check if the passed
object should be encoded with the encode
functionencode
, a function that will be called to encode an object in binary
form; this function must return a Buffer
which include the same type
for registerDecoder.
registerDecoder(type, decode(buf))
Register a new custom objet type for being automatically decoded.
The arguments are:
type
, is a greater than zero integer identificating the type once serializeddecode
, a function that will be called to decode the object from
the passed Buffer
register(type, constructor, encode(obj), decode(buf))
Register a new custom objet type for being automatically encoded and
decoded. The arguments are:
type
, is a greater than zero integer identificating the type once serializedconstructor
, the function that will be used to match the objects
with instanceof
encode
, a function that will be called to encode an object in binary
form; this function must return a Buffer
that can be
deserialized by the decode
functiondecode
, a function that will be called to decode the object from
the passed Buffer
This is just a commodity that calls
registerEncoder
and
registerDecoder
internally.
encoder(opts)
Builds a stream in object mode that encodes msgpack. By default it writes
an 4 byte length header containing the message length as a UInt32BE. This
header can be disabled by passing { header: false }
as an option.
decoder(opts)
Builds a stream in object mode that decodes msgpack. By default it expects
msgpack to have a 4 byte length header containing the packaged length as
a UInt32BE. This header can be disabled by passing { header: false }
as an option.
Disclaimer
This library is built fully on JS and on bl to
simplify the code. Every improvement that keeps the same API is welcome.
Acknowledgements
This project was kindly sponsored by nearForm.
This library was originally built as the data format for
JSChan.
License
MIT