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