Skip to content

Commit 50e4529

Browse files
authored
chore: expand inspect support of BSON types (#413)
Add a nodejs inspect symbol and `inspect` method to each BSON type. NODE-2875
1 parent a585a0c commit 50e4529

12 files changed

+197
-2
lines changed

src/binary.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,16 @@ export class Binary {
255255
}
256256
return new Binary(data, type);
257257
}
258+
259+
/** @internal */
260+
[Symbol.for('nodejs.util.inspect.custom')](): string {
261+
return this.inspect();
262+
}
263+
264+
inspect(): string {
265+
const asBuffer = this.value(true);
266+
return `Binary("${asBuffer.toString('hex')}", ${this.sub_type})`;
267+
}
258268
}
259269

260270
Object.defineProperty(Binary.prototype, '_bsontype', { value: 'Binary' });

src/code.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ export class Code {
4242
static fromExtendedJSON(doc: CodeExtended): Code {
4343
return new Code(doc.$code, doc.$scope);
4444
}
45+
46+
/** @internal */
47+
[Symbol.for('nodejs.util.inspect.custom')](): string {
48+
return this.inspect();
49+
}
50+
51+
inspect(): string {
52+
const codeJson = this.toJSON();
53+
return `Code("${codeJson.code}"${codeJson.scope ? `, ${JSON.stringify(codeJson.scope)}` : ''})`;
54+
}
4555
}
4656

4757
Object.defineProperty(Code.prototype, '_bsontype', { value: 'Code' });

src/db_ref.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ export class DBRef {
9898
delete copy.$db;
9999
return new DBRef(doc.$ref, doc.$id, doc.$db, copy);
100100
}
101+
102+
/** @internal */
103+
[Symbol.for('nodejs.util.inspect.custom')](): string {
104+
return this.inspect();
105+
}
106+
107+
inspect(): string {
108+
// NOTE: if OID is an ObjectId class it will just print the oid string.
109+
const oid =
110+
this.oid === undefined || this.oid.toString === undefined ? this.oid : this.oid.toString();
111+
return `DBRef("${this.namespace}", "${oid}"${this.db ? `, "${this.db}"` : ''})`;
112+
}
101113
}
102114

103115
Object.defineProperty(DBRef.prototype, '_bsontype', { value: 'DBRef' });

src/decimal128.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,15 @@ export class Decimal128 {
787787
static fromExtendedJSON(doc: Decimal128Extended): Decimal128 {
788788
return Decimal128.fromString(doc.$numberDecimal);
789789
}
790+
791+
/** @internal */
792+
[Symbol.for('nodejs.util.inspect.custom')](): string {
793+
return this.inspect();
794+
}
795+
796+
inspect(): string {
797+
return `Decimal128("${this.toString()}")`;
798+
}
790799
}
791800

792801
Object.defineProperty(Decimal128.prototype, '_bsontype', { value: 'Decimal128' });

src/double.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ export class Double {
7070
const doubleValue = parseFloat(doc.$numberDouble);
7171
return options && options.relaxed ? doubleValue : new Double(doubleValue);
7272
}
73+
74+
/** @internal */
75+
[Symbol.for('nodejs.util.inspect.custom')](): string {
76+
return this.inspect();
77+
}
78+
79+
inspect(): string {
80+
const eJSON = this.toExtendedJSON() as DoubleExtended;
81+
return `Double(${eJSON.$numberDouble})`;
82+
}
7383
}
7484

7585
Object.defineProperty(Double.prototype, '_bsontype', { value: 'Double' });

src/int_32.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ export class Int32 {
5050
static fromExtendedJSON(doc: Int32Extended, options?: EJSONOptions): number | Int32 {
5151
return options && options.relaxed ? parseInt(doc.$numberInt, 10) : new Int32(doc.$numberInt);
5252
}
53+
54+
/** @internal */
55+
[Symbol.for('nodejs.util.inspect.custom')](): string {
56+
return this.inspect();
57+
}
58+
59+
inspect(): string {
60+
return `Int32(${this.valueOf()})`;
61+
}
5362
}
5463

5564
Object.defineProperty(Int32.prototype, '_bsontype', { value: 'Int32' });

src/long.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,15 @@ export class Long {
943943
const result = Long.fromString(doc.$numberLong);
944944
return options && options.relaxed ? result.toNumber() : result;
945945
}
946+
947+
/** @internal */
948+
[Symbol.for('nodejs.util.inspect.custom')](): string {
949+
return this.inspect();
950+
}
951+
952+
inspect(): string {
953+
return `Long("${this.toString()}")`;
954+
}
946955
}
947956

948957
Object.defineProperty(Long.prototype, '__isLong__', { value: true });

src/max_key.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ export class MaxKey {
1919
static fromExtendedJSON(): MaxKey {
2020
return new MaxKey();
2121
}
22+
23+
/** @internal */
24+
[Symbol.for('nodejs.util.inspect.custom')](): string {
25+
return this.inspect();
26+
}
27+
28+
inspect(): string {
29+
return 'MaxKey()';
30+
}
2231
}
2332

2433
Object.defineProperty(MaxKey.prototype, '_bsontype', { value: 'MaxKey' });

src/min_key.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ export class MinKey {
1919
static fromExtendedJSON(): MinKey {
2020
return new MinKey();
2121
}
22+
23+
/** @internal */
24+
[Symbol.for('nodejs.util.inspect.custom')](): string {
25+
return this.inspect();
26+
}
27+
28+
inspect(): string {
29+
return 'MinKey()';
30+
}
2231
}
2332

