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/README.md CHANGED
@@ -230,7 +230,17 @@ try {
230
230
 
231
231
  ## React Native
232
232
 
233
- BSON vendors the required polyfills for `TextEncoder`, `TextDecoder`, `atob`, `btoa` imported from React Native and therefore doesn't expect users to polyfill these. One additional polyfill, `crypto.getRandomValues` is recommended and can be installed with the following command:
233
+ js-bson requires the `atob`, `btoa` and `TextEncoder` globals. Older versions of React Native did not support these global objects, and so
234
+ [js-bson v5.4.0](https://github.com/mongodb/js-bson/releases/tag/v5.4.0) added support for bundled polyfills for these globals. Newer versions
235
+ of Hermes includes these globals, and so the polyfills for are no longer needed in the js-bson package.
236
+
237
+ If you find yourself on a version of React Native that does not have these globals, either:
238
+
239
+ 1. polyfill them yourself
240
+ 2. upgrade to a later version of hermes
241
+ 3. use a version of js-bson `>=5.4.0` and `<7.0.0`
242
+
243
+ One additional polyfill, `crypto.getRandomValues` is recommended and can be installed with the following command:
234
244
 
235
245
  ```sh
236
246
  npm install --save react-native-get-random-values
package/bson.d.ts CHANGED
@@ -12,7 +12,10 @@ export declare class Binary extends BSONValue {
12
12
  static readonly SUBTYPE_DEFAULT = 0;
13
13
  /** Function BSON type */
14
14
  static readonly SUBTYPE_FUNCTION = 1;
15
- /** Byte Array BSON type */
15
+ /**
16
+ * Legacy default BSON Binary type
17
+ * @deprecated BSON Binary subtype 2 is deprecated in the BSON specification
18
+ */
16
19
  static readonly SUBTYPE_BYTE_ARRAY = 2;
17
20
  /** Deprecated UUID BSON type @deprecated Please use SUBTYPE_UUID */
18
21
  static readonly SUBTYPE_UUID_OLD = 3;
@@ -234,6 +237,8 @@ declare namespace BSON {
234
237
  BSONRegExp,
235
238
  Decimal128,
236
239
  BSONValue,
240
+ bsonType,
241
+ BSONTypeTag,
237
242
  BSONError,
238
243
  BSONVersionError,
239
244
  BSONRuntimeError,
@@ -408,10 +413,17 @@ export declare const BSONType: Readonly<{
408
413
  /** @public */
409
414
  export declare type BSONType = (typeof BSONType)[keyof typeof BSONType];
410
415
 
416
+ /** @public */
417
+ export declare const bsonType: unique symbol;
418
+
419
+ /** @public */
420
+ export declare type BSONTypeTag = 'BSONRegExp' | 'BSONSymbol' | 'ObjectId' | 'Binary' | 'Decimal128' | 'Double' | 'Int32' | 'Long' | 'MaxKey' | 'MinKey' | 'Timestamp' | 'Code' | 'DBRef';
421
+
411
422
  /** @public */
412
423
  export declare abstract class BSONValue {
413
424
  /** @public */
414
- abstract get _bsontype(): string;
425
+ abstract get _bsontype(): BSONTypeTag;
426
+ get [bsonType](): this['_bsontype'];
415
427
  /* Excluded from this release type: [BSON_VERSION_SYMBOL] */
416
428
  /**
417
429
  * @public
@@ -1341,17 +1353,13 @@ declare const NumberUtils: NumberUtils;
1341
1353
  * @category BSONType
1342
1354
  */
1343
1355
  export declare class ObjectId extends BSONValue {
1356
+ #private;
1344
1357
  get _bsontype(): 'ObjectId';
1345
1358
  /* Excluded from this release type: index */
1346
1359
  static cacheHexString: boolean;
1347
1360
  /* Excluded from this release type: buffer */
1348
- /**
1349
- * Create ObjectId from a number.
1350
- *
1351
- * @param inputId - A number.
1352
- * @deprecated Instead, use `static createFromTime()` to set a numeric value for the new ObjectId.
1353
- */
1354
- constructor(inputId: number);
1361
+ /** To generate a new ObjectId, use ObjectId() with no argument. */
1362
+ constructor();
1355
1363
  /**
1356
1364
  * Create ObjectId from a 24 character hex string.
1357
1365
  *
@@ -1376,14 +1384,12 @@ export declare class ObjectId extends BSONValue {
1376
1384
  * @param inputId - A 12 byte binary Buffer.
1377
1385
  */
1378
1386
  constructor(inputId: Uint8Array);
1379
- /** To generate a new ObjectId, use ObjectId() with no argument. */
1380
- constructor();
1381
1387
  /**
1382
1388
  * Implementation overload.
1383
1389
  *
1384
1390
  * @param inputId - All input types that are used in the constructor implementation.
1385
1391
  */
1386
- constructor(inputId?: string | number | ObjectId | ObjectIdLike | Uint8Array);
1392
+ constructor(inputId?: string | ObjectId | ObjectIdLike | Uint8Array);
1387
1393
  /**
1388
1394
  * The ObjectId bytes
1389
1395
  * @readonly
@@ -1436,7 +1442,7 @@ export declare class ObjectId extends BSONValue {
1436
1442
  * Checks if a value can be used to create a valid bson ObjectId
1437
1443
  * @param id - any JS value
1438
1444
  */
1439
- static isValid(id: string | number | ObjectId | ObjectIdLike | Uint8Array): boolean;
1445
+ static isValid(id: string | ObjectId | ObjectIdLike | Uint8Array): boolean;
1440
1446
  /* Excluded from this release type: toExtendedJSON */
1441
1447
  /* Excluded from this release type: fromExtendedJSON */
1442
1448
  /* Excluded from this release type: isCached */
@@ -1584,6 +1590,7 @@ declare function stringify(value: any, replacer?: (number | string)[] | ((this:
1584
1590
  */
1585
1591
  export declare class Timestamp extends LongWithoutOverridesClass {
1586
1592
  get _bsontype(): 'Timestamp';
1593
+ get [bsonType](): 'Timestamp';
1587
1594
  static readonly MAX_VALUE: Long;
1588
1595
  /**
1589
1596
  * An incrementing ordinal for operations within a given second.
@@ -1643,7 +1650,7 @@ export declare interface TimestampExtended {
1643
1650
  }
1644
1651
 
1645
1652
  /** @public */
1646
- export declare type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect';
1653
+ export declare type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect' | typeof bsonType;
1647
1654
 
1648
1655
  /**
1649
1656
  * A class representation of the BSON UUID type.
@@ -143,6 +143,7 @@ class BSONOffsetError extends BSONError {
143
143
  get name() {
144
144
  return 'BSONOffsetError';
145
145
  }
146
+ offset;
146
147
  constructor(message, offset, options) {
147
148
  super(`${message}. offset: ${offset}`, options);
148
149
  this.offset = offset;
@@ -219,11 +220,15 @@ function tryWriteBasicLatin(destination, source, offset) {
219
220
  function nodejsMathRandomBytes(byteLength) {
220
221
  return nodeJsByteUtils.fromNumberArray(Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256)));
221
222
  }
223
+ function nodejsSecureRandomBytes(byteLength) {
224
+ return crypto.getRandomValues(nodeJsByteUtils.allocate(byteLength));
225
+ }
222
226
  const nodejsRandomBytes = (() => {
223
- try {
224
- return require('crypto').randomBytes;
227
+ const { crypto } = globalThis;
228
+ if (crypto != null && typeof crypto.getRandomValues === 'function') {
229
+ return nodejsSecureRandomBytes;
225
230
  }
226
- catch {
231
+ else {
227
232
  return nodejsMathRandomBytes;
228
233
  }
229
234
  })();
@@ -442,7 +447,11 @@ const webByteUtils = {
442
447
  const hasGlobalBuffer = typeof Buffer === 'function' && Buffer.prototype?._isBuffer !== true;
443
448
  const ByteUtils = hasGlobalBuffer ? nodeJsByteUtils : webByteUtils;
444
449
 
450
+ const bsonType = Symbol.for('@@mdb.bson.type');
445
451
  class BSONValue {
452
+ get [bsonType]() {
453
+ return this._bsontype;
454
+ }
446
455
  get [BSON_VERSION_SYMBOL]() {
447
456
  return BSON_MAJOR_VERSION;
448
457
  }
@@ -493,7 +502,7 @@ const NumberUtils = {
493
502
  source[offset + 1] * 256 +
494
503
  source[offset + 2] * 65536 +
495
504
  source[offset + 3] * 16777216);
496
- return (hi << BigInt(32)) + lo;
505
+ return (hi << 32n) + lo;
497
506
  },
498
507
  getFloat64LE: isBigEndian
499
508
  ? (source, offset) => {
@@ -539,7 +548,7 @@ const NumberUtils = {
539
548
  return 4;
540
549
  },
541
550
  setBigInt64LE(destination, offset, value) {
542
- const mask32bits = BigInt(0xffff_ffff);
551
+ const mask32bits = 0xffffffffn;
543
552
  let lo = Number(value & mask32bits);
544
553
  destination[offset] = lo;
545
554
  lo >>= 8;
@@ -548,7 +557,7 @@ const NumberUtils = {
548
557
  destination[offset + 2] = lo;
549
558
  lo >>= 8;
550
559
  destination[offset + 3] = lo;
551
- let hi = Number((value >> BigInt(32)) & mask32bits);
560
+ let hi = Number((value >> 32n) & mask32bits);
552
561
  destination[offset + 4] = hi;
553
562
  hi >>= 8;
554
563
  destination[offset + 5] = hi;
@@ -589,6 +598,27 @@ class Binary extends BSONValue {
589
598
  get _bsontype() {
590
599
  return 'Binary';
591
600
  }
601
+ static BSON_BINARY_SUBTYPE_DEFAULT = 0;
602
+ static BUFFER_SIZE = 256;
603
+ static SUBTYPE_DEFAULT = 0;
604
+ static SUBTYPE_FUNCTION = 1;
605
+ static SUBTYPE_BYTE_ARRAY = 2;
606
+ static SUBTYPE_UUID_OLD = 3;
607
+ static SUBTYPE_UUID = 4;
608
+ static SUBTYPE_MD5 = 5;
609
+ static SUBTYPE_ENCRYPTED = 6;
610
+ static SUBTYPE_COLUMN = 7;
611
+ static SUBTYPE_SENSITIVE = 8;
612
+ static SUBTYPE_VECTOR = 9;
613
+ static SUBTYPE_USER_DEFINED = 128;
614
+ static VECTOR_TYPE = Object.freeze({
615
+ Int8: 0x03,
616
+ Float32: 0x27,
617
+ PackedBit: 0x10
618
+ });
619
+ buffer;
620
+ sub_type;
621
+ position;
592
622
  constructor(buffer, subType) {
593
623
  super();
594
624
  if (!(buffer == null) &&
@@ -751,6 +781,7 @@ class Binary extends BSONValue {
751
781
  if (this.buffer[0] !== Binary.VECTOR_TYPE.Int8) {
752
782
  throw new BSONError('Binary datatype field is not Int8');
753
783
  }
784
+ validateBinaryVector(this);
754
785
  return new Int8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
755
786
  }
756
787
  toFloat32Array() {
@@ -760,6 +791,7 @@ class Binary extends BSONValue {
760
791
  if (this.buffer[0] !== Binary.VECTOR_TYPE.Float32) {
761
792
  throw new BSONError('Binary datatype field is not Float32');
762
793
  }
794
+ validateBinaryVector(this);
763
795
  const floatBytes = new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
764
796
  if (NumberUtils.isBigEndian)
765
797
  ByteUtils.swap32(floatBytes);
@@ -772,6 +804,7 @@ class Binary extends BSONValue {
772
804
  if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) {
773
805
  throw new BSONError('Binary datatype field is not packed bit');
774
806
  }
807
+ validateBinaryVector(this);
775
808
  return new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
776
809
  }
777
810
  toBits() {
@@ -781,6 +814,7 @@ class Binary extends BSONValue {
781
814
  if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) {
782
815
  throw new BSONError('Binary datatype field is not packed bit');
783
816
  }
817
+ validateBinaryVector(this);
784
818
  const byteCount = this.length() - 2;
785
819
  const bitCount = byteCount * 8 - this.buffer[1];
786
820
  const bits = new Int8Array(bitCount);
@@ -799,7 +833,9 @@ class Binary extends BSONValue {
799
833
  buffer[1] = 0;
800
834
  const intBytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
801
835
  buffer.set(intBytes, 2);
802
- return new this(buffer, this.SUBTYPE_VECTOR);
836
+ const bin = new this(buffer, this.SUBTYPE_VECTOR);
837
+ validateBinaryVector(bin);
838
+ return bin;
803
839
  }
804
840
  static fromFloat32Array(array) {
805
841
  const binaryBytes = ByteUtils.allocate(array.byteLength + 2);
@@ -809,14 +845,18 @@ class Binary extends BSONValue {
809
845
  binaryBytes.set(floatBytes, 2);
810
846
  if (NumberUtils.isBigEndian)
811
847
  ByteUtils.swap32(new Uint8Array(binaryBytes.buffer, 2));
812
- return new this(binaryBytes, this.SUBTYPE_VECTOR);
848
+ const bin = new this(binaryBytes, this.SUBTYPE_VECTOR);
849
+ validateBinaryVector(bin);
850
+ return bin;
813
851
  }
814
852
  static fromPackedBits(array, padding = 0) {
815
853
  const buffer = ByteUtils.allocate(array.byteLength + 2);
816
854
  buffer[0] = Binary.VECTOR_TYPE.PackedBit;
817
855
  buffer[1] = padding;
818
856
  buffer.set(array, 2);
819
- return new this(buffer, this.SUBTYPE_VECTOR);
857
+ const bin = new this(buffer, this.SUBTYPE_VECTOR);
858
+ validateBinaryVector(bin);
859
+ return bin;
820
860
  }
821
861
  static fromBits(bits) {
822
862
  const byteLength = (bits.length + 7) >>> 3;
@@ -838,24 +878,6 @@ class Binary extends BSONValue {
838
878
  return new this(bytes, Binary.SUBTYPE_VECTOR);
839
879
  }
840
880
  }
841
- Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
842
- Binary.BUFFER_SIZE = 256;
843
- Binary.SUBTYPE_DEFAULT = 0;
844
- Binary.SUBTYPE_FUNCTION = 1;
845
- Binary.SUBTYPE_BYTE_ARRAY = 2;
846
- Binary.SUBTYPE_UUID_OLD = 3;
847
- Binary.SUBTYPE_UUID = 4;
848
- Binary.SUBTYPE_MD5 = 5;
849
- Binary.SUBTYPE_ENCRYPTED = 6;
850
- Binary.SUBTYPE_COLUMN = 7;
851
- Binary.SUBTYPE_SENSITIVE = 8;
852
- Binary.SUBTYPE_VECTOR = 9;
853
- Binary.SUBTYPE_USER_DEFINED = 128;
854
- Binary.VECTOR_TYPE = Object.freeze({
855
- Int8: 0x03,
856
- Float32: 0x27,
857
- PackedBit: 0x10
858
- });
859
881
  function validateBinaryVector(vector) {
860
882
  if (vector.sub_type !== Binary.SUBTYPE_VECTOR)
861
883
  return;
@@ -866,6 +888,11 @@ function validateBinaryVector(vector) {
866
888
  padding !== 0) {
867
889
  throw new BSONError('Invalid Vector: padding must be zero for int8 and float32 vectors');
868
890
  }
891
+ if (datatype === Binary.VECTOR_TYPE.Float32) {
892
+ if (size !== 0 && size - 2 !== 0 && (size - 2) % 4 !== 0) {
893
+ throw new BSONError('Invalid Vector: Float32 vector must contain a multiple of 4 bytes');
894
+ }
895
+ }
869
896
  if (datatype === Binary.VECTOR_TYPE.PackedBit && padding !== 0 && size === 2) {
870
897
  throw new BSONError('Invalid Vector: padding must be zero for packed bit vectors that are empty');
871
898
  }
@@ -987,6 +1014,8 @@ class Code extends BSONValue {
987
1014
  get _bsontype() {
988
1015
  return 'Code';
989
1016
  }
1017
+ code;
1018
+ scope;
990
1019
  constructor(code, scope) {
991
1020
  super();
992
1021
  this.code = code.toString();
@@ -1032,6 +1061,10 @@ class DBRef extends BSONValue {
1032
1061
  get _bsontype() {
1033
1062
  return 'DBRef';
1034
1063
  }
1064
+ collection;
1065
+ oid;
1066
+ db;
1067
+ fields;
1035
1068
  constructor(collection, oid, db, fields) {
1036
1069
  super();
1037
1070
  const parts = collection.split('.');
@@ -1141,6 +1174,9 @@ class Long extends BSONValue {
1141
1174
  get __isLong__() {
1142
1175
  return true;
1143
1176
  }
1177
+ high;
1178
+ low;
1179
+ unsigned;
1144
1180
  constructor(lowOrValue = 0, highOrUnsigned, unsigned) {
1145
1181
  super();
1146
1182
  const unsignedBool = typeof highOrUnsigned === 'boolean' ? highOrUnsigned : Boolean(unsigned);
@@ -1154,6 +1190,15 @@ class Long extends BSONValue {
1154
1190
  this.high = res.high;
1155
1191
  this.unsigned = res.unsigned;
1156
1192
  }
1193
+ static TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
1194
+ static MAX_UNSIGNED_VALUE = Long.fromBits(0xffffffff | 0, 0xffffffff | 0, true);
1195
+ static ZERO = Long.fromInt(0);
1196
+ static UZERO = Long.fromInt(0, true);
1197
+ static ONE = Long.fromInt(1);
1198
+ static UONE = Long.fromInt(1, true);
1199
+ static NEG_ONE = Long.fromInt(-1);
1200
+ static MAX_VALUE = Long.fromBits(0xffffffff | 0, 0x7fffffff | 0, false);
1201
+ static MIN_VALUE = Long.fromBits(0, 0x80000000 | 0, false);
1157
1202
  static fromBits(lowBits, highBits, unsigned) {
1158
1203
  return new Long(lowBits, highBits, unsigned);
1159
1204
  }
@@ -1194,7 +1239,7 @@ class Long extends BSONValue {
1194
1239
  return Long.MAX_UNSIGNED_VALUE;
1195
1240
  }
1196
1241
  else {
1197
- if (value <= -9223372036854776e3)
1242
+ if (value <= -TWO_PWR_63_DBL)
1198
1243
  return Long.MIN_VALUE;
1199
1244
  if (value + 1 >= TWO_PWR_63_DBL)
1200
1245
  return Long.MAX_VALUE;
@@ -1204,8 +1249,8 @@ class Long extends BSONValue {
1204
1249
  return Long.fromBits(value % TWO_PWR_32_DBL | 0, (value / TWO_PWR_32_DBL) | 0, unsigned);
1205
1250
  }
1206
1251
  static fromBigInt(value, unsigned) {
1207
- const FROM_BIGINT_BIT_MASK = BigInt(0xffffffff);
1208
- const FROM_BIGINT_BIT_SHIFT = BigInt(32);
1252
+ const FROM_BIGINT_BIT_MASK = 0xffffffffn;
1253
+ const FROM_BIGINT_BIT_SHIFT = 32n;
1209
1254
  return new Long(Number(value & FROM_BIGINT_BIT_MASK), Number((value >> FROM_BIGINT_BIT_SHIFT) & FROM_BIGINT_BIT_MASK), unsigned);
1210
1255
  }
1211
1256
  static _fromString(str, unsigned, radix) {
@@ -1238,7 +1283,7 @@ class Long extends BSONValue {
1238
1283
  static fromStringStrict(str, unsignedOrRadix, radix) {
1239
1284
  let unsigned = false;
1240
1285
  if (typeof unsignedOrRadix === 'number') {
1241
- (radix = unsignedOrRadix), (unsignedOrRadix = false);
1286
+ ((radix = unsignedOrRadix), (unsignedOrRadix = false));
1242
1287
  }
1243
1288
  else {
1244
1289
  unsigned = !!unsignedOrRadix;
@@ -1260,7 +1305,7 @@ class Long extends BSONValue {
1260
1305
  static fromString(str, unsignedOrRadix, radix) {
1261
1306
  let unsigned = false;
1262
1307
  if (typeof unsignedOrRadix === 'number') {
1263
- (radix = unsignedOrRadix), (unsignedOrRadix = false);
1308
+ ((radix = unsignedOrRadix), (unsignedOrRadix = false));
1264
1309
  }
1265
1310
  else {
1266
1311
  unsigned = !!unsignedOrRadix;
@@ -1780,15 +1825,6 @@ class Long extends BSONValue {
1780
1825
  return `new Long(${longVal}${unsignedVal})`;
1781
1826
  }
1782
1827
  }
1783
- Long.TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
1784
- Long.MAX_UNSIGNED_VALUE = Long.fromBits(0xffffffff | 0, 0xffffffff | 0, true);
1785
- Long.ZERO = Long.fromInt(0);
1786
- Long.UZERO = Long.fromInt(0, true);
1787
- Long.ONE = Long.fromInt(1);
1788
- Long.UONE = Long.fromInt(1, true);
1789
- Long.NEG_ONE = Long.fromInt(-1);
1790
- Long.MAX_VALUE = Long.fromBits(0xffffffff | 0, 0x7fffffff | 0, false);
1791
- Long.MIN_VALUE = Long.fromBits(0, 0x80000000 | 0, false);
1792
1828
 
1793
1829
  const PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/;
1794
1830
  const PARSE_INF_REGEXP = /^(\+|-)?(Infinity|inf)$/i;
@@ -1869,6 +1905,7 @@ class Decimal128 extends BSONValue {
1869
1905
  get _bsontype() {
1870
1906
  return 'Decimal128';
1871
1907
  }
1908
+ bytes;
1872
1909
  constructor(bytes) {
1873
1910
  super();
1874
1911
  if (typeof bytes === 'string') {
@@ -2344,6 +2381,7 @@ class Double extends BSONValue {
2344
2381
  get _bsontype() {
2345
2382
  return 'Double';
2346
2383
  }
2384
+ value;
2347
2385
  constructor(value) {
2348
2386
  super();
2349
2387
  if (value instanceof Number) {
@@ -2407,6 +2445,7 @@ class Int32 extends BSONValue {
2407
2445
  get _bsontype() {
2408
2446
  return 'Int32';
2409
2447
  }
2448
+ value;
2410
2449
  constructor(value) {
2411
2450
  super();
2412
2451
  if (value instanceof Number) {
@@ -2485,11 +2524,14 @@ class MinKey extends BSONValue {
2485
2524
  }
2486
2525
 
2487
2526
  let PROCESS_UNIQUE = null;
2488
- const __idCache = new WeakMap();
2489
2527
  class ObjectId extends BSONValue {
2490
2528
  get _bsontype() {
2491
2529
  return 'ObjectId';
2492
2530
  }
2531
+ static index = Math.floor(Math.random() * 0xffffff);
2532
+ static cacheHexString = false;
2533
+ buffer;
2534
+ #cachedHexString = null;
2493
2535
  constructor(inputId) {
2494
2536
  super();
2495
2537
  let workingId;
@@ -2507,8 +2549,8 @@ class ObjectId extends BSONValue {
2507
2549
  else {
2508
2550
  workingId = inputId;
2509
2551
  }
2510
- if (workingId == null || typeof workingId === 'number') {
2511
- this.buffer = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
2552
+ if (workingId == null) {
2553
+ this.buffer = ObjectId.generate();
2512
2554
  }
2513
2555
  else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
2514
2556
  this.buffer = ByteUtils.toLocalBufferType(workingId);
@@ -2517,7 +2559,7 @@ class ObjectId extends BSONValue {
2517
2559
  if (ObjectId.validateHexString(workingId)) {
2518
2560
  this.buffer = ByteUtils.fromHex(workingId);
2519
2561
  if (ObjectId.cacheHexString) {
2520
- __idCache.set(this, workingId);
2562
+ this.#cachedHexString = workingId;
2521
2563
  }
2522
2564
  }
2523
2565
  else {
@@ -2534,7 +2576,7 @@ class ObjectId extends BSONValue {
2534
2576
  set id(value) {
2535
2577
  this.buffer = value;
2536
2578
  if (ObjectId.cacheHexString) {
2537
- __idCache.set(this, ByteUtils.toHex(value));
2579
+ this.#cachedHexString = ByteUtils.toHex(value);
2538
2580
  }
2539
2581
  }
2540
2582
  static validateHexString(string) {
@@ -2552,14 +2594,11 @@ class ObjectId extends BSONValue {
2552
2594
  return true;
2553
2595
  }
2554
2596
  toHexString() {
2555
- if (ObjectId.cacheHexString) {
2556
- const __id = __idCache.get(this);
2557
- if (__id)
2558
- return __id;
2559
- }
2597
+ if (this.#cachedHexString)
2598
+ return this.#cachedHexString.toLowerCase();
2560
2599
  const hexString = ByteUtils.toHex(this.id);
2561
2600
  if (ObjectId.cacheHexString) {
2562
- __idCache.set(this, hexString);
2601
+ this.#cachedHexString = hexString;
2563
2602
  }
2564
2603
  return hexString;
2565
2604
  }
@@ -2684,14 +2723,13 @@ class ObjectId extends BSONValue {
2684
2723
  return new ObjectId(doc.$oid);
2685
2724
  }
2686
2725
  isCached() {
2687
- return ObjectId.cacheHexString && __idCache.has(this);
2726
+ return ObjectId.cacheHexString && this.#cachedHexString != null;
2688
2727
  }
2689
2728
  inspect(depth, options, inspect) {
2690
2729
  inspect ??= defaultInspect;
2691
2730
  return `new ObjectId(${inspect(this.toHexString(), options)})`;
2692
2731
  }
2693
2732
  }
2694
- ObjectId.index = Math.floor(Math.random() * 0xffffff);
2695
2733
 
2696
2734
  function internalCalculateObjectSize(object, serializeFunctions, ignoreUndefined) {
2697
2735
  let totalLength = 4 + 1;
@@ -2860,6 +2898,8 @@ class BSONRegExp extends BSONValue {
2860
2898
  get _bsontype() {
2861
2899
  return 'BSONRegExp';
2862
2900
  }
2901
+ pattern;
2902
+ options;
2863
2903
  constructor(pattern, options) {
2864
2904
  super();
2865
2905
  this.pattern = pattern;
@@ -2920,6 +2960,7 @@ class BSONSymbol extends BSONValue {
2920
2960
  get _bsontype() {
2921
2961
  return 'BSONSymbol';
2922
2962
  }
2963
+ value;
2923
2964
  constructor(value) {
2924
2965
  super();
2925
2966
  this.value = value;
@@ -2950,6 +2991,10 @@ class Timestamp extends LongWithoutOverridesClass {
2950
2991
  get _bsontype() {
2951
2992
  return 'Timestamp';
2952
2993
  }
2994
+ get [bsonType]() {
2995
+ return 'Timestamp';
2996
+ }
2997
+ static MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
2953
2998
  get i() {
2954
2999
  return this.low >>> 0;
2955
3000
  }
@@ -3029,7 +3074,6 @@ class Timestamp extends LongWithoutOverridesClass {
3029
3074
  return `new Timestamp({ t: ${t}, i: ${i} })`;
3030
3075
  }
3031
3076
  }
3032
- Timestamp.MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
3033
3077
 
3034
3078
  const JS_INT_MAX_LONG = Long.fromNumber(JS_INT_MAX);
3035
3079
  const JS_INT_MIN_LONG = Long.fromNumber(JS_INT_MIN);
@@ -4377,6 +4421,29 @@ EJSON.serialize = EJSONserialize;
4377
4421
  EJSON.deserialize = EJSONdeserialize;
4378
4422
  Object.freeze(EJSON);
4379
4423
 
4424
+ const BSONElementType = {
4425
+ double: 1,
4426
+ string: 2,
4427
+ object: 3,
4428
+ array: 4,
4429
+ binData: 5,
4430
+ undefined: 6,
4431
+ objectId: 7,
4432
+ bool: 8,
4433
+ date: 9,
4434
+ null: 10,
4435
+ regex: 11,
4436
+ dbPointer: 12,
4437
+ javascript: 13,
4438
+ symbol: 14,
4439
+ javascriptWithScope: 15,
4440
+ int: 16,
4441
+ timestamp: 17,
4442
+ long: 18,
4443
+ decimal: 19,
4444
+ minKey: 255,
4445
+ maxKey: 127
4446
+ };
4380
4447
  function getSize(source, offset) {
4381
4448
  try {
4382
4449
  return NumberUtils.getNonnegativeInt32LE(source, offset);
@@ -4421,48 +4488,48 @@ function parseToElements(bytes, startOffset = 0) {
4421
4488
  const nameLength = findNull(bytes, offset) - nameOffset;
4422
4489
  offset += nameLength + 1;
4423
4490
  let length;
4424
- if (type === 1 ||
4425
- type === 18 ||
4426
- type === 9 ||
4427
- type === 17) {
4491
+ if (type === BSONElementType.double ||
4492
+ type === BSONElementType.long ||
4493
+ type === BSONElementType.date ||
4494
+ type === BSONElementType.timestamp) {
4428
4495
  length = 8;
4429
4496
  }
4430
- else if (type === 16) {
4497
+ else if (type === BSONElementType.int) {
4431
4498
  length = 4;
4432
4499
  }
4433
- else if (type === 7) {
4500
+ else if (type === BSONElementType.objectId) {
4434
4501
  length = 12;
4435
4502
  }
4436
- else if (type === 19) {
4503
+ else if (type === BSONElementType.decimal) {
4437
4504
  length = 16;
4438
4505
  }
4439
- else if (type === 8) {
4506
+ else if (type === BSONElementType.bool) {
4440
4507
  length = 1;
4441
4508
  }
4442
- else if (type === 10 ||
4443
- type === 6 ||
4444
- type === 127 ||
4445
- type === 255) {
4509
+ else if (type === BSONElementType.null ||
4510
+ type === BSONElementType.undefined ||
4511
+ type === BSONElementType.maxKey ||
4512
+ type === BSONElementType.minKey) {
4446
4513
  length = 0;
4447
4514
  }
4448
- else if (type === 11) {
4515
+ else if (type === BSONElementType.regex) {
4449
4516
  length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
4450
4517
  }
4451
- else if (type === 3 ||
4452
- type === 4 ||
4453
- type === 15) {
4518
+ else if (type === BSONElementType.object ||
4519
+ type === BSONElementType.array ||
4520
+ type === BSONElementType.javascriptWithScope) {
4454
4521
  length = getSize(bytes, offset);
4455
4522
  }
4456
- else if (type === 2 ||
4457
- type === 5 ||
4458
- type === 12 ||
4459
- type === 13 ||
4460
- type === 14) {
4523
+ else if (type === BSONElementType.string ||
4524
+ type === BSONElementType.binData ||
4525
+ type === BSONElementType.dbPointer ||
4526
+ type === BSONElementType.javascript ||
4527
+ type === BSONElementType.symbol) {
4461
4528
  length = getSize(bytes, offset) + 4;
4462
- if (type === 5) {
4529
+ if (type === BSONElementType.binData) {
4463
4530
  length += 1;
4464
4531
  }
4465
- if (type === 12) {
4532
+ if (type === BSONElementType.dbPointer) {
4466
4533
  length += 12;
4467
4534
  }
4468
4535
  }
@@ -4558,6 +4625,7 @@ MinKey: MinKey,
4558
4625
  ObjectId: ObjectId,
4559
4626
  Timestamp: Timestamp,
4560
4627
  UUID: UUID,
4628
+ bsonType: bsonType,
4561
4629
  calculateObjectSize: calculateObjectSize,
4562
4630
  deserialize: deserialize,
4563
4631
  deserializeStream: deserializeStream,
@@ -4589,6 +4657,7 @@ exports.MinKey = MinKey;
4589
4657
  exports.ObjectId = ObjectId;
4590
4658
  exports.Timestamp = Timestamp;
4591
4659
  exports.UUID = UUID;
4660
+ exports.bsonType = bsonType;
4592
4661
  exports.calculateObjectSize = calculateObjectSize;
4593
4662
  exports.deserialize = deserialize;
4594
4663
  exports.deserializeStream = deserializeStream;