bson 6.7.0 → 6.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -0
- package/bson.d.ts +31 -7
- package/lib/bson.bundle.js +265 -202
- package/lib/bson.bundle.js.map +1 -1
- package/lib/bson.cjs +265 -202
- package/lib/bson.cjs.map +1 -1
- package/lib/bson.mjs +265 -202
- package/lib/bson.mjs.map +1 -1
- package/lib/bson.rn.cjs +269 -204
- package/lib/bson.rn.cjs.map +1 -1
- package/package.json +21 -21
- package/src/bson_value.ts +2 -1
- package/src/constants.ts +3 -0
- package/src/decimal128.ts +1 -1
- package/src/extended_json.ts +3 -2
- package/src/long.ts +43 -21
- package/src/objectid.ts +25 -4
- package/src/parser/calculate_size.ts +1 -1
- package/src/parser/serializer.ts +196 -188
- package/src/parser/utils.ts +43 -9
- package/src/timestamp.ts +19 -3
package/src/parser/serializer.ts
CHANGED
|
@@ -633,77 +633,81 @@ export function serializeInto(
|
|
|
633
633
|
value = value.toBSON();
|
|
634
634
|
}
|
|
635
635
|
|
|
636
|
-
|
|
636
|
+
// Check the type of the value
|
|
637
|
+
const type = typeof value;
|
|
638
|
+
|
|
639
|
+
if (value === undefined) {
|
|
640
|
+
index = serializeNull(buffer, key, value, index);
|
|
641
|
+
} else if (value === null) {
|
|
642
|
+
index = serializeNull(buffer, key, value, index);
|
|
643
|
+
} else if (type === 'string') {
|
|
637
644
|
index = serializeString(buffer, key, value, index);
|
|
638
|
-
} else if (
|
|
645
|
+
} else if (type === 'number') {
|
|
639
646
|
index = serializeNumber(buffer, key, value, index);
|
|
640
|
-
} else if (
|
|
647
|
+
} else if (type === 'bigint') {
|
|
641
648
|
index = serializeBigInt(buffer, key, value, index);
|
|
642
|
-
} else if (
|
|
649
|
+
} else if (type === 'boolean') {
|
|
643
650
|
index = serializeBoolean(buffer, key, value, index);
|
|
644
|
-
} else if (
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
)
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
651
|
+
} else if (type === 'object' && value._bsontype == null) {
|
|
652
|
+
if (value instanceof Date || isDate(value)) {
|
|
653
|
+
index = serializeDate(buffer, key, value, index);
|
|
654
|
+
} else if (value instanceof Uint8Array || isUint8Array(value)) {
|
|
655
|
+
index = serializeBuffer(buffer, key, value, index);
|
|
656
|
+
} else if (value instanceof RegExp || isRegExp(value)) {
|
|
657
|
+
index = serializeRegExp(buffer, key, value, index);
|
|
658
|
+
} else {
|
|
659
|
+
index = serializeObject(
|
|
660
|
+
buffer,
|
|
661
|
+
key,
|
|
662
|
+
value,
|
|
663
|
+
index,
|
|
664
|
+
checkKeys,
|
|
665
|
+
depth,
|
|
666
|
+
serializeFunctions,
|
|
667
|
+
ignoreUndefined,
|
|
668
|
+
path
|
|
669
|
+
);
|
|
670
|
+
}
|
|
671
|
+
} else if (type === 'object') {
|
|
672
|
+
if (value[constants.BSON_VERSION_SYMBOL] !== constants.BSON_MAJOR_VERSION) {
|
|
673
|
+
throw new BSONVersionError();
|
|
674
|
+
} else if (value._bsontype === 'ObjectId') {
|
|
675
|
+
index = serializeObjectId(buffer, key, value, index);
|
|
676
|
+
} else if (value._bsontype === 'Decimal128') {
|
|
677
|
+
index = serializeDecimal128(buffer, key, value, index);
|
|
678
|
+
} else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
|
|
679
|
+
index = serializeLong(buffer, key, value, index);
|
|
680
|
+
} else if (value._bsontype === 'Double') {
|
|
681
|
+
index = serializeDouble(buffer, key, value, index);
|
|
682
|
+
} else if (value._bsontype === 'Code') {
|
|
683
|
+
index = serializeCode(
|
|
684
|
+
buffer,
|
|
685
|
+
key,
|
|
686
|
+
value,
|
|
687
|
+
index,
|
|
688
|
+
checkKeys,
|
|
689
|
+
depth,
|
|
690
|
+
serializeFunctions,
|
|
691
|
+
ignoreUndefined,
|
|
692
|
+
path
|
|
693
|
+
);
|
|
694
|
+
} else if (value._bsontype === 'Binary') {
|
|
695
|
+
index = serializeBinary(buffer, key, value, index);
|
|
696
|
+
} else if (value._bsontype === 'BSONSymbol') {
|
|
697
|
+
index = serializeSymbol(buffer, key, value, index);
|
|
698
|
+
} else if (value._bsontype === 'DBRef') {
|
|
699
|
+
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
|
|
700
|
+
} else if (value._bsontype === 'BSONRegExp') {
|
|
701
|
+
index = serializeBSONRegExp(buffer, key, value, index);
|
|
702
|
+
} else if (value._bsontype === 'Int32') {
|
|
703
|
+
index = serializeInt32(buffer, key, value, index);
|
|
704
|
+
} else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
|
|
705
|
+
index = serializeMinMax(buffer, key, value, index);
|
|
706
|
+
} else if (typeof value._bsontype !== 'undefined') {
|
|
707
|
+
throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
|
|
708
|
+
}
|
|
709
|
+
} else if (type === 'function' && serializeFunctions) {
|
|
680
710
|
index = serializeFunction(buffer, key, value, index);
|
|
681
|
-
} else if (value._bsontype === 'Code') {
|
|
682
|
-
index = serializeCode(
|
|
683
|
-
buffer,
|
|
684
|
-
key,
|
|
685
|
-
value,
|
|
686
|
-
index,
|
|
687
|
-
checkKeys,
|
|
688
|
-
depth,
|
|
689
|
-
serializeFunctions,
|
|
690
|
-
ignoreUndefined,
|
|
691
|
-
path
|
|
692
|
-
);
|
|
693
|
-
} else if (value._bsontype === 'Binary') {
|
|
694
|
-
index = serializeBinary(buffer, key, value, index);
|
|
695
|
-
} else if (value._bsontype === 'BSONSymbol') {
|
|
696
|
-
index = serializeSymbol(buffer, key, value, index);
|
|
697
|
-
} else if (value._bsontype === 'DBRef') {
|
|
698
|
-
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
|
|
699
|
-
} else if (value._bsontype === 'BSONRegExp') {
|
|
700
|
-
index = serializeBSONRegExp(buffer, key, value, index);
|
|
701
|
-
} else if (value._bsontype === 'Int32') {
|
|
702
|
-
index = serializeInt32(buffer, key, value, index);
|
|
703
|
-
} else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
|
|
704
|
-
index = serializeMinMax(buffer, key, value, index);
|
|
705
|
-
} else if (typeof value._bsontype !== 'undefined') {
|
|
706
|
-
throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
|
|
707
711
|
}
|
|
708
712
|
}
|
|
709
713
|
} else if (object instanceof Map || isMap(object)) {
|
|
@@ -745,7 +749,11 @@ export function serializeInto(
|
|
|
745
749
|
}
|
|
746
750
|
}
|
|
747
751
|
|
|
748
|
-
if (
|
|
752
|
+
if (value === undefined) {
|
|
753
|
+
if (ignoreUndefined === false) index = serializeNull(buffer, key, value, index);
|
|
754
|
+
} else if (value === null) {
|
|
755
|
+
index = serializeNull(buffer, key, value, index);
|
|
756
|
+
} else if (type === 'string') {
|
|
749
757
|
index = serializeString(buffer, key, value, index);
|
|
750
758
|
} else if (type === 'number') {
|
|
751
759
|
index = serializeNumber(buffer, key, value, index);
|
|
@@ -753,67 +761,66 @@ export function serializeInto(
|
|
|
753
761
|
index = serializeBigInt(buffer, key, value, index);
|
|
754
762
|
} else if (type === 'boolean') {
|
|
755
763
|
index = serializeBoolean(buffer, key, value, index);
|
|
756
|
-
} else if (value instanceof Date || isDate(value)) {
|
|
757
|
-
index = serializeDate(buffer, key, value, index);
|
|
758
|
-
} else if (value === null || (value === undefined && ignoreUndefined === false)) {
|
|
759
|
-
index = serializeNull(buffer, key, value, index);
|
|
760
|
-
} else if (isUint8Array(value)) {
|
|
761
|
-
index = serializeBuffer(buffer, key, value, index);
|
|
762
|
-
} else if (value instanceof RegExp || isRegExp(value)) {
|
|
763
|
-
index = serializeRegExp(buffer, key, value, index);
|
|
764
764
|
} else if (type === 'object' && value._bsontype == null) {
|
|
765
|
-
|
|
766
|
-
buffer,
|
|
767
|
-
|
|
768
|
-
value,
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
key,
|
|
793
|
-
|
|
794
|
-
index,
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
765
|
+
if (value instanceof Date || isDate(value)) {
|
|
766
|
+
index = serializeDate(buffer, key, value, index);
|
|
767
|
+
} else if (value instanceof Uint8Array || isUint8Array(value)) {
|
|
768
|
+
index = serializeBuffer(buffer, key, value, index);
|
|
769
|
+
} else if (value instanceof RegExp || isRegExp(value)) {
|
|
770
|
+
index = serializeRegExp(buffer, key, value, index);
|
|
771
|
+
} else {
|
|
772
|
+
index = serializeObject(
|
|
773
|
+
buffer,
|
|
774
|
+
key,
|
|
775
|
+
value,
|
|
776
|
+
index,
|
|
777
|
+
checkKeys,
|
|
778
|
+
depth,
|
|
779
|
+
serializeFunctions,
|
|
780
|
+
ignoreUndefined,
|
|
781
|
+
path
|
|
782
|
+
);
|
|
783
|
+
}
|
|
784
|
+
} else if (type === 'object') {
|
|
785
|
+
if (value[constants.BSON_VERSION_SYMBOL] !== constants.BSON_MAJOR_VERSION) {
|
|
786
|
+
throw new BSONVersionError();
|
|
787
|
+
} else if (value._bsontype === 'ObjectId') {
|
|
788
|
+
index = serializeObjectId(buffer, key, value, index);
|
|
789
|
+
} else if (value._bsontype === 'Decimal128') {
|
|
790
|
+
index = serializeDecimal128(buffer, key, value, index);
|
|
791
|
+
} else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
|
|
792
|
+
index = serializeLong(buffer, key, value, index);
|
|
793
|
+
} else if (value._bsontype === 'Double') {
|
|
794
|
+
index = serializeDouble(buffer, key, value, index);
|
|
795
|
+
} else if (value._bsontype === 'Code') {
|
|
796
|
+
index = serializeCode(
|
|
797
|
+
buffer,
|
|
798
|
+
key,
|
|
799
|
+
value,
|
|
800
|
+
index,
|
|
801
|
+
checkKeys,
|
|
802
|
+
depth,
|
|
803
|
+
serializeFunctions,
|
|
804
|
+
ignoreUndefined,
|
|
805
|
+
path
|
|
806
|
+
);
|
|
807
|
+
} else if (value._bsontype === 'Binary') {
|
|
808
|
+
index = serializeBinary(buffer, key, value, index);
|
|
809
|
+
} else if (value._bsontype === 'BSONSymbol') {
|
|
810
|
+
index = serializeSymbol(buffer, key, value, index);
|
|
811
|
+
} else if (value._bsontype === 'DBRef') {
|
|
812
|
+
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
|
|
813
|
+
} else if (value._bsontype === 'BSONRegExp') {
|
|
814
|
+
index = serializeBSONRegExp(buffer, key, value, index);
|
|
815
|
+
} else if (value._bsontype === 'Int32') {
|
|
816
|
+
index = serializeInt32(buffer, key, value, index);
|
|
817
|
+
} else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
|
|
818
|
+
index = serializeMinMax(buffer, key, value, index);
|
|
819
|
+
} else if (typeof value._bsontype !== 'undefined') {
|
|
820
|
+
throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
|
|
821
|
+
}
|
|
822
|
+
} else if (type === 'function' && serializeFunctions) {
|
|
802
823
|
index = serializeFunction(buffer, key, value, index);
|
|
803
|
-
} else if (value._bsontype === 'Binary') {
|
|
804
|
-
index = serializeBinary(buffer, key, value, index);
|
|
805
|
-
} else if (value._bsontype === 'BSONSymbol') {
|
|
806
|
-
index = serializeSymbol(buffer, key, value, index);
|
|
807
|
-
} else if (value._bsontype === 'DBRef') {
|
|
808
|
-
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
|
|
809
|
-
} else if (value._bsontype === 'BSONRegExp') {
|
|
810
|
-
index = serializeBSONRegExp(buffer, key, value, index);
|
|
811
|
-
} else if (value._bsontype === 'Int32') {
|
|
812
|
-
index = serializeInt32(buffer, key, value, index);
|
|
813
|
-
} else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
|
|
814
|
-
index = serializeMinMax(buffer, key, value, index);
|
|
815
|
-
} else if (typeof value._bsontype !== 'undefined') {
|
|
816
|
-
throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
|
|
817
824
|
}
|
|
818
825
|
}
|
|
819
826
|
} else {
|
|
@@ -853,7 +860,11 @@ export function serializeInto(
|
|
|
853
860
|
}
|
|
854
861
|
}
|
|
855
862
|
|
|
856
|
-
if (
|
|
863
|
+
if (value === undefined) {
|
|
864
|
+
if (ignoreUndefined === false) index = serializeNull(buffer, key, value, index);
|
|
865
|
+
} else if (value === null) {
|
|
866
|
+
index = serializeNull(buffer, key, value, index);
|
|
867
|
+
} else if (type === 'string') {
|
|
857
868
|
index = serializeString(buffer, key, value, index);
|
|
858
869
|
} else if (type === 'number') {
|
|
859
870
|
index = serializeNumber(buffer, key, value, index);
|
|
@@ -861,69 +872,66 @@ export function serializeInto(
|
|
|
861
872
|
index = serializeBigInt(buffer, key, value, index);
|
|
862
873
|
} else if (type === 'boolean') {
|
|
863
874
|
index = serializeBoolean(buffer, key, value, index);
|
|
864
|
-
} else if (value instanceof Date || isDate(value)) {
|
|
865
|
-
index = serializeDate(buffer, key, value, index);
|
|
866
|
-
} else if (value === undefined) {
|
|
867
|
-
if (ignoreUndefined === false) index = serializeNull(buffer, key, value, index);
|
|
868
|
-
} else if (value === null) {
|
|
869
|
-
index = serializeNull(buffer, key, value, index);
|
|
870
|
-
} else if (isUint8Array(value)) {
|
|
871
|
-
index = serializeBuffer(buffer, key, value, index);
|
|
872
|
-
} else if (value instanceof RegExp || isRegExp(value)) {
|
|
873
|
-
index = serializeRegExp(buffer, key, value, index);
|
|
874
875
|
} else if (type === 'object' && value._bsontype == null) {
|
|
875
|
-
|
|
876
|
-
buffer,
|
|
877
|
-
|
|
878
|
-
value,
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
key,
|
|
903
|
-
|
|
904
|
-
index,
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
876
|
+
if (value instanceof Date || isDate(value)) {
|
|
877
|
+
index = serializeDate(buffer, key, value, index);
|
|
878
|
+
} else if (value instanceof Uint8Array || isUint8Array(value)) {
|
|
879
|
+
index = serializeBuffer(buffer, key, value, index);
|
|
880
|
+
} else if (value instanceof RegExp || isRegExp(value)) {
|
|
881
|
+
index = serializeRegExp(buffer, key, value, index);
|
|
882
|
+
} else {
|
|
883
|
+
index = serializeObject(
|
|
884
|
+
buffer,
|
|
885
|
+
key,
|
|
886
|
+
value,
|
|
887
|
+
index,
|
|
888
|
+
checkKeys,
|
|
889
|
+
depth,
|
|
890
|
+
serializeFunctions,
|
|
891
|
+
ignoreUndefined,
|
|
892
|
+
path
|
|
893
|
+
);
|
|
894
|
+
}
|
|
895
|
+
} else if (type === 'object') {
|
|
896
|
+
if (value[constants.BSON_VERSION_SYMBOL] !== constants.BSON_MAJOR_VERSION) {
|
|
897
|
+
throw new BSONVersionError();
|
|
898
|
+
} else if (value._bsontype === 'ObjectId') {
|
|
899
|
+
index = serializeObjectId(buffer, key, value, index);
|
|
900
|
+
} else if (value._bsontype === 'Decimal128') {
|
|
901
|
+
index = serializeDecimal128(buffer, key, value, index);
|
|
902
|
+
} else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
|
|
903
|
+
index = serializeLong(buffer, key, value, index);
|
|
904
|
+
} else if (value._bsontype === 'Double') {
|
|
905
|
+
index = serializeDouble(buffer, key, value, index);
|
|
906
|
+
} else if (value._bsontype === 'Code') {
|
|
907
|
+
index = serializeCode(
|
|
908
|
+
buffer,
|
|
909
|
+
key,
|
|
910
|
+
value,
|
|
911
|
+
index,
|
|
912
|
+
checkKeys,
|
|
913
|
+
depth,
|
|
914
|
+
serializeFunctions,
|
|
915
|
+
ignoreUndefined,
|
|
916
|
+
path
|
|
917
|
+
);
|
|
918
|
+
} else if (value._bsontype === 'Binary') {
|
|
919
|
+
index = serializeBinary(buffer, key, value, index);
|
|
920
|
+
} else if (value._bsontype === 'BSONSymbol') {
|
|
921
|
+
index = serializeSymbol(buffer, key, value, index);
|
|
922
|
+
} else if (value._bsontype === 'DBRef') {
|
|
923
|
+
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
|
|
924
|
+
} else if (value._bsontype === 'BSONRegExp') {
|
|
925
|
+
index = serializeBSONRegExp(buffer, key, value, index);
|
|
926
|
+
} else if (value._bsontype === 'Int32') {
|
|
927
|
+
index = serializeInt32(buffer, key, value, index);
|
|
928
|
+
} else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
|
|
929
|
+
index = serializeMinMax(buffer, key, value, index);
|
|
930
|
+
} else if (typeof value._bsontype !== 'undefined') {
|
|
931
|
+
throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
|
|
932
|
+
}
|
|
933
|
+
} else if (type === 'function' && serializeFunctions) {
|
|
912
934
|
index = serializeFunction(buffer, key, value, index);
|
|
913
|
-
} else if (value._bsontype === 'Binary') {
|
|
914
|
-
index = serializeBinary(buffer, key, value, index);
|
|
915
|
-
} else if (value._bsontype === 'BSONSymbol') {
|
|
916
|
-
index = serializeSymbol(buffer, key, value, index);
|
|
917
|
-
} else if (value._bsontype === 'DBRef') {
|
|
918
|
-
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
|
|
919
|
-
} else if (value._bsontype === 'BSONRegExp') {
|
|
920
|
-
index = serializeBSONRegExp(buffer, key, value, index);
|
|
921
|
-
} else if (value._bsontype === 'Int32') {
|
|
922
|
-
index = serializeInt32(buffer, key, value, index);
|
|
923
|
-
} else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
|
|
924
|
-
index = serializeMinMax(buffer, key, value, index);
|
|
925
|
-
} else if (typeof value._bsontype !== 'undefined') {
|
|
926
|
-
throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
|
|
927
935
|
}
|
|
928
936
|
}
|
|
929
937
|
}
|
package/src/parser/utils.ts
CHANGED
|
@@ -1,31 +1,65 @@
|
|
|
1
|
+
const map = new WeakMap<object, string>();
|
|
2
|
+
|
|
3
|
+
const TYPES = {
|
|
4
|
+
ArrayBuffer: '[object ArrayBuffer]',
|
|
5
|
+
SharedArrayBuffer: '[object SharedArrayBuffer]',
|
|
6
|
+
Uint8Array: '[object Uint8Array]',
|
|
7
|
+
BigInt64Array: '[object BigInt64Array]',
|
|
8
|
+
BigUint64Array: '[object BigUint64Array]',
|
|
9
|
+
RegExp: '[object RegExp]',
|
|
10
|
+
Map: '[object Map]',
|
|
11
|
+
Date: '[object Date]'
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Retrieves the prototype.toString() of a value.
|
|
16
|
+
* If the value is an object, it will cache the result in a WeakMap for future use.
|
|
17
|
+
*/
|
|
18
|
+
function getPrototypeString(value: unknown): string {
|
|
19
|
+
let str = map.get(value as object);
|
|
20
|
+
|
|
21
|
+
if (!str) {
|
|
22
|
+
str = Object.prototype.toString.call(value);
|
|
23
|
+
if (value !== null && typeof value === 'object') {
|
|
24
|
+
map.set(value, str);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return str;
|
|
28
|
+
}
|
|
29
|
+
|
|
1
30
|
export function isAnyArrayBuffer(value: unknown): value is ArrayBuffer {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
);
|
|
31
|
+
const type = getPrototypeString(value);
|
|
32
|
+
return type === TYPES.ArrayBuffer || type === TYPES.SharedArrayBuffer;
|
|
5
33
|
}
|
|
6
34
|
|
|
7
35
|
export function isUint8Array(value: unknown): value is Uint8Array {
|
|
8
|
-
|
|
36
|
+
const type = getPrototypeString(value);
|
|
37
|
+
return type === TYPES.Uint8Array;
|
|
9
38
|
}
|
|
10
39
|
|
|
11
40
|
export function isBigInt64Array(value: unknown): value is BigInt64Array {
|
|
12
|
-
|
|
41
|
+
const type = getPrototypeString(value);
|
|
42
|
+
return type === TYPES.BigInt64Array;
|
|
13
43
|
}
|
|
14
44
|
|
|
15
45
|
export function isBigUInt64Array(value: unknown): value is BigUint64Array {
|
|
16
|
-
|
|
46
|
+
const type = getPrototypeString(value);
|
|
47
|
+
return type === TYPES.BigUint64Array;
|
|
17
48
|
}
|
|
18
49
|
|
|
19
50
|
export function isRegExp(d: unknown): d is RegExp {
|
|
20
|
-
|
|
51
|
+
const type = getPrototypeString(d);
|
|
52
|
+
return type === TYPES.RegExp;
|
|
21
53
|
}
|
|
22
54
|
|
|
23
55
|
export function isMap(d: unknown): d is Map<unknown, unknown> {
|
|
24
|
-
|
|
56
|
+
const type = getPrototypeString(d);
|
|
57
|
+
return type === TYPES.Map;
|
|
25
58
|
}
|
|
26
59
|
|
|
27
60
|
export function isDate(d: unknown): d is Date {
|
|
28
|
-
|
|
61
|
+
const type = getPrototypeString(d);
|
|
62
|
+
return type === TYPES.Date;
|
|
29
63
|
}
|
|
30
64
|
|
|
31
65
|
export type InspectFn = (x: unknown, options?: unknown) => string;
|
package/src/timestamp.ts
CHANGED
|
@@ -28,6 +28,8 @@ export interface TimestampExtended {
|
|
|
28
28
|
/**
|
|
29
29
|
* @public
|
|
30
30
|
* @category BSONType
|
|
31
|
+
*
|
|
32
|
+
* A special type for _internal_ MongoDB use and is **not** associated with the regular Date type.
|
|
31
33
|
*/
|
|
32
34
|
export class Timestamp extends LongWithoutOverridesClass {
|
|
33
35
|
get _bsontype(): 'Timestamp' {
|
|
@@ -36,6 +38,20 @@ export class Timestamp extends LongWithoutOverridesClass {
|
|
|
36
38
|
|
|
37
39
|
static readonly MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
|
|
38
40
|
|
|
41
|
+
/**
|
|
42
|
+
* An incrementing ordinal for operations within a given second.
|
|
43
|
+
*/
|
|
44
|
+
get i(): number {
|
|
45
|
+
return this.low >>> 0;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A `time_t` value measuring seconds since the Unix epoch
|
|
50
|
+
*/
|
|
51
|
+
get t(): number {
|
|
52
|
+
return this.high >>> 0;
|
|
53
|
+
}
|
|
54
|
+
|
|
39
55
|
/**
|
|
40
56
|
* @param int - A 64-bit bigint representing the Timestamp.
|
|
41
57
|
*/
|
|
@@ -127,7 +143,7 @@ export class Timestamp extends LongWithoutOverridesClass {
|
|
|
127
143
|
|
|
128
144
|
/** @internal */
|
|
129
145
|
toExtendedJSON(): TimestampExtended {
|
|
130
|
-
return { $timestamp: { t: this.
|
|
146
|
+
return { $timestamp: { t: this.t, i: this.i } };
|
|
131
147
|
}
|
|
132
148
|
|
|
133
149
|
/** @internal */
|
|
@@ -144,8 +160,8 @@ export class Timestamp extends LongWithoutOverridesClass {
|
|
|
144
160
|
|
|
145
161
|
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
|
|
146
162
|
inspect ??= defaultInspect;
|
|
147
|
-
const t = inspect(this.
|
|
148
|
-
const i = inspect(this.
|
|
163
|
+
const t = inspect(this.t, options);
|
|
164
|
+
const i = inspect(this.i, options);
|
|
149
165
|
return `new Timestamp({ t: ${t}, i: ${i} })`;
|
|
150
166
|
}
|
|
151
167
|
}
|