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.cjs CHANGED
@@ -15,6 +15,26 @@ function isMap(d) {
15
15
  function isDate(d) {
16
16
  return Object.prototype.toString.call(d) === '[object Date]';
17
17
  }
18
+ function defaultInspect(x, _options) {
19
+ return JSON.stringify(x, (k, v) => {
20
+ if (typeof v === 'bigint') {
21
+ return { $numberLong: `${v}` };
22
+ }
23
+ else if (isMap(v)) {
24
+ return Object.fromEntries(v);
25
+ }
26
+ return v;
27
+ });
28
+ }
29
+ function getStylizeFunction(options) {
30
+ const stylizeExists = options != null &&
31
+ typeof options === 'object' &&
32
+ 'stylize' in options &&
33
+ typeof options.stylize === 'function';
34
+ if (stylizeExists) {
35
+ return options.stylize;
36
+ }
37
+ }
18
38
 
19
39
  const BSON_MAJOR_VERSION = 6;
20
40
  const BSON_INT32_MAX = 0x7fffffff;
@@ -95,7 +115,7 @@ class BSONVersionError extends BSONError {
95
115
  return 'BSONVersionError';
96
116
  }
97
117
  constructor() {
98
- super(`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.0 or later`);
118
+ super(`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.x.x`);
99
119
  }
100
120
  }
101
121
  class BSONRuntimeError extends BSONError {
@@ -302,6 +322,9 @@ class BSONValue {
302
322
  get [Symbol.for('@@mdb.bson.version')]() {
303
323
  return BSON_MAJOR_VERSION;
304
324
  }
325
+ [Symbol.for('nodejs.util.inspect.custom')](depth, options, inspect) {
326
+ return this.inspect(depth, options, inspect);
327
+ }
305
328
  }
306
329
 
307
330
  class Binary extends BSONValue {
@@ -452,12 +475,12 @@ class Binary extends BSONValue {
452
475
  }
453
476
  return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type);
454
477
  }
455
- [Symbol.for('nodejs.util.inspect.custom')]() {
456
- return this.inspect();
457
- }
458
- inspect() {
478
+ inspect(depth, options, inspect) {
479
+ inspect ??= defaultInspect;
459
480
  const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position));
460
- return `Binary.createFromBase64("${base64}", ${this.sub_type})`;
481
+ const base64Arg = inspect(base64, options);
482
+ const subTypeArg = inspect(this.sub_type, options);
483
+ return `Binary.createFromBase64(${base64Arg}, ${subTypeArg})`;
461
484
  }
462
485
  }
463
486
  Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
