@@ -3,26 +3,105 @@ import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec";
33import { setInt64 , setUint64 } from "./utils/int" ;
44import { ensureUint8Array } from "./utils/typedArrays" ;
55import type { ExtData } from "./ExtData" ;
6+ import type { ContextOf , SplitUndefined } from "./context" ;
7+
68
79export const DEFAULT_MAX_DEPTH = 100 ;
810export const DEFAULT_INITIAL_BUFFER_SIZE = 2048 ;
911
12+ export type EncoderOptions < ContextType = undefined > = Partial <
13+ Readonly < {
14+ extensionCodec : ExtensionCodecType < ContextType > ;
15+
16+ /**
17+ * Encodes bigint as Int64 or Uint64 if it's set to true.
18+ * {@link forceIntegerToFloat} does not affect bigint.
19+ * Depends on ES2020's {@link DataView#setBigInt64} and
20+ * {@link DataView#setBigUint64}.
21+ *
22+ * Defaults to false.
23+ */
24+ useBigInt64 : boolean ;
25+
26+ /**
27+ * The maximum depth in nested objects and arrays.
28+ *
29+ * Defaults to 100.
30+ */
31+ maxDepth : number ;
32+
33+ /**
34+ * The initial size of the internal buffer.
35+ *
36+ * Defaults to 2048.
37+ */
38+ initialBufferSize : number ;
39+
40+ /**
41+ * If `true`, the keys of an object is sorted. In other words, the encoded
42+ * binary is canonical and thus comparable to another encoded binary.
43+ *
44+ * Defaults to `false`. If enabled, it spends more time in encoding objects.
45+ */
46+ sortKeys : boolean ;
47+ /**
48+ * If `true`, non-integer numbers are encoded in float32, not in float64 (the default).
49+ *
50+ * Only use it if precisions don't matter.
51+ *
52+ * Defaults to `false`.
53+ */
54+ forceFloat32 : boolean ;
55+
56+ /**
57+ * If `true`, an object property with `undefined` value are ignored.
58+ * e.g. `{ foo: undefined }` will be encoded as `{}`, as `JSON.stringify()` does.
59+ *
60+ * Defaults to `false`. If enabled, it spends more time in encoding objects.
61+ */
62+ ignoreUndefined : boolean ;
63+
64+ /**
65+ * If `true`, integer numbers are encoded as floating point numbers,
66+ * with the `forceFloat32` option taken into account.
67+ *
68+ * Defaults to `false`.
69+ */
70+ forceIntegerToFloat : boolean ;
71+ } >
72+ > & ContextOf < ContextType > ;
73+
1074export class Encoder < ContextType = undefined > {
11- private pos = 0 ;
12- private view = new DataView ( new ArrayBuffer ( this . initialBufferSize ) ) ;
13- private bytes = new Uint8Array ( this . view . buffer ) ;
14-
15- public constructor (
16- private readonly extensionCodec : ExtensionCodecType < ContextType > = ExtensionCodec . defaultCodec as any ,
17- private readonly context : ContextType = undefined as any ,
18- private readonly useBigInt64 = false ,
19- private readonly maxDepth = DEFAULT_MAX_DEPTH ,
20- private readonly initialBufferSize = DEFAULT_INITIAL_BUFFER_SIZE ,
21- private readonly sortKeys = false ,
22- private readonly forceFloat32 = false ,
23- private readonly ignoreUndefined = false ,
24- private readonly forceIntegerToFloat = false ,
25- ) { }
75+ private readonly extensionCodec : ExtensionCodecType < ContextType > ;
76+ private readonly context : ContextType ;
77+ private readonly useBigInt64 : boolean ;
78+ private readonly maxDepth : number ;
79+ private readonly initialBufferSize : number ;
80+ private readonly sortKeys : boolean ;
81+ private readonly forceFloat32 : boolean ;
82+ private readonly ignoreUndefined : boolean ;
83+ private readonly forceIntegerToFloat : boolean ;
84+
85+ private pos : number ;
86+ private view : DataView ;
87+ private bytes : Uint8Array ;
88+
89+ public constructor ( options ?: EncoderOptions < ContextType > ) {
90+ this . extensionCodec = options ?. extensionCodec ?? ( ExtensionCodec . defaultCodec as ExtensionCodecType < ContextType > ) ;
91+ this . context = ( options as { context : ContextType } | undefined ) ?. context as ContextType ; // needs a type assertion because EncoderOptions has no context property when ContextType is undefined
92+
93+ this . useBigInt64 = options ?. useBigInt64 ?? false ;
94+ this . maxDepth = options ?. maxDepth ?? DEFAULT_MAX_DEPTH ;
95+ this . initialBufferSize = options ?. initialBufferSize ?? DEFAULT_INITIAL_BUFFER_SIZE ;
96+ this . sortKeys = options ?. sortKeys ?? false ;
97+ this . forceFloat32 = options ?. forceFloat32 ?? false ;
98+ this . ignoreUndefined = options ?. ignoreUndefined ?? false ;
99+ this . forceIntegerToFloat = options ?. forceIntegerToFloat ?? false ;
100+
101+ this . pos = 0 ;
102+ this . view = new DataView ( new ArrayBuffer ( this . initialBufferSize ) ) ;
103+ this . bytes = new Uint8Array ( this . view . buffer ) ;
104+ }
26105
27106 private reinitializeState ( ) {
28107 this . pos = 0 ;
0 commit comments