bson 6.8.0 → 6.10.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.
@@ -1,4 +1,4 @@
1
- import { Binary } from '../binary';
1
+ import { Binary, validateBinaryVector } from '../binary';
2
2
  import type { BSONSymbol, DBRef, Document, MaxKey } from '../bson';
3
3
  import type { Code } from '../code';
4
4
  import * as constants from '../constants';
@@ -495,6 +495,10 @@ function serializeBinary(buffer: Uint8Array, key: string, value: Binary, index:
495
495
  index += NumberUtils.setInt32LE(buffer, index, size);
496
496
  }
497
497
 
498
+ if (value.sub_type === Binary.SUBTYPE_VECTOR) {
499
+ validateBinaryVector(value);
500
+ }
501
+
498
502
  if (size <= 16) {
499
503
  for (let i = 0; i < size; i++) buffer[index + i] = data[i];
500
504
  } else {
@@ -633,77 +637,81 @@ export function serializeInto(
633
637
  value = value.toBSON();
634
638
  }
635
639
 
636
- if (typeof value === 'string') {
640
+ // Check the type of the value
641
+ const type = typeof value;
642
+
643
+ if (value === undefined) {
644
+ index = serializeNull(buffer, key, value, index);
645
+ } else if (value === null) {
646
+ index = serializeNull(buffer, key, value, index);
647
+ } else if (type === 'string') {
637
648
  index = serializeString(buffer, key, value, index);
638
- } else if (typeof value === 'number') {
649
+ } else if (type === 'number') {
639
650
  index = serializeNumber(buffer, key, value, index);
640
- } else if (typeof value === 'bigint') {
651
+ } else if (type === 'bigint') {
641
652
  index = serializeBigInt(buffer, key, value, index);
642
- } else if (typeof value === 'boolean') {
653
+ } else if (type === 'boolean') {
643
654
  index = serializeBoolean(buffer, key, value, index);
644
- } else if (value instanceof Date || isDate(value)) {
645
- index = serializeDate(buffer, key, value, index);
646
- } else if (value === undefined) {
647
- index = serializeNull(buffer, key, value, index);
648
- } else if (value === null) {
649
- index = serializeNull(buffer, key, value, index);
650
- } else if (isUint8Array(value)) {
651
- index = serializeBuffer(buffer, key, value, index);
652
- } else if (value instanceof RegExp || isRegExp(value)) {
653
- index = serializeRegExp(buffer, key, value, index);
654
- } else if (typeof value === 'object' && value._bsontype == null) {
655
- index = serializeObject(
656
- buffer,
657
- key,
658
- value,
659
- index,
660
- checkKeys,
661
- depth,
662
- serializeFunctions,
663
- ignoreUndefined,
664
- path
665
- );
666
- } else if (
667
- typeof value === 'object' &&
668
- value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
669
- ) {
670
- throw new BSONVersionError();
671
- } else if (value._bsontype === 'ObjectId') {
672
- index = serializeObjectId(buffer, key, value, index);
673
- } else if (value._bsontype === 'Decimal128') {
674
- index = serializeDecimal128(buffer, key, value, index);
675
- } else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
676
- index = serializeLong(buffer, key, value, index);
677
- } else if (value._bsontype === 'Double') {
678
- index = serializeDouble(buffer, key, value, index);
679
- } else if (typeof value === 'function' && serializeFunctions) {
655
+ } else if (type === 'object' && value._bsontype == null) {
656
+ if (value instanceof Date || isDate(value)) {
657
+ index = serializeDate(buffer, key, value, index);
658
+ } else if (value instanceof Uint8Array || isUint8Array(value)) {
659
+ index = serializeBuffer(buffer, key, value, index);
660
+ } else if (value instanceof RegExp || isRegExp(value)) {
661
+ index = serializeRegExp(buffer, key, value, index);
662
+ } else {
663
+ index = serializeObject(
664
+ buffer,
665
+ key,
666
+ value,
667
+ index,
668
+ checkKeys,
669
+ depth,
670
+ serializeFunctions,
671
+ ignoreUndefined,
672
+ path
673
+ );
674
+ }
675
+ } else if (type === 'object') {
676
+ if (value[constants.BSON_VERSION_SYMBOL] !== constants.BSON_MAJOR_VERSION) {
677
+ throw new BSONVersionError();
678
+ } else if (value._bsontype === 'ObjectId') {
679
+ index = serializeObjectId(buffer, key, value, index);
680
+ } else if (value._bsontype === 'Decimal128') {
681
+ index = serializeDecimal128(buffer, key, value, index);
682
+ } else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
683
+ index = serializeLong(buffer, key, value, index);
684
+ } else if (value._bsontype === 'Double') {
685
+ index = serializeDouble(buffer, key, value, index);
686
+ } else if (value._bsontype === 'Code') {
687
+ index = serializeCode(
688
+ buffer,
689
+ key,
690
+ value,
691
+ index,
692
+ checkKeys,
693
+ depth,
694
+ serializeFunctions,
695
+ ignoreUndefined,
696
+ path
697
+ );
698
+ } else if (value._bsontype === 'Binary') {
699
+ index = serializeBinary(buffer, key, value, index);
700
+ } else if (value._bsontype === 'BSONSymbol') {
701
+ index = serializeSymbol(buffer, key, value, index);
702
+ } else if (value._bsontype === 'DBRef') {
703
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
704
+ } else if (value._bsontype === 'BSONRegExp') {
705
+ index = serializeBSONRegExp(buffer, key, value, index);
706
+ } else if (value._bsontype === 'Int32') {
707
+ index = serializeInt32(buffer, key, value, index);
708
+ } else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
709
+ index = serializeMinMax(buffer, key, value, index);
710
+ } else if (typeof value._bsontype !== 'undefined') {
711
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
712
+ }
713
+ } else if (type === 'function' && serializeFunctions) {
680
714
  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
715
  }
