Skip to content

Commit 078808e

Browse files
Fully remove Deserialize
1 parent 75babc3 commit 078808e

File tree

4 files changed

+3
-62
lines changed

4 files changed

+3
-62
lines changed

index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ declare namespace BetterSqlite3 {
9898
}
9999

100100
interface DatabaseConstructor {
101-
new (filename: string | Buffer, options?: Database.Options): Database;
101+
new (filename: string, options?: Database.Options): Database;
102102
(filename: string, options?: Database.Options): Database;
103103
prototype: Database;
104104

lib/database.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ function Database(filenameGiven, options) {
1212
}
1313

1414
// Apply defaults
15-
let buffer;
16-
if (Buffer.isBuffer(filenameGiven)) {
17-
buffer = filenameGiven;
18-
filenameGiven = ':memory:';
19-
}
2015
if (filenameGiven == null) filenameGiven = '';
2116
if (options == null) options = {};
2217

@@ -36,7 +31,7 @@ function Database(filenameGiven, options) {
3631
const nativeBindingPath = 'nativeBinding' in options ? options.nativeBinding : null;
3732

3833
// Validate interpreted options
39-
if (readonly && anonymous && !buffer) throw new TypeError('In-memory/temporary databases cannot be readonly');
34+
if (readonly && anonymous) throw new TypeError('In-memory/temporary databases cannot be readonly');
4035
if (!Number.isInteger(timeout) || timeout < 0) throw new TypeError('Expected the "timeout" option to be a positive integer');
4136
if (timeout > 0x7fffffff) throw new RangeError('Option "timeout" cannot be greater than 2147483647');
4237
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) {
6156
}
6257

6358
Object.defineProperties(this, {
64-
[util.cppdb]: { value: new addon.Database(filename, filenameGiven, anonymous, readonly, fileMustExist, timeout, verbose || null, buffer || null) },
59+
[util.cppdb]: { value: new addon.Database(filename, filenameGiven, anonymous, readonly, fileMustExist, timeout, verbose || null) },
6560
...wrappers.getters,
6661
});
6762
}

src/better_sqlite3.cpp

-49
Original file line numberDiff line numberDiff line change
@@ -487,12 +487,6 @@ void Database::JS_new(v8::FunctionCallbackInfo<v8 ::Value> const& info) {
487487
"seventh"
488488
" argument");
489489
v8 ::Local<v8 ::Value> logger = info[6];
490-
if (info.Length() <= (7))
491-
return ThrowTypeError(
492-
"Expected a "
493-
"eighth"
494-
" argument");
495-
v8 ::Local<v8 ::Value> buffer = info[7];
496490

497491
Addon* addon = static_cast<Addon*>(info.Data().As<v8 ::External>()->Value());
498492
v8 ::Isolate* isolate = info.GetIsolate();
@@ -520,14 +514,6 @@ void Database::JS_new(v8::FunctionCallbackInfo<v8 ::Value> const& info) {
520514
int status = sqlite3_db_config(db_handle, SQLITE_DBCONFIG_DEFENSIVE, 1, NULL);
521515
assert(status == SQLITE_OK);
522516

523-
if (node::Buffer::HasInstance(buffer) &&
524-
!Deserialize(buffer.As<v8::Object>(), addon, db_handle, readonly)) {
525-
int status = sqlite3_close(db_handle);
526-
assert(status == SQLITE_OK);
527-
((void)status);
528-
return;
529-
}
530-
531517
v8 ::Local<v8 ::Context> ctx = isolate->GetCurrentContext();
532518
Database* db = new Database(isolate, addon, db_handle, logger);
533519
db->Wrap(info.This());
@@ -1013,41 +999,6 @@ void Database::JS_inTransaction(
1013999
info.GetReturnValue().Set(
10141000
db->open && !static_cast<bool>(sqlite3_get_autocommit(db->db_handle)));
10151001
}
1016-
bool Database::Deserialize(v8::Local<v8::Object> buffer,
1017-
Addon* addon,
1018-
sqlite3* db_handle,
1019-
bool readonly) {
1020-
size_t length = node::Buffer::Length(buffer);
1021-
unsigned char* data = (unsigned char*)sqlite3_malloc64(length);
1022-
unsigned int flags =
1023-
SQLITE_DESERIALIZE_FREEONCLOSE | SQLITE_DESERIALIZE_RESIZEABLE;
1024-
1025-
if (readonly) {
1026-
flags |= SQLITE_DESERIALIZE_READONLY;
1027-
}
1028-
if (length) {
1029-
if (!data) {
1030-
ThrowError("Out of memory");
1031-
return false;
1032-
}
1033-
memcpy(data, node::Buffer::Data(buffer), length);
1034-
}
1035-
1036-
int status =
1037-
sqlite3_deserialize(db_handle, "main", data, length, length, flags);
1038-
if (status != SQLITE_OK) {
1039-
ThrowSqliteError(addon,
1040-
status == SQLITE_ERROR ? "unable to deserialize database"
1041-
: sqlite3_errstr(status),
1042-
status);
1043-
return false;
1044-
}
1045-
1046-
return true;
1047-
}
1048-
void Database::FreeSerialization(char* data, void* _) {
1049-
sqlite3_free(data);
1050-
}
10511002
int const Database::MAX_BUFFER_SIZE;
10521003
int const Database::MAX_STRING_SIZE;
10531004
v8::Local<v8 ::Function> Statement::Init(v8::Isolate* isolate,

src/better_sqlite3.hpp

-5
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,6 @@ class Database : public node::ObjectWrap {
208208
static void JS_open(v8::FunctionCallbackInfo<v8 ::Value> const& info);
209209
static void JS_inTransaction(
210210
v8::FunctionCallbackInfo<v8 ::Value> const& info);
211-
static bool Deserialize(v8::Local<v8::Object> buffer,
212-
Addon* addon,
213-
sqlite3* db_handle,
214-
bool readonly);
215-
static void FreeSerialization(char* data, void* _);
216211
static int const MAX_BUFFER_SIZE =
217212
node::Buffer::kMaxLength > INT_MAX
218213
? INT_MAX

0 commit comments

Comments
 (0)