@@ -40,7 +40,18 @@ export type DecodeOptions<ContextType = undefined> = Readonly<
4040> &
4141 ContextOf < ContextType > ;
4242
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+
4353export const defaultDecodeOptions : DecodeOptions = { } ;
54+ const defaultDecoder = getDecoder ( defaultDecodeOptions ) ;
4455
4556/**
4657 * It decodes a single MessagePack object in a buffer.
@@ -52,15 +63,7 @@ export function decode<ContextType = undefined>(
5263 buffer : ArrayLike < number > | BufferSource ,
5364 options : DecodeOptions < SplitUndefined < ContextType > > = defaultDecodeOptions as any ,
5465) : 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 ) ;
6467 return decoder . decode ( buffer ) ;
6568}
6669
@@ -72,14 +75,6 @@ export function decodeMulti<ContextType = undefined>(
7275 buffer : ArrayLike < number > | BufferSource ,
7376 options : DecodeOptions < SplitUndefined < ContextType > > = defaultDecodeOptions as any ,
7477) : 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 ) ;
8479 return decoder . decodeMulti ( buffer ) ;
8580}
0 commit comments