708
716
  }
709
717
  } else if (object instanceof Map || isMap(object)) {
@@ -745,7 +753,11 @@ export function serializeInto(
745
753
  }
746
754
  }
747
755
 
748
- if (type === 'string') {
756
+ if (value === undefined) {
757
+ if (ignoreUndefined === false) index = serializeNull(buffer, key, value, index);
758
+ } else if (value === null) {
759
+ index = serializeNull(buffer, key, value, index);
760
+ } else if (type === 'string') {
749
761
  index = serializeString(buffer, key, value, index);
750
762
  } else if (type === 'number') {
751
763
  index = serializeNumber(buffer, key, value, index);
@@ -753,67 +765,66 @@ export function serializeInto(
753
765
  index = serializeBigInt(buffer, key, value, index);
754
766
  } else if (type === 'boolean') {
755
767
  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
768
  } else if (type === 'object' && value._bsontype == null) {
765
- index = serializeObject(
766
- buffer,
767
- key,
768
- value,
769
- index,
770
- checkKeys,
771
- depth,
772
- serializeFunctions,
773
- ignoreUndefined,
774
- path
775
- );
776
- } else if (
777
- typeof value === 'object' &&
778
- value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
779
- ) {
780
- throw new BSONVersionError();
781
- } else if (value._bsontype === 'ObjectId') {
782
- index = serializeObjectId(buffer, key, value, index);
783
- } else if (type === 'object' && value._bsontype === 'Decimal128') {
784
- index = serializeDecimal128(buffer, key, value, index);
785
- } else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
786
- index = serializeLong(buffer, key, value, index);
787
- } else if (value._bsontype === 'Double') {
788
- index = serializeDouble(buffer, key, value, index);
789
- } else if (value._bsontype === 'Code') {
790
- index = serializeCode(
791
- buffer,
792
- key,
793
- value,
794
- index,
795
- checkKeys,
796
- depth,
797
- serializeFunctions,
798
- ignoreUndefined,
799
- path
800
- );
801
- } else if (typeof value === 'function' && serializeFunctions) {
769
+ if (value instanceof Date || isDate(value)) {
770
+ index = serializeDate(buffer, key, value, index);
771
+ } else if (value instanceof Uint8Array || isUint8Array(value)) {
772
+ index = serializeBuffer(buffer, key, value, index);
773
+ } else if (value instanceof RegExp || isRegExp(value)) {
774
+ index = serializeRegExp(buffer, key, value, index);
775
+ } else {
776
+ index = serializeObject(
777
+ buffer,
778
+ key,
779
+ value,
780
+ index,
781
+ checkKeys,
782
+ depth,
783
+ serializeFunctions,
784
+ ignoreUndefined,
785
+ path
786
+ );
787
+ }
788
+ } else if (type === 'object') {
789
+ if (value[constants.BSON_VERSION_SYMBOL] !== constants.BSON_MAJOR_VERSION) {
790
+ throw new BSONVersionError();
791
+ } else if (value._bsontype === 'ObjectId') {
792
+ index = serializeObjectId(buffer, key, value, index);
793
+ } else if (value._bsontype === 'Decimal128') {
794
+ index = serializeDecimal128(buffer, key, value, index);
795
+ } else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
796
+ index = serializeLong(buffer, key, value, index);
797
+ } else if (value._bsontype === 'Double') {
798
+ index = serializeDouble(buffer, key, value, index);
799
+ } else if (value._bsontype === 'Code') {
800
+ index = serializeCode(
801
+ buffer,
802
+ key,
803
+ value,
804
+ index,
805
+ checkKeys,
806
+ depth,
807
+ serializeFunctions,
808
+ ignoreUndefined,
809
+ path
810
+ );
811
+ } else if (value._bsontype === 'Binary') {
812
+ index = serializeBinary(buffer, key, value, index);
813
+ } else if (value._bsontype === 'BSONSymbol') {
814
+ index = serializeSymbol(buffer, key, value, index);
815
+ } else if (value._bsontype === 'DBRef') {
816
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
817
+ } else if (value._bsontype === 'BSONRegExp') {
818
+ index = serializeBSONRegExp(buffer, key, value, index);
819
+ } else if (value._bsontype === 'Int32') {
820
+ index = serializeInt32(buffer, key, value, index);
821
+ } else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
822
+ index = serializeMinMax(buffer, key, value, index);
823
+ } else if (typeof value._bsontype !== 'undefined') {
824
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
825
+ }
826
+ } else if (type === 'function' && serializeFunctions) {
802
827
  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
828
  }
