bson 4.6.3 → 4.7.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.
Files changed (74) hide show
  1. package/bower.json +1 -1
  2. package/bson.d.ts +9 -9
  3. package/dist/bson.browser.esm.js +352 -400
  4. package/dist/bson.browser.esm.js.map +1 -1
  5. package/dist/bson.browser.umd.js +355 -403
  6. package/dist/bson.browser.umd.js.map +1 -1
  7. package/dist/bson.bundle.js +355 -403
  8. package/dist/bson.bundle.js.map +1 -1
  9. package/dist/bson.esm.js +349 -400
  10. package/dist/bson.esm.js.map +1 -1
  11. package/lib/binary.js +196 -10
  12. package/lib/binary.js.map +1 -1
  13. package/lib/bson.js +8 -9
  14. package/lib/bson.js.map +1 -1
  15. package/lib/code.js +1 -1
  16. package/lib/code.js.map +1 -1
  17. package/lib/db_ref.js +2 -2
  18. package/lib/db_ref.js.map +1 -1
  19. package/lib/decimal128.js +13 -13
  20. package/lib/decimal128.js.map +1 -1
  21. package/lib/double.js +2 -2
  22. package/lib/double.js.map +1 -1
  23. package/lib/ensure_buffer.js +1 -1
  24. package/lib/ensure_buffer.js.map +1 -1
  25. package/lib/extended_json.js +24 -12
  26. package/lib/extended_json.js.map +1 -1
  27. package/lib/int_32.js +1 -1
  28. package/lib/int_32.js.map +1 -1
  29. package/lib/long.js +3 -3
  30. package/lib/long.js.map +1 -1
  31. package/lib/map.js +1 -1
  32. package/lib/map.js.map +1 -1
  33. package/lib/objectid.js +9 -8
  34. package/lib/objectid.js.map +1 -1
  35. package/lib/parser/calculate_size.js +10 -9
  36. package/lib/parser/calculate_size.js.map +1 -1
  37. package/lib/parser/deserializer.js +20 -11
  38. package/lib/parser/deserializer.js.map +1 -1
  39. package/lib/parser/serializer.js +25 -22
  40. package/lib/parser/serializer.js.map +1 -1
  41. package/lib/parser/utils.js +23 -19
  42. package/lib/parser/utils.js.map +1 -1
  43. package/lib/regexp.js +4 -4
  44. package/lib/regexp.js.map +1 -1
  45. package/lib/symbol.js +1 -1
  46. package/lib/symbol.js.map +1 -1
  47. package/lib/timestamp.js +3 -3
  48. package/lib/timestamp.js.map +1 -1
  49. package/lib/utils/global.js +1 -1
  50. package/lib/utils/global.js.map +1 -1
  51. package/lib/uuid_utils.js +1 -1
  52. package/lib/uuid_utils.js.map +1 -1
  53. package/package.json +17 -24
  54. package/src/binary.ts +197 -3
  55. package/src/bson.ts +18 -23
  56. package/src/code.ts +1 -1
  57. package/src/db_ref.ts +1 -1
  58. package/src/decimal128.ts +3 -3
  59. package/src/extended_json.ts +13 -2
  60. package/src/long.ts +32 -7
  61. package/src/objectid.ts +3 -2
  62. package/src/parser/calculate_size.ts +4 -3
  63. package/src/parser/deserializer.ts +12 -4
  64. package/src/parser/serializer.ts +14 -7
  65. package/src/parser/utils.ts +24 -20
  66. package/src/timestamp.ts +1 -1
  67. package/src/utils/global.ts +1 -1
  68. package/bson-ts34.d.ts +0 -1133
  69. package/lib/float_parser.js +0 -137
  70. package/lib/float_parser.js.map +0 -1
  71. package/lib/uuid.js +0 -179
  72. package/lib/uuid.js.map +0 -1
  73. package/src/float_parser.ts +0 -152
  74. package/src/uuid.ts +0 -209
@@ -2134,11 +2134,11 @@
2134
2134
  }
2135
2135
  // https://github.com/zloirock/core-js/issues/86#issuecomment-115759028
2136
2136
  function getGlobal() {
2137
- // eslint-disable-next-line no-undef
2138
2137
  return (checkForMath(typeof globalThis === 'object' && globalThis) ||
2139
2138
  checkForMath(typeof window === 'object' && window) ||
2140
2139
  checkForMath(typeof self === 'object' && self) ||
2141
2140
  checkForMath(typeof global === 'object' && global) ||
2141
+ // eslint-disable-next-line @typescript-eslint/no-implied-eval
2142
2142
  Function('return this')());
2143
2143
  }
2144
2144
 
@@ -2164,27 +2164,20 @@
2164
2164
  return result;
2165
2165
  };
