Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Avoid mutating milestone snapshots when building from ops #651

Merged
merged 1 commit into from
May 14, 2024
Merged
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
2 changes: 1 addition & 1 deletion lib/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ Backend.prototype._fetchSnapshotByTimestamp = function(collection, id, timestamp
};

Backend.prototype._buildSnapshotFromOps = function(id, startingSnapshot, ops, callback) {
var snapshot = startingSnapshot || new Snapshot(id, 0, null, undefined, null);
var snapshot = util.clone(startingSnapshot) || new Snapshot(id, 0, null, undefined, null);
var error = ot.applyOps(snapshot, ops, {_normalizeLegacyJson0Ops: true});
callback(error, snapshot);
};
Expand Down
34 changes: 34 additions & 0 deletions test/client/snapshot-version-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var sinon = require('sinon');
var async = require('async');
var json0v2 = require('ot-json0-v2').type;
var types = require('../../lib/types');
var clone = require('../../lib/util').clone;

describe('SnapshotVersionRequest', function() {
var backend;
Expand Down Expand Up @@ -440,6 +441,39 @@ describe('SnapshotVersionRequest', function() {
], done);
});

it('does not mutate the milestone snapshot', function(done) {
var connection = backendWithMilestones.connect();
var doc = connection.get('books', 'mocking-bird');
var milestoneDb = backendWithMilestones.milestoneDb;
var milestone;

async.waterfall([
doc.create.bind(doc, {title: 'To Kill a Mocking Bird'}),
doc.submitOp.bind(doc, {p: ['author'], oi: 'Harper Lea'}),
doc.submitOp.bind(doc, {p: ['author'], od: 'Harper Lea', oi: 'Harper Lee'}),
milestoneDb.getMilestoneSnapshot.bind(milestoneDb, 'books', 'mocking-bird', 3),
function(snapshot, next) {
milestone = clone(snapshot);
next();
},
function(next) {
// Internally, this calls ot.applyOps(), which mutates the snapshot it's passed.
// This call shouldn't cause the in-memory milestone snapshot to be mutated.
connection.fetchSnapshot('books', 'mocking-bird', 3, next);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a comment indicating that this test case is making sure the client-facing connection.fetchSnapshot doesn't cause an in-place mutation to the stored milestone snapshot returned by the internal milestoneDb.getMilestoneSnapshot.

It took me a bit to realize where the mutation would formerly have occurred.

},
function(snapshot, next) {
expect(snapshot.v).to.equal(3);
expect(snapshot.data).to.eql({title: 'To Kill a Mocking Bird', author: 'Harper Lee'});
next();
},
milestoneDb.getMilestoneSnapshot.bind(milestoneDb, 'books', 'mocking-bird', 3),
function(snapshot, next) {
expect(snapshot).to.eql(milestone);
next();
}
], done);
});

describe('with version null', function() {
it('fetches a latest version', function(done) {
var doc = backendWithMilestones.connect().get('books', 'mocking-bird');
Expand Down
Loading