From cd0bdecaecdbccb775d1721bc7115ca8517db086 Mon Sep 17 00:00:00 2001 From: David Glasser Date: Thu, 19 Sep 2013 10:59:11 -0700 Subject: [PATCH] keep prototype on timestamps when they are cloned (all tests pass) --- packages/mongo-livedata/mongo_driver.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/mongo-livedata/mongo_driver.js b/packages/mongo-livedata/mongo_driver.js index 2610ee4fc4..aca94e98dc 100644 --- a/packages/mongo-livedata/mongo_driver.js +++ b/packages/mongo-livedata/mongo_driver.js @@ -28,6 +28,14 @@ var replaceNames = function (filter, thing) { return thing; }; +// Ensure that EJSON.clone keeps a Timestamp as a Timestamp (instead of just +// doing a structural clone). +// XXX how ok is this? what if there are multiple copies of MongoDB loaded? +MongoDB.Timestamp.prototype.clone = function () { + // Timestamps should be immutable. + return this; +}; + var makeMongoLegal = function (name) { return "EJSON" + name; }; var unmakeMongoLegal = function (name) { return name.substr(5); }; @@ -42,6 +50,13 @@ var replaceMongoAtomWithMeteor = function (document) { if (document["EJSON$type"] && document["EJSON$value"]) { return EJSON.fromJSONValue(replaceNames(unmakeMongoLegal, document)); } + if (document instanceof MongoDB.Timestamp) { + // For now, the Meteor representation of a Mongo timestamp type (not a date! + // this is a weird internal thing used in the oplog!) is the same as the + // Mongo representation. We need to do this explicitly or else we would do a + // structural clone and lose the prototype. + return document; + } return undefined; }; @@ -54,7 +69,15 @@ var replaceMeteorAtomWithMongo = function (document) { } if (document instanceof Meteor.Collection.ObjectID) { return new MongoDB.ObjectID(document.toHexString()); - } else if (EJSON._isCustomType(document)) { + } + if (document instanceof MongoDB.Timestamp) { + // For now, the Meteor representation of a Mongo timestamp type (not a date! + // this is a weird internal thing used in the oplog!) is the same as the + // Mongo representation. We need to do this explicitly or else we would do a + // structural clone and lose the prototype. + return document; + } + if (EJSON._isCustomType(document)) { return replaceNames(makeMongoLegal, EJSON.toJSONValue(document)); } // It is not ordinarily possible to stick dollar-sign keys into mongo