2166
2166
  var detectRandomBytes = function () {
2167
- if (typeof window !== 'undefined') {
2168
- // browser crypto implementation(s)
2169
- var target_1 = window.crypto || window.msCrypto; // allow for IE11
2170
- if (target_1 && target_1.getRandomValues) {
2171
- return function (size) { return target_1.getRandomValues(buffer_1.alloc(size)); };
2167
+ {
2168
+ if (typeof window !== 'undefined') {
2169
+ // browser crypto implementation(s)
2170
+ var target_1 = window.crypto || window.msCrypto; // allow for IE11
2171
+ if (target_1 && target_1.getRandomValues) {
2172
+ return function (size) { return target_1.getRandomValues(buffer_1.alloc(size)); };
2173
+ }
2172
2174
  }
2175
+ if (typeof global !== 'undefined' && global.crypto && global.crypto.getRandomValues) {
2176
+ // allow for RN packages such as https://www.npmjs.com/package/react-native-get-random-values to populate global
2177
+ return function (size) { return global.crypto.getRandomValues(buffer_1.alloc(size)); };
2178
+ }
2179
+ return insecureRandomBytes;
2173
2180
  }
2174
- if (typeof global !== 'undefined' && global.crypto && global.crypto.getRandomValues) {
2175
- // allow for RN packages such as https://www.npmjs.com/package/react-native-get-random-values to populate global
2176
- return function (size) { return global.crypto.getRandomValues(buffer_1.alloc(size)); };
2177
- }
2178
- var requiredRandomBytes;
2179
- try {
2180
- // eslint-disable-next-line @typescript-eslint/no-var-requires
2181
- requiredRandomBytes = require('crypto').randomBytes;
2182
- }
2183
- catch (e) {
2184
- // keep the fallback
2185
- }
2186
- // NOTE: in transpiled cases the above require might return null/undefined
2187
- return requiredRandomBytes || insecureRandomBytes;
2188
2181
  };
2189
2182
  var randomBytes = detectRandomBytes();
2190
2183
  function isAnyArrayBuffer(value) {
@@ -2278,174 +2271,84 @@
2278
2271
  : buffer.toString('hex');
2279
2272
  };
2280
2273
 
2281
- var BYTE_LENGTH = 16;
2282
- var kId$1 = Symbol('id');
2274
+ /** @internal */
2275
+ var BSON_INT32_MAX$1 = 0x7fffffff;
2276
+ /** @internal */
2277
+ var BSON_INT32_MIN$1 = -0x80000000;
2278
+ /** @internal */
2279
+ var BSON_INT64_MAX$1 = Math.pow(2, 63) - 1;
2280
+ /** @internal */
2281
+ var BSON_INT64_MIN$1 = -Math.pow(2, 63);
2283
2282
  /**
2284
- * A class representation of the BSON UUID type.
2285
- * @public
2283
+ * Any integer up to 2^53 can be precisely represented by a double.
2284
+ * @internal
2286
2285
  */
2287
- var UUID = /** @class */ (function () {
2288
- /**
2289
- * Create an UUID type
2290
- *
2291
- * @param input - Can be a 32 or 36 character hex string (dashes excluded/included) or a 16 byte binary Buffer.
2292
- */
2293
- function UUID(input) {
2294
- if (typeof input === 'undefined') {
2295
- // The most common use case (blank id, new UUID() instance)
2296
- this.id = UUID.generate();
2297
- }
2298
- else if (input instanceof UUID) {
2299
- this[kId$1] = buffer_1.from(input.id);
2300
- this.__id = input.__id;
2301
- }
2302
- else if (ArrayBuffer.isView(input) && input.byteLength === BYTE_LENGTH) {
2303
- this.id = ensureBuffer(input);
2304
- }
2305
- else if (typeof input === 'string') {
2306
- this.id = uuidHexStringToBuffer(input);
2307
- }
2308
- else {
2309
- throw new BSONTypeError('Argument passed in UUID constructor must be a UUID, a 16 byte Buffer or a 32/36 character hex string (dashes excluded/included, format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).');
2310
- }
2311
- }
2312
- Object.defineProperty(UUID.prototype, "id", {
2313
- /**
2314
- * The UUID bytes
2315
- * @readonly
2316
- */
2317
- get: function () {
2318
- return this[kId$1];
2319
- },
2320
- set: function (value) {
2321
- this[kId$1] = value;
2322
- if (UUID.cacheHexString) {
2323
- this.__id = bufferToUuidHexString(value);
2324
- }
2325
- },
2326
- enumerable: false,
2327
- configurable: true
2328
- });
2329
- /**
2330
- * Generate a 16 byte uuid v4 buffer used in UUIDs
2331
- */
2332
- /**
2333
- * Returns the UUID id as a 32 or 36 character hex string representation, excluding/including dashes (defaults to 36 character dash separated)
2334
- * @param includeDashes - should the string exclude dash-separators.
2335
- * */
2336
- UUID.prototype.toHexString = function (includeDashes) {
2337
- if (includeDashes === void 0) { includeDashes = true; }
2338
- if (UUID.cacheHexString && this.__id) {
2339
- return this.__id;
2340
- }
2341
- var uuidHexString = bufferToUuidHexString(this.id, includeDashes);
2342
- if (UUID.cacheHexString) {
2343
- this.__id = uuidHexString;
2344
- }
2345
- return uuidHexString;
2346
- };
2347
- /**
2348
- * Converts the id into a 36 character (dashes included) hex string, unless a encoding is specified.
2349
- */
2350
- UUID.prototype.toString = function (encoding) {
2351
- return encoding ? this.id.toString(encoding) : this.toHexString();
2352
- };
2353
- /**
2354
- * Converts the id into its JSON string representation.
2355
- * A 36 character (dashes included) hex string in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
2356
- */
2357
- UUID.prototype.toJSON = function () {
2358
- return this.toHexString();
2359
- };
2360
- /**
2361
- * Compares the equality of this UUID with `otherID`.
2362
- *
2363
- * @param otherId - UUID instance to compare against.
2364
- */
2365
- UUID.prototype.equals = function (otherId) {
2366
- if (!otherId) {
2367
- return false;
2368
- }
2369
- if (otherId instanceof UUID) {
2370
- return otherId.id.equals(this.id);
2371
- }
2372
- try {
2373
- return new UUID(otherId).id.equals(this.id);
2374
- }
2375
- catch (_a) {
2376
- return false;
2377
- }
2378
- };
2379
- /**
2380
- * Creates a Binary instance from the current UUID.
2381
- */
2382
- UUID.prototype.toBinary = function () {
2383
- return new Binary(this.id, Binary.SUBTYPE_UUID);
2384
- };
2385
- /**
2386
- * Generates a populated buffer containing a v4 uuid
2387
- */
2388
- UUID.generate = function () {
2389
- var bytes = randomBytes(BYTE_LENGTH);
2390
- // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
2391
- // Kindly borrowed from https://github.com/uuidjs/uuid/blob/master/src/v4.js
2392
- bytes[6] = (bytes[6] & 0x0f) | 0x40;
2393
- bytes[8] = (bytes[8] & 0x3f) | 0x80;
2394
- return buffer_1.from(bytes);
2395
- };
2396
- /**
2397
- * Checks if a value is a valid bson UUID
2398
- * @param input - UUID, string or Buffer to validate.
2399
- */
2400
- UUID.isValid = function (input) {
2401
- if (!input) {
2402
- return false;
2403
- }
2404
- if (input instanceof UUID) {
2405
- return true;
2406
- }
2407
- if (typeof input === 'string') {
2408
- return uuidValidateString(input);
2409
- }
2410
- if (isUint8Array(input)) {
2411
- // check for length & uuid version (https://tools.ietf.org/html/rfc4122#section-4.1.3)
2412
- if (input.length !== BYTE_LENGTH) {
2413
- return false;
2414
- }
2415
- try {
2416
- // get this byte as hex: xxxxxxxx-xxxx-XXxx-xxxx-xxxxxxxxxxxx
2417
- // check first part as uuid version: xxxxxxxx-xxxx-Xxxx-xxxx-xxxxxxxxxxxx
2418
- return parseInt(input[6].toString(16)[0], 10) === Binary.SUBTYPE_UUID;
2419
- }
2420
- catch (_a) {
2421
- return false;
2422
- }
2423
- }
2424
- return false;
2425
- };
2426
- /**
2427
- * Creates an UUID from a hex string representation of an UUID.
2428
- * @param hexString - 32 or 36 character hex string (dashes excluded/included).
2429
- */
2430
- UUID.createFromHexString = function (hexString) {
2431
- var buffer = uuidHexStringToBuffer(hexString);
2432
- return new UUID(buffer);
2433
- };
2434
- /**
2435
- * Converts to a string representation of this Id.
2436
- *
2437
- * @returns return the 36 character hex string representation.
2438
- * @internal
2439
- */
2440
- UUID.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
2441
- return this.inspect();
2442
- };
2443
- UUID.prototype.inspect = function () {
2444
- return "new UUID(\"" + this.toHexString() + "\")";
2445
- };
2446
- return UUID;
2447
- }());
2448
- Object.defineProperty(UUID.prototype, '_bsontype', { value: 'UUID' });
2286
+ var JS_INT_MAX = Math.pow(2, 53);
2287
+ /**
2288
+ * Any integer down to -2^53 can be precisely represented by a double.
2289
+ * @internal
2290
+ */
2291
+ var JS_INT_MIN = -Math.pow(2, 53);
2292
+ /** Number BSON Type @internal */
2293
+ var BSON_DATA_NUMBER = 1;
2294
+ /** String BSON Type @internal */
2295
+ var BSON_DATA_STRING = 2;
2296
+ /** Object BSON Type @internal */
2297
+ var BSON_DATA_OBJECT = 3;
2298
+ /** Array BSON Type @internal */
2299
+ var BSON_DATA_ARRAY = 4;
2300
+ /** Binary BSON Type @internal */
2301
+ var BSON_DATA_BINARY = 5;
2302
+ /** Binary BSON Type @internal */
2303
+ var BSON_DATA_UNDEFINED = 6;
2304
+ /** ObjectId BSON Type @internal */
2305
+ var BSON_DATA_OID = 7;
2306
+ /** Boolean BSON Type @internal */
2307
+ var BSON_DATA_BOOLEAN = 8;
2308
+ /** Date BSON Type @internal */
2309
+ var BSON_DATA_DATE = 9;
2310
+ /** null BSON Type @internal */
2311
+ var BSON_DATA_NULL = 10;
2312
+ /** RegExp BSON Type @internal */
2313
+ var BSON_DATA_REGEXP = 11;
2314
+ /** Code BSON Type @internal */
2315
+ var BSON_DATA_DBPOINTER = 12;
2316
+ /** Code BSON Type @internal */
2317
+ var BSON_DATA_CODE = 13;
2318
+ /** Symbol BSON Type @internal */
2319
+ var BSON_DATA_SYMBOL = 14;
2320
+ /** Code with Scope BSON Type @internal */
2321
+ var BSON_DATA_CODE_W_SCOPE = 15;
2322
+ /** 32 bit Integer BSON Type @internal */
2323
+ var BSON_DATA_INT = 16;
2324
+ /** Timestamp BSON Type @internal */
2325
+ var BSON_DATA_TIMESTAMP = 17;
2326
+ /** Long BSON Type @internal */
2327
+ var BSON_DATA_LONG = 18;
2328
+ /** Decimal128 BSON Type @internal */
2329
+ var BSON_DATA_DECIMAL128 = 19;
2330
+ /** MinKey BSON Type @internal */
2331
+ var BSON_DATA_MIN_KEY = 0xff;
2332
+ /** MaxKey BSON Type @internal */
2333
+ var BSON_DATA_MAX_KEY = 0x7f;
2334
+ /** Binary Default Type @internal */
2335
+ var BSON_BINARY_SUBTYPE_DEFAULT = 0;
2336
+ /** Binary Function Type @internal */
2337
+ var BSON_BINARY_SUBTYPE_FUNCTION = 1;
2338
+ /** Binary Byte Array Type @internal */
2339
+ var BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
2340
+ /** Binary Deprecated UUID Type @deprecated Please use BSON_BINARY_SUBTYPE_UUID_NEW @internal */
2341
+ var BSON_BINARY_SUBTYPE_UUID = 3;
2342
+ /** Binary UUID Type @internal */
2343
+ var BSON_BINARY_SUBTYPE_UUID_NEW = 4;
2344
+ /** Binary MD5 Type @internal */
2345
+ var BSON_BINARY_SUBTYPE_MD5 = 5;
2346
+ /** Encrypted BSON type @internal */
2347
+ var BSON_BINARY_SUBTYPE_ENCRYPTED = 6;
2348
+ /** Column BSON type @internal */
2349
+ var BSON_BINARY_SUBTYPE_COLUMN = 7;
2350
+ /** Binary User Defined Type @internal */
2351
+ var BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
2449
2352
 
2450
2353
  /**
2451
2354
  * A class representation of the BSON Binary type.
@@ -2454,6 +2357,13 @@
2454
2357
  */
2455
2358
  var Binary = /** @class */ (function () {
2456
2359
  /**
2360
+ * Create a new Binary instance.
2361
+ *
2362
+ * This constructor can accept a string as its first argument. In this case,
2363
+ * this string will be encoded using ISO-8859-1, **not** using UTF-8.
2364
+ * This is almost certainly not what you want. Use `new Binary(Buffer.from(string))`
2365
+ * instead to convert the string to a Buffer using UTF-8 first.
2366
+ *
2457
2367
  * @param buffer - a buffer object containing the binary data.
2458
2368
  * @param subType - the option binary type.
2459
2369
  */
@@ -2613,7 +2523,7 @@
2613
2523
  if (this.sub_type === Binary.SUBTYPE_UUID) {
2614
2524
  return new UUID(this.buffer.slice(0, this.position));
2615
2525
  }
2616
- throw new BSONError("Binary sub_type \"" + this.sub_type + "\" is not supported for converting to UUID. Only \"" + Binary.SUBTYPE_UUID + "\" is currently supported.");
2526
+ throw new BSONError("Binary sub_type \"".concat(this.sub_type, "\" is not supported for converting to UUID. Only \"").concat(Binary.SUBTYPE_UUID, "\" is currently supported."));
2617
2527
  };
2618
2528
  /** @internal */
2619
2529
  Binary.fromExtendedJSON = function (doc, options) {
@@ -2637,9 +2547,9 @@
2637
2547
  data = uuidHexStringToBuffer(doc.$uuid);
2638
2548
  }
2639
2549
  if (!data) {
2640
- throw new BSONTypeError("Unexpected Binary Extended JSON format " + JSON.stringify(doc));
2550
+ throw new BSONTypeError("Unexpected Binary Extended JSON format ".concat(JSON.stringify(doc)));
2641
2551
  }
2642
- return new Binary(data, type);
2552
+ return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type);
2643
2553
  };
