bson 6.10.3 → 7.0.0-alpha

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/lib/bson.rn.cjs CHANGED
@@ -150,13 +150,13 @@ class BSONOffsetError extends BSONError {
150
150
  get name() {
151
151
  return 'BSONOffsetError';
152
152
  }
153
+ offset;
153
154
  constructor(message, offset, options) {
154
155
  super(`${message}. offset: ${offset}`, options);
155
156
  this.offset = offset;
156
157
  }
157
158
  }
158
159
 
159
- const { TextDecoder } = require('../vendor/text-encoding');
160
160
  let TextDecoderFatal;
161
161
  let TextDecoderNonFatal;
162
162
  function parseUtf8(buffer, start, end, fatal) {
@@ -227,11 +227,15 @@ function tryWriteBasicLatin(destination, source, offset) {
227
227
  function nodejsMathRandomBytes(byteLength) {
228
228
  return nodeJsByteUtils.fromNumberArray(Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256)));
229
229
  }
230
+ function nodejsSecureRandomBytes(byteLength) {
231
+ return crypto.getRandomValues(nodeJsByteUtils.allocate(byteLength));
232
+ }
230
233
  const nodejsRandomBytes = (() => {
231
- try {
232
- return require('crypto').randomBytes;
234
+ const { crypto } = globalThis;
235
+ if (crypto != null && typeof crypto.getRandomValues === 'function') {
236
+ return nodejsSecureRandomBytes;
233
237
  }
234
- catch {
238
+ else {
235
239
  return nodejsMathRandomBytes;
236
240
  }
237
241
  })();
@@ -314,8 +318,6 @@ const nodeJsByteUtils = {
314
318
  }
315
319
  };
316
320
 
317
- const { TextEncoder } = require('../vendor/text-encoding');
318
- const { encode: btoa, decode: atob } = require('../vendor/base64');
319
321
  function isReactNative() {
320
322
  const { navigator } = globalThis;
321
323
  return typeof navigator === 'object' && navigator.product === 'ReactNative';
@@ -452,7 +454,11 @@ const webByteUtils = {
452
454
  const hasGlobalBuffer = typeof Buffer === 'function' && Buffer.prototype?._isBuffer !== true;
453
455
  const ByteUtils = hasGlobalBuffer ? nodeJsByteUtils : webByteUtils;
454
456
 
457
+ const bsonType = Symbol.for('@@mdb.bson.type');
455
458
  class BSONValue {
459
+ get [bsonType]() {
460
+ return this._bsontype;
461
+ }
456
462
  get [BSON_VERSION_SYMBOL]() {
457
463
  return BSON_MAJOR_VERSION;
458
464
  }
@@ -503,7 +509,7 @@ const NumberUtils = {
503
509
  source[offset + 1] * 256 +
504
510
  source[offset + 2] * 65536 +
505
511
  source[offset + 3] * 16777216);
506
- return (hi << BigInt(32)) + lo;
512
+ return (hi << 32n) + lo;
507
513
  },
508
514
  getFloat64LE: isBigEndian
509
515
  ? (source, offset) => {
@@ -549,7 +555,7 @@ const NumberUtils = {
549
555
  return 4;
550
556
  },
551
557
  setBigInt64LE(destination, offset, value) {
552
- const mask32bits = BigInt(0xffff_ffff);
558
+ const mask32bits = 0xffffffffn;
553
559
  let lo = Number(value & mask32bits);
554
560
  destination[offset] = lo;
555
561
  lo >>= 8;
@@ -558,7 +564,7 @@ const NumberUtils = {
558
564
  destination[offset + 2] = lo;
559
565
  lo >>= 8;
560
566
  destination[offset + 3] = lo;
561
- let hi = Number((value >> BigInt(32)) & mask32bits);
567
+ let hi = Number((value >> 32n) & mask32bits);
562
568
  destination[offset + 4] = hi;
563
569
  hi >>= 8;
564
570
  destination[offset + 5] = hi;
@@ -599,6 +605,27 @@ class Binary extends BSONValue {
599
605
  get _bsontype() {
600
606
  return 'Binary';
601
607
  }
608
+ static BSON_BINARY_SUBTYPE_DEFAULT = 0;
609
+ static BUFFER_SIZE = 256;
610
+ static SUBTYPE_DEFAULT = 0;
611
+ static SUBTYPE_FUNCTION = 1;
612
+ static SUBTYPE_BYTE_ARRAY = 2;
613
+ static SUBTYPE_UUID_OLD = 3;
614
+ static SUBTYPE_UUID = 4;
615
+ static SUBTYPE_MD5 = 5;
616
+ static SUBTYPE_ENCRYPTED = 6;
617
+ static SUBTYPE_COLUMN = 7;
618
+ static SUBTYPE_SENSITIVE = 8;
619
+ static SUBTYPE_VECTOR = 9;
620
+ static SUBTYPE_USER_DEFINED = 128;
621
+ static VECTOR_TYPE = Object.freeze({
622
+ Int8: 0x03,
623
+ Float32: 0x27,
624
+ PackedBit: 0x10
625
+ });
626
+ buffer;
627
+ sub_type;
628
+ position;
602
629
  constructor(buffer, subType) {
603
630
  super();
604
631
  if (!(buffer == null) &&
@@ -761,6 +788,7 @@ class Binary extends BSONValue {
761
788
  if (this.buffer[0] !== Binary.VECTOR_TYPE.Int8) {
762
789
  throw new BSONError('Binary datatype field is not Int8');
763
790
  }
791
+ validateBinaryVector(this);
764
792
  return new Int8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
765
793
  }
766
794
  toFloat32Array() {
@@ -770,6 +798,7 @@ class Binary extends BSONValue {
770
798
  if (this.buffer[0] !== Binary.VECTOR_TYPE.Float32) {
771
799
  throw new BSONError('Binary datatype field is not Float32');
772
800
  }
801
+ validateBinaryVector(this);
773
802
  const floatBytes = new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
774
803
  if (NumberUtils.isBigEndian)
775
804
  ByteUtils.swap32(floatBytes);
@@ -782,6 +811,7 @@ class Binary extends BSONValue {
782
811
  if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) {
783
812
  throw new BSONError('Binary datatype field is not packed bit');
784
813
  }
814
+ validateBinaryVector(this);
785
815
  return new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
786
816
  }
787
817
  toBits() {
@@ -791,6 +821,7 @@ class Binary extends BSONValue {
791
821
  if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) {
792
822
  throw new BSONError('Binary datatype field is not packed bit');
793
823
  }
824
+ validateBinaryVector(this);
794
825
  const byteCount = this.length() - 2;
795
826
  const bitCount = byteCount * 8 - this.buffer[1];
796
827
  const bits = new Int8Array(bitCount);
@@ -809,7 +840,9 @@ class Binary extends BSONValue {
809
840
  buffer[1] = 0;
810
841
  const intBytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
811
842
  buffer.set(intBytes, 2);
812
- return new this(buffer, this.SUBTYPE_VECTOR);
843
+ const bin = new this(buffer, this.SUBTYPE_VECTOR);
844
+ validateBinaryVector(bin);
845
+ return bin;
813
846
  }
814
847
  static fromFloat32Array(array) {
815
848
  const binaryBytes = ByteUtils.allocate(array.byteLength + 2);
@@ -819,14 +852,18 @@ class Binary extends BSONValue {
819
852
  binaryBytes.set(floatBytes, 2);
820
853
  if (NumberUtils.isBigEndian)
821
854
  ByteUtils.swap32(new Uint8Array(binaryBytes.buffer, 2));
822
- return new this(binaryBytes, this.SUBTYPE_VECTOR);
855
+ const bin = new this(binaryBytes, this.SUBTYPE_VECTOR);
856
+ validateBinaryVector(bin);
857
+ return bin;
823
858
  }
824
859
  static fromPackedBits(array, padding = 0) {
825
860
  const buffer = ByteUtils.allocate(array.byteLength + 2);
826
861
  buffer[0] = Binary.VECTOR_TYPE.PackedBit;
827
862
  buffer[1] = padding;
828
863
  buffer.set(array, 2);
829
- return new this(buffer, this.SUBTYPE_VECTOR);
864
+ const bin = new this(buffer, this.SUBTYPE_VECTOR);
865
+ validateBinaryVector(bin);
866
+ return bin;
830
867
  }
831
868
  static fromBits(bits) {
832
869
  const byteLength = (bits.length + 7) >>> 3;
@@ -848,24 +885,6 @@ class Binary extends BSONValue {
848
885
  return new this(bytes, Binary.SUBTYPE_VECTOR);
849
886
  }
850
887
  }
851
- Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
852
- Binary.BUFFER_SIZE = 256;
853
- Binary.SUBTYPE_DEFAULT = 0;
854
- Binary.SUBTYPE_FUNCTION = 1;
855
- Binary.SUBTYPE_BYTE_ARRAY = 2;
856
- Binary.SUBTYPE_UUID_OLD = 3;
857
- Binary.SUBTYPE_UUID = 4;
858
- Binary.SUBTYPE_MD5 = 5;
859
- Binary.SUBTYPE_ENCRYPTED = 6;
860
- Binary.SUBTYPE_COLUMN = 7;
861
- Binary.SUBTYPE_SENSITIVE = 8;
862
- Binary.SUBTYPE_VECTOR = 9;
863
- Binary.SUBTYPE_USER_DEFINED = 128;
864
- Binary.VECTOR_TYPE = Object.freeze({
865
- Int8: 0x03,
866
- Float32: 0x27,
867
- PackedBit: 0x10
868
- });
869
888
  function validateBinaryVector(vector) {
870
889
  if (vector.sub_type !== Binary.SUBTYPE_VECTOR)
871
890
  return;
@@ -876,6 +895,11 @@ function validateBinaryVector(vector) {
876
895
  padding !== 0) {
877
896
  throw new BSONError('Invalid Vector: padding must be zero for int8 and float32 vectors');
878
897
  }
898
+ if (datatype === Binary.VECTOR_TYPE.Float32) {
899
+ if (size !== 0 && size - 2 !== 0 && (size - 2) % 4 !== 0) {
900
+ throw new BSONError('Invalid Vector: Float32 vector must contain a multiple of 4 bytes');
901
+ }
902
+ }
879
903
  if (datatype === Binary.VECTOR_TYPE.PackedBit && padding !== 0 && size === 2) {
880
904
  throw new BSONError('Invalid Vector: padding must be zero for packed bit vectors that are empty');
881
905
  }
@@ -997,6 +1021,8 @@ class Code extends BSONValue {
997
1021
  get _bsontype() {
998
1022
  return 'Code';
999
1023
  }
1024
+ code;
1025
+ scope;
1000
1026
  constructor(code, scope) {
1001
1027
  super();
1002
1028
  this.code = code.toString();
@@ -1042,6 +1068,10 @@ class DBRef extends BSONValue {
1042
1068
  get _bsontype() {
1043
1069
  return 'DBRef';
1044
1070
  }
1071
+ collection;
1072
+ oid;
1073
+ db;
1074
+ fields;
1045
1075
  constructor(collection, oid, db, fields) {
1046
1076
  super();
1047
1077
  const parts = collection.split('.');
@@ -1151,6 +1181,9 @@ class Long extends BSONValue {
1151
1181
  get __isLong__() {
1152
1182
  return true;
1153
1183
  }
1184
+ high;
1185
+ low;
1186
+ unsigned;
1154
1187
  constructor(lowOrValue = 0, highOrUnsigned, unsigned) {
1155
1188
  super();
1156
1189
  const unsignedBool = typeof highOrUnsigned === 'boolean' ? highOrUnsigned : Boolean(unsigned);
@@ -1164,6 +1197,15 @@ class Long extends BSONValue {
1164
1197
  this.high = res.high;
1165
1198
  this.unsigned = res.unsigned;
1166
1199
  }
1200
+ static TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
1201
+ static MAX_UNSIGNED_VALUE = Long.fromBits(0xffffffff | 0, 0xffffffff | 0, true);
1202
+ static ZERO = Long.fromInt(0);
1203
+ static UZERO = Long.fromInt(0, true);
1204
+ static ONE = Long.fromInt(1);
1205
+ static UONE = Long.fromInt(1, true);
1206
+ static NEG_ONE = Long.fromInt(-1);
1207
+ static MAX_VALUE = Long.fromBits(0xffffffff | 0, 0x7fffffff | 0, false);
1208
+ static MIN_VALUE = Long.fromBits(0, 0x80000000 | 0, false);
1167
1209
  static fromBits(lowBits, highBits, unsigned) {
1168
1210
  return new Long(lowBits, highBits, unsigned);
1169
1211
  }
@@ -1214,8 +1256,8 @@ class Long extends BSONValue {
1214
1256
  return Long.fromBits(value % TWO_PWR_32_DBL | 0, (value / TWO_PWR_32_DBL) | 0, unsigned);
1215
1257
  }
1216
1258
  static fromBigInt(value, unsigned) {
1217
- const FROM_BIGINT_BIT_MASK = BigInt(0xffffffff);
1218
- const FROM_BIGINT_BIT_SHIFT = BigInt(32);
1259
+ const FROM_BIGINT_BIT_MASK = 0xffffffffn;
1260
+ const FROM_BIGINT_BIT_SHIFT = 32n;
1219
1261
  return new Long(Number(value & FROM_BIGINT_BIT_MASK), Number((value >> FROM_BIGINT_BIT_SHIFT) & FROM_BIGINT_BIT_MASK), unsigned);
1220
1262
  }
1221
1263
  static _fromString(str, unsigned, radix) {
@@ -1248,7 +1290,7 @@ class Long extends BSONValue {
1248
1290
  static fromStringStrict(str, unsignedOrRadix, radix) {
1249
1291
  let unsigned = false;
1250
1292
  if (typeof unsignedOrRadix === 'number') {
1251
- (radix = unsignedOrRadix), (unsignedOrRadix = false);
1293
+ ((radix = unsignedOrRadix), (unsignedOrRadix = false));
1252
1294
  }
1253
1295
  else {
1254
1296
  unsigned = !!unsignedOrRadix;
@@ -1270,7 +1312,7 @@ class Long extends BSONValue {
1270
1312
  static fromString(str, unsignedOrRadix, radix) {
1271
1313
  let unsigned = false;
1272
1314
  if (typeof unsignedOrRadix === 'number') {
1273
- (radix = unsignedOrRadix), (unsignedOrRadix = false);
1315
+ ((radix = unsignedOrRadix), (unsignedOrRadix = false));
1274
1316
  }
1275
1317
  else {
1276
1318
  unsigned = !!unsignedOrRadix;
@@ -1790,15 +1832,6 @@ class Long extends BSONValue {
1790
1832
  return `new Long(${longVal}${unsignedVal})`;
1791
1833
  }
1792
1834
  }
1793
- Long.TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
1794
- Long.MAX_UNSIGNED_VALUE = Long.fromBits(0xffffffff | 0, 0xffffffff | 0, true);
1795
- Long.ZERO = Long.fromInt(0);
1796
- Long.UZERO = Long.fromInt(0, true);
1797
- Long.ONE = Long.fromInt(1);
1798
- Long.UONE = Long.fromInt(1, true);
1799
- Long.NEG_ONE = Long.fromInt(-1);
1800
- Long.MAX_VALUE = Long.fromBits(0xffffffff | 0, 0x7fffffff | 0, false);
1801
- Long.MIN_VALUE = Long.fromBits(0, 0x80000000 | 0, false);
1802
1835
 
1803
1836
  const PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/;
1804
1837
  const PARSE_INF_REGEXP = /^(\+|-)?(Infinity|inf)$/i;
@@ -1879,6 +1912,7 @@ class Decimal128 extends BSONValue {
1879
1912
  get _bsontype() {
1880
1913
  return 'Decimal128';
1881
1914
  }
1915
+ bytes;
1882
1916
  constructor(bytes) {
1883
1917
  super();
1884
1918
  if (typeof bytes === 'string') {
@@ -2354,6 +2388,7 @@ class Double extends BSONValue {
2354
2388
  get _bsontype() {
2355
2389
  return 'Double';
2356
2390
  }
2391
+ value;
2357
2392
  constructor(value) {
2358
2393
  super();
2359
2394
  if (value instanceof Number) {
@@ -2417,6 +2452,7 @@ class Int32 extends BSONValue {
2417
2452
  get _bsontype() {
2418
2453
  return 'Int32';
2419
2454
  }
2455
+ value;
2420
2456
  constructor(value) {
2421
2457
  super();
2422
2458
  if (value instanceof Number) {
@@ -2495,11 +2531,14 @@ class MinKey extends BSONValue {
2495
2531
  }
2496
2532
 
2497
2533
  let PROCESS_UNIQUE = null;
2498
- const __idCache = new WeakMap();
2499
2534
  class ObjectId extends BSONValue {
2500
2535
  get _bsontype() {
2501
2536
  return 'ObjectId';
2502
2537
  }
2538
+ static index = Math.floor(Math.random() * 0xffffff);
2539
+ static cacheHexString = false;
2540
+ buffer;
2541
+ #cachedHexString = null;
2503
2542
  constructor(inputId) {
2504
2543
  super();
2505
2544
  let workingId;
@@ -2517,8 +2556,8 @@ class ObjectId extends BSONValue {
2517
2556
  else {
2518
2557
  workingId = inputId;
2519
2558
  }
2520
- if (workingId == null || typeof workingId === 'number') {
2521
- this.buffer = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
2559
+ if (workingId == null) {
2560
+ this.buffer = ObjectId.generate();
2522
2561
  }
2523
2562
  else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
2524
2563
  this.buffer = ByteUtils.toLocalBufferType(workingId);
@@ -2527,7 +2566,7 @@ class ObjectId extends BSONValue {
2527
2566
  if (ObjectId.validateHexString(workingId)) {
2528
2567
  this.buffer = ByteUtils.fromHex(workingId);
2529
2568
  if (ObjectId.cacheHexString) {
2530
- __idCache.set(this, workingId);
2569
+ this.#cachedHexString = workingId;
2531
2570
  }
2532
2571
  }
2533
2572
  else {
@@ -2544,7 +2583,7 @@ class ObjectId extends BSONValue {
2544
2583
  set id(value) {
2545
2584
  this.buffer = value;
2546
2585
  if (ObjectId.cacheHexString) {
2547
- __idCache.set(this, ByteUtils.toHex(value));
2586
+ this.#cachedHexString = ByteUtils.toHex(value);
2548
2587
  }
2549
2588
  }
2550
2589
  static validateHexString(string) {
@@ -2562,14 +2601,11 @@ class ObjectId extends BSONValue {
2562
2601
  return true;
2563
2602
  }
2564
2603
  toHexString() {
2565
- if (ObjectId.cacheHexString) {
2566
- const __id = __idCache.get(this);
2567
- if (__id)
2568
- return __id;
2569
- }
2604
+ if (this.#cachedHexString)
2605
+ return this.#cachedHexString.toLowerCase();
2570
2606
  const hexString = ByteUtils.toHex(this.id);
2571
2607
  if (ObjectId.cacheHexString) {
2572
- __idCache.set(this, hexString);
2608
+ this.#cachedHexString = hexString;
2573
2609
  }
2574
2610
  return hexString;
2575
2611
  }
@@ -2694,14 +2730,13 @@ class ObjectId extends BSONValue {
2694
2730
  return new ObjectId(doc.$oid);
2695
2731
  }
2696
2732
  isCached() {
2697
- return ObjectId.cacheHexString && __idCache.has(this);
2733
+ return ObjectId.cacheHexString && this.#cachedHexString != null;
2698
2734
  }
2699
2735
  inspect(depth, options, inspect) {
2700
2736
  inspect ??= defaultInspect;
2701
2737
  return `new ObjectId(${inspect(this.toHexString(), options)})`;
2702
2738
  }
2703
2739
  }
2704
- ObjectId.index = Math.floor(Math.random() * 0xffffff);
2705
2740
 
2706
2741
  function internalCalculateObjectSize(object, serializeFunctions, ignoreUndefined) {
2707
2742
  let totalLength = 4 + 1;
@@ -2871,6 +2906,8 @@ class BSONRegExp extends BSONValue {
2871
2906
  get _bsontype() {
2872
2907
  return 'BSONRegExp';
2873
2908
  }
2909
+ pattern;
2910
+ options;
2874
2911
  constructor(pattern, options) {
2875
2912
  super();
2876
2913
  this.pattern = pattern;
@@ -2931,6 +2968,7 @@ class BSONSymbol extends BSONValue {
2931
2968
  get _bsontype() {
2932
2969
  return 'BSONSymbol';
2933
2970
  }
2971
+ value;
2934
2972
  constructor(value) {
2935
2973
  super();
2936
2974
  this.value = value;
@@ -2961,6 +2999,10 @@ class Timestamp extends LongWithoutOverridesClass {
2961
2999
  get _bsontype() {
2962
3000
  return 'Timestamp';
2963
3001
  }
3002
+ get [bsonType]() {
3003
+ return 'Timestamp';
3004
+ }
3005
+ static MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
2964
3006
  get i() {
2965
3007
  return this.low >>> 0;
2966
3008
  }
@@ -3040,7 +3082,6 @@ class Timestamp extends LongWithoutOverridesClass {
3040
3082
  return `new Timestamp({ t: ${t}, i: ${i} })`;
3041
3083
  }
3042
3084
  }
3043
- Timestamp.MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
3044
3085
 
3045
3086
  const JS_INT_MAX_LONG = Long.fromNumber(JS_INT_MAX);
3046
3087
  const JS_INT_MIN_LONG = Long.fromNumber(JS_INT_MIN);
@@ -4389,6 +4430,29 @@ EJSON.serialize = EJSONserialize;
4389
4430
  EJSON.deserialize = EJSONdeserialize;
4390
4431
  Object.freeze(EJSON);
4391
4432
 
4433
+ const BSONElementType = {
4434
+ double: 1,
4435
+ string: 2,
4436
+ object: 3,
4437
+ array: 4,
4438
+ binData: 5,
4439
+ undefined: 6,
4440
+ objectId: 7,
4441
+ bool: 8,
4442
+ date: 9,
4443
+ null: 10,
4444
+ regex: 11,
4445
+ dbPointer: 12,
4446
+ javascript: 13,
4447
+ symbol: 14,
4448
+ javascriptWithScope: 15,
4449
+ int: 16,
4450
+ timestamp: 17,
4451
+ long: 18,
4452
+ decimal: 19,
4453
+ minKey: 255,
4454
+ maxKey: 127
4455
+ };
4392
4456
  function getSize(source, offset) {
4393
4457
  try {
4394
4458
  return NumberUtils.getNonnegativeInt32LE(source, offset);
@@ -4433,48 +4497,48 @@ function parseToElements(bytes, startOffset = 0) {
4433
4497
  const nameLength = findNull(bytes, offset) - nameOffset;
4434
4498
  offset += nameLength + 1;
4435
4499
  let length;
4436
- if (type === 1 ||
4437
- type === 18 ||
4438
- type === 9 ||
4439
- type === 17) {
4500
+ if (type === BSONElementType.double ||
4501
+ type === BSONElementType.long ||
4502
+ type === BSONElementType.date ||
4503
+ type === BSONElementType.timestamp) {
4440
4504
  length = 8;
4441
4505
  }
4442
- else if (type === 16) {
4506
+ else if (type === BSONElementType.int) {
4443
4507
  length = 4;
4444
4508
  }
4445
- else if (type === 7) {
4509
+ else if (type === BSONElementType.objectId) {
4446
4510
  length = 12;
4447
4511
  }
4448
- else if (type === 19) {
4512
+ else if (type === BSONElementType.decimal) {
4449
4513
  length = 16;
4450
4514
  }
4451
- else if (type === 8) {
4515
+ else if (type === BSONElementType.bool) {
4452
4516
  length = 1;
4453
4517
  }
4454
- else if (type === 10 ||
4455
- type === 6 ||
4456
- type === 127 ||
4457
- type === 255) {
4518
+ else if (type === BSONElementType.null ||
4519
+ type === BSONElementType.undefined ||
4520
+ type === BSONElementType.maxKey ||
4521
+ type === BSONElementType.minKey) {
4458
4522
  length = 0;
4459
4523
  }
4460
- else if (type === 11) {
4524
+ else if (type === BSONElementType.regex) {
4461
4525
  length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
4462
4526
  }
4463
- else if (type === 3 ||
4464
- type === 4 ||
4465
- type === 15) {
4527
+ else if (type === BSONElementType.object ||
4528
+ type === BSONElementType.array ||
4529
+ type === BSONElementType.javascriptWithScope) {
4466
4530
  length = getSize(bytes, offset);
4467
4531
  }
4468
- else if (type === 2 ||
4469
- type === 5 ||
4470
- type === 12 ||
4471
- type === 13 ||
4472
- type === 14) {
4532
+ else if (type === BSONElementType.string ||
4533
+ type === BSONElementType.binData ||
4534
+ type === BSONElementType.dbPointer ||
4535
+ type === BSONElementType.javascript ||
4536
+ type === BSONElementType.symbol) {
4473
4537
  length = getSize(bytes, offset) + 4;
4474
- if (type === 5) {
4538
+ if (type === BSONElementType.binData) {
4475
4539
  length += 1;
4476
4540
  }
4477
- if (type === 12) {
4541
+ if (type === BSONElementType.dbPointer) {
4478
4542
  length += 12;
4479
4543
  }
4480
4544
  }
@@ -4570,6 +4634,7 @@ var bson = /*#__PURE__*/Object.freeze({
4570
4634
  ObjectId: ObjectId,
4571
4635
  Timestamp: Timestamp,
4572
4636
  UUID: UUID,
4637
+ bsonType: bsonType,
4573
4638
  calculateObjectSize: calculateObjectSize,
4574
4639
  deserialize: deserialize,
4575
4640
  deserializeStream: deserializeStream,
@@ -4601,6 +4666,7 @@ exports.MinKey = MinKey;
4601
4666
  exports.ObjectId = ObjectId;
4602
4667
  exports.Timestamp = Timestamp;
4603
4668
  exports.UUID = UUID;
4669
+ exports.bsonType = bsonType;
4604
4670
  exports.calculateObjectSize = calculateObjectSize;
4605
4671
  exports.deserialize = deserialize;
4606
4672
  exports.deserializeStream = deserializeStream;