Skip to content

Commit 8de4140

Browse files
committed
Revert "fix(_bsontype): only check bsontype if it is a prototype member."
This reverts commit dd8a349.
1 parent 179e1ed commit 8de4140

File tree

16 files changed

+65
-233
lines changed

16 files changed

+65
-233
lines changed

lib/bson/binary.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ function Binary(buffer, subType) {
4040
throw new Error('only String, Buffer, Uint8Array or Array accepted');
4141
}
4242

43+
this._bsontype = 'Binary';
44+
4345
if (buffer instanceof Number) {
4446
this.sub_type = buffer;
4547
this.position = 0;
@@ -79,11 +81,6 @@ function Binary(buffer, subType) {
7981
}
8082
}
8183

82-
Object.defineProperty(Binary.prototype, '_bsontype', {
83-
value: 'Binary',
84-
writable: false
85-
});
86-
8784
/**
8885
* Updates this binary with byte_value.
8986
*

lib/bson/code.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@
88
*/
99
var Code = function Code(code, scope) {
1010
if (!(this instanceof Code)) return new Code(code, scope);
11+
this._bsontype = 'Code';
1112
this.code = code;
1213
this.scope = scope;
1314
};
1415

15-
Object.defineProperty(Code.prototype, '_bsontype', {
16-
value: 'Code',
17-
writable: false
18-
});
19-
2016
/**
2117
* @ignore
2218
*/

lib/bson/db_ref.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@
1010
function DBRef(namespace, oid, db) {
1111
if (!(this instanceof DBRef)) return new DBRef(namespace, oid, db);
1212

13+
this._bsontype = 'DBRef';
1314
this.namespace = namespace;
1415
this.oid = oid;
1516
this.db = db;
1617
}
1718

18-
Object.defineProperty(DBRef.prototype, '_bsontype', {
19-
value: 'DBRef',
20-
writable: false
21-
});
22-
2319
/**
2420
* @ignore
2521
* @api private

lib/bson/decimal128.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,10 @@ var lessThan = function(left, right) {
181181
* @return {Double}
182182
*/
183183
var Decimal128 = function(bytes) {
184+
this._bsontype = 'Decimal128';
184185
this.bytes = bytes;
185186
};
186187

187-
Object.defineProperty(Decimal128.prototype, '_bsontype', {
188-
value: 'Decimal128',
189-
writable: false
190-
});
191-
192188
/**
193189
* Create a Decimal128 instance from a string representation
194190
*

lib/bson/double.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,10 @@
88
function Double(value) {
99
if (!(this instanceof Double)) return new Double(value);
1010

11+
this._bsontype = 'Double';
1112
this.value = value;
1213
}
1314

14-
Object.defineProperty(Double.prototype, '_bsontype', {
15-
value: 'Double',
16-
writable: false
17-
});
18-
1915
/**
2016
* Access the number value.
2117
*

lib/bson/int_32.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,10 @@
88
var Int32 = function(value) {
99
if (!(this instanceof Int32)) return new Int32(value);
1010

11+
this._bsontype = 'Int32';
1112
this.value = value;
1213
};
1314

14-
Object.defineProperty(Int32.prototype, '_bsontype', {
15-
value: 'Int32',
16-
writable: false
17-
});
18-
1915
/**
2016
* Access the number value.
2117
*

lib/bson/long.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
function Long(low, high) {
4444
if (!(this instanceof Long)) return new Long(low, high);
4545

46+
this._bsontype = 'Long';
4647
/**
4748
* @type {number}
4849
* @ignore
@@ -56,11 +57,6 @@ function Long(low, high) {
5657
this.high_ = high | 0; // force into 32 signed bits.
5758
}
5859

59-
Object.defineProperty(Long.prototype, '_bsontype', {
60-
value: 'Long',
61-
writable: false
62-
});
63-
6460
/**
6561
* Return the int value.
6662
*

lib/bson/max_key.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
*/
77
function MaxKey() {
88
if (!(this instanceof MaxKey)) return new MaxKey();
9-
}
109

11-
Object.defineProperty(MaxKey.prototype, '_bsontype', {
12-
value: 'MaxKey',
13-
writable: false
14-
});
10+
this._bsontype = 'MaxKey';
11+
}
1512

1613
module.exports = MaxKey;
1714
module.exports.MaxKey = MaxKey;

lib/bson/min_key.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
*/
77
function MinKey() {
88
if (!(this instanceof MinKey)) return new MinKey();
9-
}
109

11-
Object.defineProperty(MinKey.prototype, '_bsontype', {
12-
value: 'MinKey',
13-
writable: false
14-
});
10+
this._bsontype = 'MinKey';
11+
}
1512

