Skip to content

Commit

Permalink
fix(_bsontype): only check bsontype if it is a prototype member.
Browse files Browse the repository at this point in the history
  • Loading branch information
daprahamian authored Nov 7, 2019
1 parent 3142508 commit dd8a349
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 65 deletions.
7 changes: 5 additions & 2 deletions lib/bson/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ function Binary(buffer, subType) {
throw new Error('only String, Buffer, Uint8Array or Array accepted');
}

this._bsontype = 'Binary';

if (buffer instanceof Number) {
this.sub_type = buffer;
this.position = 0;
Expand Down Expand Up @@ -81,6 +79,11 @@ function Binary(buffer, subType) {
}
}

Object.defineProperty(Binary.prototype, '_bsontype', {
value: 'Binary',
writable: false
});

/**
* Updates this binary with byte_value.
*
Expand Down
6 changes: 5 additions & 1 deletion lib/bson/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
*/
var Code = function Code(code, scope) {
if (!(this instanceof Code)) return new Code(code, scope);
this._bsontype = 'Code';
this.code = code;
this.scope = scope;
};

Object.defineProperty(Code.prototype, '_bsontype', {
value: 'Code',
writable: false
});

/**
* @ignore
*/
Expand Down
6 changes: 5 additions & 1 deletion lib/bson/db_ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
function DBRef(namespace, oid, db) {
if (!(this instanceof DBRef)) return new DBRef(namespace, oid, db);

this._bsontype = 'DBRef';
this.namespace = namespace;
this.oid = oid;
this.db = db;
}

Object.defineProperty(DBRef.prototype, '_bsontype', {
value: 'DBRef',
writable: false
});

/**
* @ignore
* @api private
Expand Down
6 changes: 5 additions & 1 deletion lib/bson/decimal128.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,14 @@ var lessThan = function(left, right) {
* @return {Double}
*/
var Decimal128 = function(bytes) {
this._bsontype = 'Decimal128';
this.bytes = bytes;
};

Object.defineProperty(Decimal128.prototype, '_bsontype', {
value: 'Decimal128',
writable: false
});

/**
* Create a Decimal128 instance from a string representation
*
Expand Down
6 changes: 5 additions & 1 deletion lib/bson/double.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
function Double(value) {
if (!(this instanceof Double)) return new Double(value);

this._bsontype = 'Double';
this.value = value;
}

Object.defineProperty(Double.prototype, '_bsontype', {
value: 'Double',
writable: false
});

/**
* Access the number value.
*
Expand Down
6 changes: 5 additions & 1 deletion lib/bson/int_32.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
var Int32 = function(value) {
if (!(this instanceof Int32)) return new Int32(value);

this._bsontype = 'Int32';
this.value = value;
};

Object.defineProperty(Int32.prototype, '_bsontype', {
value: 'Int32',
writable: false
});

/**
* Access the number value.
*
Expand Down
6 changes: 5 additions & 1 deletion lib/bson/long.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
function Long(low, high) {
if (!(this instanceof Long)) return new Long(low, high);

this._bsontype = 'Long';
/**
* @type {number}
* @ignore
Expand All @@ -57,6 +56,11 @@ function Long(low, high) {
this.high_ = high | 0; // force into 32 signed bits.
}

Object.defineProperty(Long.prototype, '_bsontype', {
value: 'Long',
writable: false
});

/**
* Return the int value.
*
Expand Down
7 changes: 5 additions & 2 deletions lib/bson/max_key.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
*/
function MaxKey() {
if (!(this instanceof MaxKey)) return new MaxKey();

this._bsontype = 'MaxKey';
}

Object.defineProperty(MaxKey.prototype, '_bsontype', {
value: 'MaxKey',
writable: false
});

module.exports = MaxKey;
module.exports.MaxKey = MaxKey;
7 changes: 5 additions & 2 deletions lib/bson/min_key.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
*/
function MinKey() {
if (!(this instanceof MinKey)) return new MinKey();

this._bsontype = 'MinKey';
}

Object.defineProperty(MinKey.prototype, '_bsontype', {
value: 'MinKey',
writable: false
});

