bson 6.1.0 → 6.2.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.
package/bson.d.ts CHANGED
@@ -68,7 +68,7 @@ export declare class Binary extends BSONValue {
68
68
  /** Creates an Binary instance from a base64 string */
69
69
  static createFromBase64(base64: string, subType?: number): Binary;
70
70
  /* Excluded from this release type: fromExtendedJSON */
71
- inspect(): string;
71
+ inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
72
72
  }
73
73
 
74
74
  /** @public */
@@ -187,7 +187,7 @@ export declare class BSONRegExp extends BSONValue {
187
187
  static parseOptions(options?: string): string;
188
188
  /* Excluded from this release type: toExtendedJSON */
189
189
  /* Excluded from this release type: fromExtendedJSON */
190
- inspect(): string;
190
+ inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
191
191
  }
192
192
 
193
193
  /** @public */
@@ -232,10 +232,10 @@ export declare class BSONSymbol extends BSONValue {
232
232
  /** Access the wrapped string value. */
233
233
  valueOf(): string;
234
234
  toString(): string;
235
- inspect(): string;
236
235
  toJSON(): string;
237
236
  /* Excluded from this release type: toExtendedJSON */
238
237
  /* Excluded from this release type: fromExtendedJSON */
238
+ inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
239
239
  }
240
240
 
241
241
  /** @public */
@@ -275,8 +275,12 @@ export declare type BSONType = (typeof BSONType)[keyof typeof BSONType];
275
275
  export declare abstract class BSONValue {
276
276
  /** @public */
277
277
  abstract get _bsontype(): string;
278
- /** @public */
279
- abstract inspect(): string;
278
+ /**
279
+ * @public
280
+ * Prints a human-readable string of BSON value information
281
+ * If invoked manually without node.js.inspect function, this will default to a modified JSON.stringify
282
+ */
283
+ abstract inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
280
284
  /* Excluded from this release type: toExtendedJSON */
281
285
  }
282
286
 
@@ -321,7 +325,7 @@ export declare class Code extends BSONValue {
321
325
  };
322
326
  /* Excluded from this release type: toExtendedJSON */
323
327
  /* Excluded from this release type: fromExtendedJSON */
324
- inspect(): string;
328
+ inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
325
329
  }
326
330
 
327
331
  /** @public */
@@ -352,7 +356,7 @@ export declare class DBRef extends BSONValue {
352
356
  toJSON(): DBRefLike & Document;
353
357
  /* Excluded from this release type: toExtendedJSON */
354
358
  /* Excluded from this release type: fromExtendedJSON */
355
- inspect(): string;
359
+ inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
356
360
  }
357
361
 
358
362
  /** @public */
@@ -406,7 +410,7 @@ export declare class Decimal128 extends BSONValue {
406
410
  toJSON(): Decimal128Extended;
407
411
  /* Excluded from this release type: toExtendedJSON */
408
412
  /* Excluded from this release type: fromExtendedJSON */
409
- inspect(): string;
413
+ inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
410
414
  }
411
415
 
412
416
  /** @public */
@@ -529,7 +533,7 @@ export declare class Double extends BSONValue {
529
533
  toString(radix?: number): string;
530
534
  /* Excluded from this release type: toExtendedJSON */
531
535
  /* Excluded from this release type: fromExtendedJSON */
532
- inspect(): string;
536
+ inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
533
537
  }
534
538
 
535
539
  /** @public */
@@ -579,6 +583,8 @@ export declare type EJSONOptions = {
579
583
  */
580
584
  declare function EJSONserialize(value: any, options?: EJSONOptions): Document;
581
585
 
586
+ declare type InspectFn = (x: unknown, options?: unknown) => string;
587
+
582
588
  /**
583
589
  * A class representation of a BSON Int32 type.
584
590
  * @public
@@ -603,7 +609,7 @@ export declare class Int32 extends BSONValue {
603
609
  toJSON(): number;
604
610
  /* Excluded from this release type: toExtendedJSON */
605
611
  /* Excluded from this release type: fromExtendedJSON */
606
- inspect(): string;
612
+ inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
607
613
  }
608
614
 
609
615
  /** @public */
@@ -926,7 +932,7 @@ export declare class Long extends BSONValue {
926
932
  static fromExtendedJSON(doc: {
927
933
  $numberLong: string;
928
934
  }, options?: EJSONOptions): number | Long | bigint;
929
- inspect(): string;
935
+ inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
930
936
  }
931
937
 
932
938
  /** @public */
@@ -1046,7 +1052,12 @@ export declare class ObjectId extends BSONValue {
1046
1052
  static isValid(id: string | number | ObjectId | ObjectIdLike | Uint8Array): boolean;
1047
1053
  /* Excluded from this release type: toExtendedJSON */
1048
1054
  /* Excluded from this release type: fromExtendedJSON */
1049
- inspect(): string;
1055
+ /**
1056
+ * Converts to a string representation of this Id.
1057
+ *
1058
+ * @returns return the 24 character hex string representation.
1059
+ */
1060
+ inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
1050
1061
  }
