Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix ObjectID#toString side effect

  • Loading branch information...
commit b7bcbe3929fe27b2f8309330408641db68c4d2a8 1 parent 9df94b6
Aaron Heckmann aheckmann authored
Showing with 55 additions and 14 deletions.
  1. +18 −12 lib/bson/objectid.js
  2. +37 −2 test/node/bson_test.js
30 lib/bson/objectid.js
View
@@ -24,9 +24,9 @@ var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$");
* @param {String|Number} id Can be a 24 byte hex string, 12 byte binary string or a Number.
* @return {Object} instance of ObjectID.
*/
-function ObjectID(id) {
- if(!(this instanceof ObjectID)) return new ObjectID(id);
-
+function ObjectID(id, _hex) {
+ if(!(this instanceof ObjectID)) return new ObjectID(id, _hex);
+
this._bsontype = 'ObjectID';
var self = this;
@@ -34,15 +34,17 @@ function ObjectID(id) {
if(id != null && 'number' != typeof id && (id.length != 12 && id.length != 24)) throw new Error("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters in hex format");
// Generate id based on the input
if(id == null || typeof id == 'number') {
+ // convert to 12 byte binary string
this.id = this.generate(id);
} else if(id != null && id.length === 12) {
+ // assume 12 byte string
this.id = id;
} else if(checkForHexRegExp.test(id)) {
return ObjectID.createFromHexString(id);
} else {
this.id = id;
}
-
+
/**
* Returns the generation time in seconds that this ID was generated.
*
@@ -60,8 +62,13 @@ function ObjectID(id) {
, set: function (value) {
var value = BinaryParser.encodeInt(value, 32, true, true);
this.id = value + this.id.substr(4);
+ delete this.__id;
+ this.toHexString();
}
- });
+ });
+
+ // internal hex representation for toHexString() speed
+ this.__id = _hex || this.toHexString();
};
/**
@@ -200,16 +207,15 @@ ObjectID.createPk = function createPk () {
/**
* Creates an ObjectID from a second based number, with the rest of the ObjectID zeroed out. Used for comparisons or sorting the ObjectID.
-*
+*
* @param {Number} time an integer number representing a number of seconds.
* @return {ObjectID} return the created ObjectID
* @api public
*/
-ObjectID.createFromTime = function createFromHexString(time) {
- var time4Bytes = BinaryParser.encodeInt(time, 32, true, true);
- var objectID = new ObjectID();
- objectID.id = BinaryParser.encodeInt(time, 32, true, true) + BinaryParser.encodeInt(0, 64, true, true)
- return objectID;
+ObjectID.createFromTime = function createFromTime (time) {
+ var id = BinaryParser.encodeInt(time, 32, true, true) +
+ BinaryParser.encodeInt(0, 64, true, true);
+ return new ObjectID(id);
};
/**
@@ -239,7 +245,7 @@ ObjectID.createFromHexString = function createFromHexString (hexString) {
result += BinaryParser.fromByte(number);
}
- return new ObjectID(result);
+ return new ObjectID(result, hexString);
};
/**
39 test/node/bson_test.js
View
@@ -439,7 +439,6 @@ exports['Should Correctly Serialize and Deserialize Oid'] = function(test) {
new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0);
assertBuffersEqual(test, serialized_data, serialized_data2, 0);
- delete doc.doc.__id;
test.deepEqual(doc, new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data));
test.done();
}
@@ -1195,6 +1194,42 @@ exports['ObjectID should correctly retrieve timestamp'] = function(test) {
/**
* @ignore
*/
+exports['ObjectID should have a correct cached representation of the hexString'] = function (test) {
+ var a = new ObjectID;
+ var __id = a.__id;
+ test.equal(__id, a.toHexString());
+
+ // hexString
+ a = new ObjectID(__id);
+ test.equal(__id, a.toHexString());
+
+ // fromHexString
+ a = ObjectID.createFromHexString(__id);
+ test.equal(a.__id, a.toHexString());
+ test.equal(__id, a.toHexString());
+
+ // number
+ var genTime = a.generationTime;
+ a = new ObjectID(genTime);
+ __id = a.__id;
+ test.equal(__id, a.toHexString());
+
+ // generationTime
+ delete a.__id;
+ a.generationTime = genTime;
+ test.equal(__id, a.toHexString());
+
+ // createFromTime
+ a = ObjectID.createFromTime(genTime);
+ __id = a.__id;
+ test.equal(__id, a.toHexString());
+
+ test.done();
+}
+
+/**
+ * @ignore
+ */
exports['Should Correctly throw error on bsonparser errors'] = function(test) {
var data = new Buffer(3);
var parser = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]);
@@ -1588,4 +1623,4 @@ exports.noGlobalsLeaked = function(test) {
var leaks = gleak.detectNew();
test.equal(0, leaks.length, "global var leak detected: " + leaks.join(', '));
test.done();
-}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.