Skip to content

Commit 6bb1855

Browse files
committed
NODE-2875: expand inspect support
1 parent 203402f commit 6bb1855

20 files changed

+232
-2
lines changed

src/binary.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,35 @@ 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+
switch (this.sub_type) {
267+
case Binary.SUBTYPE_MD5:
268+
return `MD5("${asBuffer.toString('hex')}")`;
269+
case Binary.SUBTYPE_UUID:
270+
if (asBuffer.length === 16) {
271+
// Format '0123456789abcdef0123456789abcdef' into
272+
// '01234567-89ab-cdef-0123-456789abcdef'.
273+
const hex = asBuffer.toString('hex');
274+
const asUUID = hex
275+
.match(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/)
276+
?.slice(1, 6)
277+
.join('-');
278+
return `UUID("${asUUID}")`;
279+
}
280+
// Fall through in case somebody did something weird and used an UUID
281+
// with a non-standard length.
282+
// eslint-disable-next-line no-fallthrough
283+
default:
284+
return `BinData(${this.sub_type}, "${asBuffer.toString('base64')}")`;
285+
}
286+
}
258287
}
259288

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

src/code.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ 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}"${
54+
codeJson.scope ? `, "scope" : ${JSON.stringify(codeJson.scope)}` : ''
55+
} }`;
56+
}
4557
}
4658

4759
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" : 1 }';
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" : 1 }';
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/binary_tests.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
const BSON = require('../register-bson');
4+
const util = require('util');
5+
const Binary = BSON.Binary;
6+
7+
describe('Binary tests', function () {
8+
it('should correctly allow for node.js inspect to work with BinData correctly', function () {
9+
const binData = new Binary('abc');
10+
expect(util.inspect(binData)).to.equal('BinData(0, "YWJj")');
11+
});
12+
13+
it('should correctly allow for node.js inspect to work with UUIDs correctly', function () {
14+
const binData = new Binary(Buffer.from('0123456789abcdef0123456789abcdef', 'hex'), 4);
15+
expect(util.inspect(binData)).to.equal('UUID("01234567-89ab-cdef-0123-456789abcdef")');
16+
});
17+
18+
it('should correctly allow for node.js inspect to work with MD5s correctly', function () {
19+
const binData = new Binary(Buffer.from('0123456789abcdef0123456789abcdef', 'hex'), 5);
20+
expect(util.inspect(binData)).to.equal('MD5("0123456789abcdef0123456789abcdef")');
21+
});
22+
});

test/node/code_tests.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const BSON = require('../register-bson');
4+
const util = require('util');
5+
const Code = BSON.Code;
6+
7+
describe('Code tests', function () {
8+
it('should correctly allow for node.js inspect to work with Code', function () {
9+
const code = new Code('abc');
10+
expect(util.inspect(code)).to.equal('{ "code" : "abc" }');
11+
12+
const codeWithScope = new Code('abc', { _id: 'someid' });
13+
expect(util.inspect(codeWithScope)).to.equal('{ "code" : "abc", "scope" : {"_id":"someid"} }');
14+
});
15+
});

test/node/db_pointer_tests.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
'use strict';
22

3+
const { DBRef } = require('../../lib/db_ref');
4+
const { ObjectId } = require('../../lib/objectid');
35
const BSON = require('../register-bson');
6+
const util = require('util');
47

58
// 0x0C foo\0 \0\0\07 String.fromCharCode(0x41, 0x42, 0xfffd, 0x43, 0x44) 12
69
const bsonSnippet = Buffer.from([
@@ -54,4 +57,16 @@ describe('dbpointer tests', function () {
5457
it('can serialize and deserialize 0xFFFD in dbpointer name', function () {
5558
expect(() => BSON.deserialize(bsonSnippet)).to.not.throw();
5659
});
60+
61+
it('should correctly allow for node.js inspect to work with DBRef', function () {
62+
const oid = new ObjectId('deadbeefdeadbeefdeadbeef');
63+
64+
const refNoDb = new DBRef('namespace', oid);
65+
expect(util.inspect(refNoDb)).to.equal('DBRef("namespace", "deadbeefdeadbeefdeadbeef")');
66+
67+
const refWithDb = new DBRef('namespace', oid, 'db');
68+
expect(util.inspect(refWithDb)).to.equal(
69+
'DBRef("namespace", "deadbeefdeadbeefdeadbeef", "db")'
70+
);
71+
});
5772
});

test/node/decimal128_tests.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const BSON = require('../register-bson');
4+
const util = require('util');
45
const Decimal128 = BSON.Decimal128;
56

67
var NAN = Buffer.from(
@@ -2131,4 +2132,9 @@ describe('Decimal128', function () {
21312132

21322133
done();
21332134
});
2135+
2136+
it('should correctly allow for node.js inspect to work with Decimal128', function () {
2137+
const dec = Decimal128.fromString('1.42');
2138+
expect(util.inspect(dec)).to.equal('Decimal128("1.42")');
2139+
});
21342140
});

test/node/int_32_tests.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const BSON = require('../register-bson');
4+
const util = require('util');
45
const Int32 = BSON.Int32;
56

67
describe('Int32', function () {
@@ -55,4 +56,9 @@ describe('Int32', function () {
5556
});
5657
});
5758
});
59+
60+
it('should correctly allow for node.js inspect to work with Decimal128', function () {
61+
const numInt = new Int32(42);
62+
expect(util.inspect(numInt)).to.equal('Int32(42)');
63+
});
5864
});

test/node/long_tests.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
const BSON = require('../register-bson');
4+
const util = require('util');
5+
const Long = BSON.Long;
6+
7+
describe('MinKey tests', function () {
8+
it('should correctly allow for node.js inspect to work with Long', function () {
9+
const numLong = Long.fromString('42');
10+
expect(util.inspect(numLong)).to.equal('Long("42")');
11+
});
12+
});

test/node/max_key_tests.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
const BSON = require('../register-bson');
4+
const util = require('util');
5+
const MaxKey = BSON.MaxKey;
6+
7+
describe('MaxKey tests', function () {
8+
it('should correctly allow for node.js inspect to work with MaxKey', function () {
9+
const minKey = new MaxKey();
10+
expect(util.inspect(minKey)).to.equal('{ "$maxKey" : 1 }');
11+
});
12+
});

test/node/min_key_tests.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
const BSON = require('../register-bson');
4+
const util = require('util');
5+
const MinKey = BSON.MinKey;
6+
7+
describe('MinKey tests', function () {
8+
it('should correctly allow for node.js inspect to work with MinKey', function () {
9+
const minKey = new MinKey();
10+
expect(util.inspect(minKey)).to.equal('{ "$minKey" : 1 }');
11+
});
12+
});

test/node/symbol_tests.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
const BSON = require('../register-bson');
4+
const util = require('util');
5+
const BSONSymbol = BSON.BSONSymbol;
6+
7+
describe('MinKey tests', function () {
8+
it('should correctly allow for node.js inspect to work with MinKey', function () {
9+
const sym = new BSONSymbol('sym');
10+
expect(util.inspect(sym)).to.equal('BSONSymbol("sym")');
11+
});
12+
});

test/node/timestamp_tests.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const BSON = require('../register-bson');
4+
const util = require('util');
45

56
describe('Timestamp', function () {
67
it('should have a MAX_VALUE equal to Long.MAX_UNSIGNED_VALUE', function () {
@@ -29,4 +30,9 @@ describe('Timestamp', function () {
2930
$timestamp: { t: 4294967295, i: 4294967295 }
3031
});
3132
});
33+
34+
it('should correctly allow for node.js inspect to work with MinKey', function () {
35+
const timestamp = new BSON.Timestamp(1, 100);
36+
expect(util.inspect(timestamp)).to.equal('Timestamp(1, 100)');
37+
});
3238
});

0 commit comments

Comments
 (0)