diff --git a/src/Decoder.ts b/src/Decoder.ts index e9257a9..9239dd1 100644 --- a/src/Decoder.ts +++ b/src/Decoder.ts @@ -1,4 +1,3 @@ -import "./utils/symbol.dispose.ts"; import { prettyByte } from "./utils/prettyByte.ts"; import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec.ts"; import { getInt64, getUint64, UINT32_MAX } from "./utils/int.ts"; @@ -305,15 +304,6 @@ export class Decoder { return new RangeError(`Extra ${view.byteLength - pos} of ${view.byteLength} byte(s) found at buffer[${posToShow}]`); } - private enteringGuard(): Disposable { - this.entered = true; - return { - [Symbol.dispose]: () => { - this.entered = false; - }, - }; - } - /** * @throws {@link DecodeError} * @throws {@link RangeError} @@ -323,17 +313,21 @@ export class Decoder { const instance = this.clone(); return instance.decode(buffer); } - // eslint-disable-next-line @typescript-eslint/no-unused-vars - using _guard = this.enteringGuard(); - this.reinitializeState(); - this.setBuffer(buffer); + try { + this.entered = true; - const object = this.doDecodeSync(); - if (this.hasRemaining(1)) { - throw this.createExtraByteError(this.pos); + this.reinitializeState(); + this.setBuffer(buffer); + + const object = this.doDecodeSync(); + if (this.hasRemaining(1)) { + throw this.createExtraByteError(this.pos); + } + return object; + } finally { + this.entered = false; } - return object; } public *decodeMulti(buffer: ArrayLike | ArrayBufferView | ArrayBufferLike): Generator { @@ -342,14 +336,18 @@ export class Decoder { yield* instance.decodeMulti(buffer); return; } - // eslint-disable-next-line @typescript-eslint/no-unused-vars - using _guard = this.enteringGuard(); - this.reinitializeState(); - this.setBuffer(buffer); + try { + this.entered = true; - while (this.hasRemaining(1)) { - yield this.doDecodeSync(); + this.reinitializeState(); + this.setBuffer(buffer); + + while (this.hasRemaining(1)) { + yield this.doDecodeSync(); + } + } finally { + this.entered = false; } } @@ -358,42 +356,46 @@ export class Decoder { const instance = this.clone(); return instance.decodeAsync(stream); } - // eslint-disable-next-line @typescript-eslint/no-unused-vars - using _guard = this.enteringGuard(); - let decoded = false; - let object: unknown; - for await (const buffer of stream) { - if (decoded) { - this.entered = false; - throw this.createExtraByteError(this.totalPos); - } + try { + this.entered = true; - this.appendBuffer(buffer); + let decoded = false; + let object: unknown; + for await (const buffer of stream) { + if (decoded) { + this.entered = false; + throw this.createExtraByteError(this.totalPos); + } - try { - object = this.doDecodeSync(); - decoded = true; - } catch (e) { - if (!(e instanceof RangeError)) { - throw e; // rethrow + this.appendBuffer(buffer); + + try { + object = this.doDecodeSync(); + decoded = true; + } catch (e) { + if (!(e instanceof RangeError)) { + throw e; // rethrow + } + // fallthrough } - // fallthrough + this.totalPos += this.pos; } - this.totalPos += this.pos; - } - if (decoded) { - if (this.hasRemaining(1)) { - throw this.createExtraByteError(this.totalPos); + if (decoded) { + if (this.hasRemaining(1)) { + throw this.createExtraByteError(this.totalPos); + } + return object; } - return object; - } - const { headByte, pos, totalPos } = this; - throw new RangeError( - `Insufficient data in parsing ${prettyByte(headByte)} at ${totalPos} (${pos} in the current buffer)`, - ); + const { headByte, pos, totalPos } = this; + throw new RangeError( + `Insufficient data in parsing ${prettyByte(headByte)} at ${totalPos} (${pos} in the current buffer)`, + ); + } finally { + this.entered = false; + } } public decodeArrayStream( @@ -412,39 +414,43 @@ export class Decoder { yield* instance.decodeMultiAsync(stream, isArray); return; } - // eslint-disable-next-line @typescript-eslint/no-unused-vars - using _guard = this.enteringGuard(); - let isArrayHeaderRequired = isArray; - let arrayItemsLeft = -1; + try { + this.entered = true; - for await (const buffer of stream) { - if (isArray && arrayItemsLeft === 0) { - throw this.createExtraByteError(this.totalPos); - } + let isArrayHeaderRequired = isArray; + let arrayItemsLeft = -1; - this.appendBuffer(buffer); + for await (const buffer of stream) { + if (isArray && arrayItemsLeft === 0) { + throw this.createExtraByteError(this.totalPos); + } - if (isArrayHeaderRequired) { - arrayItemsLeft = this.readArraySize(); - isArrayHeaderRequired = false; - this.complete(); - } + this.appendBuffer(buffer); - try { - while (true) { - yield this.doDecodeSync(); - if (--arrayItemsLeft === 0) { - break; - } + if (isArrayHeaderRequired) { + arrayItemsLeft = this.readArraySize(); + isArrayHeaderRequired = false; + this.complete(); } - } catch (e) { - if (!(e instanceof RangeError)) { - throw e; // rethrow + + try { + while (true) { + yield this.doDecodeSync(); + if (--arrayItemsLeft === 0) { + break; + } + } + } catch (e) { + if (!(e instanceof RangeError)) { + throw e; // rethrow + } + // fallthrough } - // fallthrough + this.totalPos += this.pos; } - this.totalPos += this.pos; + } finally { + this.entered = false; } } diff --git a/src/Encoder.ts b/src/Encoder.ts index 27d8ded..ffb4f6b 100644 --- a/src/Encoder.ts +++ b/src/Encoder.ts @@ -1,4 +1,3 @@ -import "./utils/symbol.dispose.ts"; import { utf8Count, utf8Encode } from "./utils/utf8.ts"; import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec.ts"; import { setInt64, setUint64 } from "./utils/int.ts"; @@ -127,15 +126,6 @@ export class Encoder { this.pos = 0; } - private enteringGuard(): Disposable { - this.entered = true; - return { - [Symbol.dispose]: () => { - this.entered = false; - }, - }; - } - /** * This is almost equivalent to {@link Encoder#encode}, but it returns an reference of the encoder's internal buffer and thus much faster than {@link Encoder#encode}. * @@ -146,12 +136,16 @@ export class Encoder { const instance = this.clone(); return instance.encodeSharedRef(object); } - // eslint-disable-next-line @typescript-eslint/no-unused-vars - using _guard = this.enteringGuard(); - this.reinitializeState(); - this.doEncode(object, 1); - return this.bytes.subarray(0, this.pos); + try { + this.entered = true; + + this.reinitializeState(); + this.doEncode(object, 1); + return this.bytes.subarray(0, this.pos); + } finally { + this.entered = false; + } } /** @@ -162,12 +156,16 @@ export class Encoder { const instance = this.clone(); return instance.encode(object); } - // eslint-disable-next-line @typescript-eslint/no-unused-vars - using _guard = this.enteringGuard(); - this.reinitializeState(); - this.doEncode(object, 1); - return this.bytes.slice(0, this.pos); + try { + this.entered = true; + + this.reinitializeState(); + this.doEncode(object, 1); + return this.bytes.slice(0, this.pos); + } finally { + this.entered = false; + } } private doEncode(object: unknown, depth: number): void { diff --git a/src/utils/symbol.dispose.ts b/src/utils/symbol.dispose.ts deleted file mode 100644 index d5d1eba..0000000 --- a/src/utils/symbol.dispose.ts +++ /dev/null @@ -1,11 +0,0 @@ -// Polyfill to Symbol.dispose - -// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -if (!Symbol.dispose) { - Object.defineProperty(Symbol, "dispose", { - value: Symbol("dispose"), - writable: false, - enumerable: false, - configurable: false, - }); -}