@@ -35,7 +35,18 @@ export type DecodeOptions<ContextType = undefined> = Readonly<
35
35
> &
36
36
ContextOf < ContextType > ;
37
37
38
+ const getDecoder = ( options : any ) => new Decoder (
39
+ options . extensionCodec ,
40
+ ( options as typeof options & { context : any } ) . context ,
41
+ options . maxStrLength ,
42
+ options . maxBinLength ,
43
+ options . maxArrayLength ,
44
+ options . maxMapLength ,
45
+ options . maxExtLength ,
46
+ ) ;
47
+
38
48
export const defaultDecodeOptions : DecodeOptions = { } ;
49
+ const defaultDecoder = getDecoder ( defaultDecodeOptions ) ;
39
50
40
51
/**
41
52
* It decodes a single MessagePack object in a buffer.
@@ -47,15 +58,7 @@ export function decode<ContextType = undefined>(
47
58
buffer : ArrayLike < number > | BufferSource ,
48
59
options : DecodeOptions < SplitUndefined < ContextType > > = defaultDecodeOptions as any ,
49
60
) : unknown {
50
- const decoder = new Decoder (
51
- options . extensionCodec ,
52
- ( options as typeof options & { context : any } ) . context ,
53
- options . maxStrLength ,
54
- options . maxBinLength ,
55
- options . maxArrayLength ,
56
- options . maxMapLength ,
57
- options . maxExtLength ,
58
- ) ;
61
+ const decoder = options === defaultDecodeOptions ? defaultDecoder : getDecoder ( options ) ;
59
62
return decoder . decode ( buffer ) ;
60
63
}
61
64
@@ -67,14 +70,6 @@ export function decodeMulti<ContextType = undefined>(
67
70
buffer : ArrayLike < number > | BufferSource ,
68
71
options : DecodeOptions < SplitUndefined < ContextType > > = defaultDecodeOptions as any ,
69
72
) : Generator < unknown , void , unknown > {
70
- const decoder = new Decoder (
71
- options . extensionCodec ,
72
- ( options as typeof options & { context : any } ) . context ,
73
- options . maxStrLength ,
74
- options . maxBinLength ,
75
- options . maxArrayLength ,
76
- options . maxMapLength ,
77
- options . maxExtLength ,
78
- ) ;
73
+ const decoder = options === defaultDecodeOptions ? defaultDecoder : getDecoder ( options ) ;
79
74
return decoder . decodeMulti ( buffer ) ;
80
75
}
0 commit comments