UNPKG

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