2644
2554
  /** @internal */
2645
2555
  Binary.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
@@ -2647,7 +2557,7 @@
2647
2557
  };
2648
2558
  Binary.prototype.inspect = function () {
2649
2559
  var asBuffer = this.value(true);
2650
- return "new Binary(Buffer.from(\"" + asBuffer.toString('hex') + "\", \"hex\"), " + this.sub_type + ")";
2560
+ return "new Binary(Buffer.from(\"".concat(asBuffer.toString('hex'), "\", \"hex\"), ").concat(this.sub_type, ")");
2651
2561
  };
2652
2562
  /**
2653
2563
  * Binary default subtype
@@ -2677,6 +2587,168 @@
2677
2587
  return Binary;
2678
2588
  }());
2679
2589
  Object.defineProperty(Binary.prototype, '_bsontype', { value: 'Binary' });
2590
+ var UUID_BYTE_LENGTH = 16;
2591
+ /**
2592
+ * A class representation of the BSON UUID type.
2593
+ * @public
2594
+ */
2595
+ var UUID = /** @class */ (function (_super) {
2596
+ __extends(UUID, _super);
2597
+ /**
2598
+ * Create an UUID type
2599
+ *
2600
+ * @param input - Can be a 32 or 36 character hex string (dashes excluded/included) or a 16 byte binary Buffer.
2601
+ */
2602
+ function UUID(input) {
2603
+ var _this = this;
2604
+ var bytes;
2605
+ var hexStr;
2606
+ if (input == null) {
2607
+ bytes = UUID.generate();
2608
+ }
2609
+ else if (input instanceof UUID) {
2610
+ bytes = buffer_1.from(input.buffer);
2611
+ hexStr = input.__id;
2612
+ }
2613
+ else if (ArrayBuffer.isView(input) && input.byteLength === UUID_BYTE_LENGTH) {
2614
+ bytes = ensureBuffer(input);
2615
+ }
2616
+ else if (typeof input === 'string') {
2617
+ bytes = uuidHexStringToBuffer(input);
2618
+ }
2619
+ else {
2620
+ throw new BSONTypeError('Argument passed in UUID constructor must be a UUID, a 16 byte Buffer or a 32/36 character hex string (dashes excluded/included, format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).');
2621
+ }
2622
+ _this = _super.call(this, bytes, BSON_BINARY_SUBTYPE_UUID_NEW) || this;
2623
+ _this.__id = hexStr;
2624
+ return _this;
2625
+ }
2626
+ Object.defineProperty(UUID.prototype, "id", {
2627
+ /**
2628
+ * The UUID bytes
2629
+ * @readonly
2630
+ */
2631
+ get: function () {
2632
+ return this.buffer;
2633
+ },
2634
+ set: function (value) {
2635
+ this.buffer = value;
2636
+ if (UUID.cacheHexString) {
2637
+ this.__id = bufferToUuidHexString(value);
2638
+ }
2639
+ },
2640
+ enumerable: false,
2641
+ configurable: true
2642
+ });
2643
+ /**
2644
+ * Returns the UUID id as a 32 or 36 character hex string representation, excluding/including dashes (defaults to 36 character dash separated)
2645
+ * @param includeDashes - should the string exclude dash-separators.
2646
+ * */
2647
+ UUID.prototype.toHexString = function (includeDashes) {
2648
+ if (includeDashes === void 0) { includeDashes = true; }
2649
+ if (UUID.cacheHexString && this.__id) {
2650
+ return this.__id;
2651
+ }
2652
+ var uuidHexString = bufferToUuidHexString(this.id, includeDashes);
2653
+ if (UUID.cacheHexString) {
2654
+ this.__id = uuidHexString;
2655
+ }
2656
+ return uuidHexString;
2657
+ };
2658
+ /**
2659
+ * Converts the id into a 36 character (dashes included) hex string, unless a encoding is specified.
2660
+ */
2661
+ UUID.prototype.toString = function (encoding) {
2662
+ return encoding ? this.id.toString(encoding) : this.toHexString();
2663
+ };
2664
+ /**
2665
+ * Converts the id into its JSON string representation.
2666
+ * A 36 character (dashes included) hex string in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
2667
+ */
2668
+ UUID.prototype.toJSON = function () {
2669
+ return this.toHexString();
2670
+ };
2671
+ /**
2672
+ * Compares the equality of this UUID with `otherID`.
2673
+ *
2674
+ * @param otherId - UUID instance to compare against.
2675
+ */
2676
+ UUID.prototype.equals = function (otherId) {
2677
+ if (!otherId) {
2678
+ return false;
2679
+ }
2680
+ if (otherId instanceof UUID) {
2681
+ return otherId.id.equals(this.id);
2682
+ }
2683
+ try {
2684
+ return new UUID(otherId).id.equals(this.id);
2685
+ }
2686
+ catch (_a) {
2687
+ return false;
2688
+ }
2689
+ };
2690
+ /**
2691
+ * Creates a Binary instance from the current UUID.
2692
+ */
2693
+ UUID.prototype.toBinary = function () {
2694
+ return new Binary(this.id, Binary.SUBTYPE_UUID);
2695
+ };
2696
+ /**
2697
+ * Generates a populated buffer containing a v4 uuid
2698
+ */
2699
+ UUID.generate = function () {
2700
+ var bytes = randomBytes(UUID_BYTE_LENGTH);
2701
+ // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
2702
+ // Kindly borrowed from https://github.com/uuidjs/uuid/blob/master/src/v4.js
2703
+ bytes[6] = (bytes[6] & 0x0f) | 0x40;
2704
+ bytes[8] = (bytes[8] & 0x3f) | 0x80;
2705
+ return buffer_1.from(bytes);
2706
+ };
2707
+ /**
2708
+ * Checks if a value is a valid bson UUID
2709
+ * @param input - UUID, string or Buffer to validate.
2710
+ */
2711
+ UUID.isValid = function (input) {
2712
+ if (!input) {
2713
+ return false;
2714
+ }
2715
+ if (input instanceof UUID) {
2716
+ return true;
2717
+ }
2718
+ if (typeof input === 'string') {
2719
+ return uuidValidateString(input);
2720
+ }
2721
+ if (isUint8Array(input)) {
2722
+ // check for length & uuid version (https://tools.ietf.org/html/rfc4122#section-4.1.3)
2723
+ if (input.length !== UUID_BYTE_LENGTH) {
2724
+ return false;
2725
+ }
2726
+ return (input[6] & 0xf0) === 0x40 && (input[8] & 0x80) === 0x80;
2727
+ }
2728
+ return false;
2729
+ };
2730
+ /**
2731
+ * Creates an UUID from a hex string representation of an UUID.
2732
+ * @param hexString - 32 or 36 character hex string (dashes excluded/included).
2733
+ */
2734
+ UUID.createFromHexString = function (hexString) {
2735
+ var buffer = uuidHexStringToBuffer(hexString);
2736
+ return new UUID(buffer);
2737
+ };
2738
+ /**
2739
+ * Converts to a string representation of this Id.
2740
+ *
2741
+ * @returns return the 36 character hex string representation.
2742
+ * @internal
2743
+ */
2744
+ UUID.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
2745
+ return this.inspect();
2746
+ };
2747
+ UUID.prototype.inspect = function () {
2748
+ return "new UUID(\"".concat(this.toHexString(), "\")");
2749
+ };
2750
+ return UUID;
2751
+ }(Binary));
2680
2752
 
