Skip to content

Remove all mentions of Explicit Resource Management features #268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 83 additions & 77 deletions src/Decoder.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -305,15 +304,6 @@ export class Decoder<ContextType = undefined> {
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}
Expand All @@ -323,17 +313,21 @@ export class Decoder<ContextType = undefined> {
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<number> | ArrayBufferView | ArrayBufferLike): Generator<unknown, void, unknown> {
Expand All @@ -342,14 +336,18 @@ export class Decoder<ContextType = undefined> {
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;
}
}

Expand All @@ -358,42 +356,46 @@ export class Decoder<ContextType = undefined> {
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(
Expand All @@ -412,39 +414,43 @@ export class Decoder<ContextType = undefined> {
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;
}
}

Expand Down
38 changes: 18 additions & 20 deletions src/Encoder.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -127,15 +126,6 @@ export class Encoder<ContextType = undefined> {
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}.
*
Expand All @@ -146,12 +136,16 @@ export class Encoder<ContextType = undefined> {
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;
}
}

/**
Expand All @@ -162,12 +156,16 @@ export class Encoder<ContextType = undefined> {
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 {
Expand Down
11 changes: 0 additions & 11 deletions src/utils/symbol.dispose.ts

This file was deleted.