fix(ext/node): represent sqlite blob as Uint8Array (#27889)

This commit is contained in:
Divy Srivastava 2025-01-31 17:53:48 +05:30 committed by GitHub
parent 08cc22105f
commit 057f257052
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 5 deletions

View file

@ -155,10 +155,10 @@ impl StatementSync {
let size = ffi::sqlite3_column_bytes(self.inner, index);
let value =
std::slice::from_raw_parts(value as *const u8, size as usize);
let value =
v8::ArrayBuffer::new_backing_store_from_vec(value.to_vec())
.make_shared();
v8::ArrayBuffer::with_backing_store(scope, &value).into()
let bs = v8::ArrayBuffer::new_backing_store_from_vec(value.to_vec())
.make_shared();
let ab = v8::ArrayBuffer::with_backing_store(scope, &bs);
v8::Uint8Array::new(scope, ab, 0, size as _).unwrap().into()
}
ffi::SQLITE_NULL => v8::null(scope).into(),
_ => v8::undefined(scope).into(),

View file

@ -1,6 +1,6 @@
// Copyright 2018-2025 the Deno authors. MIT license.
import { DatabaseSync } from "node:sqlite";
import { assertEquals, assertThrows } from "@std/assert";
import { assert, assertEquals, assertThrows } from "@std/assert";
Deno.test("[node/sqlite] in-memory databases", () => {
const db1 = new DatabaseSync(":memory:");
@ -63,3 +63,12 @@ Deno.test("[node/sqlite] StatementSync read bigints are supported", () => {
stmt.setReadBigInts(true);
assertEquals(stmt.get(), { key: 1n, __proto__: null });
});
Deno.test("[node/sqlite] StatementSync blob are Uint8Array", () => {
const db = new DatabaseSync(":memory:");
const obj = db.prepare("select cast('test' as blob)").all();
assertEquals(obj.length, 1);
const row = obj[0] as Record<string, Uint8Array>;
assert(row["cast('test' as blob)"] instanceof Uint8Array);
});