Typings for individual Decoder factory (#38631)

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.
This commit is contained in:
Marco Manino
2019-09-26 21:09:06 +02:00
committed by Michael Crane
parent 06886907f1
commit ed41e25e73
2 changed files with 19 additions and 1 deletions

View File

@@ -5,6 +5,7 @@
/// <reference types="node" />
import * as stream from 'stream';
import { EventEmitter } from 'events';
/**
* encode from JS Object to MessagePack
@@ -84,7 +85,9 @@ export interface Encoder {
end(chunk: any): void;
}
export interface Decoder {
export function Decoder(options?: DecoderOptions): Decoder;
export interface Decoder extends EventEmitter {
bufferish: any;
offset: number;
fetch(): void;

View File

@@ -84,3 +84,18 @@ function customExtensionTypes() {
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);
}