Skip to content

Commit 2110bae

Browse files
committed
NODE-2875: expand inspect support
1 parent 203402f commit 2110bae

File tree

11 files changed

+128
-2
lines changed

11 files changed

+128
-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/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: 35 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,38 @@ describe('BSON', function () {
22682269
expect(() => BSON.serialize(badArray)).to.throw();
22692270
expect(() => BSON.serialize(badMap)).to.throw();
22702271
});
2272+
2273+
/**
2274+
* @ignore
2275+
*/
2276+
it('Should support util.inspect for BSON types', function (done) {
2277+
const oid = new ObjectId('deadbeefdeadbeefdeadbeef');
2278+
const doc = {
2279+
binary: new Binary(Buffer.from('0123456789abcdef0123456789abcdef', 'hex'), 4),
2280+
code: new Code('this.a > i', { i: 1 }),
2281+
dbref: new DBRef('namespace', oid, 'integration_tests_'),
2282+
dec: Decimal128.fromString('1.42'),
2283+
int: new Int32(42),
2284+
long: Long.fromString('42'),
2285+
maxKey: new MaxKey(),
2286+
minKey: new MinKey(),
2287+
symbol: new BSONSymbol('sym'),
2288+
timestamp: new Timestamp(1, 100)
2289+
};
2290+
2291+
const inspected = inspect(doc);
2292+
expect(inspected).to.equal(`{
2293+
binary: Binary("0123456789abcdef0123456789abcdef", 4),
2294+
code: Code("this.a > i", {"i":1}),
2295+
dbref: DBRef("namespace", "deadbeefdeadbeefdeadbeef", "integration_tests_"),
2296+
dec: Decimal128("1.42"),
2297+
int: Int32(42),
2298+
long: Long("42"),
2299+
maxKey: MaxKey(),
2300+
minKey: MinKey(),
2301+
symbol: BSONSymbol("sym"),
2302+
timestamp: Timestamp(1, 100)
2303+
}`);
2304+
done();
2305+
});
22712306
});

0 commit comments

Comments
 (0)