-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdecode.ts
63 lines (59 loc) · 1.73 KB
/
decode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { ExtensionCodecType } from "./ExtensionCodec.ts";
import { Decoder } from "./Decoder.ts";
import { ContextOf, SplitUndefined } from "./context.ts";
export type DecodeOptions<ContextType = undefined> =
& Readonly<
Partial<{
extensionCodec: ExtensionCodecType<ContextType>;
/**
* Maximum string length.
* Default to 4_294_967_295 (UINT32_MAX).
*/
maxStrLength: number;
/**
* Maximum binary length.
* Default to 4_294_967_295 (UINT32_MAX).
*/
maxBinLength: number;
/**
* Maximum array length.
* Default to 4_294_967_295 (UINT32_MAX).
*/
maxArrayLength: number;
/**
* Maximum map length.
* Default to 4_294_967_295 (UINT32_MAX).
*/
maxMapLength: number;
/**
* Maximum extension length.
* Default to 4_294_967_295 (UINT32_MAX).
*/
maxExtLength: number;
}>
>
& ContextOf<ContextType>;
export const defaultDecodeOptions: DecodeOptions = {};
/**
* It decodes a MessagePack-encoded buffer.
*
* This is a synchronous decoding function. See other variants for asynchronous decoding: `decodeAsync()`, `decodeStream()`, `decodeArrayStream()`.
*/
export function decode<ContextType>(
buffer: ArrayLike<number> | ArrayBuffer,
options: DecodeOptions<SplitUndefined<ContextType>> =
// deno-lint-ignore no-explicit-any
defaultDecodeOptions as any,
): unknown {
const decoder = new Decoder<ContextType>(
options.extensionCodec,
// deno-lint-ignore no-explicit-any
(options as typeof options & { context: any }).context,
options.maxStrLength,
options.maxBinLength,
options.maxArrayLength,
options.maxMapLength,
options.maxExtLength,
);
return decoder.decode(buffer);
}