Skip to content

Commit 69295b3

Browse files
committedMar 3, 2025
Remove all mentions of Explicit Resource Management features
1 parent 4a337c5 commit 69295b3

File tree

3 files changed

+101
-108
lines changed

3 files changed

+101
-108
lines changed
 

‎src/Decoder.ts

+83-77
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import "./utils/symbol.dispose.ts";
21
import { prettyByte } from "./utils/prettyByte.ts";
32
import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec.ts";
43
import { 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

‎src/Encoder.ts

+18-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import "./utils/symbol.dispose.ts";
21
import { utf8Count, utf8Encode } from "./utils/utf8.ts";
32
import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec.ts";
43
import { setInt64, setUint64 } from "./utils/int.ts";
@@ -127,15 +126,6 @@ export class Encoder<ContextType = undefined> {
127126
this.pos = 0;
128127
}
129128

130-
private enteringGuard(): Disposable {
131-
this.entered = true;
132-
return {
133-
[Symbol.dispose]: () => {
134-
this.entered = false;
135-
},
136-
};
137-
}
138-
139129
/**
140130
* 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}.
141131
*
@@ -146,12 +136,16 @@ export class Encoder<ContextType = undefined> {
146136
const instance = this.clone();
147137
return instance.encodeSharedRef(object);
148138
}
149-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
150-
using _guard = this.enteringGuard();
151139

152-
this.reinitializeState();
153-
this.doEncode(object, 1);
154-
return this.bytes.subarray(0, this.pos);
140+
try {
141+
this.entered = true;
142+
143+
this.reinitializeState();
144+
this.doEncode(object, 1);
145+
return this.bytes.subarray(0, this.pos);
146+
} finally {
147+
this.entered = false;
148+
}
155149
}
156150

157151
/**
@@ -162,12 +156,16 @@ export class Encoder<ContextType = undefined> {
162156
const instance = this.clone();
163157
return instance.encode(object);
164158
}
165-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
166-
using _guard = this.enteringGuard();
167159

168-
this.reinitializeState();
169-
this.doEncode(object, 1);
170-
return this.bytes.slice(0, this.pos);
160+
try {
161+
this.entered = true;
162+
163+
this.reinitializeState();
164+
this.doEncode(object, 1);
165+
return this.bytes.slice(0, this.pos);
166+
} finally {
167+
this.entered = false;
168+
}
171169
}
172170

173171
private doEncode(object: unknown, depth: number): void {

‎src/utils/symbol.dispose.ts

-11
This file was deleted.

0 commit comments

Comments
 (0)
Failed to load comments.