818
829
  }
819
830
  } else {
@@ -853,7 +864,11 @@ export function serializeInto(
853
864
  }
854
865
  }
855
866
 
856
- if (type === 'string') {
867
+ if (value === undefined) {
868
+ if (ignoreUndefined === false) index = serializeNull(buffer, key, value, index);
869
+ } else if (value === null) {
870
+ index = serializeNull(buffer, key, value, index);
871
+ } else if (type === 'string') {
857
872
  index = serializeString(buffer, key, value, index);
858
873
  } else if (type === 'number') {
859
874
  index = serializeNumber(buffer, key, value, index);
@@ -861,69 +876,66 @@ export function serializeInto(
861
876
  index = serializeBigInt(buffer, key, value, index);
862
877
  } else if (type === 'boolean') {
863
878
  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
879
  } else if (type === 'object' && value._bsontype == null) {
875
- index = serializeObject(
876
- buffer,
877
- key,
878
- value,
879
- index,
880
- checkKeys,
881
- depth,
882
- serializeFunctions,
883
- ignoreUndefined,
884
- path
885
- );
886
- } else if (
887
- typeof value === 'object' &&
888
- value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
889
- ) {
890
- throw new BSONVersionError();
891
- } else if (value._bsontype === 'ObjectId') {
892
- index = serializeObjectId(buffer, key, value, index);
893
- } else if (type === 'object' && value._bsontype === 'Decimal128') {
894
- index = serializeDecimal128(buffer, key, value, index);
895
- } else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
896
- index = serializeLong(buffer, key, value, index);
897
- } else if (value._bsontype === 'Double') {
898
- index = serializeDouble(buffer, key, value, index);
899
- } else if (value._bsontype === 'Code') {
900
- index = serializeCode(
901
- buffer,
902
- key,
903
- value,
904
- index,
905
- checkKeys,
906
- depth,
907
- serializeFunctions,
908
- ignoreUndefined,
909
- path
910
- );
911
- } else if (typeof value === 'function' && serializeFunctions) {
880
+ if (value instanceof Date || isDate(value)) {
881
+ index = serializeDate(buffer, key, value, index);
882
+ } else if (value instanceof Uint8Array || isUint8Array(value)) {
883
+ index = serializeBuffer(buffer, key, value, index);
884
+ } else if (value instanceof RegExp || isRegExp(value)) {
885
+ index = serializeRegExp(buffer, key, value, index);
886
+ } else {
887
+ index = serializeObject(
888
+ buffer,
889
+ key,
890
+ value,
891
+ index,
892
+ checkKeys,
893
+ depth,
894
+ serializeFunctions,
895
+ ignoreUndefined,
896
+ path
897
+ );
898
+ }
899
+ } else if (type === 'object') {
900
+ if (value[constants.BSON_VERSION_SYMBOL] !== constants.BSON_MAJOR_VERSION) {
901
+ throw new BSONVersionError();
902
+ } else if (value._bsontype === 'ObjectId') {
903
+ index = serializeObjectId(buffer, key, value, index);
904
+ } else if (value._bsontype === 'Decimal128') {
905
+ index = serializeDecimal128(buffer, key, value, index);
906
+ } else if (value._bsontype === 'Long' || value._bsontype === 'Timestamp') {
907
+ index = serializeLong(buffer, key, value, index);
908
+ } else if (value._bsontype === 'Double') {
909
+ index = serializeDouble(buffer, key, value, index);
910
+ } else if (value._bsontype === 'Code') {
911
+ index = serializeCode(
912
+ buffer,
913
+ key,
914
+ value,
915
+ index,
916
+ checkKeys,
917
+ depth,
918
+ serializeFunctions,
919
+ ignoreUndefined,
920
+ path
921
+ );
922
+ } else if (value._bsontype === 'Binary') {
923
+ index = serializeBinary(buffer, key, value, index);
924
+ } else if (value._bsontype === 'BSONSymbol') {
925
+ index = serializeSymbol(buffer, key, value, index);
926
+ } else if (value._bsontype === 'DBRef') {
927
+ index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
928
+ } else if (value._bsontype === 'BSONRegExp') {
929
+ index = serializeBSONRegExp(buffer, key, value, index);
930
+ } else if (value._bsontype === 'Int32') {
931
+ index = serializeInt32(buffer, key, value, index);
932
+ } else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
933
+ index = serializeMinMax(buffer, key, value, index);
934
+ } else if (typeof value._bsontype !== 'undefined') {
935
+ throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
936
+ }
937
+ } else if (type === 'function' && serializeFunctions) {
912
938
  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
939
  }
