Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,10 @@ void DatabaseSync::Deserialize(const FunctionCallbackInfo<Value>& args) {
ASSIGN_OR_RETURN_UNWRAP(&db, args.This());
Environment* env = Environment::GetCurrent(args);
THROW_AND_RETURN_ON_BAD_STATE(env, !db->IsOpen(), "database is not open");
THROW_AND_RETURN_ON_BAD_STATE(
env,
db->IsInCallback(),
"database cannot be deserialized while in a callback");

if (!args[0]->IsUint8Array()) {
THROW_ERR_INVALID_ARG_TYPE(env->isolate(),
Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-sqlite-serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,23 @@ suite('DatabaseSync.prototype.serialize()', () => {
});

suite('DatabaseSync.prototype.deserialize()', () => {
test('cannot deserialize the database while in a callback', (t) => {
const source = new DatabaseSync(':memory:');
const serialized = source.serialize();
source.close();

const db = new DatabaseSync(':memory:');
t.after(() => db.close());
db.function('deserialize_database', () => db.deserialize(serialized));
const stmt = db.prepare('SELECT deserialize_database()');

t.assert.throws(() => stmt.get(), {
code: 'ERR_INVALID_STATE',
message: 'database cannot be deserialized while in a callback',
});
t.assert.strictEqual(db.isOpen, true);
});

test('loads a serialized database', (t) => {
const db1 = new DatabaseSync(':memory:');
db1.exec('CREATE TABLE t(id INTEGER PRIMARY KEY, name TEXT)');
Expand Down
Loading