Skip to content
Open
Changes from 1 commit
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
Prev Previous commit
refactor: hoist int64/uint64 bounds to module constants
Compute the int64/uint64 range bounds once at module load instead of on
every encodeBigInt64 call, per review feedback.
  • Loading branch information
spokodev committed Jul 3, 2026
commit 1a99635b4fd32a4f1ebd5f1a124a9212826d2103
6 changes: 5 additions & 1 deletion src/Encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export type EncoderOptions<ContextType = undefined> = Partial<
> &
ContextOf<ContextType>;

// Bounds of the int64/uint64 range msgpack can represent, precomputed once for encodeBigInt64.
const INT64_MIN = -(BigInt(2) ** BigInt(63));
const UINT64_MAX = BigInt(2) ** BigInt(64) - BigInt(1);

export class Encoder<ContextType = undefined> {
private readonly extensionCodec: ExtensionCodecType<ContextType>;
private readonly context: ContextType;
Expand Down Expand Up @@ -291,7 +295,7 @@ export class Encoder<ContextType = undefined> {
}

private encodeBigInt64(object: bigint): void {
if (object < -(BigInt(2) ** BigInt(63)) || object > BigInt(2) ** BigInt(64) - BigInt(1)) {
if (object < INT64_MIN || object > UINT64_MAX) {
throw new Error(`Cannot encode BigInt as int64/uint64 because it is out of range: ${object}`);
}
if (object >= BigInt(0)) {
Expand Down