Skip to content

Commit

Permalink
Merge 324646e into dbb5748
Browse files Browse the repository at this point in the history
  • Loading branch information
alecgibson committed Aug 9, 2021
2 parents dbb5748 + 324646e commit fbc88f9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,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 error = ot.applyOps(snapshot, ops, {_normalizeJson0Paths: true});
var error = ot.applyOps(snapshot, ops, {_normalizeLegacyJson0Ops: true});
callback(error, snapshot);
};

Expand Down
8 changes: 5 additions & 3 deletions lib/ot.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ exports.applyOps = function(snapshot, ops, options) {
options = options || {};
for (var index = 0; index < ops.length; index++) {
var op = ops[index];
if (options._normalizeJson0Paths) normalizeJson0Paths(snapshot, op);
if (options._normalizeLegacyJson0Ops) normalizeLegacyJson0Ops(snapshot, op);
snapshot.v = op.v;
var error = exports.apply(snapshot, op);
if (error) return error;
Expand Down Expand Up @@ -208,12 +208,14 @@ exports.transformPresence = function(presence, op, isOwnOp) {
* have this stricter validation. This method fixes up the op paths to
* pass the stricter validation
*/
function normalizeJson0Paths(snapshot, json0Op) {
function normalizeLegacyJson0Ops(snapshot, json0Op) {
if (snapshot.type !== types.defaultType.uri) return;
var components = json0Op.op;
if (!components) return;
for (var i = 0; i < components.length; i++) {
var path = components[i].p;
var component = components[i];
if (typeof component.lm === 'string') component.lm = +component.lm;
var path = component.p;
var element = snapshot.data;
for (var j = 0; j < path.length; j++) {
var key = path[j];
Expand Down
28 changes: 19 additions & 9 deletions test/client/snapshot-version-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,16 +441,15 @@ describe('SnapshotVersionRequest', function() {
});
});

describe('invalid json0 path', function() {
describe('invalid json0v2 path', function() {
beforeEach(function(done) {
var doc = backend.connect().get('series', 'his-dark-materials');
doc.create([{title: 'Golden Compass'}], function(error) {
if (error) return done(error);
doc.submitOp({p: ['0', 'title'], od: 'Golden Compass', oi: 'Northern Lights'}, function(error) {
if (error) return done(error);
doc.submitOp({p: ['1'], li: {title: 'Subtle Knife'}}, done);
});
});
async.series([
doc.create.bind(doc, [{title: 'Golden Compass'}]),
doc.submitOp.bind(doc, {p: ['0', 'title'], od: 'Golden Compass', oi: 'Northern Lights'}),
doc.submitOp.bind(doc, {p: ['1'], li: {title: 'Subtle Knife'}}),
doc.submitOp.bind(doc, {p: ['1'], lm: '0'})
], done);
});

describe('json0v1', function() {
Expand All @@ -477,13 +476,24 @@ describe('SnapshotVersionRequest', function() {
types.register(defaultType);
});

it('fetches v2 with json0v2', function(done) {
it('fetches a string-indexed list insertion with json0v2', function(done) {
backend.connect().fetchSnapshot('series', 'his-dark-materials', 2, function(error, snapshot) {
if (error) return done(error);
expect(snapshot.data).to.eql([{title: 'Northern Lights'}]);
done();
});
});

it('fetches a list move using a string target', function(done) {
backend.connect().fetchSnapshot('series', 'his-dark-materials', 4, function(error, snapshot) {
if (error) return done(error);
expect(snapshot.data).to.eql([
{title: 'Subtle Knife'},
{title: 'Northern Lights'}
]);
done();
});
});
});
});
});

0 comments on commit fbc88f9

Please sign in to comment.