Skip to content

Commit

Permalink
Merge pull request #311 from share/remove-deprecated-middleware
Browse files Browse the repository at this point in the history
Remove deprecated middleware actions
  • Loading branch information
nateps authored Nov 20, 2019
2 parents 1a483b7 + 8c70c4c commit 14bfd34
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 175 deletions.
57 changes: 0 additions & 57 deletions lib/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,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 AFTER_SUBMIT_ACTION_DEPRECATION_WARNING = 'DEPRECATED: "after submit" and "afterSubmit" middleware actions. ' +
'Use "afterWrite" instead. Pass `disableSpaceDelimitedActions: true` option to ShareDB to ' +
'disable the "after submit" and "afterSubmit" actions and this warning.';

var ERROR_CODE = ShareDBError.CODES;

function Backend(options) {
Expand All @@ -48,24 +38,11 @@ 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);

Backend.prototype.MIDDLEWARE_ACTIONS = {
// DEPRECATED: Synonym for 'afterWrite'
afterSubmit: 'afterSubmit',
// DEPRECATED: Synonym for 'afterWrite'
'after submit': 'after submit',
// An operation was successfully written to the database.
afterWrite: 'afterWrite',
// An operation is about to be applied to a snapshot before being committed to the database
Expand All @@ -74,8 +51,6 @@ Backend.prototype.MIDDLEWARE_ACTIONS = {
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
Expand Down Expand Up @@ -103,38 +78,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 actions 'after submit' and 'afterSubmit' are now 'afterWrite'.
Backend.prototype._shimAfterSubmit = function() {
if (warnDeprecatedAfterSubmit) {
warnDeprecatedAfterSubmit = false;
console.warn(AFTER_SUBMIT_ACTION_DEPRECATION_WARNING);
}

var backend = this;
this.use(backend.MIDDLEWARE_ACTIONS.afterWrite, function(request, callback) {
backend.trigger(backend.MIDDLEWARE_ACTIONS['after submit'], request.agent, request, callback);
});
this.use(backend.MIDDLEWARE_ACTIONS.afterWrite, function(request, callback) {
backend.trigger(backend.MIDDLEWARE_ACTIONS['afterSubmit'], request.agent, request, callback);
});
};

Backend.prototype.close = function(callback) {
var wait = 4;
var backend = this;
Expand Down
4 changes: 2 additions & 2 deletions test/client/submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions test/client/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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').on('error', done);
Expand All @@ -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');
Expand Down
148 changes: 36 additions & 112 deletions test/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,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);
Expand Down Expand Up @@ -108,115 +142,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() {
Expand Down Expand Up @@ -288,8 +213,7 @@ describe('middleware', function() {
});

describe('submit lifecycle', function() {
// DEPRECATED: 'after submit' and 'afterSubmit' are synonyms for 'afterWrite'
['submit', 'apply', 'commit', 'afterWrite', 'afterSubmit', 'after submit'].forEach(function(action) {
['submit', 'apply', 'commit', 'afterWrite'].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) {
Expand Down

0 comments on commit 14bfd34

Please sign in to comment.