From 2c094ac4e683008113811fab449d39956e6f07f4 Mon Sep 17 00:00:00 2001 From: DogerW <51895873+DogerW666@users.noreply.github.com> Date: Tue, 1 Dec 2026 18:24:19 +0800 Subject: [PATCH] fix(node): accept shared and cross-realm ArrayBuffers in table updates (#5325) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `Table.update({ values })` accepts `ArrayBufferLike` in TypeScript, but passing a `SharedArrayBuffer` to update a binary column throws `Unsupported value type: object value: ([object SharedArrayBuffer])`. An ordinary `ArrayBuffer` created in another Node.js VM context also fails the serializer's `instanceof ArrayBuffer` check. Use Node's `isAnyArrayBuffer` to recognize both kinds of buffer across contexts, then pass their bytes through the existing SQL binary literal encoder. The table regression writes `00 80 ff` from shared memory and reads the bytes back on each supported Arrow major (15–21). A separate serializer test covers an `ArrayBuffer` from another VM context. All eight cases failed before the fix. ### Validation - Node 22 on macOS arm64, using the upstream native build from `0d735c9`: both complete Jest files passed (701 passed, 3 skipped). - Node SDK Biome checks and strict TypeScript checks for the serializer and its unit tests passed. The full SDK type check reported the same ten diagnostics on the base commit and patched tree with this local setup (optional embedding packages and older native reranker declarations). --- nodejs/__test__/table.test.ts | 17 +++++++++++++++++ nodejs/__test__/util.test.ts | 8 ++++++++ nodejs/lancedb/util.ts | 4 +++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/nodejs/__test__/table.test.ts b/nodejs/__test__/table.test.ts index 9e5d74608..2b245ff27 100644 --- a/nodejs/__test__/table.test.ts +++ b/nodejs/__test__/table.test.ts @@ -501,6 +501,23 @@ describe.each(arrowVersions)( expect(await table.countRows("id == 10")).toBe(1); }); + it("updates a binary column from a SharedArrayBuffer", async () => { + const db = await connect(tmpDir.name); + const binaries = await db.createTable( + "binaries", + new arrow.Table({ + bytes: arrow.vectorFromArray([Buffer.from([1])], new arrow.Binary()), + }), + ); + const shared = new SharedArrayBuffer(3); + new Uint8Array(shared).set([0, 128, 255]); + + await binaries.update({ values: { bytes: shared } }); + + const [row] = await binaries.query().toArray(); + expect(Array.from(row.bytes)).toEqual([0, 128, 255]); + }); + it("should let me update float values to NaN and Infinity", async () => { const db = await connect(tmpDir.name); const floats = await db.createTable("floats", [ diff --git a/nodejs/__test__/util.test.ts b/nodejs/__test__/util.test.ts index d7e489f73..7a8d89d9a 100644 --- a/nodejs/__test__/util.test.ts +++ b/nodejs/__test__/util.test.ts @@ -1,6 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: Copyright The LanceDB Authors +import { runInNewContext } from "node:vm"; import { IntoSql, toSQL } from "../lancedb/util"; test.each([ ["string", "'string'"], @@ -21,6 +22,13 @@ test.each([ expect(toSQL(value)).toBe(expected); }); +test("toSQL serializes an ArrayBuffer from another realm", () => { + const buffer: ArrayBuffer = runInNewContext( + "Uint8Array.of(0, 128, 255).buffer", + ); + expect(toSQL(buffer)).toBe("X'0080ff'"); +}); + test("toSQL({}) throws on unsupported value type", () => { expect(() => toSQL({} as unknown as IntoSql)).toThrow( "Unsupported value type: object value: ([object Object])", diff --git a/nodejs/lancedb/util.ts b/nodejs/lancedb/util.ts index 5127a1c7f..67c78dc82 100644 --- a/nodejs/lancedb/util.ts +++ b/nodejs/lancedb/util.ts @@ -1,6 +1,8 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: Copyright The LanceDB Authors +import { isAnyArrayBuffer } from "node:util/types"; + export type IntoSql = | string | number @@ -34,7 +36,7 @@ export function toSQL(value: IntoSql): string { return `[${value.map(toSQL).join(", ")}]`; } else if (Buffer.isBuffer(value)) { return `X'${value.toString("hex")}'`; - } else if (value instanceof ArrayBuffer) { + } else if (isAnyArrayBuffer(value)) { return `X'${Buffer.from(value).toString("hex")}'`; } else { throw new Error(