From 797fa46c1b94a5c39a6bf7dfdf565ee28ef48a4c Mon Sep 17 00:00:00 2001 From: Cayman Date: Mon, 27 Oct 2025 13:55:33 -0400 Subject: [PATCH] chore: #8562 cleanup (#8563) **Motivation** - #8562 cleanup **Description** - Several differences between the nodejs and bun functions exist and are squashed here - the function signatures: relaxed to return a Uint8Array - the upstream bun bytes<->int functions only support lengths up to 8, fallback to existing implementations if we need more length --- packages/db/src/util.ts | 2 +- packages/utils/src/bytes/browser.ts | 4 ++-- packages/utils/src/bytes/bun.ts | 32 ++++++++++++++++++++++------- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/packages/db/src/util.ts b/packages/db/src/util.ts index 8012565bcf..04d653bcbb 100644 --- a/packages/db/src/util.ts +++ b/packages/db/src/util.ts @@ -23,7 +23,7 @@ export function encodeKey(bucket: number, key: Uint8Array | string | number | bi buf.write(key, prefixLength); } else if (typeof key === "number" || typeof key === "bigint") { buf = Buffer.alloc(uintLen + prefixLength); - intToBytes(BigInt(key), uintLen, "be").copy(buf, prefixLength); + buf.set(intToBytes(key, uintLen, "be"), prefixLength); } else { buf = Buffer.alloc(key.length + prefixLength); buf.set(key, prefixLength); diff --git a/packages/utils/src/bytes/browser.ts b/packages/utils/src/bytes/browser.ts index 50c92b22e1..0b95fedb9c 100644 --- a/packages/utils/src/bytes/browser.ts +++ b/packages/utils/src/bytes/browser.ts @@ -144,7 +144,7 @@ export function toHexString(bytes: Uint8Array): string { /** * Return a byte array from a number or BigInt */ -export function intToBytes(value: bigint | number, length: number, endianness: Endianness = "le"): Buffer { +export function intToBytes(value: bigint | number, length: number, endianness: Endianness = "le"): Uint8Array { return bigIntToBytes(BigInt(value), length, endianness); } @@ -155,7 +155,7 @@ export function bytesToInt(value: Uint8Array, endianness: Endianness = "le"): nu return Number(bytesToBigInt(value, endianness)); } -export function bigIntToBytes(value: bigint, length: number, endianness: Endianness = "le"): Buffer { +export function bigIntToBytes(value: bigint, length: number, endianness: Endianness = "le"): Uint8Array { if (endianness === "le") { return toBufferLE(value, length); } diff --git a/packages/utils/src/bytes/bun.ts b/packages/utils/src/bytes/bun.ts index 44cf0662db..2158cb4c47 100644 --- a/packages/utils/src/bytes/bun.ts +++ b/packages/utils/src/bytes/bun.ts @@ -44,11 +44,29 @@ export function fromHexInto(hex: string, buffer: Uint8Array): void { export const toHexString = toHex; -export const { - intToBytes, - bytesToInt, - // naming differences from upstream - intToBytes: bigIntToBytes, - bytesToBigint: bytesToBigInt, -} = bytes; +import {bytesToBigInt as bBytesToBigInt, bytesToInt as bBytesToInt, intToBytes as bIntToBytes} from "./browser.ts"; + +export function intToBytes(value: number | bigint, byteLength: number, endianness: "be" | "le" = "le"): Uint8Array { + if (byteLength > 8) { + return bIntToBytes(value, byteLength, endianness); + } + return bytes.intToBytes(value, byteLength, endianness); +} + +export function bytesToInt(buf: Uint8Array, endianness: "be" | "le" = "le"): number { + if (buf.length > 8) { + return bBytesToInt(buf, endianness); + } + return bytes.bytesToInt(buf, endianness); +} + +export const bigIntToBytes = intToBytes; + +export function bytesToBigInt(buf: Uint8Array, endianness: "be" | "le" = "le"): bigint { + if (buf.length > 8) { + return bBytesToBigInt(buf, endianness); + } + return bytes.bytesToBigint(buf, endianness); +} + export {xor} from "./browser.ts";