928
940
  }
929
941
  }
@@ -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
- return ['[object ArrayBuffer]', '[object SharedArrayBuffer]'].includes(
3
- Object.prototype.toString.call(value)
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
- return Object.prototype.toString.call(value) === '[object Uint8Array]';
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
- return Object.prototype.toString.call(value) === '[object BigInt64Array]';
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
- return Object.prototype.toString.call(value) === '[object BigUint64Array]';
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
- return Object.prototype.toString.call(d) === '[object RegExp]';
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
- return Object.prototype.toString.call(d) === '[object Map]';
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
- return Object.prototype.toString.call(d) === '[object Date]';
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.high >>> 0, i: this.low >>> 0 } };
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.high >>> 0, options);
148
- const i = inspect(this.low >>> 0, options);
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
  }
@@ -39,6 +39,8 @@ export type ByteUtils = {
39
39
  encodeUTF8Into: (destination: Uint8Array, source: string, byteOffset: number) => number;
40
40
  /** Generate a Uint8Array filled with random bytes with byteLength */
41
41
  randomBytes: (byteLength: number) => Uint8Array;
42
+ /** Interprets `buffer` as an array of 32-bit values and swaps the byte order in-place. */
43
+ swap32: (buffer: Uint8Array) => Uint8Array;
42
44
  };
43
45
 
