Skip to content

Fix a bug that encode() and decode() are not re-entrant #257

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 3 commits into from
Feb 6, 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
60 changes: 59 additions & 1 deletion src/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ export class Decoder<ContextType = undefined> {
private headByte = HEAD_BYTE_REQUIRED;
private readonly stack = new StackPool();

private entered = false;

public constructor(options?: DecoderOptions<ContextType>) {
this.extensionCodec = options?.extensionCodec ?? (ExtensionCodec.defaultCodec as ExtensionCodecType<ContextType>);
this.context = (options as { context: ContextType } | undefined)?.context as ContextType; // needs a type assertion because EncoderOptions has no context property when ContextType is undefined
Expand All @@ -235,6 +237,22 @@ export class Decoder<ContextType = undefined> {
this.keyDecoder = options?.keyDecoder !== undefined ? options.keyDecoder : sharedCachedKeyDecoder;
}

private clone(): Decoder<ContextType> {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return new Decoder({
extensionCodec: this.extensionCodec,
context: this.context,
useBigInt64: this.useBigInt64,
rawStrings: this.rawStrings,
maxStrLength: this.maxStrLength,
maxBinLength: this.maxBinLength,
maxArrayLength: this.maxArrayLength,
maxMapLength: this.maxMapLength,
maxExtLength: this.maxExtLength,
keyDecoder: this.keyDecoder,
} as any);
}

private reinitializeState() {
this.totalPos = 0;
this.headByte = HEAD_BYTE_REQUIRED;
Expand Down Expand Up @@ -274,11 +292,27 @@ 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}
*/
public decode(buffer: ArrayLike<number> | ArrayBufferView | ArrayBufferLike): unknown {
if (this.entered) {
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);

Expand All @@ -290,6 +324,14 @@ export class Decoder<ContextType = undefined> {
}

public *decodeMulti(buffer: ArrayLike<number> | ArrayBufferView | ArrayBufferLike): Generator<unknown, void, unknown> {
if (this.entered) {
const instance = this.clone();
yield* instance.decodeMulti(buffer);
return;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
using _guard = this.enteringGuard();

this.reinitializeState();
this.setBuffer(buffer);

Expand All @@ -299,10 +341,18 @@ export class Decoder<ContextType = undefined> {
}

public async decodeAsync(stream: AsyncIterable<ArrayLike<number> | ArrayBufferView | ArrayBufferLike>): Promise<unknown> {
if (this.entered) {
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);
}

Expand Down Expand Up @@ -343,7 +393,15 @@ export class Decoder<ContextType = undefined> {
return this.decodeMultiAsync(stream, false);
}

private async *decodeMultiAsync(stream: AsyncIterable<ArrayLike<number> | ArrayBufferView | ArrayBufferLike>, isArray: boolean) {
private async *decodeMultiAsync(stream: AsyncIterable<ArrayLike<number> | ArrayBufferView | ArrayBufferLike>, isArray: boolean): AsyncGenerator<unknown, void, unknown> {
if (this.entered) {
const instance = this.clone();
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;

Expand Down
42 changes: 42 additions & 0 deletions src/Encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ export class Encoder<ContextType = undefined> {
private view: DataView;
private bytes: Uint8Array;

private entered = false;

public constructor(options?: EncoderOptions<ContextType>) {
this.extensionCodec = options?.extensionCodec ?? (ExtensionCodec.defaultCodec as ExtensionCodecType<ContextType>);
this.context = (options as { context: ContextType } | undefined)?.context as ContextType; // needs a type assertion because EncoderOptions has no context property when ContextType is undefined
Expand All @@ -103,16 +105,49 @@ export class Encoder<ContextType = undefined> {
this.bytes = new Uint8Array(this.view.buffer);
}

private clone() {
// Because of slightly special argument `context`,
// type assertion is needed.
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return new Encoder<ContextType>({
extensionCodec: this.extensionCodec,
context: this.context,
useBigInt64: this.useBigInt64,
maxDepth: this.maxDepth,
initialBufferSize: this.initialBufferSize,
sortKeys: this.sortKeys,
forceFloat32: this.forceFloat32,
ignoreUndefined: this.ignoreUndefined,
forceIntegerToFloat: this.forceIntegerToFloat,
} as any);
}

private reinitializeState() {
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}.
*
* @returns Encodes the object and returns a shared reference the encoder's internal buffer.
*/
public encodeSharedRef(object: unknown): Uint8Array {
if (this.entered) {
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);
Expand All @@ -122,6 +157,13 @@ export class Encoder<ContextType = undefined> {
* @returns Encodes the object and returns a copy of the encoder's internal buffer.
*/
public encode(object: unknown): Uint8Array {
if (this.entered) {
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);
Expand Down
48 changes: 48 additions & 0 deletions test/reuse-instances-with-extensions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// https://github.com/msgpack/msgpack-javascript/issues/195

import { deepStrictEqual } from "assert";
import { Encoder, Decoder, ExtensionCodec } from "../src/index";

const MSGPACK_EXT_TYPE_BIGINT = 0;

function registerCodecs(context: MsgPackContext) {
const { extensionCodec, encode, decode } = context;

extensionCodec.register({
type: MSGPACK_EXT_TYPE_BIGINT,
encode: (value) => (typeof value === "bigint" ? encode(value.toString()) : null),
decode: (data) => BigInt(decode(data) as string),
});
}

class MsgPackContext {
readonly encode: (value: unknown) => Uint8Array;
readonly decode: (buffer: BufferSource | ArrayLike<number>) => unknown;
readonly extensionCodec = new ExtensionCodec<MsgPackContext>();

constructor() {
const encoder = new Encoder({ extensionCodec: this.extensionCodec, context: this });
const decoder = new Decoder({ extensionCodec: this.extensionCodec, context: this });

this.encode = encoder.encode.bind(encoder);
this.decode = decoder.decode.bind(decoder);

registerCodecs(this);
}
}

describe("reuse instances with extensions", () => {
it("should encode and decode a bigint", () => {
const context = new MsgPackContext();
const buf = context.encode(BigInt(42));
const data = context.decode(buf);
deepStrictEqual(data, BigInt(42));
});

it("should encode and decode bigints", () => {
const context = new MsgPackContext();
const buf = context.encode([BigInt(1), BigInt(2), BigInt(3)]);
const data = context.decode(buf);
deepStrictEqual(data, [BigInt(1), BigInt(2), BigInt(3)]);
});
});
1 change: 1 addition & 0 deletions tsconfig.dist.es5+esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"compilerOptions": {
"target": "es5",
"module": "es2015",
"downlevelIteration": true,
"outDir": "./dist.es5+esm",
"declaration": false,
"noEmitOnError": true,
Expand Down