Fully remove Deserialize

This commit is contained in:
Fedor Indutny 2024-10-16 10:12:23 -07:00
parent 75babc345a
commit 078808e7c2
4 changed files with 3 additions and 62 deletions

2
index.d.ts vendored
View File

@ -98,7 +98,7 @@ declare namespace BetterSqlite3 {
}
interface DatabaseConstructor {
new (filename: string | Buffer, options?: Database.Options): Database;
new (filename: string, options?: Database.Options): Database;
(filename: string, options?: Database.Options): Database;
prototype: Database;

View File

@ -12,11 +12,6 @@ function Database(filenameGiven, options) {
}
// Apply defaults
let buffer;
if (Buffer.isBuffer(filenameGiven)) {
buffer = filenameGiven;
filenameGiven = ':memory:';
}
if (filenameGiven == null) filenameGiven = '';
if (options == null) options = {};
@ -36,7 +31,7 @@ function Database(filenameGiven, options) {
const nativeBindingPath = 'nativeBinding' in options ? options.nativeBinding : null;
// Validate interpreted options
if (readonly && anonymous && !buffer) throw new TypeError('In-memory/temporary databases cannot be readonly');
if (readonly && anonymous) throw new TypeError('In-memory/temporary databases cannot be readonly');
if (!Number.isInteger(timeout) || timeout < 0) throw new TypeError('Expected the "timeout" option to be a positive integer');
if (timeout > 0x7fffffff) throw new RangeError('Option "timeout" cannot be greater than 2147483647');
if (verbose != null && typeof verbose !== 'function') throw new TypeError('Expected the "verbose" option to be a function');
@ -61,7 +56,7 @@ function Database(filenameGiven, options) {
}
Object.defineProperties(this, {
[util.cppdb]: { value: new addon.Database(filename, filenameGiven, anonymous, readonly, fileMustExist, timeout, verbose || null, buffer || null) },
[util.cppdb]: { value: new addon.Database(filename, filenameGiven, anonymous, readonly, fileMustExist, timeout, verbose || null) },
...wrappers.getters,
});
}

View File

@ -487,12 +487,6 @@ void Database::JS_new(v8::FunctionCallbackInfo<v8 ::Value> const& info) {
"seventh"
" argument");
v8 ::Local<v8 ::Value> logger = info[6];
if (info.Length() <= (7))
return ThrowTypeError(
"Expected a "
"eighth"
" argument");
v8 ::Local<v8 ::Value> buffer = info[7];
Addon* addon = static_cast<Addon*>(info.Data().As<v8 ::External>()->Value());
v8 ::Isolate* isolate = info.GetIsolate();
@ -520,14 +514,6 @@ void Database::JS_new(v8::FunctionCallbackInfo<v8 ::Value> const& info) {
int status = sqlite3_db_config(db_handle, SQLITE_DBCONFIG_DEFENSIVE, 1, NULL);
assert(status == SQLITE_OK);
if (node::Buffer::HasInstance(buffer) &&
!Deserialize(buffer.As<v8::Object>(), addon, db_handle, readonly)) {
int status = sqlite3_close(db_handle);
assert(status == SQLITE_OK);
((void)status);
return;
}
v8 ::Local<v8 ::Context> ctx = isolate->GetCurrentContext();
Database* db = new Database(isolate, addon, db_handle, logger);
db->Wrap(info.This());
@ -1013,41 +999,6 @@ void Database::JS_inTransaction(
info.GetReturnValue().Set(
db->open && !static_cast<bool>(sqlite3_get_autocommit(db->db_handle)));
}
bool Database::Deserialize(v8::Local<v8::Object> buffer,
Addon* addon,
sqlite3* db_handle,
bool readonly) {
size_t length = node::Buffer::Length(buffer);
unsigned char* data = (unsigned char*)sqlite3_malloc64(length);
unsigned int flags =
SQLITE_DESERIALIZE_FREEONCLOSE | SQLITE_DESERIALIZE_RESIZEABLE;
if (readonly) {
flags |= SQLITE_DESERIALIZE_READONLY;
}
if (length) {
if (!data) {
ThrowError("Out of memory");
return false;
}
memcpy(data, node::Buffer::Data(buffer), length);
}
int status =
sqlite3_deserialize(db_handle, "main", data, length, length, flags);
if (status != SQLITE_OK) {
ThrowSqliteError(addon,
status == SQLITE_ERROR ? "unable to deserialize database"
: sqlite3_errstr(status),
status);
return false;
}
return true;
}
void Database::FreeSerialization(char* data, void* _) {
sqlite3_free(data);
}
int const Database::MAX_BUFFER_SIZE;
int const Database::MAX_STRING_SIZE;
v8::Local<v8 ::Function> Statement::Init(v8::Isolate* isolate,

View File

@ -208,11 +208,6 @@ class Database : public node::ObjectWrap {
static void JS_open(v8::FunctionCallbackInfo<v8 ::Value> const& info);
static void JS_inTransaction(
v8::FunctionCallbackInfo<v8 ::Value> const& info);
static bool Deserialize(v8::Local<v8::Object> buffer,
Addon* addon,
sqlite3* db_handle,
bool readonly);
static void FreeSerialization(char* data, void* _);
static int const MAX_BUFFER_SIZE =
node::Buffer::kMaxLength > INT_MAX
? INT_MAX