2681
2753
  /**
2682
2754
  * A class representation of the BSON Code type.
@@ -2714,7 +2786,7 @@
2714
2786
  };
2715
2787
  Code.prototype.inspect = function () {
2716
2788
  var codeJson = this.toJSON();
2717
- return "new Code(\"" + codeJson.code + "\"" + (codeJson.scope ? ", " + JSON.stringify(codeJson.scope) : '') + ")";
2789
+ return "new Code(\"".concat(String(codeJson.code), "\"").concat(codeJson.scope ? ", ".concat(JSON.stringify(codeJson.scope)) : '', ")");
2718
2790
  };
2719
2791
  return Code;
2720
2792
  }());
@@ -2805,7 +2877,7 @@
2805
2877
  DBRef.prototype.inspect = function () {
2806
2878
  // NOTE: if OID is an ObjectId class it will just print the oid string.
2807
2879
  var oid = this.oid === undefined || this.oid.toString === undefined ? this.oid : this.oid.toString();
2808
- return "new DBRef(\"" + this.namespace + "\", new ObjectId(\"" + oid + "\")" + (this.db ? ", \"" + this.db + "\"" : '') + ")";
2880
+ return "new DBRef(\"".concat(this.namespace, "\", new ObjectId(\"").concat(String(oid), "\")").concat(this.db ? ", \"".concat(this.db, "\"") : '', ")");
2809
2881
  };
2810
2882
  return DBRef;
2811
2883
  }());
@@ -3042,7 +3114,6 @@
3042
3114
  /**
3043
3115
  * Tests if the specified object is a Long.
3044
3116
  */
3045
- // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
3046
3117
  Long.isLong = function (value) {
3047
3118
  return isObjectLike(value) && value['__isLong__'] === true;
3048
3119
  };
@@ -3199,6 +3270,7 @@
3199
3270
  // into the result, and subtract it from the remainder. It is critical that
3200
3271
  // the approximate value is less than or equal to the real value so that the
3201
3272
  // remainder never becomes negative.
3273
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
3202
3274
  rem = this;
3203
3275
  while (rem.gte(divisor)) {
3204
3276
  // Approximate the result of division. This may be a little greater or
@@ -3682,7 +3754,7 @@
3682
3754
  return this.inspect();
3683
3755
  };
3684
3756
  Long.prototype.inspect = function () {
3685
- return "new Long(\"" + this.toString() + "\"" + (this.unsigned ? ', true' : '') + ")";
3757
+ return "new Long(\"".concat(this.toString(), "\"").concat(this.unsigned ? ', true' : '', ")");
3686
3758
  };
3687
3759
  Long.TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
3688
3760
  /** Maximum unsigned value. */
@@ -3793,7 +3865,7 @@
3793
3865
  return false;
3794
3866
  }
3795
3867
  function invalidErr(string, message) {
3796
- throw new BSONTypeError("\"" + string + "\" is not a valid Decimal128 string - " + message);
3868
+ throw new BSONTypeError("\"".concat(string, "\" is not a valid Decimal128 string - ").concat(message));
3797
3869
  }