44
46
  declare const Buffer: { new (): unknown; prototype?: { _isBuffer?: boolean } } | undefined;
@@ -9,6 +9,7 @@ type NodeJsBuffer = ArrayBufferView &
9
9
  copy(target: Uint8Array, targetStart: number, sourceStart: number, sourceEnd: number): number;
10
10
  toString: (this: Uint8Array, encoding: NodeJsEncoding, start?: number, end?: number) => string;
11
11
  equals: (this: Uint8Array, other: Uint8Array) => boolean;
12
+ swap32: (this: NodeJsBuffer) => NodeJsBuffer;
12
13
  };
13
14
  type NodeJsBufferConstructor = Omit<Uint8ArrayConstructor, 'from'> & {
14
15
  alloc: (size: number) => NodeJsBuffer;
@@ -83,7 +84,7 @@ export const nodeJsByteUtils = {
83
84
  return Buffer.from(potentialBuffer);
84
85
  }
85
86
 
86
- throw new BSONError(`Cannot create Buffer from ${String(potentialBuffer)}`);
87
+ throw new BSONError(`Cannot create Buffer from the passed potentialBuffer.`);
87
88
  },
88
89
 
89
90
  allocate(size: number): NodeJsBuffer {
@@ -159,5 +160,9 @@ export const nodeJsByteUtils = {
159
160
  return nodeJsByteUtils.toLocalBufferType(buffer).write(source, byteOffset, undefined, 'utf8');
160
161
  },
161
162
 
162
- randomBytes: nodejsRandomBytes
163
+ randomBytes: nodejsRandomBytes,
164
+
165
+ swap32(buffer: Uint8Array): NodeJsBuffer {
166
+ return nodeJsByteUtils.toLocalBufferType(buffer).swap32();
167
+ }
163
168
  };
@@ -13,6 +13,8 @@ const isBigEndian = FLOAT_BYTES[7] === 0;
13
13
  * A collection of functions that get or set various numeric types and bit widths from a Uint8Array.
14
14
  */
15
15
  export type NumberUtils = {
16
+ /** Is true if the current system is big endian. */
17
+ isBigEndian: boolean;
16
18
  /**
17
19
  * Parses a signed int32 at offset. Throws a `RangeError` if value is negative.
18
20
  */
@@ -35,6 +37,8 @@ export type NumberUtils = {
35
37
  * @public
36
38
  */
37
39
  export const NumberUtils: NumberUtils = {
40
+ isBigEndian,
41
+
38
42
  getNonnegativeInt32LE(source: Uint8Array, offset: number): number {
39
43
  if (source[offset + 3] > 127) {
40
44
  throw new RangeError(`Size cannot be negative at offset: ${offset}`);
@@ -100,7 +100,7 @@ export const webByteUtils = {
100
100
  return new Uint8Array(potentialUint8array);
101
101
  }
102
102
 
103
- throw new BSONError(`Cannot make a Uint8Array from ${String(potentialUint8array)}`);
103
+ throw new BSONError(`Cannot make a Uint8Array from passed potentialBuffer.`);
104
104
  },
105
105
 
106
106
  allocate(size: number): Uint8Array {
@@ -193,5 +193,24 @@ export const webByteUtils = {
193
193
  return bytes.byteLength;
194
194
  },
195
195
 
196
- randomBytes: webRandomBytes
196
+ randomBytes: webRandomBytes,
197
+
198
+ swap32(buffer: Uint8Array): Uint8Array {
199
+ if (buffer.length % 4 !== 0) {
200
+ throw new RangeError('Buffer size must be a multiple of 32-bits');
201
+ }
202
+
203
+ for (let i = 0; i < buffer.length; i += 4) {
204
+ const byte0 = buffer[i];
205
+ const byte1 = buffer[i + 1];
206
+ const byte2 = buffer[i + 2];
207
+ const byte3 = buffer[i + 3];
208
+ buffer[i] = byte3;
209
+ buffer[i + 1] = byte2;
210
+ buffer[i + 2] = byte1;
211
+ buffer[i + 3] = byte0;
212
+ }
213
+
214
+ return buffer;
215
+ }
197
216
  };