Skip to content

Commit

Permalink
Revert "fix(_bsontype): only check bsontype if it is a prototype memb…
Browse files Browse the repository at this point in the history
…er."

This reverts commit dd8a349.
  • Loading branch information
daprahamian committed Nov 9, 2019
1 parent 179e1ed commit 8de4140
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 233 deletions.
7 changes: 2 additions & 5 deletions lib/bson/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ 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 @@ -79,11 +81,6 @@ function Binary(buffer, subType) {
}
}

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

/**
* Updates this binary with byte_value.
*
Expand Down
6 changes: 1 addition & 5 deletions lib/bson/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@
*/
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: 1 addition & 5 deletions lib/bson/db_ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@
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: 1 addition & 5 deletions lib/bson/decimal128.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,10 @@ 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: 1 addition & 5 deletions lib/bson/double.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@
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: 1 addition & 5 deletions lib/bson/int_32.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@
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: 1 addition & 5 deletions lib/bson/long.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
function Long(low, high) {
if (!(this instanceof Long)) return new Long(low, high);

this._bsontype = 'Long';
/**
* @type {number}
* @ignore
Expand All @@ -56,11 +57,6 @@ 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: 2 additions & 5 deletions lib/bson/max_key.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
*/
function MaxKey() {
if (!(this instanceof MaxKey)) return new MaxKey();
}

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

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

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

module.exports = MinKey;
module.exports.MinKey = MinKey;
7 changes: 2 additions & 5 deletions lib/bson/objectid.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ 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 @@ -76,11 +78,6 @@ 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: 12 additions & 15 deletions lib/bson/parser/calculate_size.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
if (value && value.toBSON) {
value = value.toBSON();
}
var _bsontype, _objPrototype;

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

0 comments on commit 8de4140

Please sign in to comment.