bson 6.10.0 → 6.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/bson.cjs CHANGED
@@ -1,45 +1,30 @@
1
1
  'use strict';
2
2
 
3
- const map = new WeakMap();
4
- const TYPES = {
5
- ArrayBuffer: '[object ArrayBuffer]',
6
- SharedArrayBuffer: '[object SharedArrayBuffer]',
7
- Uint8Array: '[object Uint8Array]',
8
- BigInt64Array: '[object BigInt64Array]',
9
- BigUint64Array: '[object BigUint64Array]',
10
- RegExp: '[object RegExp]',
11
- Map: '[object Map]',
12
- Date: '[object Date]'
13
- };
14
- function getPrototypeString(value) {
15
- let str = map.get(value);
16
- if (!str) {
17
- str = Object.prototype.toString.call(value);
18
- if (value !== null && typeof value === 'object') {
19
- map.set(value, str);
20
- }
21
- }
22
- return str;
3
+ const TypedArrayPrototypeGetSymbolToStringTag = (() => {
4
+ const g = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype), Symbol.toStringTag).get;
5
+ return (value) => g.call(value);
6
+ })();
7
+ function isUint8Array(value) {
8
+ return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Uint8Array';
23
9
  }
24
10
  function isAnyArrayBuffer(value) {
25
- const type = getPrototypeString(value);
26
- return type === TYPES.ArrayBuffer || type === TYPES.SharedArrayBuffer;
27
- }
28
- function isUint8Array(value) {
29
- const type = getPrototypeString(value);
30
- return type === TYPES.Uint8Array;
11
+ return (typeof value === 'object' &&
12
+ value != null &&
13
+ Symbol.toStringTag in value &&
14
+ (value[Symbol.toStringTag] === 'ArrayBuffer' ||
15
+ value[Symbol.toStringTag] === 'SharedArrayBuffer'));
31
16
  }
32
- function isRegExp(d) {
33
- const type = getPrototypeString(d);
34
- return type === TYPES.RegExp;
17
+ function isRegExp(regexp) {
18
+ return regexp instanceof RegExp || Object.prototype.toString.call(regexp) === '[object RegExp]';
35
19
  }
36
- function isMap(d) {
37
- const type = getPrototypeString(d);
38
- return type === TYPES.Map;
20
+ function isMap(value) {
21
+ return (typeof value === 'object' &&
22
+ value != null &&
23
+ Symbol.toStringTag in value &&
24
+ value[Symbol.toStringTag] === 'Map');
39
25
  }
40
- function isDate(d) {
41
- const type = getPrototypeString(d);
42
- return type === TYPES.Date;
26
+ function isDate(date) {
27
+ return date instanceof Date || Object.prototype.toString.call(date) === '[object Date]';
43
28
  }
44
29
  function defaultInspect(x, _options) {
45
30
  return JSON.stringify(x, (k, v) => {
@@ -2493,6 +2478,7 @@ class MinKey extends BSONValue {
2493
2478
  }
2494
2479
 
2495
2480
  let PROCESS_UNIQUE = null;
2481
+ const __idCache = new WeakMap();
2496
2482
  class ObjectId extends BSONValue {
2497
2483
  get _bsontype() {
2498
2484
  return 'ObjectId';
@@ -2523,6 +2509,9 @@ class ObjectId extends BSONValue {
2523
2509
  else if (typeof workingId === 'string') {
2524
2510
  if (ObjectId.validateHexString(workingId)) {
2525
2511
  this.buffer = ByteUtils.fromHex(workingId);
2512
+ if (ObjectId.cacheHexString) {
2513
+ __idCache.set(this, workingId);
2514
+ }
2526
2515
  }
2527
2516
  else {
2528
2517
  throw new BSONError('input must be a 24 character hex string, 12 byte Uint8Array, or an integer');
@@ -2531,9 +2520,6 @@ class ObjectId extends BSONValue {
2531
2520
  else {
2532
2521
  throw new BSONError('Argument passed in does not match the accepted types');
2533
2522
  }
2534
- if (ObjectId.cacheHexString) {
2535
- this.__id = ByteUtils.toHex(this.id);
2536
- }
2537
2523
  }
2538
2524
  get id() {
2539
2525
  return this.buffer;
@@ -2541,7 +2527,7 @@ class ObjectId extends BSONValue {
2541
2527
  set id(value) {
2542
2528
  this.buffer = value;
2543
2529
  if (ObjectId.cacheHexString) {
2544
- this.__id = ByteUtils.toHex(value);
2530
+ __idCache.set(this, ByteUtils.toHex(value));
2545
2531
  }
2546
2532
  }
2547
2533
  static validateHexString(string) {
@@ -2559,12 +2545,14 @@ class ObjectId extends BSONValue {
2559
2545
  return true;
2560
2546
  }
2561
2547
  toHexString() {
2562
- if (ObjectId.cacheHexString && this.__id) {
2563
- return this.__id;
2548
+ if (ObjectId.cacheHexString) {
2549
+ const __id = __idCache.get(this);
2550
+ if (__id)
2551
+ return __id;
2564
2552
  }
2565
2553
  const hexString = ByteUtils.toHex(this.id);
2566
- if (ObjectId.cacheHexString && !this.__id) {
2567
- this.__id = hexString;
2554
+ if (ObjectId.cacheHexString) {
2555
+ __idCache.set(this, hexString);
2568
2556
  }
2569
2557
  return hexString;
2570
2558
  }
@@ -2688,6 +2676,9 @@ class ObjectId extends BSONValue {
2688
2676
  static fromExtendedJSON(doc) {
2689
2677
  return new ObjectId(doc.$oid);
2690
2678
  }
2679
+ isCached() {
2680
+ return ObjectId.cacheHexString && __idCache.has(this);
2681
+ }
2691
2682
  inspect(depth, options, inspect) {
2692
2683
  inspect ??= defaultInspect;
2693
2684
  return `new ObjectId(${inspect(this.toHexString(), options)})`;
@@ -2845,8 +2836,14 @@ function calculateElement(name, value, serializeFunctions = false, isArray = fal
2845
2836
  ByteUtils.utf8ByteLength(value.toString()) +
2846
2837
  1);
2847
2838
  }
2839
+ return 0;
2840
+ case 'bigint':
2841
+ return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (8 + 1);
2842
+ case 'symbol':
2843
+ return 0;
2844
+ default:
2845
+ throw new BSONError(`Unrecognized JS type: ${typeof value}`);
2848
2846
  }
2849
- return 0;
2850
2847
  }
2851
2848
 
2852
2849
  function alphabetize(str) {