Socket
Socket
Sign inDemoInstall

msgpack-lite

Package Overview
Dependencies
0
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    msgpack-lite

Fast Pure JavaScript MessagePack Encoder and Decoder


Version published
Maintainers
1
Install size
120 kB
Created

Package description

What is msgpack-lite?

The msgpack-lite npm package is a library for encoding and decoding data in MessagePack format, which is a binary serialization format that is compact and efficient. It is often used for communication between different programming languages, as well as for storing and transmitting data in a space-efficient manner.

What are msgpack-lite's main functionalities?

Encoding JavaScript objects to MessagePack format

This feature allows you to take a JavaScript object and encode it into a MessagePack binary buffer, which can then be sent over a network or saved to disk.

const msgpack = require('msgpack-lite');
const object = { hello: 'world' };
const encoded = msgpack.encode(object);

Decoding MessagePack buffers to JavaScript objects

This feature allows you to take a MessagePack binary buffer and decode it into a JavaScript object, which can then be used within your application.

const msgpack = require('msgpack-lite');
const buffer = Buffer.from([0x81, 0xA5, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0xA5, 0x77, 0x6F, 0x72, 0x6C, 0x64]);
const decoded = msgpack.decode(buffer);

Stream encoding and decoding

This feature provides streaming support for encoding and decoding operations, which is useful for handling large datasets or streaming data over a network.

const msgpack = require('msgpack-lite');
const fs = require('fs');
const encodeStream = msgpack.createEncodeStream();
const decodeStream = msgpack.createDecodeStream();

fs.createReadStream('input.msgpack')
  .pipe(decodeStream)
  .on('data', console.log);

fs.createWriteStream('output.msgpack')
  .pipe(encodeStream);
encodeStream.write({ hello: 'world' });
encodeStream.end();

Other packages similar to msgpack-lite

Readme

Source

msgpack-lite npm version Build Status

Fast Pure JavaScript MessagePack Encoder and Decoder

Sauce Test Status

Online demo: http://kawanet.github.io/msgpack-lite/

Significantly Fast Encoding

  • 5x faster than other pure JavaScript libraries! (Node.js v0.12.7)
  • 50% faster than C++ node-gyp based msgpack library!

Fast Decoding

  • 20% faster than other pure JavaScript libraries!
  • 5% faster than C++ node-gyp based msgpack library!

Encoding and Decoding MessagePack

var msgpack = require("msgpack-lite");

// encode from JS Object to MessagePack (Buffer)
var buffer = msgpack.encode({"foo": "bar"});

// decode from MessagePack (Buffer) to JS Object
var data = msgpack.decode(buffer); // => {"foo": "bar"}

Writing to MessagePack Stream

var fs = require("fs");
var msgpack = require("msgpack-lite");

var writeStream = fs.createWriteStream("test.msp");
var encodeStream = msgpack.createEncodeStream();
encodeStream.pipe(writeStream);

// send multiple objects to stream
encodeStream.write({foo: "bar"});
encodeStream.write({baz: "qux"});

Reading from MessagePack Stream

var fs = require("fs");
var msgpack = require("msgpack-lite");

var readStream = fs.createReadStream("test.msp");
var decodeStream = msgpack.createDecodeStream();

// show multiple objects decoded from stream
readStream.pipe(decodeStream).on("data", console.warn);

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

Run tests on browsers:

$ make test-browser-local
open the following url in a browser:
http://localhost:4000/__zuul

Browser Build

Browser version msgpack.min.js is also available. 33KB minified, 10KB 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://rawgithub.com/kawanet/msgpack-lite/master/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>

Interoperability

It is tested to have basic compatibility with other Node.js MessagePack modules below:

Benchmark

A benchmark tool lib/benchmark.js is available to compare encoding/decoding speed (operation per second) with other MessagePack modules.

$ cat /etc/system-release
Amazon Linux AMI release 2015.03

$ node lib/benchmark.js -v
msgpack-lite 0.1.10

$ nvm use v0.10
Now using node v0.10.40

$ node lib/benchmark.js 10
operation                                                 | op / ms            | op/s
--------------------------------------------------------- | ------------------ | -----
buf = Buffer(JSON.stringify(obj));                        | 223400op / 10001ms | 2233
obj = JSON.parse(buf);                                    | 240800op / 10002ms | 2407
buf = require("msgpack").pack(obj);                       | 187000op / 10000ms | 1870
obj = require("msgpack").unpack(buf);                     | 231800op / 10001ms | 2317
buf = require("msgpack-lite").encode(obj);                | 228700op / 10004ms | 2286
obj = require("msgpack-lite").decode(buf);                | 193900op / 10001ms | 1938
buf = Buffer(require("msgpack.codec").msgpack.pack(obj)); | 138100op / 10006ms | 1380
obj = require("msgpack.codec").msgpack.unpack(buf);       | 101400op / 10011ms | 1012
buf = require("msgpack-js-v5").encode(obj);               | 29800op / 10015ms  | 297
obj = require("msgpack-js-v5").decode(buf);               | 108200op / 10002ms | 1081
buf = require("msgpack-js").encode(obj);                  | 30100op / 10008ms  | 300
obj = require("msgpack-js").decode(buf);                  | 105800op / 10018ms | 1056
buf = require("msgpack5")().encode(obj);                  | 4400op / 10152ms   | 43
obj = require("msgpack5")().decode(buf);                  | 18600op / 10024ms  | 185
obj = require("msgpack-unpack").decode(buf);              | 1600op / 10208ms   | 15