3798
3870
  /**
3799
3871
  * A class representation of the BSON Decimal128 type.
@@ -4291,35 +4363,35 @@
4291
4363
  // as + or - 0 and using the non-scientific exponent (this is for the "invalid
4292
4364
  // representation should be treated as 0/-0" spec cases in decimal128-1.json)
4293
4365
  if (significand_digits > 34) {
4294
- string.push("" + 0);
4366
+ string.push("".concat(0));
4295
4367
  if (exponent > 0)
4296
- string.push('E+' + exponent);
4368
+ string.push("E+".concat(exponent));
4297
4369
  else if (exponent < 0)
4298
- string.push('E' + exponent);
4370
+ string.push("E".concat(exponent));
4299
4371
  return string.join('');
4300
4372
  }
4301
- string.push("" + significand[index++]);
4373
+ string.push("".concat(significand[index++]));
4302
4374
  significand_digits = significand_digits - 1;
4303
4375
  if (significand_digits) {
4304
4376
  string.push('.');
4305
4377
  }
4306
4378
  for (var i = 0; i < significand_digits; i++) {
4307
- string.push("" + significand[index++]);
4379
+ string.push("".concat(significand[index++]));
4308
4380
  }
4309
4381
  // Exponent
4310
4382
  string.push('E');
4311
4383
  if (scientific_exponent > 0) {
4312
- string.push('+' + scientific_exponent);
4384
+ string.push("+".concat(scientific_exponent));
4313
4385
  }
4314
4386
  else {
4315
- string.push("" + scientific_exponent);
4387
+ string.push("".concat(scientific_exponent));
4316
4388
  }
4317
4389
  }
4318
4390
  else {
4319
4391
  // Regular format with no decimal place
4320
4392
  if (exponent >= 0) {
4321
4393
  for (var i = 0; i < significand_digits; i++) {
4322
- string.push("" + significand[index++]);
4394
+ string.push("".concat(significand[index++]));
4323
4395
  }
4324
4396
  }
4325
4397
  else {
@@ -4327,7 +4399,7 @@
4327
4399
  // non-zero digits before radix
4328
4400
  if (radix_position > 0) {
4329
4401
  for (var i = 0; i < radix_position; i++) {
4330
- string.push("" + significand[index++]);
4402
+ string.push("".concat(significand[index++]));
4331
4403
  }
4332
4404
  }
4333
4405
  else {
@@ -4339,7 +4411,7 @@
4339
4411
  string.push('0');
4340
4412
  }
4341
4413
  for (var i = 0; i < significand_digits - Math.max(radix_position - 1, 0); i++) {
4342
- string.push("" + significand[index++]);
4414
+ string.push("".concat(significand[index++]));
4343
4415
  }
4344
4416
  }
4345
4417
  }
@@ -4361,7 +4433,7 @@
4361
4433
  return this.inspect();
4362
4434
  };
4363
4435
  Decimal128.prototype.inspect = function () {
4364
- return "new Decimal128(\"" + this.toString() + "\")";
4436
+ return "new Decimal128(\"".concat(this.toString(), "\")");
4365
4437
  };
4366
4438
  return Decimal128;
4367
4439
  }());
@@ -4408,7 +4480,7 @@
4408
4480
  // NOTE: JavaScript has +0 and -0, apparently to model limit calculations. If a user
4409
4481
  // explicitly provided `-0` then we need to ensure the sign makes it into the output
4410
4482
  if (Object.is(Math.sign(this.value), -0)) {
4411
- return { $numberDouble: "-" + this.value.toFixed(1) };
4483
+ return { $numberDouble: "-".concat(this.value.toFixed(1)) };
4412
4484
  }
4413
4485
  var $numberDouble;
4414
4486
  if (Number.isInteger(this.value)) {
@@ -4433,7 +4505,7 @@
4433
4505
  };
4434
4506
  Double.prototype.inspect = function () {
4435
4507
  var eJSON = this.toExtendedJSON();
4436
- return "new Double(" + eJSON.$numberDouble + ")";
4508
+ return "new Double(".concat(eJSON.$numberDouble, ")");
4437
4509
  };
4438
4510
  return Double;
4439
4511
  }());
@@ -4487,7 +4559,7 @@
4487
4559
  return this.inspect();
4488
4560
  };
4489
4561
  Int32.prototype.inspect = function () {
4490
- return "new Int32(" + this.valueOf() + ")";
4562
+ return "new Int32(".concat(this.valueOf(), ")");
4491
4563
  };
4492
4564
  return Int32;
4493
4565
  }());
@@ -4593,7 +4665,8 @@
4593
4665
  this[kId] = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
4594
4666
  }
4595
4667
  else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
4596
- this[kId] = ensureBuffer(workingId);
4668
+ // If intstanceof matches we can escape calling ensure buffer in Node.js environments
4669
+ this[kId] = workingId instanceof buffer_1 ? workingId : ensureBuffer(workingId);
4597
4670
  }
4598
4671
  else if (typeof workingId === 'string') {
4599
4672
  if (workingId.length === 12) {
@@ -4820,7 +4893,7 @@
4820
4893
  return this.inspect();
4821
4894
  };
4822
4895
  ObjectId.prototype.inspect = function () {
4823
- return "new ObjectId(\"" + this.toHexString() + "\")";
4896
+ return "new ObjectId(\"".concat(this.toHexString(), "\")");
4824
4897
  };
4825
4898
  /** @internal */
4826
4899
  ObjectId.index = Math.floor(Math.random() * 0xffffff);
@@ -4860,10 +4933,10 @@
4860
4933
  this.pattern = pattern;
4861
4934
  this.options = alphabetize(options !== null && options !== void 0 ? options : '');
4862
4935
  if (this.pattern.indexOf('\x00') !== -1) {
4863
- throw new BSONError("BSON Regex patterns cannot contain null bytes, found: " + JSON.stringify(this.pattern));
4936
+ throw new BSONError("BSON Regex patterns cannot contain null bytes, found: ".concat(JSON.stringify(this.pattern)));
4864
4937
  }
4865
4938
  if (this.options.indexOf('\x00') !== -1) {
4866
- throw new BSONError("BSON Regex options cannot contain null bytes, found: " + JSON.stringify(this.options));
4939
+ throw new BSONError("BSON Regex options cannot contain null bytes, found: ".concat(JSON.stringify(this.options)));
4867
4940
  }
4868
4941
  // Validate options
4869
4942
  for (var i = 0; i < this.options.length; i++) {
@@ -4873,7 +4946,7 @@
4873
4946
  this.options[i] === 'l' ||
4874
4947
  this.options[i] === 's' ||
4875
4948
  this.options[i] === 'u')) {
4876
- throw new BSONError("The regular expression option [" + this.options[i] + "] is not supported");
4949
+ throw new BSONError("The regular expression option [".concat(this.options[i], "] is not supported"));
4877
4950
  }
4878
4951
  }
4879
4952
  }
@@ -4904,7 +4977,7 @@
4904
4977
  if ('$regularExpression' in doc) {
4905
4978
  return new BSONRegExp(doc.$regularExpression.pattern, BSONRegExp.parseOptions(doc.$regularExpression.options));
4906
4979
  }
4907
- throw new BSONTypeError("Unexpected BSONRegExp EJSON object form: " + JSON.stringify(doc));
4980
+ throw new BSONTypeError("Unexpected BSONRegExp EJSON object form: ".concat(JSON.stringify(doc)));
4908
4981
  };
4909
4982
  return BSONRegExp;
4910
4983
  }());
@@ -4933,7 +5006,7 @@
4933
5006
  };
4934
5007
  /** @internal */
4935
5008
  BSONSymbol.prototype.inspect = function () {
4936
- return "new BSONSymbol(\"" + this.value + "\")";
5009
+ return "new BSONSymbol(\"".concat(this.value, "\")");
4937
5010
  };
