bson 6.10.0 → 6.10.1

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/bson.d.ts CHANGED
@@ -1345,7 +1345,6 @@ export declare class ObjectId extends BSONValue {
1345
1345
  /* Excluded from this release type: index */
1346
1346
  static cacheHexString: boolean;
1347
1347
  /* Excluded from this release type: buffer */
1348
- /* Excluded from this release type: __id */
1349
1348
  /**
1350
1349
  * Create ObjectId from a number.
1351
1350
  *
@@ -1440,6 +1439,7 @@ export declare class ObjectId extends BSONValue {
1440
1439
  static isValid(id: string | number | ObjectId | ObjectIdLike | Uint8Array): boolean;
1441
1440
  /* Excluded from this release type: toExtendedJSON */
1442
1441
  /* Excluded from this release type: fromExtendedJSON */
1442
+ /* Excluded from this release type: isCached */
1443
1443
  /**
1444
1444
  * Converts to a string representation of this Id.
1445
1445
  *
@@ -1,46 +1,31 @@
1
1
  var BSON = (function (exports) {
2
2
  'use strict';
3
3
 
4
- const map = new WeakMap();
5
- const TYPES = {
6
- ArrayBuffer: '[object ArrayBuffer]',
7
- SharedArrayBuffer: '[object SharedArrayBuffer]',
8
- Uint8Array: '[object Uint8Array]',
9
- BigInt64Array: '[object BigInt64Array]',
10
- BigUint64Array: '[object BigUint64Array]',
11
- RegExp: '[object RegExp]',
12
- Map: '[object Map]',
13
- Date: '[object Date]'
14
- };
15
- function getPrototypeString(value) {
16
- let str = map.get(value);
17
- if (!str) {
18
- str = Object.prototype.toString.call(value);
19
- if (value !== null && typeof value === 'object') {
20
- map.set(value, str);
21
- }
22
- }
23
- return str;
4
+ const TypedArrayPrototypeGetSymbolToStringTag = (() => {
5
+ const g = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype), Symbol.toStringTag).get;
6
+ return (value) => g.call(value);
7
+ })();
8
+ function isUint8Array(value) {
9
+ return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Uint8Array';
24
10
  }
25
11
  function isAnyArrayBuffer(value) {
26
- const type = getPrototypeString(value);
27
- return type === TYPES.ArrayBuffer || type === TYPES.SharedArrayBuffer;
28
- }
29
- function isUint8Array(value) {
30
- const type = getPrototypeString(value);
31
- return type === TYPES.Uint8Array;
12
+ return (typeof value === 'object' &&
13
+ value != null &&
14
+ Symbol.toStringTag in value &&
15
+ (value[Symbol.toStringTag] === 'ArrayBuffer' ||
16
+ value[Symbol.toStringTag] === 'SharedArrayBuffer'));
32
17
  }
33
- function isRegExp(d) {
34
- const type = getPrototypeString(d);
35
- return type === TYPES.RegExp;
18
+ function isRegExp(regexp) {
19
+ return regexp instanceof RegExp || Object.prototype.toString.call(regexp) === '[object RegExp]';
36
20
  }
37
- function isMap(d) {
38
- const type = getPrototypeString(d);
39
- return type === TYPES.Map;
21
+ function isMap(value) {
22
+ return (typeof value === 'object' &&
23
+ value != null &&
24
+ Symbol.toStringTag in value &&
25
+ value[Symbol.toStringTag] === 'Map');
40
26
  }
41
- function isDate(d) {
42
- const type = getPrototypeString(d);
43
- return type === TYPES.Date;
27
+ function isDate(date) {
28
+ return date instanceof Date || Object.prototype.toString.call(date) === '[object Date]';
44
29
  }
45
30
  function defaultInspect(x, _options) {
46
31
  return JSON.stringify(x, (k, v) => {
@@ -2494,6 +2479,7 @@ class MinKey extends BSONValue {
2494
2479
  }
2495
2480
 
2496
2481
  let PROCESS_UNIQUE = null;
2482
+ const __idCache = new WeakMap();
2497
2483
  class ObjectId extends BSONValue {
2498
2484
  get _bsontype() {
2499
2485
  return 'ObjectId';
@@ -2524,6 +2510,9 @@ class ObjectId extends BSONValue {
2524
2510
  else if (typeof workingId === 'string') {
2525
2511
  if (ObjectId.validateHexString(workingId)) {
2526
2512
  this.buffer = ByteUtils.fromHex(workingId);
2513
+ if (ObjectId.cacheHexString) {
2514
+ __idCache.set(this, workingId);
2515
+ }
2527
2516
  }
2528
2517
  else {
2529
2518
  throw new BSONError('input must be a 24 character hex string, 12 byte Uint8Array, or an integer');
@@ -2532,9 +2521,6 @@ class ObjectId extends BSONValue {
2532
2521
  else {
2533
2522
  throw new BSONError('Argument passed in does not match the accepted types');
2534
2523
  }
2535
- if (ObjectId.cacheHexString) {
2536
- this.__id = ByteUtils.toHex(this.id);
2537
- }
2538
2524
  }
2539
2525
  get id() {
2540
2526
  return this.buffer;
@@ -2542,7 +2528,7 @@ class ObjectId extends BSONValue {
2542
2528
  set id(value) {
2543
2529
  this.buffer = value;
2544
2530
  if (ObjectId.cacheHexString) {
2545
- this.__id = ByteUtils.toHex(value);
2531
+ __idCache.set(this, ByteUtils.toHex(value));
2546
2532
  }
2547
2533
  }
2548
2534
  static validateHexString(string) {
@@ -2560,12 +2546,14 @@ class ObjectId extends BSONValue {
2560
2546
  return true;
2561
2547
  }
2562
2548
  toHexString() {
2563
- if (ObjectId.cacheHexString && this.__id) {
2564
- return this.__id;
2549
+ if (ObjectId.cacheHexString) {
2550
+ const __id = __idCache.get(this);
2551
+ if (__id)
2552
+ return __id;
2565
2553
  }
2566
2554
  const hexString = ByteUtils.toHex(this.id);
2567
- if (ObjectId.cacheHexString && !this.__id) {
2568
- this.__id = hexString;
2555
+ if (ObjectId.cacheHexString) {
2556
+ __idCache.set(this, hexString);
2569
2557
  }
2570
2558
  return hexString;
2571
2559
  }
@@ -2689,6 +2677,9 @@ class ObjectId extends BSONValue {
2689
2677
  static fromExtendedJSON(doc) {
2690
2678
  return new ObjectId(doc.$oid);
2691
2679
  }
2680
+ isCached() {
2681
+ return ObjectId.cacheHexString && __idCache.has(this);
2682
+ }
2692
2683
  inspect(depth, options, inspect) {
2693
2684
  inspect ??= defaultInspect;
2694
2685
  return `new ObjectId(${inspect(this.toHexString(), options)})`;