This runs more faster on Node.js 0.12.

$ nvm use v0.12
Now using node v0.12.7

$ node lib/benchmark.js 10
operation                                                 | op / ms            | op/s
--------------------------------------------------------- | ------------------ | -----
buf = Buffer(JSON.stringify(obj));                        | 258000op / 10012ms | 2576
obj = JSON.parse(buf);                                    | 260300op / 10002ms | 2602
buf = require("msgpack").pack(obj);                       | 163600op / 10002ms | 1635
obj = require("msgpack").unpack(buf);                     | 196400op / 10012ms | 1961
buf = require("msgpack-lite").encode(obj);                | 248900op / 10008ms | 2487
obj = require("msgpack-lite").decode(buf);                | 199600op / 10002ms | 1995
buf = Buffer(require("msgpack.codec").msgpack.pack(obj)); | 47600op / 10001ms  | 475
obj = require("msgpack.codec").msgpack.unpack(buf);       | 165500op / 10001ms | 1654
buf = require("msgpack-js-v5").encode(obj);               | 43500op / 10007ms  | 434
obj = require("msgpack-js-v5").decode(buf);               | 152700op / 10005ms | 1526
buf = require("msgpack-js").encode(obj);                  | 44200op / 10014ms  | 441
obj = require("msgpack-js").decode(buf);                  | 145300op / 10000ms | 1453
buf = require("msgpack5")().encode(obj);                  | 4500op / 10185ms   | 44
obj = require("msgpack5")().decode(buf);                  | 18700op / 10004ms  | 186
obj = require("msgpack-unpack").decode(buf);              | 1000op / 10377ms   | 9

This is also compatible with io.js.

$ nvm use iojs
Now using io.js v3.1.0

$ node lib/benchmark.js 10
operation                                                 | op / ms            | op/s
--------------------------------------------------------- | ------------------ | -----
buf = Buffer(JSON.stringify(obj));                        | 338100op / 10001ms | 3380
obj = JSON.parse(buf);                                    | 314900op / 10001ms | 3148
buf = require("msgpack-lite").encode(obj);                | 240300op / 10006ms | 2401
obj = require("msgpack-lite").decode(buf);                | 215600op / 10001ms | 2155
buf = Buffer(require("msgpack.codec").msgpack.pack(obj)); | 242700op / 10002ms | 2426
obj = require("msgpack.codec").msgpack.unpack(buf);       | 153000op / 10011ms | 1528
buf = require("msgpack-js-v5").encode(obj);               | 45000op / 10015ms  | 449
obj = require("msgpack-js-v5").decode(buf);               | 96900op / 10009ms  | 968
buf = require("msgpack-js").encode(obj);                  | 44600op / 10021ms  | 445
obj = require("msgpack-js").decode(buf);                  | 94000op / 10002ms  | 939
buf = require("msgpack5")().encode(obj);                  | 4300op / 10231ms   | 42
obj = require("msgpack5")().decode(buf);                  | 15800op / 10008ms  | 157
obj = require("msgpack-unpack").decode(buf);              | 13000op / 10068ms  | 129

MessagePack Mapping Table

The following table shows how JavaScript objects (value) will be mapped to MessagePack formats and vice versa.

Source ValueMessagePack FormatValue Decoded
null, undefinednil format familynull
Boolean (true, false)bool format familyBoolean (true, false)
Number (32bit int)int format familyNumber (int or double)
Number (64bit double)float format familyNumber (double)
Stringstr format familyString
Bufferbin format familyBuffer
Arrayarray format familyArray
Object (plain object)map format familyObject
Object (see below)ext format familyObject (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.

TypeObjectTypeObject
0x000x10
0x01EvalError0x11Int8Array
0x02RangeError0x12Uint8Array
0x03ReferenceError0x13Int16Array
0x04SyntaxError0x14Uint16Array
0x05TypeError0x15Int32Array
0x06URIError0x16Uint32Array
0x070x17Float32Array
0x080x18Float64Array
0x090x19Uint8ClampedArray
0x0ARegExp0x1AArrayBuffer
0x0BBoolean0x1B
0x0CString0x1C
0x0DDate0x1DDataView
0x0EError0x1E
0x0FNumber0x1F

Other extension types are mapped to internal ExtBuffer object.

Repository

See Also

License

The MIT License (MIT)

Copyright (c) 2015 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.

Keywords

FAQs

Last updated on 23 Aug 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc