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/lib/bson.rn.cjs CHANGED
@@ -21,6 +21,26 @@ function isMap(d) {
21
21
  function isDate(d) {
22
22
  return Object.prototype.toString.call(d) === '[object Date]';
23
23
  }
24
+ function defaultInspect(x, _options) {
25
+ return JSON.stringify(x, (k, v) => {
26
+ if (typeof v === 'bigint') {
27
+ return { $numberLong: `${v}` };
28
+ }
29
+ else if (isMap(v)) {
30
+ return Object.fromEntries(v);
31
+ }
32
+ return v;
33
+ });
34
+ }
35
+ function getStylizeFunction(options) {
36
+ const stylizeExists = options != null &&
37
+ typeof options === 'object' &&
38
+ 'stylize' in options &&
39
+ typeof options.stylize === 'function';
40
+ if (stylizeExists) {
41
+ return options.stylize;
42
+ }
43
+ }
24
44
 
25
45
  const BSON_MAJOR_VERSION = 6;
26
46
  const BSON_INT32_MAX = 0x7fffffff;
@@ -108,7 +128,7 @@ class BSONVersionError extends BSONError {
108
128
  return 'BSONVersionError';
109
129
  }
110
130
  constructor() {
111
- super(`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.0 or later`);
131
+ super(`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.x.x`);
112
132
  }
113
133
  }
114
134
  class BSONRuntimeError extends BSONError {
@@ -317,6 +337,9 @@ class BSONValue {
317
337
  get [Symbol.for('@@mdb.bson.version')]() {
318
338
  return BSON_MAJOR_VERSION;
319
339
  }
340
+ [Symbol.for('nodejs.util.inspect.custom')](depth, options, inspect) {
341
+ return this.inspect(depth, options, inspect);
342
+ }
320
343
  }
321
344
 
322
345
  class Binary extends BSONValue {
@@ -467,12 +490,12 @@ class Binary extends BSONValue {
467
490
  }
468
491
  return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type);
469
492
  }
470
- [Symbol.for('nodejs.util.inspect.custom')]() {
471
- return this.inspect();
472
- }
473
- inspect() {
493
+ inspect(depth, options, inspect) {
494
+ inspect ??= defaultInspect;
474
495
  const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position));
475
- return `Binary.createFromBase64("${base64}", ${this.sub_type})`;
496
+ const base64Arg = inspect(base64, options);
497
+ const subTypeArg = inspect(this.sub_type, options);
498
+ return `Binary.createFromBase64(${base64Arg}, ${subTypeArg})`;
476
499
  }
477
500
  }
478
501
  Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