1613
module.exports = MinKey;
1714
module.exports.MinKey = MinKey;

lib/bson/objectid.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ var ObjectID = function ObjectID(id) {
3939
if (id instanceof ObjectID) return id;
4040
if (!(this instanceof ObjectID)) return new ObjectID(id);
4141

42+
this._bsontype = 'ObjectID';
43+
4244
// The most common usecase (blank id, new objectId instance)
4345
if (id == null || typeof id === 'number') {
4446
// Generate a new id
@@ -76,11 +78,6 @@ var ObjectID = function ObjectID(id) {
7678
if (ObjectID.cacheHexString) this.__id = this.toString('hex');
7779
};
7880

79-
Object.defineProperty(ObjectID.prototype, '_bsontype', {
80-
value: 'ObjectID',
81-
writable: false
82-
});
83-
8481
// Allow usage of ObjectId as well as ObjectID
8582
// var ObjectId = ObjectID;
8683

lib/bson/parser/calculate_size.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
6161
if (value && value.toBSON) {
6262
value = value.toBSON();
6363
}
64-
var _bsontype, _objPrototype;
6564

6665
switch (typeof value) {
6766
case 'string':
@@ -85,17 +84,15 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
8584
case 'boolean':
8685
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (1 + 1);
8786
case 'object':
88-
var _objPrototype = value && Object.getPrototypeOf(value)
89-
var _bsontype = _objPrototype && _objPrototype._bsontype || undefined;
9087
if (
9188
value == null ||
9289
value instanceof MinKey ||
9390
value instanceof MaxKey ||
94-
_bsontype === 'MinKey' ||
95-
_bsontype === 'MaxKey'
91+
value['_bsontype'] === 'MinKey' ||
92+
value['_bsontype'] === 'MaxKey'
9693
) {
9794
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1;
98-
} else if (value instanceof ObjectID || _bsontype === 'ObjectID' || _bsontype === 'ObjectId') {
95+
} else if (value instanceof ObjectID || value['_bsontype'] === 'ObjectID' || value['_bsontype'] === 'ObjectId') {
9996
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (12 + 1);
10097
} else if (value instanceof Date || isDate(value)) {
10198
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
@@ -107,14 +104,14 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
107104
value instanceof Long ||
108105
value instanceof Double ||
109106
value instanceof Timestamp ||
110-
_bsontype === 'Long' ||
111-
_bsontype === 'Double' ||
112-
_bsontype === 'Timestamp'
107+
value['_bsontype'] === 'Long' ||
108+
value['_bsontype'] === 'Double' ||
109+
value['_bsontype'] === 'Timestamp'
113110
) {
114111
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
115-
} else if (value instanceof Decimal128 || _bsontype === 'Decimal128') {
112+
} else if (value instanceof Decimal128 || value['_bsontype'] === 'Decimal128') {
116113
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (16 + 1);
117-
} else if (value instanceof Code || _bsontype === 'Code') {
114+
} else if (value instanceof Code || value['_bsontype'] === 'Code') {
118115
// Calculate size depending on the availability of a scope
119116
if (value.scope != null && Object.keys(value.scope).length > 0) {
120117
return (
@@ -135,7 +132,7 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
135132
1
136133
);
137134
}
138-
} else if (value instanceof Binary || _bsontype === 'Binary') {
135+
} else if (value instanceof Binary || value['_bsontype'] === 'Binary') {
139136
// Check what kind of subtype we have
140137
if (value.sub_type === Binary.SUBTYPE_BYTE_ARRAY) {
141138
return (
@@ -147,15 +144,15 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
147144
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (value.position + 1 + 4 + 1)
148145
);
149146
}
150-
} else if (value instanceof Symbol || _bsontype === 'Symbol') {
147+
} else if (value instanceof Symbol || value['_bsontype'] === 'Symbol') {
151148
return (
152149
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
153150
Buffer.byteLength(value.value, 'utf8') +
154151
4 +
155152
1 +
156153
1
157154
);
158-
} else if (value instanceof DBRef || _bsontype === 'DBRef') {
155+
} else if (value instanceof DBRef || value['_bsontype'] === 'DBRef') {
159156
// Set up correct object for serialization
160157
var ordered_values = {
161158
$ref: value.namespace,
@@ -186,7 +183,7 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
186183
(value.multiline ? 1 : 0) +
187184
1
188185
);
189-
} else if (value instanceof BSONRegExp || _bsontype === 'BSONRegExp') {
186+
} else if (value instanceof BSONRegExp || value['_bsontype'] === 'BSONRegExp') {
190187
return (
191188
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
192189
1 +

0 commit comments

Comments
 (0)