UNPKG

4.68 kBTypeScriptView Raw
1/// <reference types="node" />
2import { EventEmitter } from "events";
3import * as stream from "stream";
4
5/**
6 * encode from JS Object to MessagePack
7 */
8export function encode(input: any, options?: EncoderOptions): Buffer;
9
10/**
11 * decode from MessagePack to JS Object
12 */
13export function decode(input: Buffer | Uint8Array | number[], options?: DecoderOptions): any;
14
15/**
16 * create a stream that encodes from JS Object to MessagePack
17 */
18export function createEncodeStream(options?: EncoderOptions & stream.TransformOptions): EncodeStream;
19
20/**
21 * create a stream that decodes from MessagePack (Buffer) to JS Object
22 */
23export function createDecodeStream(options?: DecoderOptions & stream.TransformOptions): DecodeStream;
24
25/**
26 * Codecs allow for Custom Extension Types
27 * Register a custom extension type number to serialize/deserialize your own class instances.
28 * https://github.com/kawanet/msgpack-lite#custom-extension-types-codecs
29 * If you wish to modify the default built-in codec, you can access it at msgpack.codec.preset
30 */
31export function createCodec(options?: CodecOptions): Codec;
32
33/**
34 * The default built-in codec
35 */
36export const codec: {
37 /**
38 * The default built-in codec
39 */
40 preset: Codec;
41};
42
43export interface Codec {
44 /**
45 * Register a custom extension to serialize your own class instances
46 *
47 * @param etype an integer within the range of 0 and 127 (0x0 and 0x7F)
48 * @param Class the constructor of the type you wish to serialize
49 * @param packer a function that converts an instance of T to bytes
50 */
51 addExtPacker<T>(
52 etype: number,
53 Class: new(...args: any[]) => T,
54 packer: (t: T) => Buffer | Uint8Array,
55 ): void;
56
57 /**
58 * Register a custom extension to deserialize your own class instances
59 *
60 * @param etype an integer within the range of 0 and 127 (0x0 and 0x7F)
61 * @param unpacker a function that converts bytes to an instance of T
62 */
63 addExtUnpacker<T>(etype: number, unpacker: (data: Buffer | Uint8Array) => T): void;
64}
65
66export interface Encoder {
67 bufferish: any;
68 maxBufferSize: number;
69 minBufferSize: number;
70 offset: number;
71 start: number;
72 write(chunk: any): void;
73 fetch(): void;
74 flush(): void;
75 push(chunk: any): void;
76 pull(): number;
77 read(): number;
78 reserve(length: number): number;
79 send(buffer: Buffer): void;
80 encode(chunk: any): void;
81 end(chunk: any): void;
82}
83
84export function Decoder(options?: DecoderOptions): Decoder;
85
86export interface Decoder extends EventEmitter {
87 bufferish: any;
88 offset: number;
89 fetch(): void;
90 flush(): void;
91 pull(): number;
92 read(): number;
93 write(chunk: any): void;
94 reserve(length: number): number;
95 decode(chunk: any): void;
96 push(chunk: any): void;
97 end(chunk: any): void;
98}
99
100export interface EncodeStream extends stream.Transform {
101 encoder: Encoder;
102}
103
104export interface DecodeStream extends stream.Transform {
105 decoder: Decoder;
106}
107
108export interface CodecOptions {
109 /**
110 * It includes the preset extensions for JavaScript native objects.
111 * @see https://github.com/kawanet/msgpack-lite#extension-types
112 * @default false
113 */
114 preset?: boolean | undefined;
115 /**
116 * It runs a validation of the value before writing it into buffer.
117 * This is the default behavior for some old browsers which do not support ArrayBuffer object.
118 * @default varies
119 */
120 safe?: boolean | undefined;
121 /**
122 * It uses raw formats instead of bin and str.
123 * Set true for compatibility with msgpack's old spec.
124 * @see https://github.com/kawanet/msgpack-lite#compatibility-mode
125 * @default false
126 */
127 useraw?: boolean | undefined;
128 /**
129 * It decodes msgpack's int64/uint64 formats with int64-buffer object.
130 * int64-buffer is a cutom integer type with 64 bits of precision instead
131 * of the built-in IEEE-754 53 bits. See https://github.com/kawanet/int64-buffer
132 * @default false
133 */
134 int64?: boolean | undefined;
135 /**
136 * It ties msgpack's bin format with ArrayBuffer object, instead of Buffer object.
137 * @default false
138 */
139 binarraybuffer?: boolean | undefined;
140 /**
141 * It returns Uint8Array object when encoding, instead of Buffer object.
142 * @default false
143 */
144 uint8array?: boolean | undefined;
145 /**
146 * It uses the global JavaScript Map type, if available, to unpack MessagePack map elements.
147 * @default false
148 */
149 usemap?: boolean | undefined;
150}
151
152export interface EncoderOptions {
153 codec?: Codec | undefined;
154}
155
156export interface DecoderOptions {
157 codec?: Codec | undefined;
158}
159
\No newline at end of file