module.exports = MinKey;
module.exports.MinKey = MinKey;
7 changes: 5 additions & 2 deletions lib/bson/objectid.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ var ObjectID = function ObjectID(id) {
if (id instanceof ObjectID) return id;
if (!(this instanceof ObjectID)) return new ObjectID(id);

this._bsontype = 'ObjectID';

// The most common usecase (blank id, new objectId instance)
if (id == null || typeof id === 'number') {
// Generate a new id
Expand Down Expand Up @@ -78,6 +76,11 @@ var ObjectID = function ObjectID(id) {
if (ObjectID.cacheHexString) this.__id = this.toString('hex');
};

Object.defineProperty(ObjectID.prototype, '_bsontype', {
value: 'ObjectID',
writable: false
});

// Allow usage of ObjectId as well as ObjectID
// var ObjectId = ObjectID;

Expand Down
27 changes: 15 additions & 12 deletions lib/bson/parser/calculate_size.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
if (value && value.toBSON) {
value = value.toBSON();
}
var _bsontype, _objPrototype;

switch (typeof value) {
case 'string':
Expand All @@ -84,15 +85,17 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
case 'boolean':
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (1 + 1);
case 'object':
var _objPrototype = value && Object.getPrototypeOf(value)
var _bsontype = _objPrototype && _objPrototype._bsontype || undefined;
if (
value == null ||
value instanceof MinKey ||
value instanceof MaxKey ||
value['_bsontype'] === 'MinKey' ||
value['_bsontype'] === 'MaxKey'
_bsontype === 'MinKey' ||
_bsontype === 'MaxKey'
) {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1;
} else if (value instanceof ObjectID || value['_bsontype'] === 'ObjectID' || value['_bsontype'] === 'ObjectId') {
} else if (value instanceof ObjectID || _bsontype === 'ObjectID' || _bsontype === 'ObjectId') {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (12 + 1);
} else if (value instanceof Date || isDate(value)) {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
Expand All @@ -104,14 +107,14 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
value instanceof Long ||
value instanceof Double ||
value instanceof Timestamp ||
value['_bsontype'] === 'Long' ||
value['_bsontype'] === 'Double' ||
value['_bsontype'] === 'Timestamp'
_bsontype === 'Long' ||
_bsontype === 'Double' ||
_bsontype === 'Timestamp'
) {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
} else if (value instanceof Decimal128 || value['_bsontype'] === 'Decimal128') {
} else if (value instanceof Decimal128 || _bsontype === 'Decimal128') {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (16 + 1);
} else if (value instanceof Code || value['_bsontype'] === 'Code') {
} else if (value instanceof Code || _bsontype === 'Code') {
// Calculate size depending on the availability of a scope
if (value.scope != null && Object.keys(value.scope).length > 0) {
return (
Expand All @@ -132,7 +135,7 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
1
);
}
} else if (value instanceof Binary || value['_bsontype'] === 'Binary') {
} else if (value instanceof Binary || _bsontype === 'Binary') {
// Check what kind of subtype we have
if (value.sub_type === Binary.SUBTYPE_BYTE_ARRAY) {
return (
Expand All @@ -144,15 +147,15 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (value.position + 1 + 4 + 1)
);
}
} else if (value instanceof Symbol || value['_bsontype'] === 'Symbol') {
} else if (value instanceof Symbol || _bsontype === 'Symbol') {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
Buffer.byteLength(value.value, 'utf8') +
4 +
1 +
1
);
} else if (value instanceof DBRef || value['_bsontype'] === 'DBRef') {
} else if (value instanceof DBRef || _bsontype === 'DBRef') {
// Set up correct object for serialization
var ordered_values = {
$ref: value.namespace,
Expand Down Expand Up @@ -183,7 +186,7 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
(value.multiline ? 1 : 0) +
1
);
} else if (value instanceof BSONRegExp || value['_bsontype'] === 'BSONRegExp') {
} else if (value instanceof BSONRegExp || _bsontype === 'BSONRegExp') {
return (
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
1 +
Expand Down
Loading

0 comments on commit dd8a349

Please sign in to comment.