From 3dca62f005b8e585963c20643f3902982bdf9d92 Mon Sep 17 00:00:00 2001 From: funblaster22 <53224922+funblaster22@users.noreply.github.com> Date: Mon, 1 May 2023 14:19:00 -0500 Subject: [PATCH 1/2] (#8594) - plugin override built-in methods --- packages/node_modules/pouchdb-core/src/adapter.js | 8 ++++++++ tests/integration/test.basics.js | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/packages/node_modules/pouchdb-core/src/adapter.js b/packages/node_modules/pouchdb-core/src/adapter.js index 9f90ce8452..34b4c1e77a 100644 --- a/packages/node_modules/pouchdb-core/src/adapter.js +++ b/packages/node_modules/pouchdb-core/src/adapter.js @@ -197,6 +197,7 @@ function attachmentNameError(name) { class AbstractPouchDB extends EventEmitter { _setup() { + var pluginPrototype = Object.getPrototypeOf(this); this.post = adapterFun('post', function (doc, opts, callback) { if (typeof opts === 'function') { callback = opts; @@ -919,6 +920,13 @@ class AbstractPouchDB extends EventEmitter { Promise.all(deletedMap).then(destroyDb, callback); }); }).bind(this); + + // Re-add plugin methods that may have been overridden + for (var key in pluginPrototype) { + if (this[key] !== pluginPrototype[key]) { + this[key] = pluginPrototype[key]; + } + } } _compact(opts, callback) { diff --git a/tests/integration/test.basics.js b/tests/integration/test.basics.js index 55e6b89ba8..675679e4d7 100644 --- a/tests/integration/test.basics.js +++ b/tests/integration/test.basics.js @@ -1207,6 +1207,12 @@ adapters.forEach(function (adapter) { }); } + it('#8594 Plugins can override core methods', function () { + PouchDB.plugin({bulkDocs: function () {return true;}}); + var db = new PouchDB("test"); + db.bulkDocs().should.equal(true); + }); + if (typeof process !== 'undefined' && !process.browser) { it('#5471 PouchDB.plugin() should throw error if passed wrong type or empty object', function () { (function () { From c1a5e419ffd722ac4ed175b6419a792cb4e97f9a Mon Sep 17 00:00:00 2001 From: Bart Louwers Date: Mon, 28 Aug 2023 23:57:40 +0200 Subject: [PATCH 2/2] Implement test --- tests/integration/test.basics.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/tests/integration/test.basics.js b/tests/integration/test.basics.js index 37066a23c9..bccf060f51 100644 --- a/tests/integration/test.basics.js +++ b/tests/integration/test.basics.js @@ -1205,10 +1205,23 @@ adapters.forEach(function (adapter) { }); } - it('#8594 Plugins can override core methods', function () { - PouchDB.plugin({bulkDocs: function () {return true;}}); - var db = new PouchDB("test"); - db.bulkDocs().should.equal(true); + it('8594, PouchDB.plugin() can override core methods', function (done) { + const pouchBulkDocs = PouchDB.prototype.bulkDocs; // original bulkDocs + let called = false; + PouchDB.plugin({ // override + bulkDocs: function (docs, options, callback) { + called = true; + return pouchBulkDocs.call(this, docs, options, callback); + } + }); + const db = new PouchDB("test"); + // will error, but we don't care about the error + // just checking if called === true so that + // bulkDocs indeed got overridden + db.bulkDocs().catch(function () { + called.should.equal(true); + done(); + }); }); if (typeof process !== 'undefined' && !process.browser) {