@@ -590,11 +613,9 @@ class UUID extends Binary {
590
613
  static isValidUUIDString(representation) {
591
614
  return UUID_WITHOUT_DASHES.test(representation) || UUID_WITH_DASHES.test(representation);
592
615
  }
593
- [Symbol.for('nodejs.util.inspect.custom')]() {
594
- return this.inspect();
595
- }
596
- inspect() {
597
- return `new UUID("${this.toHexString()}")`;
616
+ inspect(depth, options, inspect) {
617
+ inspect ??= defaultInspect;
618
+ return `new UUID(${inspect(this.toHexString(), options)})`;
598
619
  }
599
620
  }
600
621
 
@@ -622,12 +643,15 @@ class Code extends BSONValue {
622
643
  static fromExtendedJSON(doc) {
623
644
  return new Code(doc.$code, doc.$scope);
624
645
  }
625
- [Symbol.for('nodejs.util.inspect.custom')]() {
626
- return this.inspect();
627
- }
628
- inspect() {
629
- const codeJson = this.toJSON();
630
- return `new Code(${JSON.stringify(String(codeJson.code))}${codeJson.scope != null ? `, ${JSON.stringify(codeJson.scope)}` : ''})`;
646
+ inspect(depth, options, inspect) {
647
+ inspect ??= defaultInspect;
648
+ let parametersString = inspect(this.code, options);
649
+ const multiLineFn = parametersString.includes('\n');
650
+ if (this.scope != null) {
651
+ parametersString += `,${multiLineFn ? '\n' : ' '}${inspect(this.scope, options)}`;
652
+ }
653
+ const endingNewline = multiLineFn && this.scope === null;
654
+ return `new Code(${multiLineFn ? '\n' : ''}${parametersString}${endingNewline ? '\n' : ''})`;
631
655
  }
632
656
  }
633
657
 
@@ -692,12 +716,16 @@ class DBRef extends BSONValue {
692
716
  delete copy.$db;
693
717
  return new DBRef(doc.$ref, doc.$id, doc.$db, copy);
694
718
  }
695
- [Symbol.for('nodejs.util.inspect.custom')]() {
696
- return this.inspect();
697
- }
698
- inspect() {
699
- const oid = this.oid === undefined || this.oid.toString === undefined ? this.oid : this.oid.toString();
700
- return `new DBRef("${this.namespace}", new ObjectId("${String(oid)}")${this.db ? `, "${this.db}"` : ''})`;
719
+ inspect(depth, options, inspect) {
720
+ inspect ??= defaultInspect;
721
+ const args = [
722
+ inspect(this.namespace, options),
723
+ inspect(this.oid, options),
724
+ ...(this.db ? [inspect(this.db, options)] : []),
725
+ ...(Object.keys(this.fields).length > 0 ? [inspect(this.fields, options)] : [])
726
+ ];
727
+ args[1] = inspect === defaultInspect ? `new ObjectId(${args[1]})` : args[1];
728
+ return `new DBRef(${args.join(', ')})`;
701
729
  }
702
730
  }
703
731
 
@@ -1324,11 +1352,11 @@ class Long extends BSONValue {
1324
1352
  }
1325
1353
  return longResult;
1326
1354
  }
1327
- [Symbol.for('nodejs.util.inspect.custom')]() {
1328
- return this.inspect();
1329
- }
1330
- inspect() {
1331
- return `new Long("${this.toString()}"${this.unsigned ? ', true' : ''})`;
1355
+ inspect(depth, options, inspect) {
1356
+ inspect ??= defaultInspect;
1357
+ const longVal = inspect(this.toString(), options);
1358
+ const unsignedVal = this.unsigned ? `, ${inspect(this.unsigned, options)}` : '';
1359
+ return `new Long(${longVal}${unsignedVal})`;
1332
1360
  }
1333
1361
  }
1334
1362
  Long.TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
@@ -1884,11 +1912,10 @@ class Decimal128 extends BSONValue {
1884
1912
  static fromExtendedJSON(doc) {
1885
1913
  return Decimal128.fromString(doc.$numberDecimal);
1886
1914
  }
1887
- [Symbol.for('nodejs.util.inspect.custom')]() {
1888
- return this.inspect();
1889
- }
1890
- inspect() {
1891
- return `new Decimal128("${this.toString()}")`;
1915
+ inspect(depth, options, inspect) {
1916
+ inspect ??= defaultInspect;
1917
+ const d128string = inspect(this.toString(), options);
1918
+ return `new Decimal128(${d128string})`;
1892
1919
  }
1893
1920
  }
1894
1921
 
@@ -1927,12 +1954,9 @@ class Double extends BSONValue {
1927
1954
  const doubleValue = parseFloat(doc.$numberDouble);
1928
1955
  return options && options.relaxed ? doubleValue : new Double(doubleValue);
1929
1956
  }
1930
- [Symbol.for('nodejs.util.inspect.custom')]() {
1931
- return this.inspect();
1932
- }
1933
- inspect() {
1934
- const eJSON = this.toExtendedJSON();
1935
- return `new Double(${eJSON.$numberDouble})`;
1957
+ inspect(depth, options, inspect) {
1958
+ inspect ??= defaultInspect;
1959
+ return `new Double(${inspect(this.value, options)})`;
1936
1960
  }
1937
1961
  }
1938
1962
 
@@ -1964,11 +1988,9 @@ class Int32 extends BSONValue {
1964
1988
  static fromExtendedJSON(doc, options) {
1965
1989
  return options && options.relaxed ? parseInt(doc.$numberInt, 10) : new Int32(doc.$numberInt);
1966
1990
  }
1967
- [Symbol.for('nodejs.util.inspect.custom')]() {
1968
- return this.inspect();
1969
- }
1970
- inspect() {
1971
- return `new Int32(${this.valueOf()})`;
1991
+ inspect(depth, options, inspect) {
1992
+ inspect ??= defaultInspect;
1993
+ return `new Int32(${inspect(this.value, options)})`;
1972
1994
  }
1973
1995
  }
1974
1996
 
@@ -1982,9 +2004,6 @@ class MaxKey extends BSONValue {
1982
2004
  static fromExtendedJSON() {
1983
2005
  return new MaxKey();
1984
2006
  }
1985
- [Symbol.for('nodejs.util.inspect.custom')]() {
1986
- return this.inspect();
1987
- }
1988
2007
  inspect() {
1989
2008
  return 'new MaxKey()';
1990
2009
  }
@@ -2000,9 +2019,6 @@ class MinKey extends BSONValue {
2000
2019
  static fromExtendedJSON() {
2001
2020
  return new MinKey();
2002
2021
  }
2003
- [Symbol.for('nodejs.util.inspect.custom')]() {
2004
- return this.inspect();
2005
- }
2006
2022
  inspect() {
2007
2023
  return 'new MinKey()';
2008
2024
  }
@@ -2173,11 +2189,9 @@ class ObjectId extends BSONValue {
2173
2189
  static fromExtendedJSON(doc) {
2174
2190
  return new ObjectId(doc.$oid);
2175
2191
  }
2176
- [Symbol.for('nodejs.util.inspect.custom')]() {
2177
- return this.inspect();
2178
- }
2179
- inspect() {
2180
- return `new ObjectId("${this.toHexString()}")`;
2192
+ inspect(depth, options, inspect) {
2193
+ inspect ??= defaultInspect;
2194
+ return `new ObjectId(${inspect(this.toHexString(), options)})`;
2181
2195
  }
2182
2196
  }
2183
2197
  ObjectId.index = Math.floor(Math.random() * 0xffffff);
@@ -2390,11 +2404,12 @@ class BSONRegExp extends BSONValue {
2390
2404
  }
2391
2405
  throw new BSONError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
2392
2406
  }
2393
- [Symbol.for('nodejs.util.inspect.custom')]() {
2394
- return this.inspect();
2395
- }
2396
- inspect() {
2397
- return `new BSONRegExp(${JSON.stringify(this.pattern)}, ${JSON.stringify(this.options)})`;
2407
+ inspect(depth, options, inspect) {
2408
+ const stylize = getStylizeFunction(options) ?? (v => v);
2409
+ inspect ??= defaultInspect;
2410
+ const pattern = stylize(inspect(this.pattern), 'regexp');
2411
+ const flags = stylize(inspect(this.options), 'regexp');
2412
+ return `new BSONRegExp(${pattern}, ${flags})`;
2398
2413
  }
2399
2414
  }
2400
2415
 
@@ -2412,9 +2427,6 @@ class BSONSymbol extends BSONValue {
2412
2427
  toString() {
2413
2428
  return this.value;
2414
2429
  }
2415
- inspect() {
2416
- return `new BSONSymbol(${JSON.stringify(this.value)})`;
2417
- }
2418
2430
  toJSON() {
2419
2431
  return this.value;
2420
2432
  }
@@ -2424,8 +2436,9 @@ class BSONSymbol extends BSONValue {
2424
2436
  static fromExtendedJSON(doc) {
2425
2437
  return new BSONSymbol(doc.$symbol);
2426
2438
  }
2427
- [Symbol.for('nodejs.util.inspect.custom')]() {
2428
- return this.inspect();
2439
+ inspect(depth, options, inspect) {
2440
+ inspect ??= defaultInspect;
2441
+ return `new BSONSymbol(${inspect(this.value, options)})`;
2429
2442
  }
2430
2443
  }
2431
2444
 
@@ -2500,11 +2513,11 @@ class Timestamp extends LongWithoutOverridesClass {
2500
2513
  : doc.$timestamp.t;
2501
2514
  return new Timestamp({ t, i });
2502
2515
  }
2503
- [Symbol.for('nodejs.util.inspect.custom')]() {
2504
- return this.inspect();
2505
- }
2506
- inspect() {
2507
- return `new Timestamp({ t: ${this.getHighBits()}, i: ${this.getLowBits()} })`;
2516
+ inspect(depth, options, inspect) {
2517
+ inspect ??= defaultInspect;
2518
+ const t = inspect(this.high >>> 0, options);
2519
+ const i = inspect(this.low >>> 0, options);
2520
+ return `new Timestamp({ t: ${t}, i: ${i} })`;
2508
2521
  }
2509
2522
  }
2510
2523
  Timestamp.MAX_VALUE = Long.MAX_UNSIGNED_VALUE;