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
package/dist/bson.esm.js CHANGED
@@ -97,11 +97,11 @@ function checkForMath(potentialGlobal) {
97
97
  }
98
98
  // https://github.com/zloirock/core-js/issues/86#issuecomment-115759028
99
99
  function getGlobal() {
100
- // eslint-disable-next-line no-undef
101
100
  return (checkForMath(typeof globalThis === 'object' && globalThis) ||
102
101
  checkForMath(typeof window === 'object' && window) ||
103
102
  checkForMath(typeof self === 'object' && self) ||
104
103
  checkForMath(typeof global === 'object' && global) ||
104
+ // eslint-disable-next-line @typescript-eslint/no-implied-eval
105
105
  Function('return this')());
106
106
  }
107
107
 
@@ -127,27 +127,17 @@ var insecureRandomBytes = function insecureRandomBytes(size) {
127
127
  return result;
128
128
  };
129
129
  var detectRandomBytes = function () {
130
- if (typeof window !== 'undefined') {
131
- // browser crypto implementation(s)
132
- var target_1 = window.crypto || window.msCrypto; // allow for IE11
133
- if (target_1 && target_1.getRandomValues) {
134
- return function (size) { return target_1.getRandomValues(Buffer.alloc(size)); };
130
+ {
131
+ var requiredRandomBytes = void 0;
132
+ try {
133
+ requiredRandomBytes = require('crypto').randomBytes;
135
134
  }
135
+ catch (e) {
136
+ // keep the fallback
137
+ }
138
+ // NOTE: in transpiled cases the above require might return null/undefined
139
+ return requiredRandomBytes || insecureRandomBytes;
136
140
  }
137
- if (typeof global !== 'undefined' && global.crypto && global.crypto.getRandomValues) {
138
- // allow for RN packages such as https://www.npmjs.com/package/react-native-get-random-values to populate global
139
- return function (size) { return global.crypto.getRandomValues(Buffer.alloc(size)); };
140
- }
141
- var requiredRandomBytes;
142
- try {
143
- // eslint-disable-next-line @typescript-eslint/no-var-requires
144
- requiredRandomBytes = require('crypto').randomBytes;
145
- }
146
- catch (e) {
147
- // keep the fallback
148
- }
149
- // NOTE: in transpiled cases the above require might return null/undefined
150
- return requiredRandomBytes || insecureRandomBytes;
151
141
  };
152
142
  var randomBytes = detectRandomBytes();
153
143
  function isAnyArrayBuffer(value) {
@@ -241,174 +231,84 @@ var bufferToUuidHexString = function (buffer, includeDashes) {
241
231
  : buffer.toString('hex');
242
232
  };
243
233
 
244
- var BYTE_LENGTH = 16;
245
- var kId$1 = Symbol('id');
234
+ /** @internal */
235
+ var BSON_INT32_MAX$1 = 0x7fffffff;
236
+ /** @internal */
237
+ var BSON_INT32_MIN$1 = -0x80000000;
238
+ /** @internal */
239
+ var BSON_INT64_MAX$1 = Math.pow(2, 63) - 1;
240
+ /** @internal */
241
+ var BSON_INT64_MIN$1 = -Math.pow(2, 63);
246
242
  /**
247
- * A class representation of the BSON UUID type.
248
- * @public
243
+ * Any integer up to 2^53 can be precisely represented by a double.
244
+ * @internal
249
245
  */
250
- var UUID = /** @class */ (function () {
251
- /**
252
- * Create an UUID type
253
- *
254
- * @param input - Can be a 32 or 36 character hex string (dashes excluded/included) or a 16 byte binary Buffer.
255
- */
256
- function UUID(input) {
257
- if (typeof input === 'undefined') {
258
- // The most common use case (blank id, new UUID() instance)
259
- this.id = UUID.generate();
260
- }
261
- else if (input instanceof UUID) {
262
- this[kId$1] = Buffer.from(input.id);
263
- this.__id = input.__id;
264
- }
265
- else if (ArrayBuffer.isView(input) && input.byteLength === BYTE_LENGTH) {
266
- this.id = ensureBuffer(input);
267
- }
268
- else if (typeof input === 'string') {
269
- this.id = uuidHexStringToBuffer(input);
270
- }
271
- else {
272
- 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).');
273
- }
274
- }
275
- Object.defineProperty(UUID.prototype, "id", {
276
- /**
277
- * The UUID bytes
278
- * @readonly
279
- */
280
- get: function () {
281
- return this[kId$1];
282
- },
283
- set: function (value) {
284
- this[kId$1] = value;
285
- if (UUID.cacheHexString) {
286
- this.__id = bufferToUuidHexString(value);
287
- }
288
- },
289
- enumerable: false,
290
- configurable: true
291
- });
292
- /**
293
- * Generate a 16 byte uuid v4 buffer used in UUIDs
294
- */
295
- /**
296
- * Returns the UUID id as a 32 or 36 character hex string representation, excluding/including dashes (defaults to 36 character dash separated)
297
- * @param includeDashes - should the string exclude dash-separators.
298
- * */
299
- UUID.prototype.toHexString = function (includeDashes) {
300
- if (includeDashes === void 0) { includeDashes = true; }
301
- if (UUID.cacheHexString && this.__id) {
302
- return this.__id;
303
- }
304
- var uuidHexString = bufferToUuidHexString(this.id, includeDashes);
305
- if (UUID.cacheHexString) {
306
- this.__id = uuidHexString;
307
- }
308
- return uuidHexString;
309
- };
310
- /**
311
- * Converts the id into a 36 character (dashes included) hex string, unless a encoding is specified.
312
- */
313
- UUID.prototype.toString = function (encoding) {
314
- return encoding ? this.id.toString(encoding) : this.toHexString();
315
- };
316
- /**
317
- * Converts the id into its JSON string representation.
318
- * A 36 character (dashes included) hex string in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
319
- */
320
- UUID.prototype.toJSON = function () {
321
- return this.toHexString();
322
- };
323
- /**
324
- * Compares the equality of this UUID with `otherID`.
325
- *
326
- * @param otherId - UUID instance to compare against.
327
- */
328
- UUID.prototype.equals = function (otherId) {
329
- if (!otherId) {
330
- return false;
331
- }
332
- if (otherId instanceof UUID) {
333
- return otherId.id.equals(this.id);
334
- }
335
- try {
336
- return new UUID(otherId).id.equals(this.id);
337
- }
338
- catch (_a) {
339
- return false;
340
- }
341
- };
342
- /**
343
- * Creates a Binary instance from the current UUID.
344
- */
345
- UUID.prototype.toBinary = function () {
346
- return new Binary(this.id, Binary.SUBTYPE_UUID);
347
- };
348
- /**
349
- * Generates a populated buffer containing a v4 uuid
350
- */
351
- UUID.generate = function () {
352
- var bytes = randomBytes(BYTE_LENGTH);
353
- // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
354
- // Kindly borrowed from https://github.com/uuidjs/uuid/blob/master/src/v4.js
355
- bytes[6] = (bytes[6] & 0x0f) | 0x40;
356
- bytes[8] = (bytes[8] & 0x3f) | 0x80;
357
- return Buffer.from(bytes);
358
- };
359
- /**
360
- * Checks if a value is a valid bson UUID
361
- * @param input - UUID, string or Buffer to validate.
362
- */
363
- UUID.isValid = function (input) {
364
- if (!input) {
365
- return false;
366
- }
367
- if (input instanceof UUID) {
368
- return true;
369
- }
370
- if (typeof input === 'string') {
371
- return uuidValidateString(input);
372
- }
373
- if (isUint8Array(input)) {
374
- // check for length & uuid version (https://tools.ietf.org/html/rfc4122#section-4.1.3)
375
- if (input.length !== BYTE_LENGTH) {
376
- return false;
377
- }
378
- try {
379
- // get this byte as hex: xxxxxxxx-xxxx-XXxx-xxxx-xxxxxxxxxxxx
380
- // check first part as uuid version: xxxxxxxx-xxxx-Xxxx-xxxx-xxxxxxxxxxxx
381
- return parseInt(input[6].toString(16)[0], 10) === Binary.SUBTYPE_UUID;
382
- }
383
- catch (_a) {
384
- return false;
385
- }
386
- }
387
- return false;
388
- };
389
- /**
390
- * Creates an UUID from a hex string representation of an UUID.
391
- * @param hexString - 32 or 36 character hex string (dashes excluded/included).
392
- */
393
- UUID.createFromHexString = function (hexString) {
394
- var buffer = uuidHexStringToBuffer(hexString);
395
- return new UUID(buffer);
396
- };
397
- /**
398
- * Converts to a string representation of this Id.
399
- *
400
- * @returns return the 36 character hex string representation.
401
- * @internal
402
- */
403
- UUID.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
404
- return this.inspect();
405
- };
406
- UUID.prototype.inspect = function () {
407
- return "new UUID(\"" + this.toHexString() + "\")";
408
- };
409
- return UUID;
410
- }());
411
- Object.defineProperty(UUID.prototype, '_bsontype', { value: 'UUID' });
246
+ var JS_INT_MAX = Math.pow(2, 53);
247
+ /**
248
+ * Any integer down to -2^53 can be precisely represented by a double.
249
+ * @internal
250
+ */
251
+ var JS_INT_MIN = -Math.pow(2, 53);
252
+ /** Number BSON Type @internal */
253
+ var BSON_DATA_NUMBER = 1;
254
+ /** String BSON Type @internal */
255
+ var BSON_DATA_STRING = 2;
256
+ /** Object BSON Type @internal */
257
+ var BSON_DATA_OBJECT = 3;
258
+ /** Array BSON Type @internal */
259
+ var BSON_DATA_ARRAY = 4;
260
+ /** Binary BSON Type @internal */
261
+ var BSON_DATA_BINARY = 5;
262
+ /** Binary BSON Type @internal */
263
+ var BSON_DATA_UNDEFINED = 6;
264
+ /** ObjectId BSON Type @internal */
265
+ var BSON_DATA_OID = 7;
266
+ /** Boolean BSON Type @internal */
267
+ var BSON_DATA_BOOLEAN = 8;
268
+ /** Date BSON Type @internal */
269
+ var BSON_DATA_DATE = 9;
270
+ /** null BSON Type @internal */
271
+ var BSON_DATA_NULL = 10;
272
+ /** RegExp BSON Type @internal */
273
+ var BSON_DATA_REGEXP = 11;
274
+ /** Code BSON Type @internal */
275
+ var BSON_DATA_DBPOINTER = 12;
276
+ /** Code BSON Type @internal */
277
+ var BSON_DATA_CODE = 13;
278
+ /** Symbol BSON Type @internal */
279
+ var BSON_DATA_SYMBOL = 14;
280
+ /** Code with Scope BSON Type @internal */
281
+ var BSON_DATA_CODE_W_SCOPE = 15;
282
+ /** 32 bit Integer BSON Type @internal */
283
+ var BSON_DATA_INT = 16;
284
+ /** Timestamp BSON Type @internal */
285
+ var BSON_DATA_TIMESTAMP = 17;
286
+ /** Long BSON Type @internal */
287
+ var BSON_DATA_LONG = 18;
288
+ /** Decimal128 BSON Type @internal */
289
+ var BSON_DATA_DECIMAL128 = 19;
290
+ /** MinKey BSON Type @internal */
291
+ var BSON_DATA_MIN_KEY = 0xff;
292
+ /** MaxKey BSON Type @internal */
293
+ var BSON_DATA_MAX_KEY = 0x7f;
294
+ /** Binary Default Type @internal */
295
+ var BSON_BINARY_SUBTYPE_DEFAULT = 0;
296
+ /** Binary Function Type @internal */
297
+ var BSON_BINARY_SUBTYPE_FUNCTION = 1;
298
+ /** Binary Byte Array Type @internal */
299
+ var BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
300
+ /** Binary Deprecated UUID Type @deprecated Please use BSON_BINARY_SUBTYPE_UUID_NEW @internal */
301
+ var BSON_BINARY_SUBTYPE_UUID = 3;
302
+ /** Binary UUID Type @internal */
303
+ var BSON_BINARY_SUBTYPE_UUID_NEW = 4;
304
+ /** Binary MD5 Type @internal */
305
+ var BSON_BINARY_SUBTYPE_MD5 = 5;
306
+ /** Encrypted BSON type @internal */
307
+ var BSON_BINARY_SUBTYPE_ENCRYPTED = 6;
308
+ /** Column BSON type @internal */
309
+ var BSON_BINARY_SUBTYPE_COLUMN = 7;
310
+ /** Binary User Defined Type @internal */
311
+ var BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
412
312
 
413
313
  /**
414
314
  * A class representation of the BSON Binary type.
@@ -417,6 +317,13 @@ Object.defineProperty(UUID.prototype, '_bsontype', { value: 'UUID' });
417
317
  */
418
318
  var Binary = /** @class */ (function () {
419
319
  /**
320
+ * Create a new Binary instance.
321
+ *
322
+ * This constructor can accept a string as its first argument. In this case,
323
+ * this string will be encoded using ISO-8859-1, **not** using UTF-8.
324
+ * This is almost certainly not what you want. Use `new Binary(Buffer.from(string))`
325
+ * instead to convert the string to a Buffer using UTF-8 first.
326
+ *
420
327
  * @param buffer - a buffer object containing the binary data.
421
328
  * @param subType - the option binary type.
422
329
  */
@@ -576,7 +483,7 @@ var Binary = /** @class */ (function () {
576
483
  if (this.sub_type === Binary.SUBTYPE_UUID) {
577
484
  return new UUID(this.buffer.slice(0, this.position));
578
485
  }
579
- throw new BSONError("Binary sub_type \"" + this.sub_type + "\" is not supported for converting to UUID. Only \"" + Binary.SUBTYPE_UUID + "\" is currently supported.");
486
+ 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."));
580
487
  };
581
488
  /** @internal */
582
489
  Binary.fromExtendedJSON = function (doc, options) {
@@ -600,9 +507,9 @@ var Binary = /** @class */ (function () {
600
507
  data = uuidHexStringToBuffer(doc.$uuid);
601
508
  }
602
509
  if (!data) {
603
- throw new BSONTypeError("Unexpected Binary Extended JSON format " + JSON.stringify(doc));
510
+ throw new BSONTypeError("Unexpected Binary Extended JSON format ".concat(JSON.stringify(doc)));
604
511
  }
605
- return new Binary(data, type);
512
+ return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type);
606
513
  };
607
514
  /** @internal */
608
515
  Binary.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
@@ -610,7 +517,7 @@ var Binary = /** @class */ (function () {
610
517
  };
611
518
  Binary.prototype.inspect = function () {
612
519
  var asBuffer = this.value(true);
613
- return "new Binary(Buffer.from(\"" + asBuffer.toString('hex') + "\", \"hex\"), " + this.sub_type + ")";
520
+ return "new Binary(Buffer.from(\"".concat(asBuffer.toString('hex'), "\", \"hex\"), ").concat(this.sub_type, ")");
614
521
  };
615
522
  /**
616
523
  * Binary default subtype
@@ -640,6 +547,168 @@ var Binary = /** @class */ (function () {
640
547
  return Binary;
641
548
  }());
642
549
  Object.defineProperty(Binary.prototype, '_bsontype', { value: 'Binary' });
550
+ var UUID_BYTE_LENGTH = 16;
551
+ /**
552
+ * A class representation of the BSON UUID type.
553
+ * @public
554
+ */
555
+ var UUID = /** @class */ (function (_super) {
556
+ __extends(UUID, _super);
557
+ /**
558
+ * Create an UUID type
559
+ *
560
+ * @param input - Can be a 32 or 36 character hex string (dashes excluded/included) or a 16 byte binary Buffer.
561
+ */
562
+ function UUID(input) {
563
+ var _this = this;
564
+ var bytes;
565
+ var hexStr;
566
+ if (input == null) {
567
+ bytes = UUID.generate();
568
+ }
569
+ else if (input instanceof UUID) {
570
+ bytes = Buffer.from(input.buffer);
571
+ hexStr = input.__id;
572
+ }
573
+ else if (ArrayBuffer.isView(input) && input.byteLength === UUID_BYTE_LENGTH) {
574
+ bytes = ensureBuffer(input);
575
+ }
576
+ else if (typeof input === 'string') {
577
+ bytes = uuidHexStringToBuffer(input);
578
+ }
579
+ else {
580
+ 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).');
581
+ }
582
+ _this = _super.call(this, bytes, BSON_BINARY_SUBTYPE_UUID_NEW) || this;
583
+ _this.__id = hexStr;
584
+ return _this;
585
+ }
586
+ Object.defineProperty(UUID.prototype, "id", {
587
+ /**
588
+ * The UUID bytes
589
+ * @readonly
590
+ */
591
+ get: function () {
592
+ return this.buffer;
593
+ },
594
+ set: function (value) {
595
+ this.buffer = value;
596
+ if (UUID.cacheHexString) {
597
+ this.__id = bufferToUuidHexString(value);
598
+ }
599
+ },
600
+ enumerable: false,
601
+ configurable: true
602
+ });
603
+ /**
604
+ * Returns the UUID id as a 32 or 36 character hex string representation, excluding/including dashes (defaults to 36 character dash separated)
605
+ * @param includeDashes - should the string exclude dash-separators.
606
+ * */
607
+ UUID.prototype.toHexString = function (includeDashes) {
608
+ if (includeDashes === void 0) { includeDashes = true; }
609
+ if (UUID.cacheHexString && this.__id) {
610
+ return this.__id;
611
+ }
612
+ var uuidHexString = bufferToUuidHexString(this.id, includeDashes);
613
+ if (UUID.cacheHexString) {
614
+ this.__id = uuidHexString;
615
+ }
616
+ return uuidHexString;
617
+ };
618
+ /**
619
+ * Converts the id into a 36 character (dashes included) hex string, unless a encoding is specified.
620
+ */
621
+ UUID.prototype.toString = function (encoding) {
622
+ return encoding ? this.id.toString(encoding) : this.toHexString();
623
+ };
624
+ /**
625
+ * Converts the id into its JSON string representation.
626
+ * A 36 character (dashes included) hex string in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
627
+ */
628
+ UUID.prototype.toJSON = function () {
629
+ return this.toHexString();
630
+ };
631
+ /**
632
+ * Compares the equality of this UUID with `otherID`.
633
+ *
634
+ * @param otherId - UUID instance to compare against.
635
+ */
636
+ UUID.prototype.equals = function (otherId) {
637
+ if (!otherId) {
638
+ return false;
639
+ }
640
+ if (otherId instanceof UUID) {
641
+ return otherId.id.equals(this.id);
642
+ }
643
+ try {
644
+ return new UUID(otherId).id.equals(this.id);
645
+ }
646
+ catch (_a) {
647
+ return false;
648
+ }
649
+ };
650
+ /**
651
+ * Creates a Binary instance from the current UUID.
652
+ */
653
+ UUID.prototype.toBinary = function () {
654
+ return new Binary(this.id, Binary.SUBTYPE_UUID);
655
+ };
656
+ /**
657
+ * Generates a populated buffer containing a v4 uuid
658
+ */
659
+ UUID.generate = function () {
660
+ var bytes = randomBytes(UUID_BYTE_LENGTH);
661
+ // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
662
+ // Kindly borrowed from https://github.com/uuidjs/uuid/blob/master/src/v4.js
663
+ bytes[6] = (bytes[6] & 0x0f) | 0x40;
664
+ bytes[8] = (bytes[8] & 0x3f) | 0x80;
665
+ return Buffer.from(bytes);
666
+ };
667
+ /**
668
+ * Checks if a value is a valid bson UUID
669
+ * @param input - UUID, string or Buffer to validate.
670
+ */
671
+ UUID.isValid = function (input) {
672
+ if (!input) {
673
+ return false;
674
+ }
675
+ if (input instanceof UUID) {
676
+ return true;
677
+ }
678
+ if (typeof input === 'string') {
679
+ return uuidValidateString(input);
680
+ }
681
+ if (isUint8Array(input)) {
682
+ // check for length & uuid version (https://tools.ietf.org/html/rfc4122#section-4.1.3)
683
+ if (input.length !== UUID_BYTE_LENGTH) {
684
+ return false;
685
+ }
686
+ return (input[6] & 0xf0) === 0x40 && (input[8] & 0x80) === 0x80;
687
+ }
688
+ return false;
689
+ };
690
+ /**
691
+ * Creates an UUID from a hex string representation of an UUID.
692
+ * @param hexString - 32 or 36 character hex string (dashes excluded/included).
693
+ */
694
+ UUID.createFromHexString = function (hexString) {
695
+ var buffer = uuidHexStringToBuffer(hexString);
696
+ return new UUID(buffer);
697
+ };
698
+ /**
699
+ * Converts to a string representation of this Id.
700
+ *
701
+ * @returns return the 36 character hex string representation.
702
+ * @internal
703
+ */
704
+ UUID.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
705
+ return this.inspect();
706
+ };
707
+ UUID.prototype.inspect = function () {
708
+ return "new UUID(\"".concat(this.toHexString(), "\")");
709
+ };
710
+ return UUID;
711
+ }(Binary));
643
712
 
644
713
  /**
645
714
  * A class representation of the BSON Code type.
@@ -677,7 +746,7 @@ var Code = /** @class */ (function () {
677
746
  };
678
747
  Code.prototype.inspect = function () {
679
748
  var codeJson = this.toJSON();
680
- return "new Code(\"" + codeJson.code + "\"" + (codeJson.scope ? ", " + JSON.stringify(codeJson.scope) : '') + ")";
749
+ return "new Code(\"".concat(String(codeJson.code), "\"").concat(codeJson.scope ? ", ".concat(JSON.stringify(codeJson.scope)) : '', ")");
681
750
  };
682
751
  return Code;
683
752
  }());
@@ -768,7 +837,7 @@ var DBRef = /** @class */ (function () {
768
837
  DBRef.prototype.inspect = function () {
769
838
  // NOTE: if OID is an ObjectId class it will just print the oid string.
770
839
  var oid = this.oid === undefined || this.oid.toString === undefined ? this.oid : this.oid.toString();
771
- return "new DBRef(\"" + this.namespace + "\", new ObjectId(\"" + oid + "\")" + (this.db ? ", \"" + this.db + "\"" : '') + ")";
840
+ return "new DBRef(\"".concat(this.namespace, "\", new ObjectId(\"").concat(String(oid), "\")").concat(this.db ? ", \"".concat(this.db, "\"") : '', ")");
772
841
  };
773
842
  return DBRef;
774
843
  }());
@@ -1005,7 +1074,6 @@ var Long = /** @class */ (function () {
1005
1074
  /**
1006
1075
  * Tests if the specified object is a Long.
1007
1076
  */
1008
- // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
1009
1077
  Long.isLong = function (value) {
1010
1078
  return isObjectLike(value) && value['__isLong__'] === true;
1011
1079
  };
@@ -1162,6 +1230,7 @@ var Long = /** @class */ (function () {
1162
1230
  // into the result, and subtract it from the remainder. It is critical that
1163
1231
  // the approximate value is less than or equal to the real value so that the
1164
1232
  // remainder never becomes negative.
1233
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
1165
1234
  rem = this;
1166
1235
  while (rem.gte(divisor)) {
1167
1236
  // Approximate the result of division. This may be a little greater or
@@ -1645,7 +1714,7 @@ var Long = /** @class */ (function () {
1645
1714
  return this.inspect();
1646
1715
  };
1647
1716
  Long.prototype.inspect = function () {
1648
- return "new Long(\"" + this.toString() + "\"" + (this.unsigned ? ', true' : '') + ")";
1717
+ return "new Long(\"".concat(this.toString(), "\"").concat(this.unsigned ? ', true' : '', ")");
1649
1718
  };
1650
1719
  Long.TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
1651
1720
  /** Maximum unsigned value. */
@@ -1756,7 +1825,7 @@ function lessThan(left, right) {
1756
1825
  return false;
1757
1826
  }
1758
1827
  function invalidErr(string, message) {
1759
- throw new BSONTypeError("\"" + string + "\" is not a valid Decimal128 string - " + message);
1828
+ throw new BSONTypeError("\"".concat(string, "\" is not a valid Decimal128 string - ").concat(message));
1760
1829
  }
1761
1830
  /**
1762
1831
  * A class representation of the BSON Decimal128 type.
@@ -2254,35 +2323,35 @@ var Decimal128 = /** @class */ (function () {
2254
2323
  // as + or - 0 and using the non-scientific exponent (this is for the "invalid
2255
2324
  // representation should be treated as 0/-0" spec cases in decimal128-1.json)
2256
2325
  if (significand_digits > 34) {
2257
- string.push("" + 0);
2326
+ string.push("".concat(0));
2258
2327
  if (exponent > 0)
2259
- string.push('E+' + exponent);
2328
+ string.push("E+".concat(exponent));
2260
2329
  else if (exponent < 0)
2261
- string.push('E' + exponent);
2330
+ string.push("E".concat(exponent));
2262
2331
  return string.join('');
2263
2332
  }
2264
- string.push("" + significand[index++]);
2333
+ string.push("".concat(significand[index++]));
2265
2334
  significand_digits = significand_digits - 1;
2266
2335
  if (significand_digits) {
2267
2336
  string.push('.');
2268
2337
  }
2269
2338
  for (var i = 0; i < significand_digits; i++) {
2270
- string.push("" + significand[index++]);
2339
+ string.push("".concat(significand[index++]));
2271
2340
  }
2272
2341
  // Exponent
2273
2342
  string.push('E');
2274
2343
  if (scientific_exponent > 0) {
2275
- string.push('+' + scientific_exponent);
2344
+ string.push("+".concat(scientific_exponent));
2276
2345
  }
2277
2346
  else {
2278
- string.push("" + scientific_exponent);
2347
+ string.push("".concat(scientific_exponent));
2279
2348
  }
2280
2349
  }
2281
2350
  else {
2282
2351
  // Regular format with no decimal place
2283
2352
  if (exponent >= 0) {
2284
2353
  for (var i = 0; i < significand_digits; i++) {
2285
- string.push("" + significand[index++]);
2354
+ string.push("".concat(significand[index++]));
2286
2355
  }
2287
2356
  }
2288
2357
  else {
@@ -2290,7 +2359,7 @@ var Decimal128 = /** @class */ (function () {
2290
2359
  // non-zero digits before radix
2291
2360
  if (radix_position > 0) {
2292
2361
  for (var i = 0; i < radix_position; i++) {
2293
- string.push("" + significand[index++]);
2362
+ string.push("".concat(significand[index++]));
2294
2363
  }
2295
2364
  }
2296
2365
  else {
@@ -2302,7 +2371,7 @@ var Decimal128 = /** @class */ (function () {
2302
2371
  string.push('0');
2303
2372
  }
2304
2373
  for (var i = 0; i < significand_digits - Math.max(radix_position - 1, 0); i++) {
2305
- string.push("" + significand[index++]);
2374
+ string.push("".concat(significand[index++]));
2306
2375
  }
2307
2376
  }
2308
2377
  }
@@ -2324,7 +2393,7 @@ var Decimal128 = /** @class */ (function () {
2324
2393
  return this.inspect();
2325
2394
  };
2326
2395
  Decimal128.prototype.inspect = function () {
2327
- return "new Decimal128(\"" + this.toString() + "\")";
2396
+ return "new Decimal128(\"".concat(this.toString(), "\")");
2328
2397
  };
2329
2398
  return Decimal128;
2330
2399
  }());
@@ -2371,7 +2440,7 @@ var Double = /** @class */ (function () {
2371
2440
  // NOTE: JavaScript has +0 and -0, apparently to model limit calculations. If a user
2372
2441
  // explicitly provided `-0` then we need to ensure the sign makes it into the output
2373
2442
  if (Object.is(Math.sign(this.value), -0)) {
2374
- return { $numberDouble: "-" + this.value.toFixed(1) };
2443
+ return { $numberDouble: "-".concat(this.value.toFixed(1)) };
2375
2444
  }
2376
2445
  var $numberDouble;
2377
2446
  if (Number.isInteger(this.value)) {
@@ -2396,7 +2465,7 @@ var Double = /** @class */ (function () {
2396
2465
  };
2397
2466
  Double.prototype.inspect = function () {
2398
2467
  var eJSON = this.toExtendedJSON();
2399
- return "new Double(" + eJSON.$numberDouble + ")";
2468
+ return "new Double(".concat(eJSON.$numberDouble, ")");
2400
2469
  };
2401
2470
  return Double;
2402
2471
  }());
@@ -2450,7 +2519,7 @@ var Int32 = /** @class */ (function () {
2450
2519
  return this.inspect();
2451
2520
  };
2452
2521
  Int32.prototype.inspect = function () {
2453
- return "new Int32(" + this.valueOf() + ")";
2522
+ return "new Int32(".concat(this.valueOf(), ")");
2454
2523
  };
2455
2524
  return Int32;
2456
2525
  }());
@@ -2556,7 +2625,8 @@ var ObjectId = /** @class */ (function () {
2556
2625
  this[kId] = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
2557
2626
  }
2558
2627
  else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
2559
- this[kId] = ensureBuffer(workingId);
2628
+ // If intstanceof matches we can escape calling ensure buffer in Node.js environments
2629
+ this[kId] = workingId instanceof Buffer ? workingId : ensureBuffer(workingId);
2560
2630
  }
2561
2631
  else if (typeof workingId === 'string') {
2562
2632
  if (workingId.length === 12) {
@@ -2783,7 +2853,7 @@ var ObjectId = /** @class */ (function () {
2783
2853
  return this.inspect();
2784
2854
  };
2785
2855
  ObjectId.prototype.inspect = function () {
2786
- return "new ObjectId(\"" + this.toHexString() + "\")";
2856
+ return "new ObjectId(\"".concat(this.toHexString(), "\")");
2787
2857
  };
2788
2858
  /** @internal */
2789
2859
  ObjectId.index = Math.floor(Math.random() * 0xffffff);
@@ -2823,10 +2893,10 @@ var BSONRegExp = /** @class */ (function () {
2823
2893
  this.pattern = pattern;
2824
2894
  this.options = alphabetize(options !== null && options !== void 0 ? options : '');
2825
2895
  if (this.pattern.indexOf('\x00') !== -1) {
2826
- throw new BSONError("BSON Regex patterns cannot contain null bytes, found: " + JSON.stringify(this.pattern));
2896
+ throw new BSONError("BSON Regex patterns cannot contain null bytes, found: ".concat(JSON.stringify(this.pattern)));
2827
2897
  }
2828
2898
  if (this.options.indexOf('\x00') !== -1) {
2829
- throw new BSONError("BSON Regex options cannot contain null bytes, found: " + JSON.stringify(this.options));
2899
+ throw new BSONError("BSON Regex options cannot contain null bytes, found: ".concat(JSON.stringify(this.options)));
2830
2900
  }
2831
2901
  // Validate options
2832
2902
  for (var i = 0; i < this.options.length; i++) {
@@ -2836,7 +2906,7 @@ var BSONRegExp = /** @class */ (function () {
2836
2906
  this.options[i] === 'l' ||
2837
2907
  this.options[i] === 's' ||
2838
2908
  this.options[i] === 'u')) {
2839
- throw new BSONError("The regular expression option [" + this.options[i] + "] is not supported");
2909
+ throw new BSONError("The regular expression option [".concat(this.options[i], "] is not supported"));
2840
2910
  }
2841
2911
  }
2842
2912
  }
@@ -2867,7 +2937,7 @@ var BSONRegExp = /** @class */ (function () {
2867
2937
  if ('$regularExpression' in doc) {
2868
2938
  return new BSONRegExp(doc.$regularExpression.pattern, BSONRegExp.parseOptions(doc.$regularExpression.options));
2869
2939
  }
2870
- throw new BSONTypeError("Unexpected BSONRegExp EJSON object form: " + JSON.stringify(doc));
2940
+ throw new BSONTypeError("Unexpected BSONRegExp EJSON object form: ".concat(JSON.stringify(doc)));
2871
2941
  };
2872
2942
  return BSONRegExp;
2873
2943
  }());
@@ -2896,7 +2966,7 @@ var BSONSymbol = /** @class */ (function () {
2896
2966
  };
2897
2967
  /** @internal */
2898
2968
  BSONSymbol.prototype.inspect = function () {
2899
- return "new BSONSymbol(\"" + this.value + "\")";
2969
+ return "new BSONSymbol(\"".concat(this.value, "\")");
2900
2970
  };
2901
2971
  BSONSymbol.prototype.toJSON = function () {
2902
2972
  return this.value;
@@ -2928,7 +2998,7 @@ var Timestamp = /** @class */ (function (_super) {
2928
2998
  function Timestamp(low, high) {
2929
2999
  var _this = this;
2930
3000
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2931
- ///@ts-expect-error
3001
+ // @ts-expect-error
2932
3002
  if (!(_this instanceof Timestamp))
2933
3003
  return new Timestamp(low, high);
2934
3004
  if (Long.isLong(low)) {
@@ -2992,7 +3062,7 @@ var Timestamp = /** @class */ (function (_super) {
2992
3062
  return this.inspect();
2993
3063
  };
2994
3064
  Timestamp.prototype.inspect = function () {
2995
- return "new Timestamp({ t: " + this.getHighBits() + ", i: " + this.getLowBits() + " })";
3065
+ return "new Timestamp({ t: ".concat(this.getHighBits(), ", i: ").concat(this.getLowBits(), " })");
2996
3066
  };
2997
3067
  Timestamp.MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
2998
3068
  return Timestamp;
@@ -3002,11 +3072,12 @@ function isBSONType(value) {
3002
3072
  return (isObjectLike(value) && Reflect.has(value, '_bsontype') && typeof value._bsontype === 'string');
3003
3073
  }
3004
3074
  // INT32 boundaries
3005
- var BSON_INT32_MAX$1 = 0x7fffffff;
3006
- var BSON_INT32_MIN$1 = -0x80000000;
3075
+ var BSON_INT32_MAX = 0x7fffffff;
3076
+ var BSON_INT32_MIN = -0x80000000;
3007
3077
  // INT64 boundaries
3008
- var BSON_INT64_MAX$1 = 0x7fffffffffffffff;
3009
- var BSON_INT64_MIN$1 = -0x8000000000000000;
3078
+ // const BSON_INT64_MAX = 0x7fffffffffffffff; // TODO(NODE-4377): This number cannot be precisely represented in JS
3079
+ var BSON_INT64_MAX = 0x8000000000000000;
3080
+ var BSON_INT64_MIN = -0x8000000000000000;
3010
3081
  // all the types where we don't need to do any special processing and can just pass the EJSON
3011
3082
  //straight to type.fromExtendedJSON
3012
3083
  var keysToCodecs = {
@@ -3034,9 +3105,9 @@ function deserializeValue(value, options) {
3034
3105
  // if it's an integer, should interpret as smallest BSON integer
3035
3106
  // that can represent it exactly. (if out of range, interpret as double.)
3036
3107
  if (Math.floor(value) === value) {
3037
- if (value >= BSON_INT32_MIN$1 && value <= BSON_INT32_MAX$1)
3108
+ if (value >= BSON_INT32_MIN && value <= BSON_INT32_MAX)
3038
3109
  return new Int32(value);
3039
- if (value >= BSON_INT64_MIN$1 && value <= BSON_INT64_MAX$1)
3110
+ if (value >= BSON_INT64_MIN && value <= BSON_INT64_MAX)
3040
3111
  return Long.fromNumber(value);
3041
3112
  }
3042
3113
  // If the number is a non-integer or out of integer range, should interpret as BSON Double.
@@ -3101,7 +3172,7 @@ function deserializeValue(value, options) {
3101
3172
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
3102
3173
  function serializeArray(array, options) {
3103
3174
  return array.map(function (v, index) {
3104
- options.seenObjects.push({ propertyName: "index " + index, obj: null });
3175
+ options.seenObjects.push({ propertyName: "index ".concat(index), obj: null });
3105
3176
  try {
3106
3177
  return serializeValue(v, options);
3107
3178
  }
@@ -3123,20 +3194,20 @@ function serializeValue(value, options) {
3123
3194
  var props = options.seenObjects.map(function (entry) { return entry.propertyName; });
3124
3195
  var leadingPart = props
3125
3196
  .slice(0, index)
3126
- .map(function (prop) { return prop + " -> "; })
3197
+ .map(function (prop) { return "".concat(prop, " -> "); })
3127
3198
  .join('');
3128
3199
  var alreadySeen = props[index];
3129
3200
  var circularPart = ' -> ' +
3130
3201
  props
3131
3202
  .slice(index + 1, props.length - 1)
3132
- .map(function (prop) { return prop + " -> "; })
3203
+ .map(function (prop) { return "".concat(prop, " -> "); })
3133
3204
  .join('');
3134
3205
  var current = props[props.length - 1];
3135
3206
  var leadingSpace = ' '.repeat(leadingPart.length + alreadySeen.length / 2);
3136
3207
  var dashes = '-'.repeat(circularPart.length + (alreadySeen.length + current.length) / 2 - 1);
3137
3208
  throw new BSONTypeError('Converting circular structure to EJSON:\n' +
3138
- (" " + leadingPart + alreadySeen + circularPart + current + "\n") +
3139
- (" " + leadingSpace + "\\" + dashes + "/"));
3209
+ " ".concat(leadingPart).concat(alreadySeen).concat(circularPart).concat(current, "\n") +
3210
+ " ".concat(leadingSpace, "\\").concat(dashes, "/"));
3140
3211
  }
3141
3212
  options.seenObjects[options.seenObjects.length - 1].obj = value;
3142
3213
  }
@@ -3160,7 +3231,7 @@ function serializeValue(value, options) {
3160
3231
  if (typeof value === 'number' && (!options.relaxed || !isFinite(value))) {
3161
3232
  // it's an integer
3162
3233
  if (Math.floor(value) === value) {
3163
- var int32Range = value >= BSON_INT32_MIN$1 && value <= BSON_INT32_MAX$1, int64Range = value >= BSON_INT64_MIN$1 && value <= BSON_INT64_MAX$1;
3234
+ var int32Range = value >= BSON_INT32_MIN && value <= BSON_INT32_MAX, int64Range = value >= BSON_INT64_MIN && value <= BSON_INT64_MAX;
3164
3235
  // interpret as being of the smallest BSON integer type that can represent the number exactly
3165
3236
  if (int32Range)
3166
3237
  return { $numberInt: value.toString() };
@@ -3215,7 +3286,18 @@ function serializeDocument(doc, options) {
3215
3286
  for (var name in doc) {
3216
3287
  options.seenObjects.push({ propertyName: name, obj: null });
3217
3288
  try {
3218
- _doc[name] = serializeValue(doc[name], options);
3289
+ var value = serializeValue(doc[name], options);
3290
+ if (name === '__proto__') {
3291
+ Object.defineProperty(_doc, name, {
3292
+ value: value,
3293
+ writable: true,
3294
+ enumerable: true,
3295
+ configurable: true
3296
+ });
3297
+ }
3298
+ else {
3299
+ _doc[name] = value;
3300
+ }
3219
3301
  }
3220
3302
  finally {
3221
3303
  options.seenObjects.pop();
@@ -3285,7 +3367,7 @@ var EJSON;
3285
3367
  finalOptions.relaxed = !finalOptions.strict;
3286
3368
  return JSON.parse(text, function (key, value) {
3287
3369
  if (key.indexOf('\x00') !== -1) {
3288
- throw new BSONError("BSON Document field names cannot contain null bytes, found: " + JSON.stringify(key));
3370
+ throw new BSONError("BSON Document field names cannot contain null bytes, found: ".concat(JSON.stringify(key)));
3289
3371
  }
3290
3372
  return deserializeValue(value, finalOptions);
3291
3373
  });
@@ -3474,85 +3556,6 @@ else {
3474
3556
  }());
3475
3557
  }
3476
3558
 
3477
- /** @internal */
3478
- var BSON_INT32_MAX = 0x7fffffff;
3479
- /** @internal */
3480
- var BSON_INT32_MIN = -0x80000000;
3481
- /** @internal */
3482
- var BSON_INT64_MAX = Math.pow(2, 63) - 1;
3483
- /** @internal */
3484
- var BSON_INT64_MIN = -Math.pow(2, 63);
3485
- /**
3486
- * Any integer up to 2^53 can be precisely represented by a double.
3487
- * @internal
3488
- */
3489
- var JS_INT_MAX = Math.pow(2, 53);
3490
- /**
3491
- * Any integer down to -2^53 can be precisely represented by a double.
3492
- * @internal
3493
- */
3494
- var JS_INT_MIN = -Math.pow(2, 53);
3495
- /** Number BSON Type @internal */
3496
- var BSON_DATA_NUMBER = 1;
3497
- /** String BSON Type @internal */
3498
- var BSON_DATA_STRING = 2;
3499
- /** Object BSON Type @internal */
3500
- var BSON_DATA_OBJECT = 3;
3501
- /** Array BSON Type @internal */
3502
- var BSON_DATA_ARRAY = 4;
3503
- /** Binary BSON Type @internal */
3504
- var BSON_DATA_BINARY = 5;
3505
- /** Binary BSON Type @internal */
3506
- var BSON_DATA_UNDEFINED = 6;
3507
- /** ObjectId BSON Type @internal */
3508
- var BSON_DATA_OID = 7;
3509
- /** Boolean BSON Type @internal */
3510
- var BSON_DATA_BOOLEAN = 8;
3511
- /** Date BSON Type @internal */
3512
- var BSON_DATA_DATE = 9;
3513
- /** null BSON Type @internal */
3514
- var BSON_DATA_NULL = 10;
3515
- /** RegExp BSON Type @internal */
3516
- var BSON_DATA_REGEXP = 11;
3517
- /** Code BSON Type @internal */
3518
- var BSON_DATA_DBPOINTER = 12;
3519
- /** Code BSON Type @internal */
3520
- var BSON_DATA_CODE = 13;
3521
- /** Symbol BSON Type @internal */
3522
- var BSON_DATA_SYMBOL = 14;
3523
- /** Code with Scope BSON Type @internal */
3524
- var BSON_DATA_CODE_W_SCOPE = 15;
3525
- /** 32 bit Integer BSON Type @internal */
3526
- var BSON_DATA_INT = 16;
3527
- /** Timestamp BSON Type @internal */
3528
- var BSON_DATA_TIMESTAMP = 17;
3529
- /** Long BSON Type @internal */
3530
- var BSON_DATA_LONG = 18;
3531
- /** Decimal128 BSON Type @internal */
3532
- var BSON_DATA_DECIMAL128 = 19;
3533
- /** MinKey BSON Type @internal */
3534
- var BSON_DATA_MIN_KEY = 0xff;
3535
- /** MaxKey BSON Type @internal */
3536
- var BSON_DATA_MAX_KEY = 0x7f;
3537
- /** Binary Default Type @internal */
3538
- var BSON_BINARY_SUBTYPE_DEFAULT = 0;
3539
- /** Binary Function Type @internal */
3540
- var BSON_BINARY_SUBTYPE_FUNCTION = 1;
3541
- /** Binary Byte Array Type @internal */
3542
- var BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
3543
- /** Binary Deprecated UUID Type @deprecated Please use BSON_BINARY_SUBTYPE_UUID_NEW @internal */
3544
- var BSON_BINARY_SUBTYPE_UUID = 3;
3545
- /** Binary UUID Type @internal */
3546
- var BSON_BINARY_SUBTYPE_UUID_NEW = 4;
3547
- /** Binary MD5 Type @internal */
3548
- var BSON_BINARY_SUBTYPE_MD5 = 5;
3549
- /** Encrypted BSON type @internal */
3550
- var BSON_BINARY_SUBTYPE_ENCRYPTED = 6;
3551
- /** Column BSON type @internal */
3552
- var BSON_BINARY_SUBTYPE_COLUMN = 7;
3553
- /** Binary User Defined Type @internal */
3554
- var BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
3555
-
3556
3559
  function calculateObjectSize$1(object, serializeFunctions, ignoreUndefined) {
3557
3560
  var totalLength = 4 + 1;
3558
3561
  if (Array.isArray(object)) {
@@ -3590,7 +3593,7 @@ value, serializeFunctions, isArray, ignoreUndefined) {
3590
3593
  if (Math.floor(value) === value &&
3591
3594
  value >= JS_INT_MIN &&
3592
3595
  value <= JS_INT_MAX) {
3593
- if (value >= BSON_INT32_MIN && value <= BSON_INT32_MAX) {
3596
+ if (value >= BSON_INT32_MIN$1 && value <= BSON_INT32_MAX$1) {
3594
3597
  // 32 bit
3595
3598
  return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (4 + 1);
3596
3599
  }
@@ -3651,13 +3654,14 @@ value, serializeFunctions, isArray, ignoreUndefined) {
3651
3654
  }
3652
3655
  }
3653
3656
  else if (value['_bsontype'] === 'Binary') {
3657
+ var binary = value;
3654
3658
  // Check what kind of subtype we have
3655
- if (value.sub_type === Binary.SUBTYPE_BYTE_ARRAY) {
3659
+ if (binary.sub_type === Binary.SUBTYPE_BYTE_ARRAY) {
3656
3660
  return ((name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
3657
- (value.position + 1 + 4 + 1 + 4));
3661
+ (binary.position + 1 + 4 + 1 + 4));
3658
3662
  }
3659
3663
  else {
3660
- return ((name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (value.position + 1 + 4 + 1));
3664
+ return ((name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (binary.position + 1 + 4 + 1));
3661
3665
  }
3662
3666
  }
3663
3667
  else if (value['_bsontype'] === 'Symbol') {
@@ -3794,16 +3798,16 @@ function deserialize$1(buffer, options, isArray) {
3794
3798
  (buffer[index + 2] << 16) |
3795
3799
  (buffer[index + 3] << 24);
3796
3800
  if (size < 5) {
3797
- throw new BSONError("bson size must be >= 5, is " + size);
3801
+ throw new BSONError("bson size must be >= 5, is ".concat(size));
3798
3802
  }
3799
3803
  if (options.allowObjectSmallerThanBufferSize && buffer.length < size) {
3800
- throw new BSONError("buffer length " + buffer.length + " must be >= bson size " + size);
3804
+ throw new BSONError("buffer length ".concat(buffer.length, " must be >= bson size ").concat(size));
3801
3805
  }
3802
3806
  if (!options.allowObjectSmallerThanBufferSize && buffer.length !== size) {
3803
- throw new BSONError("buffer length " + buffer.length + " must === bson size " + size);
3807
+ throw new BSONError("buffer length ".concat(buffer.length, " must === bson size ").concat(size));
3804
3808
  }
3805
3809
  if (size + index > buffer.byteLength) {
3806
- throw new BSONError("(bson size " + size + " + options.index " + index + " must be <= buffer length " + buffer.byteLength + ")");
3810
+ throw new BSONError("(bson size ".concat(size, " + options.index ").concat(index, " must be <= buffer length ").concat(buffer.byteLength, ")"));
3807
3811
  }
3808
3812
  // Illegal end value
3809
3813
  if (buffer[index + size - 1] !== 0) {
@@ -3880,6 +3884,7 @@ function deserializeObject(buffer, index, options, isArray) {
3880
3884
  var done = false;
3881
3885
  var isPossibleDBRef = isArray ? false : null;
3882
3886
  // While we have more left data left keep parsing
3887
+ var dataview = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
3883
3888
  while (!done) {
3884
3889
  // Read the type
3885
3890
  var elementType = buffer[index++];
@@ -3940,11 +3945,11 @@ function deserializeObject(buffer, index, options, isArray) {
3940
3945
  (buffer[index++] << 24);
3941
3946
  }
3942
3947
  else if (elementType === BSON_DATA_NUMBER && promoteValues === false) {
3943
- value = new Double(buffer.readDoubleLE(index));
3948
+ value = new Double(dataview.getFloat64(index, true));
3944
3949
  index = index + 8;
3945
3950
  }
3946
3951
  else if (elementType === BSON_DATA_NUMBER) {
3947
- value = buffer.readDoubleLE(index);
3952
+ value = dataview.getFloat64(index, true);
3948
3953
  index = index + 8;
3949
3954
  }
3950
3955
  else if (elementType === BSON_DATA_DATE) {
@@ -4090,6 +4095,9 @@ function deserializeObject(buffer, index, options, isArray) {
4090
4095
  }
4091
4096
  else {
4092
4097
  value = new Binary(buffer.slice(index, index + binarySize), subType);
4098
+ if (subType === BSON_BINARY_SUBTYPE_UUID_NEW) {
4099
+ value = value.toUUID();
4100
+ }
4093
4101
  }
4094
4102
  }
4095
4103
  else {
@@ -4115,8 +4123,11 @@ function deserializeObject(buffer, index, options, isArray) {
4115
4123
  if (promoteBuffers && promoteValues) {
4116
4124
  value = _buffer;
4117
4125
  }
4126
+ else if (subType === BSON_BINARY_SUBTYPE_UUID_NEW) {
4127
+ value = new Binary(buffer.slice(index, index + binarySize), subType).toUUID();
4128
+ }
4118
4129
  else {
4119
- value = new Binary(_buffer, subType);
4130
+ value = new Binary(buffer.slice(index, index + binarySize), subType);
4120
4131
  }
4121
4132
  }
4122
4133
  // Update the index
@@ -4342,7 +4353,7 @@ function deserializeObject(buffer, index, options, isArray) {
4342
4353
  value = new DBRef(namespace, oid);
4343
4354
  }
4344
4355
  else {
4345
- throw new BSONError('Detected unknown BSON type ' + elementType.toString(16) + ' for fieldname "' + name + '"');
4356
+ throw new BSONError("Detected unknown BSON type ".concat(elementType.toString(16), " for fieldname \"").concat(name, "\""));
4346
4357
  }
4347
4358
  if (name === '__proto__') {
4348
4359
  Object.defineProperty(object, name, {
@@ -4380,10 +4391,12 @@ function deserializeObject(buffer, index, options, isArray) {
4380
4391
  * @internal
4381
4392
  */
4382
4393
  function isolateEval(functionString, functionCache, object) {
4394
+ // eslint-disable-next-line @typescript-eslint/no-implied-eval
4383
4395
  if (!functionCache)
4384
4396
  return new Function(functionString);
4385
4397
  // Check for cache hit, eval if missing and return cached function
4386
4398
  if (functionCache[functionString] == null) {
4399
+ // eslint-disable-next-line @typescript-eslint/no-implied-eval
4387
4400
  functionCache[functionString] = new Function(functionString);
4388
4401
  }
4389
4402
  // Set the object
@@ -4405,74 +4418,6 @@ function getValidatedString(buffer, start, end, shouldValidateUtf8) {
4405
4418
  return value;
4406
4419
  }
4407
4420
 
4408
- // Copyright (c) 2008, Fair Oaks Labs, Inc.
4409
- function writeIEEE754(buffer, value, offset, endian, mLen, nBytes) {
4410
- var e;
4411
- var m;
4412
- var c;
4413
- var bBE = endian === 'big';
4414
- var eLen = nBytes * 8 - mLen - 1;
4415
- var eMax = (1 << eLen) - 1;
4416
- var eBias = eMax >> 1;
4417
- var rt = mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0;
4418
- var i = bBE ? nBytes - 1 : 0;
4419
- var d = bBE ? -1 : 1;
4420
- var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
4421
- value = Math.abs(value);
4422
- if (isNaN(value) || value === Infinity) {
4423
- m = isNaN(value) ? 1 : 0;
4424
- e = eMax;
4425
- }
4426
- else {
4427
- e = Math.floor(Math.log(value) / Math.LN2);
4428
- if (value * (c = Math.pow(2, -e)) < 1) {
4429
- e--;
4430
- c *= 2;
4431
- }
4432
- if (e + eBias >= 1) {
4433
- value += rt / c;
4434
- }
4435
- else {
4436
- value += rt * Math.pow(2, 1 - eBias);
4437
- }
4438
- if (value * c >= 2) {
4439
- e++;
4440
- c /= 2;
4441
- }
4442
- if (e + eBias >= eMax) {
4443
- m = 0;
4444
- e = eMax;
4445
- }
4446
- else if (e + eBias >= 1) {
4447
- m = (value * c - 1) * Math.pow(2, mLen);
4448
- e = e + eBias;
4449
- }
4450
- else {
4451
- m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
4452
- e = 0;
4453
- }
4454
- }
4455
- if (isNaN(value))
4456
- m = 0;
4457
- while (mLen >= 8) {
4458
- buffer[offset + i] = m & 0xff;
4459
- i += d;
4460
- m /= 256;
4461
- mLen -= 8;
4462
- }
4463
- e = (e << mLen) | m;
4464
- if (isNaN(value))
4465
- e += 8;
4466
- eLen += mLen;
4467
- while (eLen > 0) {
4468
- buffer[offset + i] = e & 0xff;
4469
- i += d;
4470
- e /= 256;
4471
- eLen -= 8;
4472
- }
4473
- buffer[offset + i - d] |= s * 128;
4474
- }
4475
-
4476
4421
  var regexp = /\x00/; // eslint-disable-line no-control-regex
4477
4422
  var ignoreKeys = new Set(['$db', '$ref', '$id', '$clusterTime']);
4478
4423
  /*
@@ -4503,12 +4448,14 @@ function serializeString(buffer, key, value, index, isArray) {
4503
4448
  buffer[index++] = 0;
4504
4449
  return index;
4505
4450
  }
4451
+ var SPACE_FOR_FLOAT64 = new Uint8Array(8);
4452
+ var DV_FOR_FLOAT64 = new DataView(SPACE_FOR_FLOAT64.buffer, SPACE_FOR_FLOAT64.byteOffset, SPACE_FOR_FLOAT64.byteLength);
4506
4453
  function serializeNumber(buffer, key, value, index, isArray) {
4507
4454
  // We have an integer value
4508
4455
  // TODO(NODE-2529): Add support for big int
4509
4456
  if (Number.isInteger(value) &&
4510
- value >= BSON_INT32_MIN &&
4511
- value <= BSON_INT32_MAX) {
4457
+ value >= BSON_INT32_MIN$1 &&
4458
+ value <= BSON_INT32_MAX$1) {
4512
4459
  // If the value fits in 32 bits encode as int32
4513
4460
  // Set int type 32 bits or less
4514
4461
  buffer[index++] = BSON_DATA_INT;
@@ -4536,7 +4483,8 @@ function serializeNumber(buffer, key, value, index, isArray) {
4536
4483
  index = index + numberOfWrittenBytes;
4537
4484
  buffer[index++] = 0;
4538
4485
  // Write float
4539
- writeIEEE754(buffer, value, index, 'little', 52, 8);
4486
+ DV_FOR_FLOAT64.setFloat64(0, value, true);
4487
+ buffer.set(SPACE_FOR_FLOAT64, index);
4540
4488
  // Adjust index
4541
4489
  index = index + 8;
4542
4490
  }
@@ -4815,7 +4763,8 @@ function serializeDouble(buffer, key, value, index, isArray) {
4815
4763
  index = index + numberOfWrittenBytes;
4816
4764
  buffer[index++] = 0;
4817
4765
  // Write float
4818
- writeIEEE754(buffer, value.value, index, 'little', 52, 8);
4766
+ DV_FOR_FLOAT64.setFloat64(0, value.value, true);
4767
+ buffer.set(SPACE_FOR_FLOAT64, index);
4819
4768
  // Adjust index
4820
4769
  index = index + 8;
4821
4770
  return index;
@@ -5025,7 +4974,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
5025
4974
  if (Array.isArray(object)) {
5026
4975
  // Get object keys
5027
4976
  for (var i = 0; i < object.length; i++) {
5028
- var key = '' + i;
4977
+ var key = "".concat(i);
5029
4978
  var value = object[i];
5030
4979
  // Is there an override value
5031
4980
  if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
@@ -5100,7 +5049,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
5100
5049
  index = serializeMinMax(buffer, key, value, index, true);
5101
5050
  }
5102
5051
  else if (typeof value['_bsontype'] !== 'undefined') {
5103
- throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
5052
+ throw new BSONTypeError("Unrecognized or invalid _bsontype: ".concat(String(value['_bsontype'])));
5104
5053
  }
5105
5054
  }
5106
5055
  }
@@ -5199,7 +5148,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
5199
5148
  index = serializeMinMax(buffer, key, value, index);
5200
5149
  }
5201
5150
  else if (typeof value['_bsontype'] !== 'undefined') {
5202
- throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
5151
+ throw new BSONTypeError("Unrecognized or invalid _bsontype: ".concat(String(value['_bsontype'])));
5203
5152
  }
5204
5153
  }
5205
5154
  }
@@ -5304,7 +5253,7 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
5304
5253
  index = serializeMinMax(buffer, key, value, index);
5305
5254
  }
5306
5255
  else if (typeof value['_bsontype'] !== 'undefined') {
5307
- throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
5256
+ throw new BSONTypeError("Unrecognized or invalid _bsontype: ".concat(String(value['_bsontype'])));
5308
5257
  }
5309
5258
  }
5310
5259
  }
@@ -5483,5 +5432,5 @@ var BSON = {
5483
5432
  };
5484
5433
 
5485
5434
  export default BSON;
5486
- export { BSONError, BSONRegExp, BSONSymbol, BSONTypeError, BSON_BINARY_SUBTYPE_BYTE_ARRAY, BSON_BINARY_SUBTYPE_COLUMN, BSON_BINARY_SUBTYPE_DEFAULT, BSON_BINARY_SUBTYPE_ENCRYPTED, BSON_BINARY_SUBTYPE_FUNCTION, BSON_BINARY_SUBTYPE_MD5, BSON_BINARY_SUBTYPE_USER_DEFINED, BSON_BINARY_SUBTYPE_UUID, BSON_BINARY_SUBTYPE_UUID_NEW, BSON_DATA_ARRAY, BSON_DATA_BINARY, BSON_DATA_BOOLEAN, BSON_DATA_CODE, BSON_DATA_CODE_W_SCOPE, BSON_DATA_DATE, BSON_DATA_DBPOINTER, BSON_DATA_DECIMAL128, BSON_DATA_INT, BSON_DATA_LONG, BSON_DATA_MAX_KEY, BSON_DATA_MIN_KEY, BSON_DATA_NULL, BSON_DATA_NUMBER, BSON_DATA_OBJECT, BSON_DATA_OID, BSON_DATA_REGEXP, BSON_DATA_STRING, BSON_DATA_SYMBOL, BSON_DATA_TIMESTAMP, BSON_DATA_UNDEFINED, BSON_INT32_MAX, BSON_INT32_MIN, BSON_INT64_MAX, BSON_INT64_MIN, Binary, Code, DBRef, Decimal128, Double, EJSON, Int32, Long, LongWithoutOverridesClass, bsonMap as Map, MaxKey, MinKey, ObjectId as ObjectID, ObjectId, Timestamp, UUID, calculateObjectSize, deserialize, deserializeStream, serialize, serializeWithBufferAndIndex, setInternalBufferSize };
5435
+ export { BSONError, BSONRegExp, BSONSymbol, BSONTypeError, BSON_BINARY_SUBTYPE_BYTE_ARRAY, BSON_BINARY_SUBTYPE_COLUMN, BSON_BINARY_SUBTYPE_DEFAULT, BSON_BINARY_SUBTYPE_ENCRYPTED, BSON_BINARY_SUBTYPE_FUNCTION, BSON_BINARY_SUBTYPE_MD5, BSON_BINARY_SUBTYPE_USER_DEFINED, BSON_BINARY_SUBTYPE_UUID, BSON_BINARY_SUBTYPE_UUID_NEW, BSON_DATA_ARRAY, BSON_DATA_BINARY, BSON_DATA_BOOLEAN, BSON_DATA_CODE, BSON_DATA_CODE_W_SCOPE, BSON_DATA_DATE, BSON_DATA_DBPOINTER, BSON_DATA_DECIMAL128, BSON_DATA_INT, BSON_DATA_LONG, BSON_DATA_MAX_KEY, BSON_DATA_MIN_KEY, BSON_DATA_NULL, BSON_DATA_NUMBER, BSON_DATA_OBJECT, BSON_DATA_OID, BSON_DATA_REGEXP, BSON_DATA_STRING, BSON_DATA_SYMBOL, BSON_DATA_TIMESTAMP, BSON_DATA_UNDEFINED, BSON_INT32_MAX$1 as BSON_INT32_MAX, BSON_INT32_MIN$1 as BSON_INT32_MIN, BSON_INT64_MAX$1 as BSON_INT64_MAX, BSON_INT64_MIN$1 as BSON_INT64_MIN, Binary, Code, DBRef, Decimal128, Double, EJSON, Int32, Long, LongWithoutOverridesClass, bsonMap as Map, MaxKey, MinKey, ObjectId as ObjectID, ObjectId, Timestamp, UUID, calculateObjectSize, deserialize, deserializeStream, serialize, serializeWithBufferAndIndex, setInternalBufferSize };
5487
5436
  //# sourceMappingURL=bson.esm.js.map