2433
Object.defineProperty(MinKey.prototype, '_bsontype', { value: 'MinKey' });

src/symbol.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class BSONSymbol {
3030

3131
/** @internal */
3232
inspect(): string {
33-
return this.value;
33+
return `BSONSymbol("${this.value}")`;
3434
}
3535

3636
/** @internal */
@@ -47,6 +47,11 @@ export class BSONSymbol {
4747
static fromExtendedJSON(doc: BSONSymbolExtended): BSONSymbol {
4848
return new BSONSymbol(doc.$symbol);
4949
}
50+
51+
/** @internal */
52+
[Symbol.for('nodejs.util.inspect.custom')](): string {
53+
return this.inspect();
54+
}
5055
}
5156

5257
Object.defineProperty(BSONSymbol.prototype, '_bsontype', { value: 'Symbol' });

src/timestamp.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Long } from './long';
22

33
/** @public */
4-
export type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON';
4+
export type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect';
55
/** @public */
66
export type LongWithoutOverrides = new (low: number | Long, high?: number, unsigned?: boolean) => {
77
[P in Exclude<keyof Long, TimestampOverrides>]: Long[P];
@@ -91,4 +91,13 @@ export class Timestamp extends LongWithoutOverridesClass {
9191
static fromExtendedJSON(doc: TimestampExtended): Timestamp {
9292
return new Timestamp(doc.$timestamp.i, doc.$timestamp.t);
9393
}
94+
95+
/** @internal */
96+
[Symbol.for('nodejs.util.inspect.custom')](): string {
97+
return this.inspect();
98+
}
99+
100+
inspect(): string {
101+
return `Timestamp(${this.getLowBits().toString()}, ${this.getHighBits().toString()})`;
102+
}
94103
}

test/node/bson_test.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const MaxKey = BSON.MaxKey;
1818
const { BinaryParser } = require('../binary_parser');
1919
const vm = require('vm');
2020
const { assertBuffersEqual } = require('./tools/utils');
21+
const { inspect } = require('util');
2122

2223
/**
2324
* Module for parsing an ISO 8601 formatted string into a Date object.
@@ -2268,4 +2269,97 @@ describe('BSON', function () {
22682269
expect(() => BSON.serialize(badArray)).to.throw();
22692270
expect(() => BSON.serialize(badMap)).to.throw();
22702271
});
2272+
2273+
describe('Should support util.inspect for', function () {
2274+
/**
2275+
* @ignore
2276+
*/
2277+
it('Binary', function () {
2278+
const binary = new Binary(Buffer.from('0123456789abcdef0123456789abcdef', 'hex'), 4);
2279+
expect(inspect(binary)).to.equal('Binary("0123456789abcdef0123456789abcdef", 4)');
2280+
});
2281+
2282+
/**
2283+
* @ignore
2284+
*/
2285+
it('BSONSymbol', function () {
2286+
const symbol = new BSONSymbol('sym');
2287+
expect(inspect(symbol)).to.equal('BSONSymbol("sym")');
2288+
});
2289+
2290+
/**
2291+
* @ignore
2292+
*/
2293+
it('Code', function () {
2294+
const code = new Code('this.a > i', { i: 1 });
2295+
expect(inspect(code)).to.equal('Code("this.a > i", {"i":1})');
2296+
});
2297+
2298+
/**
2299+
* @ignore
2300+
*/
2301+
it('DBRef', function () {
2302+
const oid = new ObjectId('deadbeefdeadbeefdeadbeef');
2303+
const dbref = new DBRef('namespace', oid, 'integration_tests_');
2304+
expect(inspect(dbref)).to.equal(
2305+
'DBRef("namespace", "deadbeefdeadbeefdeadbeef", "integration_tests_")'
2306+
);
2307+
});
2308+
2309+
/**
2310+
* @ignore
2311+
*/
2312+
it('Decimal128', function () {
2313+
const dec = Decimal128.fromString('1.42');
2314+
expect(inspect(dec)).to.equal('Decimal128("1.42")');
2315+
});
2316+
2317+
/**
2318+
* @ignore
2319+
*/
2320+
it('Double', function () {
2321+
const double = new Double(-42.42);
2322+
expect(inspect(double)).to.equal('Double(-42.42)');
2323+
});
2324+
2325+
/**
2326+
* @ignore
2327+
*/
2328+
it('Int32', function () {
2329+
const int = new Int32(42);
2330+
expect(inspect(int)).to.equal('Int32(42)');
2331+
});
2332+
2333+
/**
2334+
* @ignore
2335+
*/
2336+
it('Long', function () {
2337+
const long = Long.fromString('42');
2338+
expect(inspect(long)).to.equal('Long("42")');
2339+
});
2340+
2341+
/**
2342+
* @ignore
2343+
*/
2344+
it('MaxKey', function () {
2345+
const maxKey = new MaxKey();
2346+
expect(inspect(maxKey)).to.equal('MaxKey()');
2347+
});
2348+
2349+
/**
2350+
* @ignore
2351+
*/
2352+
it('MinKey', function () {
2353+
const minKey = new MinKey();
2354+
expect(inspect(minKey)).to.equal('MinKey()');
2355+
});
2356+
2357+
/**
2358+
* @ignore
2359+
*/
2360+
it('Timestamp', function () {
2361+
const timestamp = new Timestamp(1, 100);
2362+
expect(inspect(timestamp)).to.equal('Timestamp(1, 100)');
2363+
});
2364+
});
22712365
});

0 commit comments

Comments
 (0)