@@ -705,6 +705,9 @@ var serializeInto = function serializeInto(
705
705
}
706
706
707
707
var type = typeof value ;
708
+ var _objPrototype = value && type === 'object' && Object . getPrototypeOf ( value )
709
+ var _bsontype = _objPrototype && _objPrototype . _bsontype || undefined ;
710
+
708
711
if ( type === 'string' ) {
709
712
index = serializeString ( buffer , key , value , index , true ) ;
710
713
} else if ( type === 'number' ) {
@@ -717,13 +720,13 @@ var serializeInto = function serializeInto(
717
720
index = serializeNull ( buffer , key , value , index , true ) ;
718
721
} else if ( value === null ) {
719
722
index = serializeNull ( buffer , key , value , index , true ) ;
720
- } else if ( value [ ' _bsontype' ] === 'ObjectID' || value [ ' _bsontype' ] === 'ObjectId' ) {
723
+ } else if ( _bsontype === 'ObjectID' || _bsontype === 'ObjectId' ) {
721
724
index = serializeObjectId ( buffer , key , value , index , true ) ;
722
725
} else if ( Buffer . isBuffer ( value ) ) {
723
726
index = serializeBuffer ( buffer , key , value , index , true ) ;
724
727
} else if ( value instanceof RegExp || isRegExp ( value ) ) {
725
728
index = serializeRegExp ( buffer , key , value , index , true ) ;
726
- } else if ( type === 'object' && value [ ' _bsontype' ] == null ) {
729
+ } else if ( type === 'object' && _bsontype == null ) {
727
730
index = serializeObject (
728
731
buffer ,
729
732
key ,
@@ -736,11 +739,11 @@ var serializeInto = function serializeInto(
736
739
true ,
737
740
path
738
741
) ;
739
- } else if ( type === 'object' && value [ ' _bsontype' ] === 'Decimal128' ) {
742
+ } else if ( type === 'object' && _bsontype === 'Decimal128' ) {
740
743
index = serializeDecimal128 ( buffer , key , value , index , true ) ;
741
- } else if ( value [ ' _bsontype' ] === 'Long' || value [ ' _bsontype' ] === 'Timestamp' ) {
744
+ } else if ( _bsontype === 'Long' || _bsontype === 'Timestamp' ) {
742
745
index = serializeLong ( buffer , key , value , index , true ) ;
743
- } else if ( value [ ' _bsontype' ] === 'Double' ) {
746
+ } else if ( _bsontype === 'Double' ) {
744
747
index = serializeDouble ( buffer , key , value , index , true ) ;
745
748
} else if ( typeof value === 'function' && serializeFunctions ) {
746
749
index = serializeFunction (
@@ -753,7 +756,7 @@ var serializeInto = function serializeInto(
753
756
serializeFunctions ,
754
757
true
755
758
) ;
756
- } else if ( value [ ' _bsontype' ] === 'Code' ) {
759
+ } else if ( _bsontype === 'Code' ) {
757
760
index = serializeCode (
758
761
buffer ,
759
762
key ,
@@ -765,18 +768,20 @@ var serializeInto = function serializeInto(
765
768
ignoreUndefined ,
766
769
true
767
770
) ;
768
- } else if ( value [ ' _bsontype' ] === 'Binary' ) {
771
+ } else if ( _bsontype === 'Binary' ) {
769
772
index = serializeBinary ( buffer , key , value , index , true ) ;
770
- } else if ( value [ ' _bsontype' ] === 'Symbol' ) {
773
+ } else if ( _bsontype === 'Symbol' ) {
771
774
index = serializeSymbol ( buffer , key , value , index , true ) ;
772
- } else if ( value [ ' _bsontype' ] === 'DBRef' ) {
775
+ } else if ( _bsontype === 'DBRef' ) {
773
776
index = serializeDBRef ( buffer , key , value , index , depth , serializeFunctions , true ) ;
774
- } else if ( value [ ' _bsontype' ] === 'BSONRegExp' ) {
777
+ } else if ( _bsontype === 'BSONRegExp' ) {
775
778
index = serializeBSONRegExp ( buffer , key , value , index , true ) ;
776
- } else if ( value [ ' _bsontype' ] === 'Int32' ) {
779
+ } else if ( _bsontype === 'Int32' ) {
777
780
index = serializeInt32 ( buffer , key , value , index , true ) ;
778
- } else if ( value [ ' _bsontype' ] === 'MinKey' || value [ ' _bsontype' ] === 'MaxKey' ) {
781
+ } else if ( _bsontype === 'MinKey' || _bsontype === 'MaxKey' ) {
779
782
index = serializeMinMax ( buffer , key , value , index , true ) ;
783
+ } else if ( typeof value [ '_bsontype' ] !== 'undefined' ) {
784
+ throw new TypeError ( 'Unrecognized or invalid _bsontype: ' + value [ '_bsontype' ] ) ;
780
785
}
781
786
}
782
787
} else if ( object instanceof Map ) {
@@ -797,6 +802,9 @@ var serializeInto = function serializeInto(
797
802
// Check the type of the value
798
803
type = typeof value ;
799
804
805
+ var _objPrototype = value && type === 'object' && Object . getPrototypeOf ( value )
806
+ var _bsontype = _objPrototype && _objPrototype . _bsontype || undefined ;
807
+
800
808
// Check the key and throw error if it's illegal
801
809
if ( typeof key === 'string' && ignoreKeys . indexOf ( key ) === - 1 ) {
802
810
if ( key . match ( regexp ) != null ) {
@@ -825,13 +833,13 @@ var serializeInto = function serializeInto(
825
833
// } else if (value === undefined && ignoreUndefined === true) {
826
834
} else if ( value === null || ( value === undefined && ignoreUndefined === false ) ) {
827
835
index = serializeNull ( buffer , key , value , index ) ;
828
- } else if ( value [ ' _bsontype' ] === 'ObjectID' || value [ ' _bsontype' ] === 'ObjectId' ) {
836
+ } else if ( _bsontype === 'ObjectID' || _bsontype === 'ObjectId' ) {
829
837
index = serializeObjectId ( buffer , key , value , index ) ;
830
838
} else if ( Buffer . isBuffer ( value ) ) {
831
839
index = serializeBuffer ( buffer , key , value , index ) ;
832
840
} else if ( value instanceof RegExp || isRegExp ( value ) ) {
833
841
index = serializeRegExp ( buffer , key , value , index ) ;
834
- } else if ( type === 'object' && value [ ' _bsontype' ] == null ) {
842
+ } else if ( type === 'object' && _bsontype == null ) {
835
843
index = serializeObject (
836
844
buffer ,
837
845
key ,
@@ -844,13 +852,13 @@ var serializeInto = function serializeInto(
844
852
false ,
845
853
path
846
854
) ;
847
- } else if ( type === 'object' && value [ ' _bsontype' ] === 'Decimal128' ) {
855
+ } else if ( type === 'object' && _bsontype === 'Decimal128' ) {
848
856
index = serializeDecimal128 ( buffer , key , value , index ) ;
849
- } else if ( value [ ' _bsontype' ] === 'Long' || value [ ' _bsontype' ] === 'Timestamp' ) {
857
+ } else if ( _bsontype === 'Long' || _bsontype === 'Timestamp' ) {
850
858
index = serializeLong ( buffer , key , value , index ) ;
851
- } else if ( value [ ' _bsontype' ] === 'Double' ) {
859
+ } else if ( _bsontype === 'Double' ) {
852
860
index = serializeDouble ( buffer , key , value , index ) ;
853
- } else if ( value [ ' _bsontype' ] === 'Code' ) {
861
+ } else if ( _bsontype === 'Code' ) {
854
862
index = serializeCode (
855
863
buffer ,
856
864
key ,
@@ -863,18 +871,20 @@ var serializeInto = function serializeInto(
863
871
) ;
864
872
} else if ( typeof value === 'function' && serializeFunctions ) {
865
873
index = serializeFunction ( buffer , key , value , index , checkKeys , depth , serializeFunctions ) ;
866
- } else if ( value [ ' _bsontype' ] === 'Binary' ) {
874
+ } else if ( _bsontype === 'Binary' ) {
867
875
index = serializeBinary ( buffer , key , value , index ) ;
868
- } else if ( value [ ' _bsontype' ] === 'Symbol' ) {
876
+ } else if ( _bsontype === 'Symbol' ) {
869
877
index = serializeSymbol ( buffer , key , value , index ) ;
870
- } else if ( value [ ' _bsontype' ] === 'DBRef' ) {
878
+ } else if ( _bsontype === 'DBRef' ) {
871
879
index = serializeDBRef ( buffer , key , value , index , depth , serializeFunctions ) ;
872
- } else if ( value [ ' _bsontype' ] === 'BSONRegExp' ) {
880
+ } else if ( _bsontype === 'BSONRegExp' ) {
873
881
index = serializeBSONRegExp ( buffer , key , value , index ) ;
874
- } else if ( value [ ' _bsontype' ] === 'Int32' ) {
882
+ } else if ( _bsontype === 'Int32' ) {
875
883
index = serializeInt32 ( buffer , key , value , index ) ;
876
- } else if ( value [ ' _bsontype' ] === 'MinKey' || value [ ' _bsontype' ] === 'MaxKey' ) {
884
+ } else if ( _bsontype === 'MinKey' || _bsontype === 'MaxKey' ) {
877
885
index = serializeMinMax ( buffer , key , value , index ) ;
886
+ } else if ( typeof value [ '_bsontype' ] !== 'undefined' ) {
887
+ throw new TypeError ( 'Unrecognized or invalid _bsontype: ' + value [ '_bsontype' ] ) ;
878
888
}
879
889
}
880
890
} else {
@@ -898,6 +908,9 @@ var serializeInto = function serializeInto(
898
908
// Check the type of the value
899
909
type = typeof value ;
900
910
911
+ var _objPrototype = value && type === 'object' && Object . getPrototypeOf ( value )
912
+ var _bsontype = _objPrototype && _objPrototype . _bsontype || undefined ;
913
+
901
914
// Check the key and throw error if it's illegal
902
915
if ( typeof key === 'string' && ignoreKeys . indexOf ( key ) === - 1 ) {
903
916
if ( key . match ( regexp ) != null ) {
@@ -927,13 +940,13 @@ var serializeInto = function serializeInto(
927
940
if ( ignoreUndefined === false ) index = serializeNull ( buffer , key , value , index ) ;
928
941
} else if ( value === null ) {
929
942
index = serializeNull ( buffer , key , value , index ) ;
930
- } else if ( value [ ' _bsontype' ] === 'ObjectID' || value [ ' _bsontype' ] === 'ObjectId' ) {
943
+ } else if ( _bsontype === 'ObjectID' || _bsontype === 'ObjectId' ) {
931
944
index = serializeObjectId ( buffer , key , value , index ) ;
932
945
} else if ( Buffer . isBuffer ( value ) ) {
933
946
index = serializeBuffer ( buffer , key , value , index ) ;
934
947
} else if ( value instanceof RegExp || isRegExp ( value ) ) {
935
948
index = serializeRegExp ( buffer , key , value , index ) ;
936
- } else if ( type === 'object' && value [ ' _bsontype' ] == null ) {
949
+ } else if ( type === 'object' && _bsontype == null ) {
937
950
index = serializeObject (
938
951
buffer ,
939
952
key ,
@@ -946,13 +959,13 @@ var serializeInto = function serializeInto(
946
959
false ,
947
960
path
948
961
) ;
949
- } else if ( type === 'object' && value [ ' _bsontype' ] === 'Decimal128' ) {
962
+ } else if ( type === 'object' && _bsontype === 'Decimal128' ) {
950
963
index = serializeDecimal128 ( buffer , key , value , index ) ;
951
- } else if ( value [ ' _bsontype' ] === 'Long' || value [ ' _bsontype' ] === 'Timestamp' ) {
964
+ } else if ( _bsontype === 'Long' || _bsontype === 'Timestamp' ) {
952
965
index = serializeLong ( buffer , key , value , index ) ;
953
- } else if ( value [ ' _bsontype' ] === 'Double' ) {
966
+ } else if ( _bsontype === 'Double' ) {
954
967
index = serializeDouble ( buffer , key , value , index ) ;
955
- } else if ( value [ ' _bsontype' ] === 'Code' ) {
968
+ } else if ( _bsontype === 'Code' ) {
956
969
index = serializeCode (
957
970
buffer ,
958
971
key ,
@@ -965,18 +978,20 @@ var serializeInto = function serializeInto(
965
978
) ;
966
979
} else if ( typeof value === 'function' && serializeFunctions ) {
967
980
index = serializeFunction ( buffer , key , value , index , checkKeys , depth , serializeFunctions ) ;
968
- } else if ( value [ ' _bsontype' ] === 'Binary' ) {
981
+ } else if ( _bsontype === 'Binary' ) {
969
982
index = serializeBinary ( buffer , key , value , index ) ;
970
- } else if ( value [ ' _bsontype' ] === 'Symbol' ) {
983
+ } else if ( _bsontype === 'Symbol' ) {
971
984
index = serializeSymbol ( buffer , key , value , index ) ;
972
- } else if ( value [ ' _bsontype' ] === 'DBRef' ) {
985
+ } else if ( _bsontype === 'DBRef' ) {
973
986
index = serializeDBRef ( buffer , key , value , index , depth , serializeFunctions ) ;
974
- } else if ( value [ ' _bsontype' ] === 'BSONRegExp' ) {
987
+ } else if ( _bsontype === 'BSONRegExp' ) {
975
988
index = serializeBSONRegExp ( buffer , key , value , index ) ;
976
- } else if ( value [ ' _bsontype' ] === 'Int32' ) {
989
+ } else if ( _bsontype === 'Int32' ) {
977
990
index = serializeInt32 ( buffer , key , value , index ) ;
978
- } else if ( value [ ' _bsontype' ] === 'MinKey' || value [ ' _bsontype' ] === 'MaxKey' ) {
991
+ } else if ( _bsontype === 'MinKey' || _bsontype === 'MaxKey' ) {
979
992
index = serializeMinMax ( buffer , key , value , index ) ;
993
+ } else if ( typeof value [ '_bsontype' ] !== 'undefined' ) {
994
+ throw new TypeError ( 'Unrecognized or invalid _bsontype: ' + value [ '_bsontype' ] ) ;
980
995
}
981
996
}
982
997
}
0 commit comments