4938
5011
  BSONSymbol.prototype.toJSON = function () {
4939
5012
  return this.value;
@@ -4965,7 +5038,7 @@
4965
5038
  function Timestamp(low, high) {
4966
5039
  var _this = this;
4967
5040
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
4968
- ///@ts-expect-error
5041
+ // @ts-expect-error
4969
5042
  if (!(_this instanceof Timestamp))
4970
5043
  return new Timestamp(low, high);
4971
5044
  if (Long.isLong(low)) {
@@ -5029,7 +5102,7 @@
5029
5102
  return this.inspect();
5030
5103
  };
5031
5104
  Timestamp.prototype.inspect = function () {
5032
- return "new Timestamp({ t: " + this.getHighBits() + ", i: " + this.getLowBits() + " })";
5105
+ return "new Timestamp({ t: ".concat(this.getHighBits(), ", i: ").concat(this.getLowBits(), " })");
5033
5106
  };
5034
5107
  Timestamp.MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
5035
5108
  return Timestamp;
@@ -5039,11 +5112,12 @@
5039
5112
  return (isObjectLike(value) && Reflect.has(value, '_bsontype') && typeof value._bsontype === 'string');
5040
5113
  }
5041
5114
  // INT32 boundaries
5042
- var BSON_INT32_MAX$1 = 0x7fffffff;
5043
- var BSON_INT32_MIN$1 = -0x80000000;
5115
+ var BSON_INT32_MAX = 0x7fffffff;
5116
+ var BSON_INT32_MIN = -0x80000000;
5044
5117
  // INT64 boundaries
5045
- var BSON_INT64_MAX$1 = 0x7fffffffffffffff;
5046
- var BSON_INT64_MIN$1 = -0x8000000000000000;
5118
+ // const BSON_INT64_MAX = 0x7fffffffffffffff; // TODO(NODE-4377): This number cannot be precisely represented in JS
5119
+ var BSON_INT64_MAX = 0x8000000000000000;
5120
+ var BSON_INT64_MIN = -0x8000000000000000;
5047
5121
  // all the types where we don't need to do any special processing and can just pass the EJSON
5048
5122
  //straight to type.fromExtendedJSON
5049
5123
  var keysToCodecs = {
@@ -5071,9 +5145,9 @@
5071
5145
  // if it's an integer, should interpret as smallest BSON integer
5072
5146
  // that can represent it exactly. (if out of range, interpret as double.)
5073
5147
  if (Math.floor(value) === value) {
5074
- if (value >= BSON_INT32_MIN$1 && value <= BSON_INT32_MAX$1)
5148
+ if (value >= BSON_INT32_MIN && value <= BSON_INT32_MAX)
5075
5149
  return new Int32(value);
5076
- if (value >= BSON_INT64_MIN$1 && value <= BSON_INT64_MAX$1)
5150
+ if (value >= BSON_INT64_MIN && value <= BSON_INT64_MAX)
5077
5151
  return Long.fromNumber(value);
5078
5152
  }
5079
5153
  // If the number is a non-integer or out of integer range, should interpret as BSON Double.
@@ -5138,7 +5212,7 @@
5138
5212
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
5139
5213
  function serializeArray(array, options) {
5140
5214
  return array.map(function (v, index) {
5141
- options.seenObjects.push({ propertyName: "index " + index, obj: null });
5215
+ options.seenObjects.push({ propertyName: "index ".concat(index), obj: null });
5142
5216
  try {
5143
5217
  return serializeValue(v, options);
5144
5218
  }
@@ -5160,20 +5234,20 @@
5160
5234
  var props = options.seenObjects.map(function (entry) { return entry.propertyName; });
5161
5235
  var leadingPart = props
5162
5236
  .slice(0, index)
5163
- .map(function (prop) { return prop + " -> "; })
5237
+ .map(function (prop) { return "".concat(prop, " -> "); })
5164
5238
  .join('');
5165
5239
  var alreadySeen = props[index];
5166
5240
  var circularPart = ' -> ' +
5167
5241
  props
5168
5242
  .slice(index + 1, props.length - 1)
5169
- .map(function (prop) { return prop + " -> "; })
5243
+ .map(function (prop) { return "".concat(prop, " -> "); })
5170
5244
  .join('');
5171
5245
  var current = props[props.length - 1];
5172
5246
  var leadingSpace = ' '.repeat(leadingPart.length + alreadySeen.length / 2);
5173
5247
  var dashes = '-'.repeat(circularPart.length + (alreadySeen.length + current.length) / 2 - 1);
5174
5248
  throw new BSONTypeError('Converting circular structure to EJSON:\n' +
5175
- (" " + leadingPart + alreadySeen + circularPart + current + "\n") +
5176
- (" " + leadingSpace + "\\" + dashes + "/"));
5249
+ " ".concat(leadingPart).concat(alreadySeen).concat(circularPart).concat(current, "\n") +
5250
+ " ".concat(leadingSpace, "\\").concat(dashes, "/"));
5177
5251
  }
5178
5252
  options.seenObjects[options.seenObjects.length - 1].obj = value;
5179
5253
  }
@@ -5197,7 +5271,7 @@
5197
5271
  if (typeof value === 'number' && (!options.relaxed || !isFinite(value))) {
5198
5272
  // it's an integer
5199
5273
  if (Math.floor(value) === value) {
5200
- var int32Range = value >= BSON_INT32_MIN$1 && value <= BSON_INT32_MAX$1, int64Range = value >= BSON_INT64_MIN$1 && value <= BSON_INT64_MAX$1;
5274
+ var int32Range = value >= BSON_INT32_MIN && value <= BSON_INT32_MAX, int64Range = value >= BSON_INT64_MIN && value <= BSON_INT64_MAX;
5201
5275
  // interpret as being of the smallest BSON integer type that can represent the number exactly
5202
5276
  if (int32Range)
5203
5277
  return { $numberInt: value.toString() };
@@ -5252,7 +5326,18 @@
5252
5326
  for (var name in doc) {
5253
5327
  options.seenObjects.push({ propertyName: name, obj: null });
5254
5328
  try {
5255
- _doc[name] = serializeValue(doc[name], options);
5329
+ var value = serializeValue(doc[name], options);
5330
+ if (name === '__proto__') {
5331
+ Object.defineProperty(_doc, name, {
5332
+ value: value,
5333
+ writable: true,
5334
+ enumerable: true,
5335
+ configurable: true
5336
+ });
5337
+ }
5338
+ else {
5339
+ _doc[name] = value;
5340
+ }
5256
5341
  }
5257
5342
  finally {
5258
5343
  options.seenObjects.pop();
@@ -5322,7 +5407,7 @@
5322
5407
  finalOptions.relaxed = !finalOptions.strict;
5323
5408
  return JSON.parse(text, function (key, value) {
5324
5409
  if (key.indexOf('\x00') !== -1) {
5325
- throw new BSONError("BSON Document field names cannot contain null bytes, found: " + JSON.stringify(key));
5410
+ throw new BSONError("BSON Document field names cannot contain null bytes, found: ".concat(JSON.stringify(key)));
5326
5411
  }
5327
5412
  return deserializeValue(value, finalOptions);
5328
5413
  });
@@ -5511,85 +5596,6 @@
5511
5596
  }());
5512
5597
  }
5513
5598
 
5514
- /** @internal */
5515
- var BSON_INT32_MAX = 0x7fffffff;
5516
- /** @internal */
5517
- var BSON_INT32_MIN = -0x80000000;
5518
- /** @internal */
5519
- var BSON_INT64_MAX = Math.pow(2, 63) - 1;
5520
- /** @internal */
5521
- var BSON_INT64_MIN = -Math.pow(2, 63);
5522
- /**
5523
- * Any integer up to 2^53 can be precisely represented by a double.
5524
- * @internal
5525
- */
5526
- var JS_INT_MAX = Math.pow(2, 53);
5527
- /**
5528
- * Any integer down to -2^53 can be precisely represented by a double.
5529
- * @internal
5530
- */
5531
- var JS_INT_MIN = -Math.pow(2, 53);
5532
- /** Number BSON Type @internal */
5533
- var BSON_DATA_NUMBER = 1;
5534
- /** String BSON Type @internal */
5535
- var BSON_DATA_STRING = 2;
5536
- /** Object BSON Type @internal */
5537
- var BSON_DATA_OBJECT = 3;
5538
- /** Array BSON Type @internal */
5539
- var BSON_DATA_ARRAY = 4;
5540
- /** Binary BSON Type @internal */
5541
- var BSON_DATA_BINARY = 5;
5542
- /** Binary BSON Type @internal */
5543
- var BSON_DATA_UNDEFINED = 6;
5544
- /** ObjectId BSON Type @internal */
5545
- var BSON_DATA_OID = 7;
5546
- /** Boolean BSON Type @internal */
5547
- var BSON_DATA_BOOLEAN = 8;
5548
- /** Date BSON Type @internal */
5549
- var BSON_DATA_DATE = 9;
5550
- /** null BSON Type @internal */
5551
- var BSON_DATA_NULL = 10;
5552
- /** RegExp BSON Type @internal */
5553
- var BSON_DATA_REGEXP = 11;
5554
- /** Code BSON Type @internal */
5555
- var BSON_DATA_DBPOINTER = 12;
5556
- /** Code BSON Type @internal */
5557
- var BSON_DATA_CODE = 13;
5558
- /** Symbol BSON Type @internal */
5559
- var BSON_DATA_SYMBOL = 14;
5560
- /** Code with Scope BSON Type @internal */
5561
- var BSON_DATA_CODE_W_SCOPE = 15;
5562
- /** 32 bit Integer BSON Type @internal */
5563
- var BSON_DATA_INT = 16;
5564
- /** Timestamp BSON Type @internal */
5565
- var BSON_DATA_TIMESTAMP = 17;
5566
- /** Long BSON Type @internal */
5567
- var BSON_DATA_LONG = 18;
5568
- /** Decimal128 BSON Type @internal */
5569
- var BSON_DATA_DECIMAL128 = 19;
5570
- /** MinKey BSON Type @internal */
5571
- var BSON_DATA_MIN_KEY = 0xff;
5572
- /** MaxKey BSON Type @internal */
5573
- var BSON_DATA_MAX_KEY = 0x7f;
5574
- /** Binary Default Type @internal */
5575
- var BSON_BINARY_SUBTYPE_DEFAULT = 0;
5576
- /** Binary Function Type @internal */
5577
- var BSON_BINARY_SUBTYPE_FUNCTION = 1;
5578
- /** Binary Byte Array Type @internal */
5579
- var BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
5580
- /** Binary Deprecated UUID Type @deprecated Please use BSON_BINARY_SUBTYPE_UUID_NEW @internal */
5581
- var BSON_BINARY_SUBTYPE_UUID = 3;
5582
- /** Binary UUID Type @internal */
5583
- var BSON_BINARY_SUBTYPE_UUID_NEW = 4;
5584
- /** Binary MD5 Type @internal */
5585
- var BSON_BINARY_SUBTYPE_MD5 = 5;
5586
- /** Encrypted BSON type @internal */
5587
- var BSON_BINARY_SUBTYPE_ENCRYPTED = 6;
5588
- /** Column BSON type @internal */
5589
- var BSON_BINARY_SUBTYPE_COLUMN = 7;
5590
- /** Binary User Defined Type @internal */
5591
- var BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
5592
-
5593
5599
  function calculateObjectSize$1(object, serializeFunctions, ignoreUndefined) {
5594
5600
  var totalLength = 4 + 1;
5595
5601
  if (Array.isArray(object)) {
@@ -5627,7 +5633,7 @@
5627
5633
  if (Math.floor(value) === value &&
5628
5634
  value >= JS_INT_MIN &&
5629
5635
  value <= JS_INT_MAX) {
5630
- if (value >= BSON_INT32_MIN && value <= BSON_INT32_MAX) {
5636
+ if (value >= BSON_INT32_MIN$1 && value <= BSON_INT32_MAX$1) {
5631
5637
  // 32 bit
5632
5638
  return (name != null ? buffer_1.byteLength(name, 'utf8') + 1 : 0) + (4 + 1);
5633
5639
  }
@@ -5688,13 +5694,14 @@
5688
5694
  }
5689
5695
  }
5690
5696
  else if (value['_bsontype'] === 'Binary') {
5697
+ var binary = value;
5691
5698
  // Check what kind of subtype we have
5692
- if (value.sub_type === Binary.SUBTYPE_BYTE_ARRAY) {
5699
+ if (binary.sub_type === Binary.SUBTYPE_BYTE_ARRAY) {
5693
5700
  return ((name != null ? buffer_1.byteLength(name, 'utf8') + 1 : 0) +
5694
- (value.position + 1 + 4 + 1 + 4));
5701
+ (binary.position + 1 + 4 + 1 + 4));
5695
5702
  }
5696
5703
  else {
5697
- return ((name != null ? buffer_1.byteLength(name, 'utf8') + 1 : 0) + (value.position + 1 + 4 + 1));
5704
+ return ((name != null ? buffer_1.byteLength(name, 'utf8') + 1 : 0) + (binary.position + 1 + 4 + 1));
5698
5705
  }
5699
5706
  }
5700
5707
  else if (value['_bsontype'] === 'Symbol') {
@@ -5831,16 +5838,16 @@
5831
5838
  (buffer[index + 2] << 16) |
5832
5839
  (buffer[index + 3] << 24);
5833
5840
  if (size < 5) {
5834
- throw new BSONError("bson size must be >= 5, is " + size);
5841
+ throw new BSONError("bson size must be >= 5, is ".concat(size));
5835
5842
  }
5836
5843
  if (options.allowObjectSmallerThanBufferSize && buffer.length < size) {
5837
- throw new BSONError("buffer length " + buffer.length + " must be >= bson size " + size);
5844
+ throw new BSONError("buffer length ".concat(buffer.length, " must be >= bson size ").concat(size));
5838
5845
  }
5839
5846
  if (!options.allowObjectSmallerThanBufferSize && buffer.length !== size) {
5840
- throw new BSONError("buffer length " + buffer.length + " must === bson size " + size);
5847
+ throw new BSONError("buffer length ".concat(buffer.length, " must === bson size ").concat(size));
5841
5848
  }
5842
5849
  if (size + index > buffer.byteLength) {
5843
- throw new BSONError("(bson size " + size + " + options.index " + index + " must be <= buffer length " + buffer.byteLength + ")");
5850
+ throw new BSONError("(bson size ".concat(size, " + options.index ").concat(index, " must be <= buffer length ").concat(buffer.byteLength, ")"));
5844
5851
  }
5845
5852
  // Illegal end value
5846
5853
  if (buffer[index + size - 1] !== 0) {
@@ -5917,6 +5924,7 @@
5917
5924
  var done = false;
5918
5925
  var isPossibleDBRef = isArray ? false : null;
5919
5926
  // While we have more left data left keep parsing
5927
+ var dataview = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
5920
5928
  while (!done) {
5921
5929
  // Read the type
5922
5930
  var elementType = buffer[index++];
@@ -5977,11 +5985,11 @@
5977
5985
  (buffer[index++] << 24);
5978
5986
  }
5979
5987
  else if (elementType === BSON_DATA_NUMBER && promoteValues === false) {
5980
- value = new Double(buffer.readDoubleLE(index));
5988
+ value = new Double(dataview.getFloat64(index, true));
5981
5989
  index = index + 8;
5982
5990
  }
5983
5991
  else if (elementType === BSON_DATA_NUMBER) {
5984
- value = buffer.readDoubleLE(index);
5992
+ value = dataview.getFloat64(index, true);
5985
5993
  index = index + 8;
5986
5994
  }
5987
5995
  else if (elementType === BSON_DATA_DATE) {
@@ -6127,6 +6135,9 @@
6127
6135
  }
6128
6136
  else {
6129
6137
  value = new Binary(buffer.slice(index, index + binarySize), subType);
6138
+ if (subType === BSON_BINARY_SUBTYPE_UUID_NEW) {
6139
+ value = value.toUUID();
6140
+ }
6130
6141
  }
6131
6142
  }
6132
6143
  else {
@@ -6152,8 +6163,11 @@
6152
6163
  if (promoteBuffers && promoteValues) {
6153
6164
  value = _buffer;
6154
6165
  }
6166
+ else if (subType === BSON_BINARY_SUBTYPE_UUID_NEW) {
6167
+ value = new Binary(buffer.slice(index, index + binarySize), subType).toUUID();
6168
+ }
6155
6169
  else {
6156
- value = new Binary(_buffer, subType);
6170
+ value = new Binary(buffer.slice(index, index + binarySize), subType);
6157
6171
  }
6158
6172
  }
6159
6173
  // Update the index
@@ -6379,7 +6393,7 @@
6379
6393
  value = new DBRef(namespace, oid);
6380
6394
  }
6381
6395
  else {
6382
- throw new BSONError('Detected unknown BSON type ' + elementType.toString(16) + ' for fieldname "' + name + '"');
6396
+ throw new BSONError("Detected unknown BSON type ".concat(elementType.toString(16), " for fieldname \"").concat(name, "\""));
6383
6397
  }
6384
6398
  if (name === '__proto__') {
6385
6399
  Object.defineProperty(object, name, {
@@ -6417,10 +6431,12 @@
6417
6431
  * @internal
6418
6432
  */
6419
6433
  function isolateEval(functionString, functionCache, object) {
6434
+ // eslint-disable-next-line @typescript-eslint/no-implied-eval
6420
6435
  if (!functionCache)
6421
6436
  return new Function(functionString);
6422
6437
  // Check for cache hit, eval if missing and return cached function
6423
6438
  if (functionCache[functionString] == null) {
6439
+ // eslint-disable-next-line @typescript-eslint/no-implied-eval
6424
6440
  functionCache[functionString] = new Function(functionString);
6425
6441
  }
6426
6442
  // Set the object
@@ -6442,74 +6458,6 @@
6442
6458
  return value;
6443
6459
  }
6444
6460
 
6445
- // Copyright (c) 2008, Fair Oaks Labs, Inc.
6446
- function writeIEEE754(buffer, value, offset, endian, mLen, nBytes) {
6447
- var e;
6448
- var m;
6449
- var c;
6450
- var bBE = endian === 'big';
6451
- var eLen = nBytes * 8 - mLen - 1;
6452
- var eMax = (1 << eLen) - 1;
6453
- var eBias = eMax >> 1;
6454
- var rt = mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0;
6455
- var i = bBE ? nBytes - 1 : 0;
6456
- var d = bBE ? -1 : 1;
6457
- var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
6458
- value = Math.abs(value);
6459
- if (isNaN(value) || value === Infinity) {
6460
- m = isNaN(value) ? 1 : 0;
6461
- e = eMax;
6462
- }
6463
- else {
6464
- e = Math.floor(Math.log(value) / Math.LN2);
6465
- if (value * (c = Math.pow(2, -e)) < 1) {
6466
- e--;
6467
- c *= 2;
6468
- }
6469
- if (e + eBias >= 1) {
6470
- value += rt / c;
6471
- }
6472
- else {
6473
- value += rt * Math.pow(2, 1 - eBias);
6474
- }
6475
- if (value * c >= 2) {
6476
- e++;
6477
- c /= 2;
6478
- }
6479
- if (e + eBias >= eMax) {
6480
- m = 0;
6481
- e = eMax;
6482
- }
6483
- else if (e + eBias >= 1) {
6484
- m = (value * c - 1) * Math.pow(2, mLen);
6485
- e = e + eBias;
6486
- }
6487
- else {
6488
- m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
6489
- e = 0;
6490
- }
6491
- }
6492
- if (isNaN(value))
6493
- m = 0;
6494
- while (mLen >= 8) {
6495
- buffer[offset + i] = m & 0xff;
6496
- i += d;
6497
- m /= 256;
6498
- mLen -= 8;
6499
- }
6500
- e = (e << mLen) | m;
6501
- if (isNaN(value))
6502
- e += 8;
6503
- eLen += mLen;
6504
- while (eLen > 0) {
6505
- buffer[offset + i] = e & 0xff;
6506
- i += d;
6507
- e /= 256;
6508
- eLen -= 8;
6509
- }
6510
- buffer[offset + i - d] |= s * 128;
6511
- }
6512
-
6513
6461
  var regexp = /\x00/; // eslint-disable-line no-control-regex
6514
6462
  var ignoreKeys = new Set(['$db', '$ref', '$id', '$clusterTime']);
6515
6463
  /*
@@ -6540,12 +6488,14 @@
6540
6488
  buffer[index++] = 0;
6541
6489
  return index;
6542
6490
  }
6491
+ var SPACE_FOR_FLOAT64 = new Uint8Array(8);
6492
+ var DV_FOR_FLOAT64 = new DataView(SPACE_FOR_FLOAT64.buffer, SPACE_FOR_FLOAT64.byteOffset, SPACE_FOR_FLOAT64.byteLength);
6543
6493
  function serializeNumber(buffer, key, value, index, isArray) {
6544
6494
  // We have an integer value
6545
6495
  // TODO(NODE-2529): Add support for big int
6546
6496
  if (Number.isInteger(value) &&
6547
- value >= BSON_INT32_MIN &&
6548
- value <= BSON_INT32_MAX) {
6497
+ value >= BSON_INT32_MIN$1 &&
6498
+ value <= BSON_INT32_MAX$1) {
6549
6499
  // If the value fits in 32 bits encode as int32
6550
6500
  // Set int type 32 bits or less
6551
6501
  buffer[index++] = BSON_DATA_INT;
@@ -6573,7 +6523,8 @@
6573
6523
  index = index + numberOfWrittenBytes;
6574
6524
  buffer[index++] = 0;
6575
6525
  // Write float
6576
- writeIEEE754(buffer, value, index, 'little', 52, 8);
6526
+ DV_FOR_FLOAT64.setFloat64(0, value, true);
6527
+ buffer.set(SPACE_FOR_FLOAT64, index);
6577
6528
  // Adjust index
6578
6529
  index = index + 8;
6579
6530
  }
@@ -6852,7 +6803,8 @@
6852
6803
  index = index + numberOfWrittenBytes;
6853
6804
  buffer[index++] = 0;
6854
6805
  // Write float
6855
- writeIEEE754(buffer, value.value, index, 'little', 52, 8);
6806
+ DV_FOR_FLOAT64.setFloat64(0, value.value, true);
6807
+ buffer.set(SPACE_FOR_FLOAT64, index);
6856
6808
  // Adjust index
6857
6809
  index = index + 8;
6858
6810
  return index;
@@ -7062,7 +7014,7 @@
7062
7014
  if (Array.isArray(object)) {
7063
7015
  // Get object keys
7064
7016
  for (var i = 0; i < object.length; i++) {
7065
- var key = '' + i;
7017
+ var key = "".concat(i);
7066
7018
  var value = object[i];
7067
7019
  // Is there an override value
7068
7020
  if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
@@ -7137,7 +7089,7 @@
7137
7089
  index = serializeMinMax(buffer, key, value, index, true);
7138
7090
  }
7139
7091
  else if (typeof value['_bsontype'] !== 'undefined') {
7140
- throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
7092
+ throw new BSONTypeError("Unrecognized or invalid _bsontype: ".concat(String(value['_bsontype'])));
7141
7093
  }
7142
7094
  }
7143
7095
  }
@@ -7236,7 +7188,7 @@
7236
7188
  index = serializeMinMax(buffer, key, value, index);
7237
7189
  }
7238
7190
  else if (typeof value['_bsontype'] !== 'undefined') {
7239
- throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
7191
+ throw new BSONTypeError("Unrecognized or invalid _bsontype: ".concat(String(value['_bsontype'])));
7240
7192
  }
7241
7193
  }
7242
7194
  }
@@ -7341,7 +7293,7 @@
7341
7293
  index = serializeMinMax(buffer, key, value, index);
7342
7294
  }
7343
7295
  else if (typeof value['_bsontype'] !== 'undefined') {
7344
- throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
7296
+ throw new BSONTypeError("Unrecognized or invalid _bsontype: ".concat(String(value['_bsontype'])));
7345
7297
  }
7346
7298
  }
7347
7299
  }
@@ -7553,10 +7505,10 @@
7553
7505
  exports.BSON_DATA_SYMBOL = BSON_DATA_SYMBOL;
7554
7506
  exports.BSON_DATA_TIMESTAMP = BSON_DATA_TIMESTAMP;
7555
7507
  exports.BSON_DATA_UNDEFINED = BSON_DATA_UNDEFINED;
7556
- exports.BSON_INT32_MAX = BSON_INT32_MAX;
7557
- exports.BSON_INT32_MIN = BSON_INT32_MIN;
7558
- exports.BSON_INT64_MAX = BSON_INT64_MAX;
7559
- exports.BSON_INT64_MIN = BSON_INT64_MIN;
7508
+ exports.BSON_INT32_MAX = BSON_INT32_MAX$1;
7509
+ exports.BSON_INT32_MIN = BSON_INT32_MIN$1;
7510
+ exports.BSON_INT64_MAX = BSON_INT64_MAX$1;
7511
+ exports.BSON_INT64_MIN = BSON_INT64_MIN$1;
7560
7512
  exports.Binary = Binary;
7561
7513
  exports.Code = Code;
7562
7514
  exports.DBRef = DBRef;