在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:kawanet/msgpack-lite开源软件地址:https://github.com/kawanet/msgpack-lite开源编程语言:JavaScript 99.0%开源软件介绍:msgpack-liteFast Pure JavaScript MessagePack Encoder and Decoder Online demo: http://kawanet.github.io/msgpack-lite/ Features
Encoding and Decoding MessagePackvar 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"}
// if encode/decode receives an invalid argument an error is thrown Writing to MessagePack Streamvar 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"});
// call this once you're done writing to the stream.
encodeStream.end(); Reading from MessagePack Streamvar 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); Decoding MessagePack Bytes Arrayvar msgpack = require("msgpack-lite");
// decode() accepts Buffer instance per default
msgpack.decode(Buffer([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 InterfaceA 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 TestsRun 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 BuildBrowser 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://rawgit.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> MessagePack With BrowserifyStep #1: write some code at first. var msgpack = require("msgpack-lite");
var buffer = msgpack.encode({"foo": "bar"});
var data = msgpack.decode(buffer);
console.warn(data); // => {"foo": "bar"} Proceed to the next steps if you prefer faster browserify compilation time. Step #2: add {
"dependencies": {
"msgpack-lite": "*"
},
"browser": {
"msgpack-lite": "msgpack-lite/global"
}
} Step #3: compile it with browserify and uglifyjs. browserify src/main.js -o tmp/main.browserify.js -s main
uglifyjs tmp/main.browserify.js -m -c -o js/main.min.js
cp node_modules/msgpack-lite/dist/msgpack.min.js js/msgpack.min.js Step #4: load msgpack.min.js before your code. <script src="js/msgpack.min.js"></script>
<script src="js/main.min.js"></script> InteroperabilityIt is tested to have basic compatibility with other Node.js MessagePack modules below:
BenchmarksA benchmark tool $ npm install msgpack msgpack-js msgpack-js-v5 msgpack-unpack msgpack5 notepack
$ npm run benchmark 10
Streaming benchmark tool $ npm run benchmark-stream 2
Test environment: msgpack-lite 0.1.14, Node v4.2.3, Intel(R) Xeon(R) CPU E5-2666 v3 @ 2.90GHz MessagePack Mapping TableThe following table shows how JavaScript objects (value) will be mapped to MessagePack formats and vice versa.
Note that both Extension TypesThe MessagePack specification allows 128 application-specific extension types. The library uses the following types to make round-trip conversion possible for JavaScript native objects.
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. var msgpack = require("msgpack-lite");
var codec = msgpack.createCodec();
codec.addExtPacker(0x3F, MyVector, myVectorPacker);
codec.addExtUnpacker(0x3F, myVectorUnpacker);
var data = new MyVector(1, 2);
var encoded = msgpack.encode(data, {codec: codec});
var decoded = msgpack.decode(encoded, {codec: codec});
function MyVector(x, y) {
this.x = x;
this.y = y;
}
function myVectorPacker(vector) {
var array = [vector.x, vector.y];
return msgpack.encode(array); // return Buffer serialized
}
function myVectorUnpacker(buffer) {
var array = msgpack.decode(buffer);
return new MyVector(array[0], array[1]); // return Object deserialized
} The first argument of If you pass an array of functions to codec.addExtPacker(0x00, Date, [Number, msgpack.encode]); You can also pass the If you wish to modify the default built-in codec, you can access it at Custom Codec Options
It does NOT have the preset extension types defined when no options given. var codec = msgpack.createCodec();
var codec = msgpack.createCodec({preset: true});
var codec = msgpack.createCodec({safe: true});
var codec = msgpack.createCodec({useraw: true});
var codec = msgpack.createCodec({int64: true});
var codec = msgpack.createCodec({binarraybuffer: true, preset: true});
var codec = msgpack.createCodec({uint8array: true});
var codec = msgpack.createCodec({usemap: true}); Compatibility ModeThe compatibility mode respects for msgpack's old spec. Set // default mode handles both str and bin formats individually
msgpack.encode("Aa"); // => <Buffer a2 41 61> (str format)
msgpack.encode(new Buffer([0x41, 0x61])); // => <Buffer c4 02 41 61> (bin format)
msgpack.decode(new Buffer([0xa2, 0x41, 0x61])); // => 'Aa' (String)
msgpack.decode(new Buffer([0xc4, 0x02, 0x41, 0x61])); // => <Buffer 41 61> (Buffer)
// compatibility mode handles only raw format both for String and Buffer
var options = {codec: msgpack.createCodec({useraw: true})};
msgpack.encode("Aa", options); // => <Buffer a2 41 61> (raw format)
msgpack.encode(new Buffer([0x41, 0x61]), options); // => <Buffer a2 41 61> (raw format)
msgpack.decode(new Buffer([0xa2, 0x41, 0x61]), options); // => <Buffer 41 61> (Buffer)
msgpack.decode(new Buffer([0xa2, 0x41, 0x61]), options).toString(); // => 'Aa' (String) Repository |