@@ -70,8 +70,8 @@ export class Decoder<ContextType> {
7070 readonly stack : Array < StackState > = [ ] ;
7171
7272 constructor (
73- readonly extensionCodec : ExtensionCodecType < ContextType > = ExtensionCodec . defaultCodec as any ,
7473 readonly context : ContextType ,
74+ readonly extensionCodec : ExtensionCodecType < ContextType > = ExtensionCodec . defaultCodec as any ,
7575 readonly maxStrLength = DEFAULT_MAX_LENGTH ,
7676 readonly maxBinLength = DEFAULT_MAX_LENGTH ,
7777 readonly maxArrayLength = DEFAULT_MAX_LENGTH ,
@@ -80,6 +80,11 @@ export class Decoder<ContextType> {
8080 readonly cachedKeyDecoder : CachedKeyDecoder | null = sharedCachedKeyDecoder ,
8181 ) { }
8282
83+ private reinitializeState ( ) {
84+ this . totalPos = 0 ;
85+ this . headByte = HEAD_BYTE_REQUIRED ;
86+ }
87+
8388 setBuffer ( buffer : ArrayLike < number > | ArrayBuffer ) : void {
8489 this . bytes = ensureUint8Array ( buffer ) ;
8590 this . view = createDataView ( this . bytes ) ;
@@ -106,11 +111,21 @@ export class Decoder<ContextType> {
106111
107112 createNoExtraBytesError ( posToShow : number ) : Error {
108113 const { view, pos } = this ;
109- return new RangeError ( `Extra ${ view . byteLength - pos } byte(s) found at buffer[${ posToShow } ]` ) ;
114+ return new RangeError ( `Extra ${ view . byteLength - pos } of ${ view . byteLength } byte(s) found at buffer[${ posToShow } ]` ) ;
115+ }
116+
117+ /**
118+ * A synchronous interface to decode a byte buffer. It mutates the decoder instance.
119+ * @param buffer A byte buffer encoded in MessagePack.
120+ */
121+ decodeSync ( buffer : ArrayLike < number > | ArrayBuffer ) : unknown {
122+ this . reinitializeState ( ) ;
123+ this . setBuffer ( buffer ) ;
124+ return this . doDecodeSingleSync ( ) ;
110125 }
111126
112- decodeSingleSync ( ) : unknown {
113- const object = this . decodeSync ( ) ;
127+ private doDecodeSingleSync ( ) : unknown {
128+ const object = this . doDecodeSync ( ) ;
114129 if ( this . hasRemaining ( ) ) {
115130 throw this . createNoExtraBytesError ( this . pos ) ;
116131 }
@@ -128,7 +143,7 @@ export class Decoder<ContextType> {
128143 this . appendBuffer ( buffer ) ;
129144
130145 try {
131- object = this . decodeSync ( ) ;
146+ object = this . doDecodeSync ( ) ;
132147 decoded = true ;
133148 } catch ( e ) {
134149 if ( ! ( e instanceof DataViewIndexOutOfBoundsError ) ) {
@@ -179,7 +194,7 @@ export class Decoder<ContextType> {
179194
180195 try {
181196 while ( true ) {
182- yield this . decodeSync ( ) ;
197+ yield this . doDecodeSync ( ) ;
183198 if ( -- arrayItemsLeft === 0 ) {
184199 break ;
185200 }
@@ -194,7 +209,7 @@ export class Decoder<ContextType> {
194209 }
195210 }
196211
197- decodeSync ( ) : unknown {
212+ private doDecodeSync ( ) : unknown {
198213 DECODE: while ( true ) {
199214 const headByte = this . readHeadByte ( ) ;
200215 let object : unknown ;
0 commit comments