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