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.mjs CHANGED
@@ -1,43 +1,28 @@
1
- const map = new WeakMap();
2
- const TYPES = {
3
- ArrayBuffer: '[object ArrayBuffer]',
4
- SharedArrayBuffer: '[object SharedArrayBuffer]',
5
- Uint8Array: '[object Uint8Array]',
6
- BigInt64Array: '[object BigInt64Array]',
7
- BigUint64Array: '[object BigUint64Array]',
8
- RegExp: '[object RegExp]',
9
- Map: '[object Map]',
10
- Date: '[object Date]'
11
- };
12
- function getPrototypeString(value) {
13
- let str = map.get(value);
14
- if (!str) {
15
- str = Object.prototype.toString.call(value);
16
- if (value !== null && typeof value === 'object') {
17
- map.set(value, str);
18
- }
19
- }
20
- return str;
1
+ const TypedArrayPrototypeGetSymbolToStringTag = (() => {
2
+ const g = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype), Symbol.toStringTag).get;
3
+ return (value) => g.call(value);
4
+ })();
5
+ function isUint8Array(value) {
6
+ return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Uint8Array';
21
7
  }
22
8
  function isAnyArrayBuffer(value) {
23
- const type = getPrototypeString(value);
24
- return type === TYPES.ArrayBuffer || type === TYPES.SharedArrayBuffer;
25
- }
26
- function isUint8Array(value) {
27
- const type = getPrototypeString(value);
28
- return type === TYPES.Uint8Array;
9
+ return (typeof value === 'object' &&
10
+ value != null &&
11
+ Symbol.toStringTag in value &&
12
+ (value[Symbol.toStringTag] === 'ArrayBuffer' ||
13
+ value[Symbol.toStringTag] === 'SharedArrayBuffer'));
29
14
  }
30
- function isRegExp(d) {
31
- const type = getPrototypeString(d);
32
- return type === TYPES.RegExp;
15
+ function isRegExp(regexp) {
16
+ return regexp instanceof RegExp || Object.prototype.toString.call(regexp) === '[object RegExp]';
33
17
  }
34
- function isMap(d) {
35
- const type = getPrototypeString(d);
36
- return type === TYPES.Map;
18
+ function isMap(value) {
19
+ return (typeof value === 'object' &&
20
+ value != null &&
21
+ Symbol.toStringTag in value &&
22
+ value[Symbol.toStringTag] === 'Map');
37
23
  }
38
- function isDate(d) {
39
- const type = getPrototypeString(d);
40
- return type === TYPES.Date;
24
+ function isDate(date) {
25
+ return date instanceof Date || Object.prototype.toString.call(date) === '[object Date]';
41
26
  }
42
27
  function defaultInspect(x, _options) {
43
28
  return JSON.stringify(x, (k, v) => {
@@ -2491,6 +2476,7 @@ class MinKey extends BSONValue {
2491
2476
  }
2492
2477
 
2493
2478
  let PROCESS_UNIQUE = null;
2479
+ const __idCache = new WeakMap();
2494
2480
  class ObjectId extends BSONValue {
2495
2481
  get _bsontype() {
2496
2482
  return 'ObjectId';
@@ -2521,6 +2507,9 @@ class ObjectId extends BSONValue {
2521
2507
  else if (typeof workingId === 'string') {
2522
2508
  if (ObjectId.validateHexString(workingId)) {
2523
2509
  this.buffer = ByteUtils.fromHex(workingId);
2510
+ if (ObjectId.cacheHexString) {
2511
+ __idCache.set(this, workingId);
2512
+ }
2524
2513
  }
2525
2514
  else {
2526
2515
  throw new BSONError('input must be a 24 character hex string, 12 byte Uint8Array, or an integer');
@@ -2529,9 +2518,6 @@ class ObjectId extends BSONValue {
2529
2518
  else {
2530
2519
  throw new BSONError('Argument passed in does not match the accepted types');
2531
2520
  }
2532
- if (ObjectId.cacheHexString) {
2533
- this.__id = ByteUtils.toHex(this.id);
2534
- }
2535
2521
  }
2536
2522
  get id() {
2537
2523
  return this.buffer;
@@ -2539,7 +2525,7 @@ class ObjectId extends BSONValue {
2539
2525
  set id(value) {
2540
2526
  this.buffer = value;
2541
2527
  if (ObjectId.cacheHexString) {
2542
- this.__id = ByteUtils.toHex(value);
2528
+ __idCache.set(this, ByteUtils.toHex(value));
2543
2529
  }
2544
2530
  }
2545
2531
  static validateHexString(string) {
@@ -2557,12 +2543,14 @@ class ObjectId extends BSONValue {
2557
2543
  return true;
2558
2544
  }
2559
2545
  toHexString() {
2560
- if (ObjectId.cacheHexString && this.__id) {
2561
- return this.__id;
2546
+ if (ObjectId.cacheHexString) {
2547
+ const __id = __idCache.get(this);
2548
+ if (__id)
2549
+ return __id;
2562
2550
  }
2563
2551
  const hexString = ByteUtils.toHex(this.id);
2564
- if (ObjectId.cacheHexString && !this.__id) {
2565
- this.__id = hexString;
2552
+ if (ObjectId.cacheHexString) {
2553
+ __idCache.set(this, hexString);
2566
2554
  }
2567
2555
  return hexString;
2568
2556
  }
@@ -2686,6 +2674,9 @@ class ObjectId extends BSONValue {
2686
2674
  static fromExtendedJSON(doc) {
2687
2675
  return new ObjectId(doc.$oid);
2688
2676
  }
2677
+ isCached() {
2678
+ return ObjectId.cacheHexString && __idCache.has(this);
2679
+ }
2689
2680
  inspect(depth, options, inspect) {
2690
2681
  inspect ??= defaultInspect;
2691
2682
  return `new ObjectId(${inspect(this.toHexString(), options)})`;
@@ -2843,8 +2834,14 @@ function calculateElement(name, value, serializeFunctions = false, isArray = fal
2843
2834
  ByteUtils.utf8ByteLength(value.toString()) +
2844
2835
  1);
2845
2836
  }
2837
+ return 0;
2838
+ case 'bigint':
2839
+ return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (8 + 1);
2840
+ case 'symbol':
2841
+ return 0;
2842
+ default:
2843
+ throw new BSONError(`Unrecognized JS type: ${typeof value}`);
2846
2844
  }
2847
- return 0;
2848
2845
  }
2849
2846
 
2850
2847
  function alphabetize(str) {