1051
1062
 
1052
1063
  /** @public */
@@ -1203,7 +1214,7 @@ export declare class Timestamp extends LongWithoutOverridesClass {
1203
1214
  static fromString(str: string, optRadix: number): Timestamp;
1204
1215
  /* Excluded from this release type: toExtendedJSON */
1205
1216
  /* Excluded from this release type: fromExtendedJSON */
1206
- inspect(): string;
1217
+ inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
1207
1218
  }
1208
1219
 
1209
1220
  /** @public */
@@ -1278,7 +1289,13 @@ export declare class UUID extends Binary {
1278
1289
  static createFromBase64(base64: string): UUID;
1279
1290
  /* Excluded from this release type: bytesFromString */
1280
1291
  /* Excluded from this release type: isValidUUIDString */
1281
- inspect(): string;
1292
+ /**
1293
+ * Converts to a string representation of this Id.
1294
+ *
1295
+ * @returns return the 36 character hex string representation.
1296
+ *
1297
+ */
1298
+ inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
1282
1299
  }
1283
1300
 
1284
1301
  /** @public */
@@ -16,6 +16,26 @@ function isMap(d) {
16
16
  function isDate(d) {
17
17
  return Object.prototype.toString.call(d) === '[object Date]';
18
18
  }
19
+ function defaultInspect(x, _options) {
20
+ return JSON.stringify(x, (k, v) => {
21
+ if (typeof v === 'bigint') {
22
+ return { $numberLong: `${v}` };
23
+ }
24
+ else if (isMap(v)) {
25
+ return Object.fromEntries(v);
26
+ }
27
+ return v;
28
+ });
29
+ }
30
+ function getStylizeFunction(options) {
31
+ const stylizeExists = options != null &&
32
+ typeof options === 'object' &&
33
+ 'stylize' in options &&
34
+ typeof options.stylize === 'function';
35
+ if (stylizeExists) {
36
+ return options.stylize;
37
+ }
38
+ }
19
39
 
20
40
  const BSON_MAJOR_VERSION = 6;
21
41
  const BSON_INT32_MAX = 0x7fffffff;
@@ -96,7 +116,7 @@ class BSONVersionError extends BSONError {
96
116
  return 'BSONVersionError';
97
117
  }
98
118
  constructor() {
99
- super(`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.0 or later`);
119
+ super(`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.x.x`);
100
120
  }
101
121
  }
102
122
  class BSONRuntimeError extends BSONError {
@@ -303,6 +323,9 @@ class BSONValue {
303
323
  get [Symbol.for('@@mdb.bson.version')]() {
304
324
  return BSON_MAJOR_VERSION;
305
325
  }
326
+ [Symbol.for('nodejs.util.inspect.custom')](depth, options, inspect) {
327
+ return this.inspect(depth, options, inspect);
328
+ }
306
329
  }
307
330
 
308
331
  class Binary extends BSONValue {
@@ -453,12 +476,12 @@ class Binary extends BSONValue {
453
476
  }
454
477
  return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type);
455
478
  }
456
- [Symbol.for('nodejs.util.inspect.custom')]() {
457
- return this.inspect();
458
- }
459
- inspect() {
479
+ inspect(depth, options, inspect) {
480
+ inspect ??= defaultInspect;
460
481
  const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position));
461
- return `Binary.createFromBase64("${base64}", ${this.sub_type})`;
482
+ const base64Arg = inspect(base64, options);
483
+ const subTypeArg = inspect(this.sub_type, options);
484
+ return `Binary.createFromBase64(${base64Arg}, ${subTypeArg})`;
462
485
  }
463
486
  }
464
487
  Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