@@ -575,11 +598,9 @@ class UUID extends Binary {
575
598
  static isValidUUIDString(representation) {
576
599
  return UUID_WITHOUT_DASHES.test(representation) || UUID_WITH_DASHES.test(representation);
577
600
  }
578
- [Symbol.for('nodejs.util.inspect.custom')]() {
579
- return this.inspect();
580
- }
581
- inspect() {
582
- return `new UUID("${this.toHexString()}")`;
601
+ inspect(depth, options, inspect) {
602
+ inspect ??= defaultInspect;
603
+ return `new UUID(${inspect(this.toHexString(), options)})`;
583
604
  }
584
605
  }
585
606
 
@@ -607,12 +628,15 @@ class Code extends BSONValue {
607
628
  static fromExtendedJSON(doc) {
608
629
  return new Code(doc.$code, doc.$scope);
609
630
  }
610
- [Symbol.for('nodejs.util.inspect.custom')]() {
611
- return this.inspect();
612
- }
613
- inspect() {
614
- const codeJson = this.toJSON();
615
- return `new Code(${JSON.stringify(String(codeJson.code))}${codeJson.scope != null ? `, ${JSON.stringify(codeJson.scope)}` : ''})`;
631
+ inspect(depth, options, inspect) {
632
+ inspect ??= defaultInspect;
633
+ let parametersString = inspect(this.code, options);
634
+ const multiLineFn = parametersString.includes('\n');
635
+ if (this.scope != null) {
636
+ parametersString += `,${multiLineFn ? '\n' : ' '}${inspect(this.scope, options)}`;
637
+ }
638
+ const endingNewline = multiLineFn && this.scope === null;
639
+ return `new Code(${multiLineFn ? '\n' : ''}${parametersString}${endingNewline ? '\n' : ''})`;
616
640
  }
617
641
  }
618
642
 
@@ -677,12 +701,16 @@ class DBRef extends BSONValue {
677
701
  delete copy.$db;
678
702
  return new DBRef(doc.$ref, doc.$id, doc.$db, copy);
679
703
  }
680
- [Symbol.for('nodejs.util.inspect.custom')]() {
681
- return this.inspect();
682
- }
683
- inspect() {
684
- const oid = this.oid === undefined || this.oid.toString === undefined ? this.oid : this.oid.toString();
685
- return `new DBRef("${this.namespace}", new ObjectId("${String(oid)}")${this.db ? `, "${this.db}"` : ''})`;
704
+ inspect(depth, options, inspect) {
705
+ inspect ??= defaultInspect;
706
+ const args = [
707
+ inspect(this.namespace, options),
708
+ inspect(this.oid, options),
709
+ ...(this.db ? [inspect(this.db, options)] : []),
710
+ ...(Object.keys(this.fields).length > 0 ? [inspect(this.fields, options)] : [])
711
+ ];
712
+ args[1] = inspect === defaultInspect ? `new ObjectId(${args[1]})` : args[1];
713
+ return `new DBRef(${args.join(', ')})`;
686
714
  }
687
715
  }
688
716
 
@@ -1309,11 +1337,11 @@ class Long extends BSONValue {
1309
1337
  }
1310
1338
  return longResult;
1311
1339
  }
1312
- [Symbol.for('nodejs.util.inspect.custom')]() {
1313
- return this.inspect();
1314
- }
1315
- inspect() {
1316
- return `new Long("${this.toString()}"${this.unsigned ? ', true' : ''})`;
1340
+ inspect(depth, options, inspect) {
1341
+ inspect ??= defaultInspect;
1342
+ const longVal = inspect(this.toString(), options);
1343
+ const unsignedVal = this.unsigned ? `, ${inspect(this.unsigned, options)}` : '';
1344
+ return `new Long(${longVal}${unsignedVal})`;
1317
1345
  }
1318
1346
  }
1319
1347
  Long.TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
@@ -1869,11 +1897,10 @@ class Decimal128 extends BSONValue {
1869
1897
  static fromExtendedJSON(doc) {
1870
1898
  return Decimal128.fromString(doc.$numberDecimal);
1871
1899
  }
1872
- [Symbol.for('nodejs.util.inspect.custom')]() {
1873
- return this.inspect();
1874
- }
1875
- inspect() {
1876
- return `new Decimal128("${this.toString()}")`;
1900
+ inspect(depth, options, inspect) {
1901
+ inspect ??= defaultInspect;
1902
+ const d128string = inspect(this.toString(), options);
1903
+ return `new Decimal128(${d128string})`;
1877
1904
  }
1878
1905
  }
1879
1906
 
@@ -1912,12 +1939,9 @@ class Double extends BSONValue {
1912
1939
  const doubleValue = parseFloat(doc.$numberDouble);
1913
1940
  return options && options.relaxed ? doubleValue : new Double(doubleValue);
1914
1941
  }
1915
- [Symbol.for('nodejs.util.inspect.custom')]() {
1916
- return this.inspect();
1917
- }
1918
- inspect() {
1919
- const eJSON = this.toExtendedJSON();
1920
- return `new Double(${eJSON.$numberDouble})`;
1942
+ inspect(depth, options, inspect) {
1943
+ inspect ??= defaultInspect;
1944
+ return `new Double(${inspect(this.value, options)})`;
1921
1945
  }
1922
1946
  }
1923
1947
 
@@ -1949,11 +1973,9 @@ class Int32 extends BSONValue {
1949
1973
  static fromExtendedJSON(doc, options) {
1950
1974
  return options && options.relaxed ? parseInt(doc.$numberInt, 10) : new Int32(doc.$numberInt);
1951
1975
  }
1952
- [Symbol.for('nodejs.util.inspect.custom')]() {
1953
- return this.inspect();
1954
- }
1955
- inspect() {
1956
- return `new Int32(${this.valueOf()})`;
1976
+ inspect(depth, options, inspect) {
1977
+ inspect ??= defaultInspect;
1978
+ return `new Int32(${inspect(this.value, options)})`;
1957
1979
  }
1958
1980
  }
1959
1981
 
@@ -1967,9 +1989,6 @@ class MaxKey extends BSONValue {
1967
1989
  static fromExtendedJSON() {
1968
1990
  return new MaxKey();
1969
1991
  }
1970
- [Symbol.for('nodejs.util.inspect.custom')]() {
1971
- return this.inspect();
1972
- }
1973
1992
  inspect() {
1974
1993
  return 'new MaxKey()';
1975
1994
  }
@@ -1985,9 +2004,6 @@ class MinKey extends BSONValue {
1985
2004
  static fromExtendedJSON() {
1986
2005
  return new MinKey();
1987
2006
  }
1988
- [Symbol.for('nodejs.util.inspect.custom')]() {
1989
- return this.inspect();
1990
- }
1991
2007
  inspect() {
1992
2008
  return 'new MinKey()';
1993
2009
  }
@@ -2158,11 +2174,9 @@ class ObjectId extends BSONValue {
2158
2174
  static fromExtendedJSON(doc) {
2159
2175
  return new ObjectId(doc.$oid);
2160
2176
  }
2161
- [Symbol.for('nodejs.util.inspect.custom')]() {
2162
- return this.inspect();
2163
- }
2164
- inspect() {
2165
- return `new ObjectId("${this.toHexString()}")`;
2177
+ inspect(depth, options, inspect) {
2178
+ inspect ??= defaultInspect;
2179
+ return `new ObjectId(${inspect(this.toHexString(), options)})`;
2166
2180
  }
2167
2181
  }
2168
2182
  ObjectId.index = Math.floor(Math.random() * 0xffffff);
@@ -2375,11 +2389,12 @@ class BSONRegExp extends BSONValue {
2375
2389
  }
2376
2390
  throw new BSONError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
2377
2391
  }
2378
- [Symbol.for('nodejs.util.inspect.custom')]() {
2379
- return this.inspect();
2380
- }
2381
- inspect() {
2382
- return `new BSONRegExp(${JSON.stringify(this.pattern)}, ${JSON.stringify(this.options)})`;
2392
+ inspect(depth, options, inspect) {
2393
+ const stylize = getStylizeFunction(options) ?? (v => v);
2394
+ inspect ??= defaultInspect;
2395
+ const pattern = stylize(inspect(this.pattern), 'regexp');
2396
+ const flags = stylize(inspect(this.options), 'regexp');
2397
+ return `new BSONRegExp(${pattern}, ${flags})`;
2383
2398
  }
2384
2399
  }
2385
2400
 
@@ -2397,9 +2412,6 @@ class BSONSymbol extends BSONValue {
2397
2412
  toString() {
2398
2413
  return this.value;
2399
2414
  }
2400
- inspect() {
2401
- return `new BSONSymbol(${JSON.stringify(this.value)})`;
2402
- }
2403
2415
  toJSON() {
2404
2416
  return this.value;
2405
2417
  }
@@ -2409,8 +2421,9 @@ class BSONSymbol extends BSONValue {
2409
2421
  static fromExtendedJSON(doc) {
2410
2422
  return new BSONSymbol(doc.$symbol);
2411
2423
  }
2412
- [Symbol.for('nodejs.util.inspect.custom')]() {
2413
- return this.inspect();
2424
+ inspect(depth, options, inspect) {
2425
+ inspect ??= defaultInspect;
2426
+ return `new BSONSymbol(${inspect(this.value, options)})`;
2414
2427
  }
2415
2428
  }
2416
2429
 
@@ -2485,11 +2498,11 @@ class Timestamp extends LongWithoutOverridesClass {
2485
2498
  : doc.$timestamp.t;
2486
2499
  return new Timestamp({ t, i });
2487
2500
  }
2488
- [Symbol.for('nodejs.util.inspect.custom')]() {
2489
- return this.inspect();
2490
- }
2491
- inspect() {
2492
- return `new Timestamp({ t: ${this.getHighBits()}, i: ${this.getLowBits()} })`;
2501
+ inspect(depth, options, inspect) {
2502
+ inspect ??= defaultInspect;
2503
+ const t = inspect(this.high >>> 0, options);
2504
+ const i = inspect(this.low >>> 0, options);
2505
+ return `new Timestamp({ t: ${t}, i: ${i} })`;
2493
2506
  }
2494
2507
  }
2495
2508
  Timestamp.MAX_VALUE = Long.MAX_UNSIGNED_VALUE;