From 2bd5694cd106f8609a3a2ec8fe1e7bc0dcf0d7f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9?= Date: Sat, 1 Feb 2025 21:19:54 +0000 Subject: [PATCH] sqlite: allow returning `ArrayBufferView`s from user-defined functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/56790 Reviewed-By: Michaƫl Zasso Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- doc/api/sqlite.md | 14 +++++++------- src/node_sqlite.cc | 2 +- test/parallel/test-sqlite-custom-functions.js | 8 +++++++- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/doc/api/sqlite.md b/doc/api/sqlite.md index 7b47e003b3a35c..cfd57f48922dfd 100644 --- a/doc/api/sqlite.md +++ b/doc/api/sqlite.md @@ -518,13 +518,13 @@ more data types than SQLite, only a subset of JavaScript types are supported. Attempting to write an unsupported data type to SQLite will result in an exception. -| SQLite | JavaScript | -| --------- | -------------------- | -| `NULL` | {null} | -| `INTEGER` | {number} or {bigint} | -| `REAL` | {number} | -| `TEXT` | {string} | -| `BLOB` | {Uint8Array} | +| SQLite | JavaScript | +| --------- | -------------------------- | +| `NULL` | {null} | +| `INTEGER` | {number} or {bigint} | +| `REAL` | {number} | +| `TEXT` | {string} | +| `BLOB` | {TypedArray} or {DataView} | ## `sqlite.constants` diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc index 0b8f7ef2a21763..2c830961e72817 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -213,7 +213,7 @@ class UserDefinedFunction { } else if (result->IsString()) { Utf8Value val(isolate, result.As()); sqlite3_result_text(ctx, *val, val.length(), SQLITE_TRANSIENT); - } else if (result->IsUint8Array()) { + } else if (result->IsArrayBufferView()) { ArrayBufferViewContents buf(result); sqlite3_result_blob(ctx, buf.data(), buf.length(), SQLITE_TRANSIENT); } else if (result->IsBigInt()) { diff --git a/test/parallel/test-sqlite-custom-functions.js b/test/parallel/test-sqlite-custom-functions.js index 591378ae827e18..2c854d47102f9b 100644 --- a/test/parallel/test-sqlite-custom-functions.js +++ b/test/parallel/test-sqlite-custom-functions.js @@ -274,13 +274,18 @@ suite('DatabaseSync.prototype.function()', () => { db.function('retString', () => { return 'foo'; }); db.function('retBigInt', () => { return 5n; }); db.function('retUint8Array', () => { return new Uint8Array([1, 2, 3]); }); + db.function('retArrayBufferView', () => { + const arrayBuffer = new Uint8Array([1, 2, 3]).buffer; + return new DataView(arrayBuffer); + }); const stmt = db.prepare(`SELECT retUndefined() AS retUndefined, retNull() AS retNull, retNumber() AS retNumber, retString() AS retString, retBigInt() AS retBigInt, - retUint8Array() AS retUint8Array + retUint8Array() AS retUint8Array, + retArrayBufferView() AS retArrayBufferView `); assert.deepStrictEqual(stmt.get(), { __proto__: null, @@ -290,6 +295,7 @@ suite('DatabaseSync.prototype.function()', () => { retString: 'foo', retBigInt: 5, retUint8Array: new Uint8Array([1, 2, 3]), + retArrayBufferView: new Uint8Array([1, 2, 3]), }); });