From 243fc436e99959e48a3ffb3c54abfe0ef3fa9bc9 Mon Sep 17 00:00:00 2001 From: Alexander Floh Date: Mon, 11 Oct 2021 15:05:02 +0200 Subject: [PATCH 1/4] fix method declarations to conform with napi default for methods --- src/database.cc | 18 +++++++------- test/patching.test.js | 56 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 test/patching.test.js diff --git a/src/database.cc b/src/database.cc index c8d125cb9..deebab58c 100644 --- a/src/database.cc +++ b/src/database.cc @@ -13,16 +13,18 @@ Napi::FunctionReference Database::constructor; Napi::Object Database::Init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); + // declare napi_default_method here as it is only available in Node v14.12.0+ + napi_property_attributes napi_default_method = static_cast(napi_writable | napi_configurable); Napi::Function t = DefineClass(env, "Database", { - InstanceMethod("close", &Database::Close), - InstanceMethod("exec", &Database::Exec), - InstanceMethod("wait", &Database::Wait), - InstanceMethod("loadExtension", &Database::LoadExtension), - InstanceMethod("serialize", &Database::Serialize), - InstanceMethod("parallelize", &Database::Parallelize), - InstanceMethod("configure", &Database::Configure), - InstanceMethod("interrupt", &Database::Interrupt), + InstanceMethod("close", &Database::Close, napi_default_method), + InstanceMethod("exec", &Database::Exec, napi_default_method), + InstanceMethod("wait", &Database::Wait, napi_default_method), + InstanceMethod("loadExtension", &Database::LoadExtension, napi_default_method), + InstanceMethod("serialize", &Database::Serialize, napi_default_method), + InstanceMethod("parallelize", &Database::Parallelize, napi_default_method), + InstanceMethod("configure", &Database::Configure, napi_default_method), + InstanceMethod("interrupt", &Database::Interrupt, napi_default_method), InstanceAccessor("open", &Database::OpenGetter, nullptr) }); diff --git a/test/patching.test.js b/test/patching.test.js new file mode 100644 index 000000000..8b1f51e38 --- /dev/null +++ b/test/patching.test.js @@ -0,0 +1,56 @@ +var sqlite3 = require('..'); +var assert = require('assert'); + +describe('patching', function() { + var db; + var originalClose; + + it('allow patching native functions', function() { + var myFun = function myFunction() { + return "Success"; + } + originalClose = sqlite3.Database.prototype.close; + assert.doesNotThrow(() => { + sqlite3.Database.prototype.close = myFun; + }); + assert.doesNotThrow(() => { + sqlite3.Database.prototype.exec = myFun; + }); + assert.doesNotThrow(() => { + sqlite3.Database.prototype.wait = myFun; + }); + assert.doesNotThrow(() => { + sqlite3.Database.prototype.loadExtension = myFun; + }); + assert.doesNotThrow(() => { + sqlite3.Database.prototype.serialize = myFun; + }); + assert.doesNotThrow(() => { + sqlite3.Database.prototype.parallelize = myFun; + }); + assert.doesNotThrow(() => { + sqlite3.Database.prototype.configure = myFun; + }); + assert.doesNotThrow(() => { + sqlite3.Database.prototype.interrupt = myFun; + }); + + db = new sqlite3.Database(':memory:'); + assert.strictEqual(db.close(), "Success"); + assert.strictEqual(db.exec(), "Success"); + assert.strictEqual(db.wait(), "Success"); + assert.strictEqual(db.loadExtension(), "Success"); + assert.strictEqual(db.serialize(), "Success"); + assert.strictEqual(db.parallelize(), "Success"); + assert.strictEqual(db.configure(), "Success"); + assert.strictEqual(db.interrupt(), "Success"); + }); + + after(function() { + if(db != null) { + db.close = originalClose; + db.close(); + } + }); + +}); From 4269ca4c63dd8f5b7700b0a54b0b4d893af96e61 Mon Sep 17 00:00:00 2001 From: Alexander Floh Date: Tue, 12 Oct 2021 08:24:11 +0200 Subject: [PATCH 2/4] fixup! fix method declarations to conform with napi default for methods --- test/patching.test.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/test/patching.test.js b/test/patching.test.js index 8b1f51e38..bb4debf4f 100644 --- a/test/patching.test.js +++ b/test/patching.test.js @@ -3,13 +3,24 @@ var assert = require('assert'); describe('patching', function() { var db; - var originalClose; + var originalFunctions = {}; + + before(function() { + originalFunctions.close = sqlite3.Database.prototype.close; + originalFunctions.exec = sqlite3.Database.prototype.exec; + originalFunctions.wait = sqlite3.Database.prototype.wait; + originalFunctions.loadExtension = sqlite3.Database.prototype.loadExtension; + originalFunctions.serialize = sqlite3.Database.prototype.serialize; + originalFunctions.parallelize = sqlite3.Database.prototype.parallelize; + originalFunctions.configure = sqlite3.Database.prototype.configure; + originalFunctions.interrupt = sqlite3.Database.prototype.interrupt; + }); it('allow patching native functions', function() { var myFun = function myFunction() { return "Success"; } - originalClose = sqlite3.Database.prototype.close; + assert.doesNotThrow(() => { sqlite3.Database.prototype.close = myFun; }); @@ -48,7 +59,14 @@ describe('patching', function() { after(function() { if(db != null) { - db.close = originalClose; + sqlite3.Database.prototype.close = originalFunctions.close; + sqlite3.Database.prototype.exec = originalFunctions.exec; + sqlite3.Database.prototype.wait = originalFunctions.wait; + sqlite3.Database.prototype.loadExtension = originalFunctions.loadExtension; + sqlite3.Database.prototype.serialize = originalFunctions.serialize; + sqlite3.Database.prototype.parallelize = originalFunctions.parallelize; + sqlite3.Database.prototype.configure = originalFunctions.configure; + sqlite3.Database.prototype.interrupt = originalFunctions.interrupt; db.close(); } }); From b8b7913b0e726a4a693e16ff448a1baf7d54a1cb Mon Sep 17 00:00:00 2001 From: Alexander Floh Date: Thu, 8 Sep 2022 11:46:54 +0200 Subject: [PATCH 3/4] Follow-up to PR#1510: fix remaining method declarations. --- src/backup.cc | 7 +- src/statement.cc | 17 ++-- test/patching.test.js | 215 ++++++++++++++++++++++++++++++------------ 3 files changed, 172 insertions(+), 67 deletions(-) diff --git a/src/backup.cc b/src/backup.cc index 78570cddb..9f3893b96 100644 --- a/src/backup.cc +++ b/src/backup.cc @@ -10,9 +10,12 @@ using namespace node_sqlite3; Napi::Object Backup::Init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); + // declare napi_default_method here as it is only available in Node v14.12.0+ + napi_property_attributes napi_default_method = static_cast(napi_writable | napi_configurable); + Napi::Function t = DefineClass(env, "Backup", { - InstanceMethod("step", &Backup::Step), - InstanceMethod("finish", &Backup::Finish), + InstanceMethod("step", &Backup::Step, napi_default_method), + InstanceMethod("finish", &Backup::Finish, napi_default_method), InstanceAccessor("idle", &Backup::IdleGetter, nullptr), InstanceAccessor("completed", &Backup::CompletedGetter, nullptr), InstanceAccessor("failed", &Backup::FailedGetter, nullptr), diff --git a/src/statement.cc b/src/statement.cc index 1edd1fcd6..6adb123f3 100644 --- a/src/statement.cc +++ b/src/statement.cc @@ -11,14 +11,17 @@ using namespace node_sqlite3; Napi::Object Statement::Init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); + // declare napi_default_method here as it is only available in Node v14.12.0+ + napi_property_attributes napi_default_method = static_cast(napi_writable | napi_configurable); + Napi::Function t = DefineClass(env, "Statement", { - InstanceMethod("bind", &Statement::Bind), - InstanceMethod("get", &Statement::Get), - InstanceMethod("run", &Statement::Run), - InstanceMethod("all", &Statement::All), - InstanceMethod("each", &Statement::Each), - InstanceMethod("reset", &Statement::Reset), - InstanceMethod("finalize", &Statement::Finalize_), + InstanceMethod("bind", &Statement::Bind, napi_default_method), + InstanceMethod("get", &Statement::Get, napi_default_method), + InstanceMethod("run", &Statement::Run, napi_default_method), + InstanceMethod("all", &Statement::All, napi_default_method), + InstanceMethod("each", &Statement::Each, napi_default_method), + InstanceMethod("reset", &Statement::Reset, napi_default_method), + InstanceMethod("finalize", &Statement::Finalize_, napi_default_method), }); exports.Set("Statement", t); diff --git a/test/patching.test.js b/test/patching.test.js index bb4debf4f..b3a635977 100644 --- a/test/patching.test.js +++ b/test/patching.test.js @@ -2,73 +2,172 @@ var sqlite3 = require('..'); var assert = require('assert'); describe('patching', function() { - var db; - var originalFunctions = {}; + describe("Database", function() { + var db; + var originalFunctions = {}; - before(function() { - originalFunctions.close = sqlite3.Database.prototype.close; - originalFunctions.exec = sqlite3.Database.prototype.exec; - originalFunctions.wait = sqlite3.Database.prototype.wait; - originalFunctions.loadExtension = sqlite3.Database.prototype.loadExtension; - originalFunctions.serialize = sqlite3.Database.prototype.serialize; - originalFunctions.parallelize = sqlite3.Database.prototype.parallelize; - originalFunctions.configure = sqlite3.Database.prototype.configure; - originalFunctions.interrupt = sqlite3.Database.prototype.interrupt; - }); - - it('allow patching native functions', function() { - var myFun = function myFunction() { - return "Success"; - } - - assert.doesNotThrow(() => { - sqlite3.Database.prototype.close = myFun; - }); - assert.doesNotThrow(() => { - sqlite3.Database.prototype.exec = myFun; + before(function() { + originalFunctions.close = sqlite3.Database.prototype.close; + originalFunctions.exec = sqlite3.Database.prototype.exec; + originalFunctions.wait = sqlite3.Database.prototype.wait; + originalFunctions.loadExtension = sqlite3.Database.prototype.loadExtension; + originalFunctions.serialize = sqlite3.Database.prototype.serialize; + originalFunctions.parallelize = sqlite3.Database.prototype.parallelize; + originalFunctions.configure = sqlite3.Database.prototype.configure; + originalFunctions.interrupt = sqlite3.Database.prototype.interrupt; }); - assert.doesNotThrow(() => { - sqlite3.Database.prototype.wait = myFun; + + it('allow patching native functions', function() { + var myFun = function myFunction() { + return "Success"; + } + + assert.doesNotThrow(() => { + sqlite3.Database.prototype.close = myFun; + }); + assert.doesNotThrow(() => { + sqlite3.Database.prototype.exec = myFun; + }); + assert.doesNotThrow(() => { + sqlite3.Database.prototype.wait = myFun; + }); + assert.doesNotThrow(() => { + sqlite3.Database.prototype.loadExtension = myFun; + }); + assert.doesNotThrow(() => { + sqlite3.Database.prototype.serialize = myFun; + }); + assert.doesNotThrow(() => { + sqlite3.Database.prototype.parallelize = myFun; + }); + assert.doesNotThrow(() => { + sqlite3.Database.prototype.configure = myFun; + }); + assert.doesNotThrow(() => { + sqlite3.Database.prototype.interrupt = myFun; + }); + + db = new sqlite3.Database(':memory:'); + assert.strictEqual(db.close(), "Success"); + assert.strictEqual(db.exec(), "Success"); + assert.strictEqual(db.wait(), "Success"); + assert.strictEqual(db.loadExtension(), "Success"); + assert.strictEqual(db.serialize(), "Success"); + assert.strictEqual(db.parallelize(), "Success"); + assert.strictEqual(db.configure(), "Success"); + assert.strictEqual(db.interrupt(), "Success"); }); - assert.doesNotThrow(() => { - sqlite3.Database.prototype.loadExtension = myFun; + + after(function() { + if(db != null) { + sqlite3.Database.prototype.close = originalFunctions.close; + sqlite3.Database.prototype.exec = originalFunctions.exec; + sqlite3.Database.prototype.wait = originalFunctions.wait; + sqlite3.Database.prototype.loadExtension = originalFunctions.loadExtension; + sqlite3.Database.prototype.serialize = originalFunctions.serialize; + sqlite3.Database.prototype.parallelize = originalFunctions.parallelize; + sqlite3.Database.prototype.configure = originalFunctions.configure; + sqlite3.Database.prototype.interrupt = originalFunctions.interrupt; + db.close(); + } }); - assert.doesNotThrow(() => { - sqlite3.Database.prototype.serialize = myFun; + }); + + describe('Statement', function() { + var statement; + var originalFunctions = {}; + + before(function() { + originalFunctions.bind = sqlite3.Statement.prototype.bind; + originalFunctions.get = sqlite3.Statement.prototype.get; + originalFunctions.run = sqlite3.Statement.prototype.run; + originalFunctions.all = sqlite3.Statement.prototype.all; + originalFunctions.each = sqlite3.Statement.prototype.each; + originalFunctions.reset = sqlite3.Statement.prototype.reset; + originalFunctions.finalize = sqlite3.Statement.prototype.finalize; }); - assert.doesNotThrow(() => { - sqlite3.Database.prototype.parallelize = myFun; + + it('allow patching native functions', function() { + var myFun = function myFunction() { + return "Success"; + } + + assert.doesNotThrow(() => { + sqlite3.Statement.prototype.bind = myFun; + }); + assert.doesNotThrow(() => { + sqlite3.Statement.prototype.get = myFun; + }); + assert.doesNotThrow(() => { + sqlite3.Statement.prototype.run = myFun; + }); + assert.doesNotThrow(() => { + sqlite3.Statement.prototype.all = myFun; + }); + assert.doesNotThrow(() => { + sqlite3.Statement.prototype.each = myFun; + }); + assert.doesNotThrow(() => { + sqlite3.Statement.prototype.reset = myFun; + }); + assert.doesNotThrow(() => { + sqlite3.Statement.prototype.finalize = myFun; + }); + + statement = new sqlite3.Statement(); + assert.strictEqual(statement.bind(), "Success"); + assert.strictEqual(statement.get(), "Success"); + assert.strictEqual(statement.run(), "Success"); + assert.strictEqual(statement.all(), "Success"); + assert.strictEqual(statement.each(), "Success"); + assert.strictEqual(statement.reset(), "Success"); + assert.strictEqual(statement.finalize(), "Success"); }); - assert.doesNotThrow(() => { - sqlite3.Database.prototype.configure = myFun; + + after(function() { + if(statement != null) { + sqlite3.Statement.prototype.bind = originalFunctions.bind; + sqlite3.Statement.prototype.get = originalFunctions.get; + sqlite3.Statement.prototype.run = originalFunctions.run; + sqlite3.Statement.prototype.all = originalFunctions.all; + sqlite3.Statement.prototype.each = originalFunctions.each; + sqlite3.Statement.prototype.reset = originalFunctions.reset; + sqlite3.Statement.prototype.finalize = originalFunctions.finalize; + } }); - assert.doesNotThrow(() => { - sqlite3.Database.prototype.interrupt = myFun; + }); + + describe('Backup', function() { + var backup; + var originalFunctions = {}; + + before(function() { + originalFunctions.step = sqlite3.Backup.prototype.step; + originalFunctions.finish = sqlite3.Backup.prototype.finish; }); - db = new sqlite3.Database(':memory:'); - assert.strictEqual(db.close(), "Success"); - assert.strictEqual(db.exec(), "Success"); - assert.strictEqual(db.wait(), "Success"); - assert.strictEqual(db.loadExtension(), "Success"); - assert.strictEqual(db.serialize(), "Success"); - assert.strictEqual(db.parallelize(), "Success"); - assert.strictEqual(db.configure(), "Success"); - assert.strictEqual(db.interrupt(), "Success"); - }); + it('allow patching native functions', function() { + var myFun = function myFunction() { + return "Success"; + } + + assert.doesNotThrow(() => { + sqlite3.Backup.prototype.step = myFun; + }); + assert.doesNotThrow(() => { + sqlite3.Backup.prototype.finish = myFun; + }); - after(function() { - if(db != null) { - sqlite3.Database.prototype.close = originalFunctions.close; - sqlite3.Database.prototype.exec = originalFunctions.exec; - sqlite3.Database.prototype.wait = originalFunctions.wait; - sqlite3.Database.prototype.loadExtension = originalFunctions.loadExtension; - sqlite3.Database.prototype.serialize = originalFunctions.serialize; - sqlite3.Database.prototype.parallelize = originalFunctions.parallelize; - sqlite3.Database.prototype.configure = originalFunctions.configure; - sqlite3.Database.prototype.interrupt = originalFunctions.interrupt; - db.close(); - } - }); + backup = new sqlite3.Backup(); + assert.strictEqual(backup.step(), "Success"); + assert.strictEqual(backup.finish(), "Success"); + }); + after(function() { + if(statement != null) { + sqlite3.Backup.prototype.step = originalFunctions.step; + sqlite3.Backup.prototype.finish = originalFunctions.finish; + } + }); + }); }); From 313d05dc1e6b2e68c440ff69a1eda8bc5de49f9b Mon Sep 17 00:00:00 2001 From: Alexander Floh Date: Thu, 8 Sep 2022 12:58:19 +0200 Subject: [PATCH 4/4] fixup! Follow-up to PR#1510: fix remaining method declarations. --- test/patching.test.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/test/patching.test.js b/test/patching.test.js index b3a635977..3f9f68d59 100644 --- a/test/patching.test.js +++ b/test/patching.test.js @@ -74,6 +74,7 @@ describe('patching', function() { }); describe('Statement', function() { + var db; var statement; var originalFunctions = {}; @@ -114,7 +115,8 @@ describe('patching', function() { sqlite3.Statement.prototype.finalize = myFun; }); - statement = new sqlite3.Statement(); + db = new sqlite3.Database(':memory:'); + statement = db.prepare(""); assert.strictEqual(statement.bind(), "Success"); assert.strictEqual(statement.get(), "Success"); assert.strictEqual(statement.run(), "Success"); @@ -134,10 +136,14 @@ describe('patching', function() { sqlite3.Statement.prototype.reset = originalFunctions.reset; sqlite3.Statement.prototype.finalize = originalFunctions.finalize; } + if(db != null) { + db.close(); + } }); }); describe('Backup', function() { + var db; var backup; var originalFunctions = {}; @@ -158,15 +164,20 @@ describe('patching', function() { sqlite3.Backup.prototype.finish = myFun; }); - backup = new sqlite3.Backup(); + db = new sqlite3.Database(':memory:'); + backup = db.backup("somefile", myFun); assert.strictEqual(backup.step(), "Success"); assert.strictEqual(backup.finish(), "Success"); }); after(function() { - if(statement != null) { + if(backup != null) { sqlite3.Backup.prototype.step = originalFunctions.step; sqlite3.Backup.prototype.finish = originalFunctions.finish; + backup.finish(); + } + if(db != null) { + db.close(); } }); });