1
- import "./utils/symbol.dispose.ts" ;
2
1
import { prettyByte } from "./utils/prettyByte.ts" ;
3
2
import { ExtensionCodec , ExtensionCodecType } from "./ExtensionCodec.ts" ;
4
3
import { getInt64 , getUint64 , UINT32_MAX } from "./utils/int.ts" ;
@@ -305,15 +304,6 @@ export class Decoder<ContextType = undefined> {
305
304
return new RangeError ( `Extra ${ view . byteLength - pos } of ${ view . byteLength } byte(s) found at buffer[${ posToShow } ]` ) ;
306
305
}
307
306
308
- private enteringGuard ( ) : Disposable {
309
- this . entered = true ;
310
- return {
311
- [ Symbol . dispose ] : ( ) => {
312
- this . entered = false ;
313
- } ,
314
- } ;
315
- }
316
-
317
307
/**
318
308
* @throws {@link DecodeError }
319
309
* @throws {@link RangeError }
@@ -323,17 +313,21 @@ export class Decoder<ContextType = undefined> {
323
313
const instance = this . clone ( ) ;
324
314
return instance . decode ( buffer ) ;
325
315
}
326
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
327
- using _guard = this . enteringGuard ( ) ;
328
316
329
- this . reinitializeState ( ) ;
330
- this . setBuffer ( buffer ) ;
317
+ try {
318
+ this . entered = true ;
331
319
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 ;
335
330
}
336
- return object ;
337
331
}
338
332
339
333
public * decodeMulti ( buffer : ArrayLike < number > | ArrayBufferView | ArrayBufferLike ) : Generator < unknown , void , unknown > {
@@ -342,14 +336,18 @@ export class Decoder<ContextType = undefined> {
342
336
yield * instance . decodeMulti ( buffer ) ;
343
337
return ;
344
338
}
345
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
346
- using _guard = this . enteringGuard ( ) ;
347
339
348
- this . reinitializeState ( ) ;
349
- this . setBuffer ( buffer ) ;
340
+ try {
341
+ this . entered = true ;
350
342
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 ;
353
351
}
354
352
}
355
353
@@ -358,42 +356,46 @@ export class Decoder<ContextType = undefined> {
358
356
const instance = this . clone ( ) ;
359
357
return instance . decodeAsync ( stream ) ;
360
358
}
361
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
362
- using _guard = this . enteringGuard ( ) ;
363
359
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 ;
371
362
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
+ }
373
370
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
380
381
}
381
- // fallthrough
382
+ this . totalPos += this . pos ;
382
383
}
383
- this . totalPos += this . pos ;
384
- }
385
384
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 ;
389
390
}
390
- return object ;
391
- }
392
391
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
+ }
397
399
}
398
400
399
401
public decodeArrayStream (
@@ -412,39 +414,43 @@ export class Decoder<ContextType = undefined> {
412
414
yield * instance . decodeMultiAsync ( stream , isArray ) ;
413
415
return ;
414
416
}
415
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
416
- using _guard = this . enteringGuard ( ) ;
417
417
418
- let isArrayHeaderRequired = isArray ;
419
- let arrayItemsLeft = - 1 ;
418
+ try {
419
+ this . entered = true ;
420
420
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 ;
425
423
426
- this . appendBuffer ( buffer ) ;
424
+ for await ( const buffer of stream ) {
425
+ if ( isArray && arrayItemsLeft === 0 ) {
426
+ throw this . createExtraByteError ( this . totalPos ) ;
427
+ }
427
428
428
- if ( isArrayHeaderRequired ) {
429
- arrayItemsLeft = this . readArraySize ( ) ;
430
- isArrayHeaderRequired = false ;
431
- this . complete ( ) ;
432
- }
429
+ this . appendBuffer ( buffer ) ;
433
430
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 ( ) ;
440
435
}
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
444
449
}
445
- // fallthrough
450
+ this . totalPos += this . pos ;
446
451
}
447
- this . totalPos += this . pos ;
452
+ } finally {
453
+ this . entered = false ;
448
454
}
449
455
}
450
456
0 commit comments