forked from msgpack/msgpack-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.ts
More file actions
53 lines (48 loc) · 1.64 KB
/
encode.ts
File metadata and controls
53 lines (48 loc) · 1.64 KB
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
import { ExtensionCodecType } from "./ExtensionCodec";
import { Encoder } from "./Encoder";
import { ContextOf, SplitUndefined } from "./context";
export type EncodeOptions<ContextType = undefined> = Partial<
Readonly<{
extensionCodec: ExtensionCodecType<ContextType>;
maxDepth: number;
initialBufferSize: number;
sortKeys: boolean;
/**
* If `true`, non-integer numbers are encoded in float32, not in float64 (the default).
*
* Only use it if precisions don't matter.
*/
forceFloat32: boolean;
/**
* If `true`, an object property with `undefined` value are ignored.
* e.g. `{ foo: undefined }` will be encoded as `{}`, as `JSON.stringify()` does.
*
* The default is `false`. Note that it needs more time to encode.
*/
ignoreUndefined: boolean;
}>
> &
ContextOf<ContextType>;
const defaultEncodeOptions: EncodeOptions = {};
/**
* It encodes `value` in the MessagePack format and
* returns a byte buffer.
*
* The returned buffer is a slice of a larger `ArrayBuffer`, so you have to use its `#byteOffset` and `#byteLength` in order to convert it to another typed arrays including NodeJS `Buffer`.
*/
export function encode<ContextType>(
value: unknown,
options: EncodeOptions<SplitUndefined<ContextType>> = defaultEncodeOptions as any,
): Uint8Array {
const encoder = new Encoder<ContextType>(
options.extensionCodec,
(options as typeof options & { context: any }).context,
options.maxDepth,
options.initialBufferSize,
options.sortKeys,
options.forceFloat32,
options.ignoreUndefined,
);
encoder.encode(value, 1);
return encoder.getUint8Array();
}