@@ -61,6 +61,7 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
61
61
if ( value && value . toBSON ) {
62
62
value = value . toBSON ( ) ;
63
63
}
64
+ var _bsontype , _objPrototype ;
64
65
65
66
switch ( typeof value ) {
66
67
case 'string' :
@@ -84,15 +85,17 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
84
85
case 'boolean' :
85
86
return ( name != null ? Buffer . byteLength ( name , 'utf8' ) + 1 : 0 ) + ( 1 + 1 ) ;
86
87
case 'object' :
88
+ var _objPrototype = value && Object . getPrototypeOf ( value )
89
+ var _bsontype = _objPrototype && _objPrototype . _bsontype || undefined ;
87
90
if (
88
91
value == null ||
89
92
value instanceof MinKey ||
90
93
value instanceof MaxKey ||
91
- value [ ' _bsontype' ] === 'MinKey' ||
92
- value [ ' _bsontype' ] === 'MaxKey'
94
+ _bsontype === 'MinKey' ||
95
+ _bsontype === 'MaxKey'
93
96
) {
94
97
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' ) {
96
99
return ( name != null ? Buffer . byteLength ( name , 'utf8' ) + 1 : 0 ) + ( 12 + 1 ) ;
97
100
} else if ( value instanceof Date || isDate ( value ) ) {
98
101
return ( name != null ? Buffer . byteLength ( name , 'utf8' ) + 1 : 0 ) + ( 8 + 1 ) ;
@@ -104,14 +107,14 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
104
107
value instanceof Long ||
105
108
value instanceof Double ||
106
109
value instanceof Timestamp ||
107
- value [ ' _bsontype' ] === 'Long' ||
108
- value [ ' _bsontype' ] === 'Double' ||
109
- value [ ' _bsontype' ] === 'Timestamp'
110
+ _bsontype === 'Long' ||
111
+ _bsontype === 'Double' ||
112
+ _bsontype === 'Timestamp'
110
113
) {
111
114
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' ) {
113
116
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' ) {
115
118
// Calculate size depending on the availability of a scope
116
119
if ( value . scope != null && Object . keys ( value . scope ) . length > 0 ) {
117
120
return (
@@ -132,7 +135,7 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
132
135
1
133
136
) ;
134
137
}
135
- } else if ( value instanceof Binary || value [ ' _bsontype' ] === 'Binary' ) {
138
+ } else if ( value instanceof Binary || _bsontype === 'Binary' ) {
136
139
// Check what kind of subtype we have
137
140
if ( value . sub_type === Binary . SUBTYPE_BYTE_ARRAY ) {
138
141
return (
@@ -144,15 +147,15 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
144
147
( name != null ? Buffer . byteLength ( name , 'utf8' ) + 1 : 0 ) + ( value . position + 1 + 4 + 1 )
145
148
) ;
146
149
}
147
- } else if ( value instanceof Symbol || value [ ' _bsontype' ] === 'Symbol' ) {
150
+ } else if ( value instanceof Symbol || _bsontype === 'Symbol' ) {
148
151
return (
149
152
( name != null ? Buffer . byteLength ( name , 'utf8' ) + 1 : 0 ) +
150
153
Buffer . byteLength ( value . value , 'utf8' ) +
151
154
4 +
152
155
1 +
153
156
1
154
157
) ;
155
- } else if ( value instanceof DBRef || value [ ' _bsontype' ] === 'DBRef' ) {
158
+ } else if ( value instanceof DBRef || _bsontype === 'DBRef' ) {
156
159
// Set up correct object for serialization
157
160
var ordered_values = {
158
161
$ref : value . namespace ,
@@ -183,7 +186,7 @@ function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefi
183
186
( value . multiline ? 1 : 0 ) +
184
187
1
185
188
) ;
186
- } else if ( value instanceof BSONRegExp || value [ ' _bsontype' ] === 'BSONRegExp' ) {
189
+ } else if ( value instanceof BSONRegExp || _bsontype === 'BSONRegExp' ) {
187
190
return (
188
191
( name != null ? Buffer . byteLength ( name , 'utf8' ) + 1 : 0 ) +
189
192
1 +
0 commit comments