Skip to content

Commit dd8a349

Browse files
authoredNov 7, 2019
fix(_bsontype): only check bsontype if it is a prototype member.
1 parent 3142508 commit dd8a349

16 files changed

+233
-65
lines changed
 

‎lib/bson/binary.js

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

43-
this._bsontype = 'Binary';
44-
4543
if (buffer instanceof Number) {
4644
this.sub_type = buffer;
4745
this.position = 0;
@@ -81,6 +79,11 @@ function Binary(buffer, subType) {
8179
}
8280
}
8381

82+
Object.defineProperty(Binary.prototype, '_bsontype', {
83+
value: 'Binary',
84+
writable: false
85+
});
86+
8487
/**
8588
* Updates this binary with byte_value.
8689
*

‎lib/bson/code.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
*/
99
var Code = function Code(code, scope) {
1010
if (!(this instanceof Code)) return new Code(code, scope);
11-
this._bsontype = 'Code';
1211
this.code = code;
1312
this.scope = scope;
1413
};
1514

15+
Object.defineProperty(Code.prototype, '_bsontype', {
16+
value: 'Code',
17+
writable: false
18+
});
19+
1620
/**
1721
* @ignore
1822
*/

‎lib/bson/db_ref.js

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

13-
this._bsontype = 'DBRef';
1413
this.namespace = namespace;
1514
this.oid = oid;
1615
this.db = db;
1716
}
1817

18+
Object.defineProperty(DBRef.prototype, '_bsontype', {
19+
value: 'DBRef',
20+
writable: false
21+
});
22+
1923
/**
2024
* @ignore
2125
* @api private

‎lib/bson/decimal128.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,14 @@ var lessThan = function(left, right) {
181181
* @return {Double}
182182
*/
183183
var Decimal128 = function(bytes) {
184-
this._bsontype = 'Decimal128';
185184
this.bytes = bytes;
186185
};
187186

187+
Object.defineProperty(Decimal128.prototype, '_bsontype', {
188+
value: 'Decimal128',
189+
writable: false
190+
});
191+
188192
/**
189193
* Create a Decimal128 instance from a string representation
190194
*

‎lib/bson/double.js

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

11-
this._bsontype = 'Double';
1211
this.value = value;
1312
}
1413

14+
Object.defineProperty(Double.prototype, '_bsontype', {
15+
value: 'Double',
16+
writable: false
17+
});
18+
1519
/**
1620
* Access the number value.
1721
*

‎lib/bson/int_32.js

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

11-
this._bsontype = 'Int32';
1211
this.value = value;
1312
};
1413

14+
Object.defineProperty(Int32.prototype, '_bsontype', {
15+
value: 'Int32',
16+
writable: false
17+
});
18+
1519
/**
1620
* Access the number value.
1721
*

‎lib/bson/long.js

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

46-
this._bsontype = 'Long';
4746
/**
4847
* @type {number}
4948
* @ignore
@@ -57,6 +56,11 @@ function Long(low, high) {
5756
this.high_ = high | 0; // force into 32 signed bits.
5857
}
5958

59+
Object.defineProperty(Long.prototype, '_bsontype', {
60+
value: 'Long',
61+
writable: false
62+
});
63+
6064
/**
6165
* Return the int value.
6266
*

‎lib/bson/max_key.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
*/
77
function MaxKey() {
88
if (!(this instanceof MaxKey)) return new MaxKey();
9-
10-
this._bsontype = 'MaxKey';
119
}
1210

11+
Object.defineProperty(MaxKey.prototype, '_bsontype', {
12+
value: 'MaxKey',
13+
writable: false
14+
});
15+
1316
module.exports = MaxKey;
1417
module.exports.MaxKey = MaxKey;

‎lib/bson/min_key.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
*/
77
function MinKey() {
88
if (!(this instanceof MinKey)) return new MinKey();
9-
10-
this._bsontype = 'MinKey';
119
}
1210

11+
Object.defineProperty(MinKey.prototype, '_bsontype', {
12+
value: 'MinKey',
13+
writable: false
14+
});
15+
1316
module.exports = MinKey;
1417
module.exports.MinKey = MinKey;

