Gathering detailed insights and metrics for msgpack-lite
Gathering detailed insights and metrics for msgpack-lite
Gathering detailed insights and metrics for msgpack-lite
Gathering detailed insights and metrics for msgpack-lite
Fast Pure JavaScript MessagePack Encoder and Decoder / msgpack.org[JavaScript]
npm install msgpack-lite
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
990 Stars
327 Commits
131 Forks
23 Watching
7 Branches
7 Contributors
Updated on 18 Nov 2024
JavaScript (99.01%)
Shell (0.82%)
HTML (0.17%)
Cumulative downloads
Total Downloads
Last day
2%
563,970
Compared to previous day
Last week
4.9%
2,866,812
Compared to previous week
Last month
12.2%
11,552,755
Compared to previous month
Last year
248.4%
102,254,795
Compared to previous year
4
8
Fast Pure JavaScript MessagePack Encoder and Decoder
Online demo: http://kawanet.github.io/msgpack-lite/
1var msgpack = require("msgpack-lite"); 2 3// encode from JS Object to MessagePack (Buffer) 4var buffer = msgpack.encode({"foo": "bar"}); 5 6// decode from MessagePack (Buffer) to JS Object 7var data = msgpack.decode(buffer); // => {"foo": "bar"} 8 9// if encode/decode receives an invalid argument an error is thrown
1var fs = require("fs"); 2var msgpack = require("msgpack-lite"); 3 4var writeStream = fs.createWriteStream("test.msp"); 5var encodeStream = msgpack.createEncodeStream(); 6encodeStream.pipe(writeStream); 7 8// send multiple objects to stream 9encodeStream.write({foo: "bar"}); 10encodeStream.write({baz: "qux"}); 11 12// call this once you're done writing to the stream. 13encodeStream.end();
1var fs = require("fs"); 2var msgpack = require("msgpack-lite"); 3 4var readStream = fs.createReadStream("test.msp"); 5var decodeStream = msgpack.createDecodeStream(); 6 7// show multiple objects decoded from stream 8readStream.pipe(decodeStream).on("data", console.warn);
1var msgpack = require("msgpack-lite"); 2 3// decode() accepts Buffer instance per default 4msgpack.decode(Buffer([0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72])); 5 6// decode() also accepts Array instance 7msgpack.decode([0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72]); 8 9// decode() accepts raw Uint8Array instance as well 10msgpack.decode(new Uint8Array([0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72]));
A CLI tool bin/msgpack converts data stream from JSON to MessagePack and vice versa.
1$ echo '{"foo": "bar"}' | ./bin/msgpack -Jm | od -tx1 20000000 81 a3 66 6f 6f a3 62 61 72 3 4$ echo '{"foo": "bar"}' | ./bin/msgpack -Jm | ./bin/msgpack -Mj 5{"foo":"bar"}
1$ npm install --save msgpack-lite
Run tests on node.js:
1$ make test
Run tests on browsers:
1$ make test-browser-local 2open the following url in a browser: 3http://localhost:4000/__zuul
Browser version msgpack.min.js is also available. 50KB minified, 14KB gziped.
1<!--[if lte IE 9]> 2<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.1.10/es5-shim.min.js"></script> 3<script src="https://cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.min.js"></script> 4<![endif]--> 5<script src="https://rawgit.com/kawanet/msgpack-lite/master/dist/msgpack.min.js"></script> 6<script> 7// encode from JS Object to MessagePack (Uint8Array) 8var buffer = msgpack.encode({foo: "bar"}); 9 10// decode from MessagePack (Uint8Array) to JS Object 11var array = new Uint8Array([0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72]); 12var data = msgpack.decode(array); 13</script>
Step #1: write some code at first.
1var msgpack = require("msgpack-lite"); 2var buffer = msgpack.encode({"foo": "bar"}); 3var data = msgpack.decode(buffer); 4console.warn(data); // => {"foo": "bar"}
Proceed to the next steps if you prefer faster browserify compilation time.
Step #2: add browser
property on package.json
in your project. This refers the global msgpack
object instead of including whole of msgpack-lite
source code.
1{ 2 "dependencies": { 3 "msgpack-lite": "*" 4 }, 5 "browser": { 6 "msgpack-lite": "msgpack-lite/global" 7 } 8}
Step #3: compile it with browserify and uglifyjs.
1browserify src/main.js -o tmp/main.browserify.js -s main 2uglifyjs tmp/main.browserify.js -m -c -o js/main.min.js 3cp node_modules/msgpack-lite/dist/msgpack.min.js js/msgpack.min.js
Step #4: load msgpack.min.js before your code.
1<script src="js/msgpack.min.js"></script> 2<script src="js/main.min.js"></script>
It is tested to have basic compatibility with other Node.js MessagePack modules below:
A benchmark tool lib/benchmark.js
is available to compare encoding/decoding speed
(operation per second) with other MessagePack modules.
It counts operations of 1KB JSON document in 10 seconds.
1$ npm install msgpack msgpack-js msgpack-js-v5 msgpack-unpack msgpack5 notepack 2$ npm run benchmark 10
operation | op | ms | op/s |
---|---|---|---|
buf = Buffer(JSON.stringify(obj)); | 1055200 | 10000 | 105520 |
obj = JSON.parse(buf); | 863800 | 10000 | 86380 |
buf = require("msgpack-lite").encode(obj); | 969100 | 10000 | 96910 |
obj = require("msgpack-lite").decode(buf); | 600300 | 10000 | 60030 |
buf = require("msgpack").pack(obj); | 503500 | 10001 | 50344 |
obj = require("msgpack").unpack(buf); | 560200 | 10001 | 56014 |
buf = Buffer(require("msgpack.codec").msgpack.pack(obj)); | 653500 | 10000 | 65349 |
obj = require("msgpack.codec").msgpack.unpack(buf); | 367500 | 10001 | 36746 |
buf = require("msgpack-js-v5").encode(obj); | 189500 | 10002 | 18946 |
obj = require("msgpack-js-v5").decode(buf); | 408900 | 10000 | 40890 |
buf = require("msgpack-js").encode(obj); | 189200 | 10000 | 18920 |
obj = require("msgpack-js").decode(buf); | 375600 | 10002 | 37552 |
buf = require("msgpack5")().encode(obj); | 110500 | 10009 | 11040 |
obj = require("msgpack5")().decode(buf); | 165500 | 10000 | 16550 |
buf = require("notepack")().encode(obj); | 847800 | 10000 | 84780 |
obj = require("notepack")().decode(buf); | 599800 | 10000 | 59980 |
obj = require("msgpack-unpack").decode(buf); | 48100 | 10002 | 4809 |
Streaming benchmark tool lib/benchmark-stream.js
is also available.
It counts milliseconds for 1,000,000 operations of 30 bytes fluentd msgpack fragment.
This shows streaming encoding and decoding are super faster.
1$ npm run benchmark-stream 2
operation (1000000 x 2) | op | ms | op/s |
---|---|---|---|
stream.write(msgpack.encode(obj)); | 1000000 | 3027 | 330360 |
stream.write(notepack.encode(obj)); | 1000000 | 2012 | 497017 |
msgpack.Encoder().on("data",ondata).encode(obj); | 1000000 | 2956 | 338294 |
msgpack.createEncodeStream().write(obj); | 1000000 | 1888 | 529661 |
stream.write(msgpack.decode(buf)); | 1000000 | 2020 | 495049 |
stream.write(notepack.decode(buf)); | 1000000 | 1794 | 557413 |
msgpack.Decoder().on("data",ondata).decode(buf); | 1000000 | 2744 | 364431 |
msgpack.createDecodeStream().write(buf); | 1000000 | 1341 | 745712 |
Test environment: msgpack-lite 0.1.14, Node v4.2.3, Intel(R) Xeon(R) CPU E5-2666 v3 @ 2.90GHz
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.
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.
Register a custom extension type number to serialize/deserialize your own class instances.
1var msgpack = require("msgpack-lite"); 2 3var codec = msgpack.createCodec(); 4codec.addExtPacker(0x3F, MyVector, myVectorPacker); 5codec.addExtUnpacker(0x3F, myVectorUnpacker); 6 7var data = new MyVector(1, 2); 8var encoded = msgpack.encode(data, {codec: codec}); 9var decoded = msgpack.decode(encoded, {codec: codec}); 10 11function MyVector(x, y) { 12 this.x = x; 13 this.y = y; 14} 15 16function myVectorPacker(vector) { 17 var array = [vector.x, vector.y]; 18 return msgpack.encode(array); // return Buffer serialized 19} 20 21function myVectorUnpacker(buffer) { 22 var array = msgpack.decode(buffer); 23 return new MyVector(array[0], array[1]); // return Object deserialized 24}
The first argument of addExtPacker
and addExtUnpacker
should be an integer within the range of 0 and 127 (0x0 and 0x7F). myClassPacker
is a function that accepts an instance of MyClass
, and should return a buffer representing that instance. myClassUnpacker
is the opposite: it accepts a buffer and should return an instance of MyClass
.
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:
1codec.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
.
msgpack.createCodec()
function accepts some options.
It does NOT have the preset extension types defined when no options given.
1var codec = msgpack.createCodec();
preset
: It has the preset extension types described above.
1var 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.
1var codec = msgpack.createCodec({safe: true});
useraw
: It uses raw
formats instead of bin
and str
.
1var codec = msgpack.createCodec({useraw: true});
int64
: It decodes msgpack's int64
/uint64
formats with int64-buffer object.
1var codec = msgpack.createCodec({int64: true});
binarraybuffer
: It ties msgpack's bin
format with ArrayBuffer
object, instead of Buffer
object.
1var codec = msgpack.createCodec({binarraybuffer: true, preset: true});
uint8array
: It returns Uint8Array object when encoding, instead of Buffer
object.
1var codec = msgpack.createCodec({uint8array: true});
usemap
: Uses the global JavaScript Map type, if available, to unpack
MessagePack map elements.
1var codec = msgpack.createCodec({usemap: true});
The compatibility mode respects for msgpack's old spec. Set true
to useraw
.
1// default mode handles both str and bin formats individually 2msgpack.encode("Aa"); // => <Buffer a2 41 61> (str format) 3msgpack.encode(new Buffer([0x41, 0x61])); // => <Buffer c4 02 41 61> (bin format) 4 5msgpack.decode(new Buffer([0xa2, 0x41, 0x61])); // => 'Aa' (String) 6msgpack.decode(new Buffer([0xc4, 0x02, 0x41, 0x61])); // => <Buffer 41 61> (Buffer) 7 8// compatibility mode handles only raw format both for String and Buffer 9var options = {codec: msgpack.createCodec({useraw: true})}; 10msgpack.encode("Aa", options); // => <Buffer a2 41 61> (raw format) 11msgpack.encode(new Buffer([0x41, 0x61]), options); // => <Buffer a2 41 61> (raw format) 12 13msgpack.decode(new Buffer([0xa2, 0x41, 0x61]), options); // => <Buffer 41 61> (Buffer) 14msgpack.decode(new Buffer([0xa2, 0x41, 0x61]), options).toString(); // => 'Aa' (String)
The MIT License (MIT)
Copyright (c) 2015-2016 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.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 2/22 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More