mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
Decoder can be built standalone (is part of the actual public interface and part of the performance-test interface described in the README.md of the project). Moreover, Decoder is an event emitter. The interface has been update accordingly.
102 lines
3.0 KiB
TypeScript
102 lines
3.0 KiB
TypeScript
import * as msgpack from 'msgpack-lite';
|
|
|
|
// https://github.com/kawanet/msgpack-lite#encoding-and-decoding-messagepack
|
|
function encodingAndDecoding() {
|
|
// 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"}
|
|
}
|
|
|
|
function customCodec() {
|
|
msgpack.createCodec({
|
|
preset: true,
|
|
safe: true,
|
|
useraw: true,
|
|
int64: true,
|
|
binarraybuffer: true,
|
|
uint8array: true,
|
|
usemap: true
|
|
});
|
|
}
|
|
|
|
// https://github.com/kawanet/msgpack-lite#writing-to-messagepack-stream
|
|
function writingToStream() {
|
|
const fs = require("fs");
|
|
|
|
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();
|
|
}
|
|
|
|
// https://github.com/kawanet/msgpack-lite#reading-from-messagepack-stream
|
|
function readingFromStream() {
|
|
const fs = require("fs");
|
|
|
|
const readStream = fs.createReadStream("test.msp");
|
|
const decodeStream = msgpack.createDecodeStream();
|
|
|
|
// show multiple objects decoded from stream
|
|
readStream.pipe(decodeStream).on("data", console.warn);
|
|
}
|
|
|
|
// https://github.com/kawanet/msgpack-lite#decoding-messagepack-bytes-array
|
|
function decodingBytesArray() {
|
|
// decode() accepts Buffer instance per default
|
|
msgpack.decode(new 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]));
|
|
}
|
|
|
|
// https://github.com/kawanet/msgpack-lite#custom-extension-types-codecs
|
|
function customExtensionTypes() {
|
|
class MyVector {
|
|
constructor(public x: number, public y: number) {}
|
|
}
|
|
|
|
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});
|
|
const decoded = msgpack.decode(encoded, {codec});
|
|
|
|
function myVectorPacker(vector: MyVector) {
|
|
const array = [vector.x, vector.y];
|
|
return msgpack.encode(array); // return Buffer serialized
|
|
}
|
|
|
|
function myVectorUnpacker(buffer: Buffer | Uint8Array): MyVector {
|
|
const array = msgpack.decode(buffer);
|
|
return new MyVector(array[0], array[1]); // return Object deserialized
|
|
}
|
|
}
|
|
|
|
// https://github.com/kawanet/msgpack-lite#benchmarks
|
|
// (this is not well documented, but in the test there is an example usage.)
|
|
function standaloneDecoder() {
|
|
const decoder = msgpack.Decoder();
|
|
const object = { test: "Object" };
|
|
const encoded = msgpack.encode(object);
|
|
decoder.on('data', (obj) => {
|
|
if (object.test !== obj.test) {
|
|
throw Error();
|
|
}
|
|
});
|
|
|
|
decoder.push(encoded);
|
|
}
|