‎lib/bson/objectid.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ 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-
4442
// The most common usecase (blank id, new objectId instance)
4543
if (id == null || typeof id === 'number') {
4644
// Generate a new id
@@ -78,6 +76,11 @@ var ObjectID = function ObjectID(id) {
7876
if (ObjectID.cacheHexString) this.__id = this.toString('hex');
7977
};
8078

79+
Object.defineProperty(ObjectID.prototype, '_bsontype', {
80+
value: 'ObjectID',
81+
writable: false
82+
});
83+
8184
// Allow usage of ObjectId as well as ObjectID
8285
// var ObjectId = ObjectID;
8386

‎lib/bson/parser/calculate_size.js

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

6566
switch (typeof value) {
6667
case 'string':
@@ -84,15 +85,17 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
8485
case 'boolean':
8586
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (1 + 1);
8687
case 'object':
88+
var _objPrototype = value && Object.getPrototypeOf(value)
89+
var _bsontype = _objPrototype && _objPrototype._bsontype || undefined;
8790
if (
8891
value == null ||
8992
value instanceof MinKey ||
9093
value instanceof MaxKey ||
91-
value['_bsontype'] === 'MinKey' ||
92-
value['_bsontype'] === 'MaxKey'
94+
_bsontype === 'MinKey' ||
95+
_bsontype === 'MaxKey'
9396
) {
9497
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1;
95-
} else if (value instanceof ObjectID || value['_bsontype'] === 'ObjectID' || value['_bsontype'] === 'ObjectId') {
98+
} else if (value instanceof ObjectID || _bsontype === 'ObjectID' || _bsontype === 'ObjectId') {
9699
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (12 + 1);
97100
} else if (value instanceof Date || isDate(value)) {
98101
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
@@ -104,14 +107,14 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
104107
value instanceof Long ||
105108
value instanceof Double ||
106109
value instanceof Timestamp ||
107-
value['_bsontype'] === 'Long' ||
108-
value['_bsontype'] === 'Double' ||
109-
value['_bsontype'] === 'Timestamp'
110+
_bsontype === 'Long' ||
111+
_bsontype === 'Double' ||
112+
_bsontype === 'Timestamp'
110113
) {
111114
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
112-
} else if (value instanceof Decimal128 || value['_bsontype'] === 'Decimal128') {
115+
} else if (value instanceof Decimal128 || _bsontype === 'Decimal128') {
113116
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (16 + 1);
114-
} else if (value instanceof Code || value['_bsontype'] === 'Code') {
117+
} else if (value instanceof Code || _bsontype === 'Code') {
115118
// Calculate size depending on the availability of a scope
116119
if (value.scope != null && Object.keys(value.scope).length > 0) {
117120
return (
@@ -132,7 +135,7 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
132135
1
133136
);
134137
}
135-
} else if (value instanceof Binary || value['_bsontype'] === 'Binary') {
138+
} else if (value instanceof Binary || _bsontype === 'Binary') {
136139
// Check what kind of subtype we have
137140
if (value.sub_type === Binary.SUBTYPE_BYTE_ARRAY) {
138141
return (
@@ -144,15 +147,15 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
144147
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (value.position + 1 + 4 + 1)
145148
);
146149
}
147-
} else if (value instanceof Symbol || value['_bsontype'] === 'Symbol') {
150+
} else if (value instanceof Symbol || _bsontype === 'Symbol') {
148151
return (
149152
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
150153
Buffer.byteLength(value.value, 'utf8') +
151154
4 +
152155
1 +
153156
1
154157
);
155-
} else if (value instanceof DBRef || value['_bsontype'] === 'DBRef') {
158+
} else if (value instanceof DBRef || _bsontype === 'DBRef') {
156159
// Set up correct object for serialization
157160
var ordered_values = {
158161
$ref: value.namespace,
@@ -183,7 +186,7 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
183186
(value.multiline ? 1 : 0) +
184187
1
185188
);
186-
} else if (value instanceof BSONRegExp || value['_bsontype'] === 'BSONRegExp') {
189+
} else if (value instanceof BSONRegExp || _bsontype === 'BSONRegExp') {
187190
return (
188191
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
189192
1 +

‎lib/bson/parser/serializer.js

+51-36
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,9 @@ var serializeInto = function serializeInto(
705705
}
706706

707707
var type = typeof value;
708+
var _objPrototype = value && type === 'object' && Object.getPrototypeOf(value)
709+
var _bsontype = _objPrototype && _objPrototype._bsontype || undefined;
710+
708711
if (type === 'string') {
709712
index = serializeString(buffer, key, value, index, true);
710713
} else if (type === 'number') {
@@ -717,13 +720,13 @@ var serializeInto = function serializeInto(
717720
index = serializeNull(buffer, key, value, index, true);
718721
} else if (value === null) {
719722
index = serializeNull(buffer, key, value, index, true);
720-
} else if (value['_bsontype'] === 'ObjectID' || value['_bsontype'] === 'ObjectId') {
723+
} else if (_bsontype === 'ObjectID' || _bsontype === 'ObjectId') {
721724
index = serializeObjectId(buffer, key, value, index, true);
722725
} else if (Buffer.isBuffer(value)) {
723726
index = serializeBuffer(buffer, key, value, index, true);
724727
} else if (value instanceof RegExp || isRegExp(value)) {
725728
index = serializeRegExp(buffer, key, value, index, true);
726-
} else if (type === 'object' && value['_bsontype'] == null) {
729+
} else if (type === 'object' && _bsontype == null) {
727730
index = serializeObject(
728731
buffer,
729732
key,
@@ -736,11 +739,11 @@ var serializeInto = function serializeInto(
736739
true,
737740
path
738741
);
739-
} else if (type === 'object' && value['_bsontype'] === 'Decimal128') {
742+
} else if (type === 'object' && _bsontype === 'Decimal128') {
740743
index = serializeDecimal128(buffer, key, value, index, true);
741-
} else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') {
744+
} else if (_bsontype === 'Long' || _bsontype === 'Timestamp') {
742745
index = serializeLong(buffer, key, value, index, true);
743-
} else if (value['_bsontype'] === 'Double') {
746+
} else if (_bsontype === 'Double') {
744747
index = serializeDouble(buffer, key, value, index, true);
745748
} else if (typeof value === 'function' && serializeFunctions) {
746749
index = serializeFunction(
@@ -753,7 +756,7 @@ var serializeInto = function serializeInto(
753756
serializeFunctions,
754757
true
755758
);
756-
} else if (value['_bsontype'] === 'Code') {
759+
} else if (_bsontype === 'Code') {
757760
index = serializeCode(
758761
buffer,
759762
key,
@@ -765,18 +768,20 @@ var serializeInto = function serializeInto(
765768
ignoreUndefined,
766769
true
767770
);
768-
} else if (value['_bsontype'] === 'Binary') {
771+
} else if (_bsontype === 'Binary') {
769772
index = serializeBinary(buffer, key, value, index, true);
770-
} else if (value['_bsontype'] === 'Symbol') {
773+
} else if (_bsontype === 'Symbol') {
771774
index = serializeSymbol(buffer, key, value, index, true);
772-
} else if (value['_bsontype'] === 'DBRef') {
775+
} else if (_bsontype === 'DBRef') {
773776
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, true);
774-
} else if (value['_bsontype'] === 'BSONRegExp') {
777+
} else if (_bsontype === 'BSONRegExp') {
775778
index = serializeBSONRegExp(buffer, key, value, index, true);
776-
} else if (value['_bsontype'] === 'Int32') {
779+
} else if (_bsontype === 'Int32') {
777780
index = serializeInt32(buffer, key, value, index, true);
778-
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
781+
} else if (_bsontype === 'MinKey' || _bsontype === 'MaxKey') {
779782
index = serializeMinMax(buffer, key, value, index, true);
783+
} else if (typeof value['_bsontype'] !== 'undefined') {
784+
throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
780785
}
781786
}
782787
} else if (object instanceof Map) {
@@ -797,6 +802,9 @@ var serializeInto = function serializeInto(
797802
// Check the type of the value
798803
type = typeof value;
799804

805+
var _objPrototype = value && type === 'object' && Object.getPrototypeOf(value)
806+
var _bsontype = _objPrototype && _objPrototype._bsontype || undefined;
807+
800808
// Check the key and throw error if it's illegal
801809
if (typeof key === 'string' && ignoreKeys.indexOf(key) === -1) {
802810
if (key.match(regexp) != null) {
@@ -825,13 +833,13 @@ var serializeInto = function serializeInto(
825833
// } else if (value === undefined && ignoreUndefined === true) {
826834
} else if (value === null || (value === undefined && ignoreUndefined === false)) {
827835
index = serializeNull(buffer, key, value, index);
828-
} else if (value['_bsontype'] === 'ObjectID' || value['_bsontype'] === 'ObjectId') {
836+
} else if (_bsontype === 'ObjectID' || _bsontype === 'ObjectId') {
829837
index = serializeObjectId(buffer, key, value, index);
830838
} else if (Buffer.isBuffer(value)) {
831839
index = serializeBuffer(buffer, key, value, index);
832840
} else if (value instanceof RegExp || isRegExp(value)) {
833841
index = serializeRegExp(buffer, key, value, index);
834-
} else if (type === 'object' && value['_bsontype'] == null) {
842+
} else if (type === 'object' && _bsontype == null) {
835843
index = serializeObject(
836844
buffer,
837845
key,
@@ -844,13 +852,13 @@ var serializeInto = function serializeInto(
844852
false,
845853
path
846854
);
847-
} else if (type === 'object' && value['_bsontype'] === 'Decimal128') {
855+
} else if (type === 'object' && _bsontype === 'Decimal128') {
848856
index = serializeDecimal128(buffer, key, value, index);
849-
} else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') {
857+
} else if (_bsontype === 'Long' || _bsontype === 'Timestamp') {
850858
index = serializeLong(buffer, key, value, index);
851-
} else if (value['_bsontype'] === 'Double') {
859+
} else if (_bsontype === 'Double') {
852860
index = serializeDouble(buffer, key, value, index);
853-
} else if (value['_bsontype'] === 'Code') {
861+
} else if (_bsontype === 'Code') {
854862
index = serializeCode(
855863
buffer,
856864
key,
@@ -863,18 +871,20 @@ var serializeInto = function serializeInto(
863871
);
864872
} else if (typeof value === 'function' && serializeFunctions) {
865873
index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
866-
} else if (value['_bsontype'] === 'Binary') {
874+
} else if (_bsontype === 'Binary') {
867875
index = serializeBinary(buffer, key, value, index);
868-
} else if (value['_bsontype'] === 'Symbol') {
876+
} else if (_bsontype === 'Symbol') {
869877
index = serializeSymbol(buffer, key, value, index);
870-
} else if (value['_bsontype'] === 'DBRef') {
878+
} else if (_bsontype === 'DBRef') {
871879
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions);
872-
} else if (value['_bsontype'] === 'BSONRegExp') {
880+
} else if (_bsontype === 'BSONRegExp') {
873881
index = serializeBSONRegExp(buffer, key, value, index);
874-
} else if (value['_bsontype'] === 'Int32') {
882+
} else if (_bsontype === 'Int32') {
875883
index = serializeInt32(buffer, key, value, index);
876-
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
884+
} else if (_bsontype === 'MinKey' || _bsontype === 'MaxKey') {
877885
index = serializeMinMax(buffer, key, value, index);
886+
} else if (typeof value['_bsontype'] !== 'undefined') {
887+
throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
878888
}
879889
}
880890
} else {
@@ -898,6 +908,9 @@ var serializeInto = function serializeInto(
898908
// Check the type of the value
899909
type = typeof value;
900910

911+
var _objPrototype = value && type === 'object' && Object.getPrototypeOf(value)
912+
var _bsontype = _objPrototype && _objPrototype._bsontype || undefined;
913+
901914
// Check the key and throw error if it's illegal
902915
if (typeof key === 'string' && ignoreKeys.indexOf(key) === -1) {
903916
if (key.match(regexp) != null) {
@@ -927,13 +940,13 @@ var serializeInto = function serializeInto(
927940
if (ignoreUndefined === false) index = serializeNull(buffer, key, value, index);
928941
} else if (value === null) {
929942
index = serializeNull(buffer, key, value, index);
930-
} else if (value['_bsontype'] === 'ObjectID' || value['_bsontype'] === 'ObjectId') {
943+
} else if (_bsontype === 'ObjectID' || _bsontype === 'ObjectId') {
931944
index = serializeObjectId(buffer, key, value, index);
932945
} else if (Buffer.isBuffer(value)) {
933946
index = serializeBuffer(buffer, key, value, index);
934947
} else if (value instanceof RegExp || isRegExp(value)) {
935948
index = serializeRegExp(buffer, key, value, index);
936-
} else if (type === 'object' && value['_bsontype'] == null) {
949+
} else if (type === 'object' && _bsontype == null) {
937950
index = serializeObject(
938951
buffer,
939952
key,
@@ -946,13 +959,13 @@ var serializeInto = function serializeInto(
946959
false,
947960
path
948961
);
949-
} else if (type === 'object' && value['_bsontype'] === 'Decimal128') {
962+
} else if (type === 'object' && _bsontype === 'Decimal128') {
950963
index = serializeDecimal128(buffer, key, value, index);
951-
} else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') {
964+
} else if (_bsontype === 'Long' || _bsontype === 'Timestamp') {
952965
index = serializeLong(buffer, key, value, index);
953-
} else if (value['_bsontype'] === 'Double') {
966+
} else if (_bsontype === 'Double') {
954967
index = serializeDouble(buffer, key, value, index);
955-
} else if (value['_bsontype'] === 'Code') {
968+
} else if (_bsontype === 'Code') {
956969
index = serializeCode(
957970
buffer,
958971
key,
@@ -965,18 +978,20 @@ var serializeInto = function serializeInto(
965978
);
966979
} else if (typeof value === 'function' && serializeFunctions) {
967980
index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
968-
} else if (value['_bsontype'] === 'Binary') {
981+
} else if (_bsontype === 'Binary') {
969982
index = serializeBinary(buffer, key, value, index);
970-
} else if (value['_bsontype'] === 'Symbol') {
983+
} else if (_bsontype === 'Symbol') {
971984
index = serializeSymbol(buffer, key, value, index);
972-
} else if (value['_bsontype'] === 'DBRef') {
985+
} else if (_bsontype === 'DBRef') {
973986
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions);
974-
} else if (value['_bsontype'] === 'BSONRegExp') {
987+
} else if (_bsontype === 'BSONRegExp') {
975988
index = serializeBSONRegExp(buffer, key, value, index);
976-
} else if (value['_bsontype'] === 'Int32') {
989+
} else if (_bsontype === 'Int32') {
977990
index = serializeInt32(buffer, key, value, index);
978-
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
991+
} else if (_bsontype === 'MinKey' || _bsontype === 'MaxKey') {
979992
index = serializeMinMax(buffer, key, value, index);
993+
} else if (typeof value['_bsontype'] !== 'undefined') {
994+
throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
980995
}
981996
}
982997
}

‎lib/bson/regexp.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ function BSONRegExp(pattern, options) {
88
if (!(this instanceof BSONRegExp)) return new BSONRegExp();
99

1010
// Execute
11-
this._bsontype = 'BSONRegExp';
1211
this.pattern = pattern || '';
1312
this.options = options || '';
1413

@@ -29,5 +28,10 @@ function BSONRegExp(pattern, options) {
2928
}
3029
}
3130

31+
Object.defineProperty(BSONRegExp.prototype, '_bsontype', {
32+
value: 'BSONRegExp',
33+
writable: false
34+
});
35+
3236
module.exports = BSONRegExp;
3337
module.exports.BSONRegExp = BSONRegExp;

‎lib/bson/symbol.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ var inspect = Buffer ? require('util').inspect.custom || 'inspect' : 'inspect';
1111
*/
1212
function Symbol(value) {
1313
if (!(this instanceof Symbol)) return new Symbol(value);
14-
this._bsontype = 'Symbol';
1514
this.value = value;
1615
}
1716

17+
Object.defineProperty(Symbol.prototype, '_bsontype', {
18+
value: 'Symbol',
19+
writable: false
20+
});
21+
1822
/**
1923
* Access the wrapped string value.
2024
*

‎lib/bson/timestamp.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
*/
4545
function Timestamp(low, high) {
4646
if (!(this instanceof Timestamp)) return new Timestamp(low, high);
47-
this._bsontype = 'Timestamp';
4847
/**
4948
* @type {number}
5049
* @ignore
@@ -58,6 +57,11 @@ function Timestamp(low, high) {
5857
this.high_ = high | 0; // force into 32 signed bits.
5958
}
6059

60+
Object.defineProperty(Timestamp.prototype, '_bsontype', {
61+
value: 'Timestamp',
62+
writable: false
63+
});
64+
6165
/**
6266
* Return the int value.
6367
*

‎test/node/bsontype_test.js

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
'use strict';
2+
3+
var createBSON = require('../utils');
4+
var M = require('../../lib/bson/map');
5+
6+
function testSerializeAndDeserialize(test, expected) {
7+
var result;
8+
test.doesNotThrow(function() {
9+
var bson = createBSON();
10+
result = bson.deserialize(bson.serialize(expected));
11+
});
12+
test.deepEqual(result, expected);
13+
}
14+
15+
exports['Should throw on an unknown _bsontype'] = function(test) {
16+
function myBsonType () {
17+
this.x = 12;
18+
}
19+
myBsonType.prototype._bsontype = 'myBsonType';
20+
21+
test.throws(function() {
22+
createBSON().serialize({ a: new myBsonType() });
23+
});
24+
test.done();
25+
}
26+
27+
exports['Should ignore _bsontype that is not on the prototype'] = function(test) {
28+
testSerializeAndDeserialize(test, { a: { _bsontype: 'foo' } });
29+
test.done();
30+
}
31+
32+
exports['Should be able to handle serializing null and Object.create(null)'] = function(test) {
33+
testSerializeAndDeserialize(test, {
34+
a: null,
35+
b: Object.create(null)
36+
});
37+
test.done();
38+
}
39+
40+
exports['Should throw on an unknown _bsontype in an array'] = function(test) {
41+
function myBsonType () {
42+
this.x = 12;
43+
}
44+
myBsonType.prototype._bsontype = 'myBsonType';
45+
46+
test.throws(function() {
47+
createBSON().serialize({ a: [ new myBsonType() ] });
48+
});
49+
test.done();
50+
}
51+
52+
exports['Should ingore _bsontype that is not on the prototype in an array'] = function(test) {
53+
testSerializeAndDeserialize(test, { a: [{ _bsontype: 'foo' }] });
54+
test.done();
55+
}
56+
57+
exports['Should be able to handle serializing null and Object.create(null) in an array'] = function(test) {
58+
testSerializeAndDeserialize(test, {
59+
a: [
60+
null,
61+
Object.create(null)
62+
]
63+
});
64+
test.done();
65+
}
66+
67+
exports['Should throw on an unknown _bsontype in a Map'] = function(test) {
68+
function myBsonType () {
69+
this.x = 12;
70+
}
71+
myBsonType.prototype._bsontype = 'myBsonType';
72+
73+
test.throws(function() {
74+
createBSON().serialize({ a: new M([ ['x', new myBsonType()] ]) });
75+
});
76+
test.done();
77+
}
78+
79+
exports['Should ingore _bsontype that is not on the prototype in a Map'] = function(test) {
80+
var result;
81+
test.doesNotThrow(function() {
82+
var bson = createBSON();
83+
result = bson.deserialize(bson.serialize({ a: new M([[ '_bsontype', 'foo' ]]) }));
84+
});
85+
test.deepEqual(result, { a: { _bsontype: 'foo' } });
86+
test.done();
87+
}
88+
89+
exports['Should be able to handle serializing null and Object.create(null) in a Map'] = function(test) {
90+
var result;
91+
test.doesNotThrow(function() {
92+
var bson = createBSON();
93+
result = bson.deserialize(bson.serialize({
94+
a: new M([
95+
['a', null],
96+
['b', Object.create(null)]
97+
])
98+
}));
99+
});
100+
test.deepEqual(result, { a: { a: null, b: {} } });
101+
test.done();
102+
}

0 commit comments

Comments
 (0)
Please sign in to comment.