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/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "vendor"
15
15
  ],
16
16
  "types": "bson.d.ts",
17
- "version": "6.10.0",
17
+ "version": "6.10.1",
18
18
  "author": {
19
19
  "name": "The MongoDB NodeJS Team",
20
20
  "email": "dbx-node@mongodb.com"
@@ -55,7 +55,6 @@
55
55
  "sinon": "^18.0.0",
56
56
  "sinon-chai": "^3.7.0",
57
57
  "source-map-support": "^0.5.21",
58
- "standard-version": "^9.5.0",
59
58
  "tar": "^7.4.3",
60
59
  "ts-node": "^10.9.2",
61
60
  "tsd": "^0.31.1",
package/src/objectid.ts CHANGED
@@ -7,6 +7,9 @@ import { NumberUtils } from './utils/number_utils';
7
7
  // Unique sequence for the current process (initialized on first use)
8
8
  let PROCESS_UNIQUE: Uint8Array | null = null;
9
9
 
10
+ /** ObjectId hexString cache @internal */
11
+ const __idCache = new WeakMap(); // TODO(NODE-6549): convert this to #__id private field when target updated to ES2022
12
+
10
13
  /** @public */
11
14
  export interface ObjectIdLike {
12
15
  id: string | Uint8Array;
@@ -36,8 +39,6 @@ export class ObjectId extends BSONValue {
36
39
 
37
40
  /** ObjectId Bytes @internal */
38
41
  private buffer!: Uint8Array;
39
- /** ObjectId hexString cache @internal */
40
- private __id?: string;
41
42
 
42
43
  /**
43
44
  * Create ObjectId from a number.
@@ -111,6 +112,10 @@ export class ObjectId extends BSONValue {
111
112
  } else if (typeof workingId === 'string') {
112
113
  if (ObjectId.validateHexString(workingId)) {
113
114
  this.buffer = ByteUtils.fromHex(workingId);
115
+ // If we are caching the hex string
116
+ if (ObjectId.cacheHexString) {
117
+ __idCache.set(this, workingId);
118
+ }
114
119
  } else {
115
120
  throw new BSONError(
116
121
  'input must be a 24 character hex string, 12 byte Uint8Array, or an integer'
@@ -119,10 +124,6 @@ export class ObjectId extends BSONValue {
119
124
  } else {
120
125
  throw new BSONError('Argument passed in does not match the accepted types');
121
126
  }
122
- // If we are caching the hex string
123
- if (ObjectId.cacheHexString) {
124
- this.__id = ByteUtils.toHex(this.id);
125
- }
126
127
  }
127
128
 
128
129
  /**
@@ -136,7 +137,7 @@ export class ObjectId extends BSONValue {
136
137
  set id(value: Uint8Array) {
137
138
  this.buffer = value;
138
139
  if (ObjectId.cacheHexString) {
139
- this.__id = ByteUtils.toHex(value);
140
+ __idCache.set(this, ByteUtils.toHex(value));
140
141
  }
141
142
  }
142
143
 
@@ -165,14 +166,15 @@ export class ObjectId extends BSONValue {
165
166
 
166
167
  /** Returns the ObjectId id as a 24 lowercase character hex string representation */
167
168
  toHexString(): string {
168
- if (ObjectId.cacheHexString && this.__id) {
169
- return this.__id;
169
+ if (ObjectId.cacheHexString) {
170
+ const __id = __idCache.get(this);
171
+ if (__id) return __id;
170
172
  }
171
173
 
172
174
  const hexString = ByteUtils.toHex(this.id);
173
175
 
174
- if (ObjectId.cacheHexString && !this.__id) {
175
- this.__id = hexString;
176
+ if (ObjectId.cacheHexString) {
177
+ __idCache.set(this, hexString);
176
178
  }
177
179
 
178
180
  return hexString;
@@ -370,6 +372,11 @@ export class ObjectId extends BSONValue {
370
372
  return new ObjectId(doc.$oid);
371
373
  }
372
374
 
375
+ /** @internal */
376
+ private isCached(): boolean {
377
+ return ObjectId.cacheHexString && __idCache.has(this);
378
+ }
379
+
373
380
  /**
374
381
  * Converts to a string representation of this Id.
375
382
  *
@@ -1,65 +1,44 @@
1
- const map = new WeakMap<object, string>();
1
+ const TypedArrayPrototypeGetSymbolToStringTag = (() => {
2
+ // Type check system lovingly referenced from:
3
+ // https://github.com/nodejs/node/blob/7450332339ed40481f470df2a3014e2ec355d8d8/lib/internal/util/types.js#L13-L15
4
+ // eslint-disable-next-line @typescript-eslint/unbound-method -- the intention is to call this method with a bound value
5
+ const g = Object.getOwnPropertyDescriptor(
6
+ Object.getPrototypeOf(Uint8Array.prototype),
7
+ Symbol.toStringTag
8
+ )!.get!;
2
9
 
3
- const TYPES = {
4
- ArrayBuffer: '[object ArrayBuffer]',
5
- SharedArrayBuffer: '[object SharedArrayBuffer]',
6
- Uint8Array: '[object Uint8Array]',
7
- BigInt64Array: '[object BigInt64Array]',
8
- BigUint64Array: '[object BigUint64Array]',
9
- RegExp: '[object RegExp]',
10
- Map: '[object Map]',
11
- Date: '[object Date]'
12
- };
13
-
14
- /**
15
- * Retrieves the prototype.toString() of a value.
16
- * If the value is an object, it will cache the result in a WeakMap for future use.
17
- */
18
- function getPrototypeString(value: unknown): string {
19
- let str = map.get(value as object);
20
-
21
- if (!str) {
22
- str = Object.prototype.toString.call(value);
23
- if (value !== null && typeof value === 'object') {
24
- map.set(value, str);
25
- }
26
- }
27
- return str;
28
- }
29
-
30
- export function isAnyArrayBuffer(value: unknown): value is ArrayBuffer {
31
- const type = getPrototypeString(value);
32
- return type === TYPES.ArrayBuffer || type === TYPES.SharedArrayBuffer;
33
- }
10
+ return (value: unknown) => g.call(value);
11
+ })();
34
12
 
35
13
  export function isUint8Array(value: unknown): value is Uint8Array {
36
- const type = getPrototypeString(value);
37
- return type === TYPES.Uint8Array;
14
+ return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Uint8Array';
38
15
  }
39
16
 
40
- export function isBigInt64Array(value: unknown): value is BigInt64Array {
41
- const type = getPrototypeString(value);
42
- return type === TYPES.BigInt64Array;
43
- }
44
-
45
- export function isBigUInt64Array(value: unknown): value is BigUint64Array {
46
- const type = getPrototypeString(value);
47
- return type === TYPES.BigUint64Array;
17
+ export function isAnyArrayBuffer(value: unknown): value is ArrayBuffer {
18
+ return (
19
+ typeof value === 'object' &&
20
+ value != null &&
21
+ Symbol.toStringTag in value &&
22
+ (value[Symbol.toStringTag] === 'ArrayBuffer' ||
23
+ value[Symbol.toStringTag] === 'SharedArrayBuffer')
24
+ );
48
25
  }
49
26
 
50
- export function isRegExp(d: unknown): d is RegExp {
51
- const type = getPrototypeString(d);
52
- return type === TYPES.RegExp;
27
+ export function isRegExp(regexp: unknown): regexp is RegExp {
28
+ return regexp instanceof RegExp || Object.prototype.toString.call(regexp) === '[object RegExp]';
53
29
  }
54
30
 
55
- export function isMap(d: unknown): d is Map<unknown, unknown> {
56
- const type = getPrototypeString(d);
57
- return type === TYPES.Map;
31
+ export function isMap(value: unknown): value is Map<unknown, unknown> {
32
+ return (
33
+ typeof value === 'object' &&
34
+ value != null &&
35
+ Symbol.toStringTag in value &&
36
+ value[Symbol.toStringTag] === 'Map'
37
+ );
58
38
  }
59
39
 
60
- export function isDate(d: unknown): d is Date {
61
- const type = getPrototypeString(d);
62
- return type === TYPES.Date;
40
+ export function isDate(date: unknown): date is Date {
41
+ return date instanceof Date || Object.prototype.toString.call(date) === '[object Date]';
63
42
  }
64
43
 
65
44
  export type InspectFn = (x: unknown, options?: unknown) => string;