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(