From d53ff82b64c2c931a5e31e590ebe2456e7b00025 Mon Sep 17 00:00:00 2001 From: Alec Gibson Date: Wed, 18 Sep 2019 18:02:47 +0100 Subject: [PATCH 1/2] Remove deprecated middleware actions We previously deprecated the [`doc`][1] and [`after submit`][2] middleware actions in favour of `readSnapshots` and `afterSubmit` respectively. In preparation for the breaking v1.0 release, this change removes these deprecated middlewares and their associated shims. [1]: https://github.com/share/sharedb/pull/209 [2]: https://github.com/share/sharedb/pull/212 --- lib/backend.js | 52 -------------- test/client/submit.js | 4 +- test/client/subscribe.js | 8 +-- test/middleware.js | 148 ++++++++++----------------------------- 4 files changed, 42 insertions(+), 170 deletions(-) diff --git a/lib/backend.js b/lib/backend.js index dc3b025ce..bdac897fa 100644 --- a/lib/backend.js +++ b/lib/backend.js @@ -12,16 +12,6 @@ var Snapshot = require('./snapshot'); var StreamSocket = require('./stream-socket'); var SubmitRequest = require('./submit-request'); -var warnDeprecatedDoc = true; -var warnDeprecatedAfterSubmit = true; - -var DOC_ACTION_DEPRECATION_WARNING = 'DEPRECATED: "doc" middleware action. Use "readSnapshots" instead. ' + - 'Pass `disableDocAction: true` option to ShareDB to disable the "doc" action and this warning.'; - -var AFFTER_SUBMIT_ACTION_DEPRECATION_WARNING = 'DEPRECATED: "after submit" middleware action. ' + - 'Use "afterSubmit" instead. Pass `disableSpaceDelimitedActions: true` option to ShareDB to ' + - 'disable the "after submit" action and this warning.'; - function Backend(options) { if (!(this instanceof Backend)) return new Backend(options); emitter.EventEmitter.call(this); @@ -45,15 +35,6 @@ function Backend(options) { // The number of open agents for monitoring and testing memory leaks this.agentsCount = 0; this.remoteAgentsCount = 0; - - // The below shims are for backwards compatibility. These options will be - // removed in a future major version - if (!options.disableDocAction) { - this._shimDocAction(); - } - if (!options.disableSpaceDelimitedActions) { - this._shimAfterSubmit(); - } } module.exports = Backend; emitter.mixin(Backend); @@ -61,16 +42,12 @@ emitter.mixin(Backend); Backend.prototype.MIDDLEWARE_ACTIONS = { // An operation was successfully submitted to the database. afterSubmit: 'afterSubmit', - // DEPRECATED: Synonym for 'afterSubmit' - 'after submit': 'after submit', // An operation is about to be applied to a snapshot before being committed to the database apply: 'apply', // An operation was applied to a snapshot; The operation and new snapshot are about to be written to the database. commit: 'commit', // A new client connected to the server. connect: 'connect', - // DEPRECATED: A snapshot was loaded from the database. - doc: 'doc', // An operation was loaded from the database op: 'op', // A query is about to be sent to the database @@ -98,35 +75,6 @@ Backend.prototype.SNAPSHOT_TYPES = { byTimestamp: 'byTimestamp' }; -Backend.prototype._shimDocAction = function() { - if (warnDeprecatedDoc) { - warnDeprecatedDoc = false; - console.warn(DOC_ACTION_DEPRECATION_WARNING); - } - - var backend = this; - this.use(this.MIDDLEWARE_ACTIONS.readSnapshots, function(request, callback) { - async.each(request.snapshots, function(snapshot, eachCb) { - var docRequest = {collection: request.collection, id: snapshot.id, snapshot: snapshot}; - backend.trigger(backend.MIDDLEWARE_ACTIONS.doc, request.agent, docRequest, eachCb); - }, callback); - }); -}; - -// Shim for backwards compatibility with deprecated middleware action name. -// The action 'after submit' is now 'afterSubmit'. -Backend.prototype._shimAfterSubmit = function() { - if (warnDeprecatedAfterSubmit) { - warnDeprecatedAfterSubmit = false; - console.warn(AFFTER_SUBMIT_ACTION_DEPRECATION_WARNING); - } - - var backend = this; - this.use(backend.MIDDLEWARE_ACTIONS.afterSubmit, function(request, callback) { - backend.trigger(backend.MIDDLEWARE_ACTIONS['after submit'], request.agent, request, callback); - }); -}; - Backend.prototype.close = function(callback) { var wait = 4; var backend = this; diff --git a/test/client/submit.js b/test/client/submit.js index 63a5a46ff..2e47a4442 100644 --- a/test/client/submit.js +++ b/test/client/submit.js @@ -768,7 +768,7 @@ module.exports = function() { it('snapshot fetch does not revert the version of deleted doc without pending ops', function(done) { var doc = this.backend.connect().get('dogs', 'fido'); - this.backend.use('doc', function(request, next) { + this.backend.use('readSnapshots', function(request, next) { doc.create({age: 3}); doc.del(next); }); @@ -781,7 +781,7 @@ module.exports = function() { it('snapshot fetch does not revert the version of deleted doc with pending ops', function(done) { var doc = this.backend.connect().get('dogs', 'fido'); - this.backend.use('doc', function(request, next) { + this.backend.use('readSnapshots', function(request, next) { doc.create({age: 3}, function(err) { if (err) return done(err); next(); diff --git a/test/client/subscribe.js b/test/client/subscribe.js index 2af398681..f1c3a63a2 100644 --- a/test/client/subscribe.js +++ b/test/client/subscribe.js @@ -257,8 +257,8 @@ module.exports = function() { }); }); - it(method + ' returns error passed to doc read middleware', function(done) { - this.backend.use('doc', function(request, next) { + it(method + ' returns error passed to readSnapshots middleware', function(done) { + this.backend.use('readSnapshots', function(request, next) { next({message: 'Reject doc read'}); }); var doc = this.backend.connect().get('dogs', 'fido'); @@ -274,8 +274,8 @@ module.exports = function() { }); }); - it(method + ' emits error passed to doc read middleware', function(done) { - this.backend.use('doc', function(request, next) { + it(method + ' emits error passed to readSnapshots middleware', function(done) { + this.backend.use('readSnapshots', function(request, next) { next({message: 'Reject doc read'}); }); var doc = this.backend.connect().get('dogs', 'fido'); diff --git a/test/middleware.js b/test/middleware.js index 85ceb686e..0da9f068c 100644 --- a/test/middleware.js +++ b/test/middleware.js @@ -54,7 +54,41 @@ describe('middleware', function() { }); }); - function testReadDoc(expectFidoOnly, expectFidoAndSpot) { + describe('readSnapshots', function() { + function expectFido(request) { + expect(request.collection).to.equal('dogs'); + expect(request.snapshots[0]).to.have.property('id', 'fido'); + expect(request.snapshots[0]).to.have.property('data').eql({age: 3}); + } + function expectSpot(request) { + expect(request.collection).to.equal('dogs'); + expect(request.snapshots[1]).to.have.property('id', 'spot'); + expect(request.snapshots[1]).to.have.property('type').equal(null); + } + + function expectFidoOnly(backend, done) { + var doneAfter = util.callAfter(1, done); + backend.use('readSnapshots', function(request, next) { + expect(request.snapshots).to.have.length(1); + expectFido(request); + doneAfter(); + next(); + }); + return doneAfter; + } + + function expectFidoAndSpot(backend, done) { + var doneAfter = util.callAfter(1, done); + backend.use('readSnapshots', function(request, next) { + expect(request.snapshots).to.have.length(2); + expectFido(request); + expectSpot(request); + doneAfter(); + next(); + }); + return doneAfter; + } + beforeEach('Add fido to db', function(done) { this.snapshot = {v: 1, type: 'json0', data: {age: 3}}; this.backend.db.commit('dogs', 'fido', {v: 0, create: {}}, this.snapshot, null, done); @@ -103,115 +137,6 @@ describe('middleware', function() { this.backend[bulkMethod]({}, 'dogs', ['fido', 'spot'], getErrorTest(done)); }); }); - } - - describe('doc', function() { - describe('with default options for backend constructor', function() { - function expectFido(request) { - expect(request.collection).to.equal('dogs'); - expect(request.id).to.equal('fido'); - expect(request.snapshot).to.have.property('data').eql({age: 3}); - } - function expectSpot(request) { - expect(request.collection).to.equal('dogs'); - expect(request.id).to.equal('spot'); - expect(request.snapshot).to.have.property('type').equal(null); - } - - function expectFidoOnly(backend, done) { - var doneAfter = util.callAfter(1, done); - backend.use('doc', function(request, next) { - expectFido(request); - doneAfter(); - next(); - }); - return doneAfter; - } - - function expectFidoAndSpot(backend, done) { - var doneAfter = util.callAfter(2, done); - backend.use('doc', function(request, next) { - doneAfter(); - if (doneAfter.called === 1) { - expectFido(request); - } else { - expectSpot(request); - } - next(); - }); - return doneAfter; - } - - testReadDoc(expectFidoOnly, expectFidoAndSpot); - }); - - describe('with disableDocAction option set to true for backend constructor', function() { - beforeEach('Create backend with disableDocAction option', function() { - this.backend = new Backend({disableDocAction: true}); - }); - - it('is not triggered when a document is retrieved with fetch', function(done) { - this.backend.use('doc', passError); - this.backend.fetch({}, 'dogs', 'fido', done); - }); - - it('is not triggered when a document is retrieved with subscribe', function(done) { - this.backend.use('doc', passError); - this.backend.subscribe({}, 'dogs', 'fido', null, done); - }); - - ['queryFetch', 'querySubscribe'].forEach(function(queryMethod) { - it('is not triggered when multiple documents are retrieved with ' + queryMethod, function(done) { - this.backend.use('doc', passError); - this.backend[queryMethod]({}, 'dogs', {age: 3}, {}, done); - }); - }); - - ['fetchBulk', 'subscribeBulk'].forEach(function(bulkMethod) { - it('is not triggered when a document is retrieved with ' + bulkMethod, function(done) { - this.backend.use('doc', passError); - this.backend[bulkMethod]({}, 'dogs', ['fido', 'spot'], done); - }); - }); - }); - }); - - describe('readSnapshots', function() { - function expectFido(request) { - expect(request.collection).to.equal('dogs'); - expect(request.snapshots[0]).to.have.property('id', 'fido'); - expect(request.snapshots[0]).to.have.property('data').eql({age: 3}); - } - function expectSpot(request) { - expect(request.collection).to.equal('dogs'); - expect(request.snapshots[1]).to.have.property('id', 'spot'); - expect(request.snapshots[1]).to.have.property('type').equal(null); - } - - function expectFidoOnly(backend, done) { - var doneAfter = util.callAfter(1, done); - backend.use('readSnapshots', function(request, next) { - expect(request.snapshots).to.have.length(1); - expectFido(request); - doneAfter(); - next(); - }); - return doneAfter; - } - - function expectFidoAndSpot(backend, done) { - var doneAfter = util.callAfter(1, done); - backend.use('readSnapshots', function(request, next) { - expect(request.snapshots).to.have.length(2); - expectFido(request); - expectSpot(request); - doneAfter(); - next(); - }); - return doneAfter; - } - - testReadDoc(expectFidoOnly, expectFidoAndSpot); }); describe('reply', function() { @@ -283,8 +208,7 @@ describe('middleware', function() { }); describe('submit lifecycle', function() { - // DEPRECATED: 'after submit' is a synonym for 'afterSubmit' - ['submit', 'apply', 'commit', 'afterSubmit', 'after submit'].forEach(function(action) { + ['submit', 'apply', 'commit', 'afterSubmit'].forEach(function(action) { it(action + ' gets options passed to backend.submit', function(done) { var doneAfter = util.callAfter(1, done); this.backend.use(action, function(request, next) { From 8c70c4ce82d40c5af5e5b6d766f60bc62407930b Mon Sep 17 00:00:00 2001 From: Alec Gibson Date: Thu, 21 Nov 2019 06:40:42 +1300 Subject: [PATCH 2/2] Fix bad merge --- lib/backend.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/backend.js b/lib/backend.js index cc301c79e..32f24824c 100644 --- a/lib/backend.js +++ b/lib/backend.js @@ -13,6 +13,8 @@ var Snapshot = require('./snapshot'); var StreamSocket = require('./stream-socket'); var SubmitRequest = require('./submit-request'); +var ERROR_CODE = ShareDBError.CODES; + function Backend(options) { if (!(this instanceof Backend)) return new Backend(options); emitter.EventEmitter.call(this);