@@ -576,11 +599,9 @@ class UUID extends Binary {
576
599
  static isValidUUIDString(representation) {
577
600
  return UUID_WITHOUT_DASHES.test(representation) || UUID_WITH_DASHES.test(representation);
578
601
  }
579
- [Symbol.for('nodejs.util.inspect.custom')]() {
580
- return this.inspect();
581
- }
582
- inspect() {
583
- return `new UUID("${this.toHexString()}")`;
602
+ inspect(depth, options, inspect) {
603
+ inspect ??= defaultInspect;
604
+ return `new UUID(${inspect(this.toHexString(), options)})`;
584
605
  }
585
606
  }
586
607
 
@@ -608,12 +629,15 @@ class Code extends BSONValue {
608
629
  static fromExtendedJSON(doc) {
609
630
  return new Code(doc.$code, doc.$scope);
610
631
  }
611
- [Symbol.for('nodejs.util.inspect.custom')]() {
612
- return this.inspect();
613
- }
614
- inspect() {
615
- const codeJson = this.toJSON();
616
- return `new Code(${JSON.stringify(String(codeJson.code))}${codeJson.scope != null ? `, ${JSON.stringify(codeJson.scope)}` : ''})`;
632
+ inspect(depth, options, inspect) {
633
+ inspect ??= defaultInspect;
634
+ let parametersString = inspect(this.code, options);
635
+ const multiLineFn = parametersString.includes('\n');
636
+ if (this.scope != null) {
637
+ parametersString += `,${multiLineFn ? '\n' : ' '}${inspect(this.scope, options)}`;
638
+ }
639
+ const endingNewline = multiLineFn && this.scope === null;
640
+ return `new Code(${multiLineFn ? '\n' : ''}${parametersString}${endingNewline ? '\n' : ''})`;
617
641
  }
618
642
  }
619
643
 
@@ -678,12 +702,16 @@ class DBRef extends BSONValue {
678
702
  delete copy.$db;
679
703
  return new DBRef(doc.$ref, doc.$id, doc.$db, copy);
680
704
  }
681
- [Symbol.for('nodejs.util.inspect.custom')]() {
682
- return this.inspect();
683
- }
684
- inspect() {
685
- const oid = this.oid === undefined || this.oid.toString === undefined ? this.oid : this.oid.toString();
686
- return `new DBRef("${this.namespace}", new ObjectId("${String(oid)}")${this.db ? `, "${this.db}"` : ''})`;
705
+ inspect(depth, options, inspect) {
706
+ inspect ??= defaultInspect;
707
+ const args = [
708
+ inspect(this.namespace, options),
709
+ inspect(this.oid, options),
710
+ ...(this.db ? [inspect(this.db, options)] : []),
711
+ ...(Object.keys(this.fields).length > 0 ? [inspect(this.fields, options)] : [])
712
+ ];
713
+ args[1] = inspect === defaultInspect ? `new ObjectId(${args[1]})` : args[1];
714
+ return `new DBRef(${args.join(', ')})`;
687
715
  }
688
716
  }
689
717
 
@@ -1310,11 +1338,11 @@ class Long extends BSONValue {
1310
1338
  }
1311
1339
  return longResult;
1312
1340
  }
1313
- [Symbol.for('nodejs.util.inspect.custom')]() {
1314
- return this.inspect();
1315
- }
1316
- inspect() {
1317
- return `new Long("${this.toString()}"${this.unsigned ? ', true' : ''})`;
1341
+ inspect(depth, options, inspect) {
1342
+ inspect ??= defaultInspect;
1343
+ const longVal = inspect(this.toString(), options);
1344
+ const unsignedVal = this.unsigned ? `, ${inspect(this.unsigned, options)}` : '';
1345
+ return `new Long(${longVal}${unsignedVal})`;
1318
1346
  }
1319
1347
  }
1320
1348
  Long.TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
@@ -1870,11 +1898,10 @@ class Decimal128 extends BSONValue {
1870
1898
  static fromExtendedJSON(doc) {
1871
1899
  return Decimal128.fromString(doc.$numberDecimal);
1872
1900
  }
1873
- [Symbol.for('nodejs.util.inspect.custom')]() {
1874
- return this.inspect();
1875
- }
1876
- inspect() {
1877
- return `new Decimal128("${this.toString()}")`;
1901
+ inspect(depth, options, inspect) {
1902
+ inspect ??= defaultInspect;
1903
+ const d128string = inspect(this.toString(), options);
1904
+ return `new Decimal128(${d128string})`;
1878
1905
  }
1879
1906
  }
1880
1907
 
@@ -1913,12 +1940,9 @@ class Double extends BSONValue {
1913
1940
  const doubleValue = parseFloat(doc.$numberDouble);
1914
1941
  return options && options.relaxed ? doubleValue : new Double(doubleValue);
1915
1942
  }
1916
- [Symbol.for('nodejs.util.inspect.custom')]() {
1917
- return this.inspect();
1918
- }
1919
- inspect() {
1920
- const eJSON = this.toExtendedJSON();
1921
- return `new Double(${eJSON.$numberDouble})`;
1943
+ inspect(depth, options, inspect) {
1944
+ inspect ??= defaultInspect;
1945
+ return `new Double(${inspect(this.value, options)})`;
1922
1946
  }
1923
1947
  }
1924
1948
 
@@ -1950,11 +1974,9 @@ class Int32 extends BSONValue {
1950
1974
  static fromExtendedJSON(doc, options) {
1951
1975
  return options && options.relaxed ? parseInt(doc.$numberInt, 10) : new Int32(doc.$numberInt);
1952
1976
  }
1953
- [Symbol.for('nodejs.util.inspect.custom')]() {
1954
- return this.inspect();
1955
- }
1956
- inspect() {
1957
- return `new Int32(${this.valueOf()})`;
1977
+ inspect(depth, options, inspect) {
1978
+ inspect ??= defaultInspect;
1979
+ return `new Int32(${inspect(this.value, options)})`;
1958
1980
  }
1959
1981
  }
1960
1982
 
@@ -1968,9 +1990,6 @@ class MaxKey extends BSONValue {
1968
1990
  static fromExtendedJSON() {
1969
1991
  return new MaxKey();
1970
1992
  }
1971
- [Symbol.for('nodejs.util.inspect.custom')]() {
1972
- return this.inspect();
1973
- }
1974
1993
  inspect() {
1975
1994
  return 'new MaxKey()';
1976
1995
  }
@@ -1986,9 +2005,6 @@ class MinKey extends BSONValue {
1986
2005
  static fromExtendedJSON() {
1987
2006
  return new MinKey();
1988
2007
  }
1989
- [Symbol.for('nodejs.util.inspect.custom')]() {
1990
- return this.inspect();
1991
- }
1992
2008
  inspect() {
1993
2009
  return 'new MinKey()';
1994
2010
  }
@@ -2159,11 +2175,9 @@ class ObjectId extends BSONValue {
2159
2175
  static fromExtendedJSON(doc) {
2160
2176
  return new ObjectId(doc.$oid);
2161
2177
  }
2162
- [Symbol.for('nodejs.util.inspect.custom')]() {
2163
- return this.inspect();
2164
- }
2165
- inspect() {
2166
- return `new ObjectId("${this.toHexString()}")`;
2178
+ inspect(depth, options, inspect) {
2179
+ inspect ??= defaultInspect;
2180
+ return `new ObjectId(${inspect(this.toHexString(), options)})`;
2167
2181
  }
2168
2182
  }
2169
2183
  ObjectId.index = Math.floor(Math.random() * 0xffffff);
@@ -2376,11 +2390,12 @@ class BSONRegExp extends BSONValue {
2376
2390
  }
2377
2391
  throw new BSONError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
2378
2392
  }
2379
- [Symbol.for('nodejs.util.inspect.custom')]() {
2380
- return this.inspect();
2381
- }
2382
- inspect() {
2383
- return `new BSONRegExp(${JSON.stringify(this.pattern)}, ${JSON.stringify(this.options)})`;
2393
+ inspect(depth, options, inspect) {
2394
+ const stylize = getStylizeFunction(options) ?? (v => v);
2395
+ inspect ??= defaultInspect;
2396
+ const pattern = stylize(inspect(this.pattern), 'regexp');
2397
+ const flags = stylize(inspect(this.options), 'regexp');
2398
+ return `new BSONRegExp(${pattern}, ${flags})`;
2384
2399
  }
2385
2400
  }
2386
2401
 
@@ -2398,9 +2413,6 @@ class BSONSymbol extends BSONValue {
2398
2413
  toString() {
2399
2414
  return this.value;
2400
2415
  }
2401
- inspect() {
2402
- return `new BSONSymbol(${JSON.stringify(this.value)})`;
2403
- }
2404
2416
  toJSON() {
2405
2417
  return this.value;
2406
2418
  }
@@ -2410,8 +2422,9 @@ class BSONSymbol extends BSONValue {
2410
2422
  static fromExtendedJSON(doc) {
2411
2423
  return new BSONSymbol(doc.$symbol);
2412
2424
  }
2413
- [Symbol.for('nodejs.util.inspect.custom')]() {
2414
- return this.inspect();
2425
+ inspect(depth, options, inspect) {
2426
+ inspect ??= defaultInspect;
2427
+ return `new BSONSymbol(${inspect(this.value, options)})`;
2415
2428
  }
2416
2429
  }
2417
2430
 
@@ -2486,11 +2499,11 @@ class Timestamp extends LongWithoutOverridesClass {
2486
2499
  : doc.$timestamp.t;
2487
2500
  return new Timestamp({ t, i });
2488
2501
  }
2489
- [Symbol.for('nodejs.util.inspect.custom')]() {
2490
- return this.inspect();
2491
- }
2492
- inspect() {
2493
- return `new Timestamp({ t: ${this.getHighBits()}, i: ${this.getLowBits()} })`;
2502
+ inspect(depth, options, inspect) {
2503
+ inspect ??= defaultInspect;
2504
+ const t = inspect(this.high >>> 0, options);
2505
+ const i = inspect(this.low >>> 0, options);
2506
+ return `new Timestamp({ t: ${t}, i: ${i} })`;
2494
2507
  }
2495
2508
  }
2496
2509
  Timestamp.MAX_VALUE = Long.MAX_UNSIGNED_VALUE;