1- import "./utils/symbol.dispose.ts" ;
21import { prettyByte } from "./utils/prettyByte.ts" ;
32import { ExtensionCodec , ExtensionCodecType } from "./ExtensionCodec.ts" ;
43import { getInt64 , getUint64 , UINT32_MAX } from "./utils/int.ts" ;
@@ -305,15 +304,6 @@ export class Decoder<ContextType = undefined> {
305304 return new RangeError ( `Extra ${ view . byteLength - pos } of ${ view . byteLength } byte(s) found at buffer[${ posToShow } ]` ) ;
306305 }
307306
308- private enteringGuard ( ) : Disposable {
309- this . entered = true ;
310- return {
311- [ Symbol . dispose ] : ( ) => {
312- this . entered = false ;
313- } ,
314- } ;
315- }
316-
317307 /**
318308 * @throws {@link DecodeError }
319309 * @throws {@link RangeError }
@@ -323,17 +313,21 @@ export class Decoder<ContextType = undefined> {
323313 const instance = this . clone ( ) ;
324314 return instance . decode ( buffer ) ;
325315 }
326- // eslint-disable-next-line @typescript-eslint/no-unused-vars
327- using _guard = this . enteringGuard ( ) ;
328316
329- this . reinitializeState ( ) ;
330- this . setBuffer ( buffer ) ;
317+ try {
318+ this . entered = true ;
331319
332- const object = this . doDecodeSync ( ) ;
333- if ( this . hasRemaining ( 1 ) ) {
334- throw this . createExtraByteError ( this . pos ) ;
320+ this . reinitializeState ( ) ;
321+ this . setBuffer ( buffer ) ;
322+
323+ const object = this . doDecodeSync ( ) ;
324+ if ( this . hasRemaining ( 1 ) ) {
325+ throw this . createExtraByteError ( this . pos ) ;
326+ }
327+ return object ;
328+ } finally {
329+ this . entered = false ;
335330 }
336- return object ;
337331 }
338332
339333 public * decodeMulti ( buffer : ArrayLike < number > | ArrayBufferView | ArrayBufferLike ) : Generator < unknown , void , unknown > {
@@ -342,14 +336,18 @@ export class Decoder<ContextType = undefined> {
342336 yield * instance . decodeMulti ( buffer ) ;
343337 return ;
344338 }
345- // eslint-disable-next-line @typescript-eslint/no-unused-vars
346- using _guard = this . enteringGuard ( ) ;
347339
348- this . reinitializeState ( ) ;
349- this . setBuffer ( buffer ) ;
340+ try {
341+ this . entered = true ;
350342
351- while ( this . hasRemaining ( 1 ) ) {
352- yield this . doDecodeSync ( ) ;
343+ this . reinitializeState ( ) ;
344+ this . setBuffer ( buffer ) ;
345+
346+ while ( this . hasRemaining ( 1 ) ) {
347+ yield this . doDecodeSync ( ) ;
348+ }
349+ } finally {
350+ this . entered = false ;
353351 }
354352 }
355353
@@ -358,42 +356,46 @@ export class Decoder<ContextType = undefined> {
358356 const instance = this . clone ( ) ;
359357 return instance . decodeAsync ( stream ) ;
360358 }
361- // eslint-disable-next-line @typescript-eslint/no-unused-vars
362- using _guard = this . enteringGuard ( ) ;
363359
364- let decoded = false ;
365- let object : unknown ;
366- for await ( const buffer of stream ) {
367- if ( decoded ) {
368- this . entered = false ;
369- throw this . createExtraByteError ( this . totalPos ) ;
370- }
360+ try {
361+ this . entered = true ;
371362
372- this . appendBuffer ( buffer ) ;
363+ let decoded = false ;
364+ let object : unknown ;
365+ for await ( const buffer of stream ) {
366+ if ( decoded ) {
367+ this . entered = false ;
368+ throw this . createExtraByteError ( this . totalPos ) ;
369+ }
373370
374- try {
375- object = this . doDecodeSync ( ) ;
376- decoded = true ;
377- } catch ( e ) {
378- if ( ! ( e instanceof RangeError ) ) {
379- throw e ; // rethrow
371+ this . appendBuffer ( buffer ) ;
372+
373+ try {
374+ object = this . doDecodeSync ( ) ;
375+ decoded = true ;
376+ } catch ( e ) {
377+ if ( ! ( e instanceof RangeError ) ) {
378+ throw e ; // rethrow
379+ }
380+ // fallthrough
380381 }
381- // fallthrough
382+ this . totalPos += this . pos ;
382383 }
383- this . totalPos += this . pos ;
384- }
385384
386- if ( decoded ) {
387- if ( this . hasRemaining ( 1 ) ) {
388- throw this . createExtraByteError ( this . totalPos ) ;
385+ if ( decoded ) {
386+ if ( this . hasRemaining ( 1 ) ) {
387+ throw this . createExtraByteError ( this . totalPos ) ;
388+ }
389+ return object ;
389390 }
390- return object ;
391- }
392391
393- const { headByte, pos, totalPos } = this ;
394- throw new RangeError (
395- `Insufficient data in parsing ${ prettyByte ( headByte ) } at ${ totalPos } (${ pos } in the current buffer)` ,
396- ) ;
392+ const { headByte, pos, totalPos } = this ;
393+ throw new RangeError (
394+ `Insufficient data in parsing ${ prettyByte ( headByte ) } at ${ totalPos } (${ pos } in the current buffer)` ,
395+ ) ;
396+ } finally {
397+ this . entered = false ;
398+ }
397399 }
398400
399401 public decodeArrayStream (
@@ -412,39 +414,43 @@ export class Decoder<ContextType = undefined> {
412414 yield * instance . decodeMultiAsync ( stream , isArray ) ;
413415 return ;
414416 }
415- // eslint-disable-next-line @typescript-eslint/no-unused-vars
416- using _guard = this . enteringGuard ( ) ;
417417
418- let isArrayHeaderRequired = isArray ;
419- let arrayItemsLeft = - 1 ;
418+ try {
419+ this . entered = true ;
420420
421- for await ( const buffer of stream ) {
422- if ( isArray && arrayItemsLeft === 0 ) {
423- throw this . createExtraByteError ( this . totalPos ) ;
424- }
421+ let isArrayHeaderRequired = isArray ;
422+ let arrayItemsLeft = - 1 ;
425423
426- this . appendBuffer ( buffer ) ;
424+ for await ( const buffer of stream ) {
425+ if ( isArray && arrayItemsLeft === 0 ) {
426+ throw this . createExtraByteError ( this . totalPos ) ;
427+ }
427428
428- if ( isArrayHeaderRequired ) {
429- arrayItemsLeft = this . readArraySize ( ) ;
430- isArrayHeaderRequired = false ;
431- this . complete ( ) ;
432- }
429+ this . appendBuffer ( buffer ) ;
433430
434- try {
435- while ( true ) {
436- yield this . doDecodeSync ( ) ;
437- if ( -- arrayItemsLeft === 0 ) {
438- break ;
439- }
431+ if ( isArrayHeaderRequired ) {
432+ arrayItemsLeft = this . readArraySize ( ) ;
433+ isArrayHeaderRequired = false ;
434+ this . complete ( ) ;
440435 }
441- } catch ( e ) {
442- if ( ! ( e instanceof RangeError ) ) {
443- throw e ; // rethrow
436+
437+ try {
438+ while ( true ) {
439+ yield this . doDecodeSync ( ) ;
440+ if ( -- arrayItemsLeft === 0 ) {
441+ break ;
442+ }
443+ }
444+ } catch ( e ) {
445+ if ( ! ( e instanceof RangeError ) ) {
446+ throw e ; // rethrow
447+ }
448+ // fallthrough
444449 }
445- // fallthrough
450+ this . totalPos += this . pos ;
446451 }
447- this . totalPos += this . pos ;
452+ } finally {
453+ this . entered = false ;
448454 }
449455 }
450456
0 commit comments