Skip to content

Commit d93774d

Browse files
committed
refactor: use util.inspect with passed options
1 parent a373db8 commit d93774d

File tree

2 files changed

+62
-40
lines changed

2 files changed

+62
-40
lines changed

packages/browser-repl/src/components/utils/inspect.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { CustomInspectFunction } from 'util';
12
import { inspect as utilInspect } from 'util';
23
import { bsonStringifiers } from '@mongosh/service-provider-core';
34

@@ -14,18 +15,16 @@ import { bsonStringifiers } from '@mongosh/service-provider-core';
1415
const customInspect = utilInspect.custom || 'inspect';
1516
const visitedObjects = new WeakSet();
1617

17-
function tryAddInspect(
18-
obj: any,
19-
stringifier: (this: any, depth: any, options: any) => string
20-
): void {
18+
function tryAddInspect(obj: unknown, stringifier: CustomInspectFunction): void {
2119
try {
2220
Object.defineProperty(obj, customInspect, {
2321
writable: true,
2422
configurable: true,
2523
enumerable: false,
26-
value: function (...args: [any, any]) {
24+
value: function (...args: Parameters<CustomInspectFunction>): string {
2725
try {
2826
return stringifier.call(this, ...args);
27+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2928
} catch (err: any) {
3029
// eslint-disable-next-line no-console
3130
console.warn('Could not inspect bson object', { obj: this, err });

packages/service-provider-core/src/printable-bson.ts

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { bson as BSON } from './bson-export';
2+
import type { InspectOptionsStylized, CustomInspectFunction } from 'util';
23
import { inspect as utilInspect } from 'util';
34
const inspectCustom = Symbol.for('nodejs.util.inspect.custom');
45
type BSONClassKey = (typeof BSON)[Exclude<
@@ -8,26 +9,73 @@ type BSONClassKey = (typeof BSON)[Exclude<
89

910
// Turn e.g. 'new Double(...)' into 'Double(...)' but preserve possible leading whitespace
1011
function removeNewFromInspectResult(str: string): string {
11-
return String(str).replace(/^(\s*)(new )/, '$1');
12+
return str.replace(/^(\s*)(new )/, '$1');
13+
}
14+
15+
/** Typed array such as Int8Array have a format like 'Int8Array(3) [1, 2, 3]'
16+
* and we want to remove the prefix and keep just the array contents. */
17+
function removeTypedArrayPrefixFromInspectResult(str: string): string {
18+
return str.replace(/^\s*\S+\s*\(\d+\)\s*/, '');
1219
}
1320

1421
// Create a Node.js-util-inspect() style custom inspect function that
1522
// strips 'new ' from inspect results but otherwise uses the Node.js
1623
// driver's bson library's inspect functions.
17-
function makeClasslessInspect<K extends BSONClassKey>(className: K) {
24+
function makeClasslessInspect<K extends BSONClassKey>(
25+
className: K
26+
): CustomInspectFunction {
1827
const originalInspect = BSON[className].prototype.inspect;
1928
return function (
20-
this: (typeof BSON)[typeof className]['prototype'],
21-
...args: any
29+
this: typeof originalInspect,
30+
...args: Parameters<typeof originalInspect>
2231
) {
2332
return removeNewFromInspectResult(originalInspect.apply(this, args));
24-
};
33+
} as CustomInspectFunction;
2534
}
2635

2736
const binaryInspect = makeClasslessInspect('Binary');
37+
38+
const binaryVectorInspect = function (
39+
this: typeof BSON.Binary.prototype,
40+
depth: number,
41+
options: InspectOptionsStylized
42+
): string {
43+
switch (this.buffer[0]) {
44+
case BSON.Binary.VECTOR_TYPE.Int8:
45+
return `Binary.fromInt8Array(new Int8Array(${removeTypedArrayPrefixFromInspectResult(
46+
utilInspect(this.toInt8Array(), {
47+
depth,
48+
...options,
49+
// These arrays can be very large, so would prefer to use the default options instead.
50+
maxArrayLength: utilInspect.defaultOptions.maxArrayLength,
51+
})
52+
)})`;
53+
case BSON.Binary.VECTOR_TYPE.Float32:
54+
return `Binary.fromFloat32Array(new Float32Array(${removeTypedArrayPrefixFromInspectResult(
55+
utilInspect(this.toFloat32Array(), {
56+
depth,
57+
...options,
58+
// These arrays can be very large, so would prefer to use the default options instead.
59+
maxArrayLength: utilInspect.defaultOptions.maxArrayLength,
60+
})
61+
)})`;
62+
case BSON.Binary.VECTOR_TYPE.PackedBit:
63+
return `Binary.fromPackedBitArray(new Uint8Array(${removeTypedArrayPrefixFromInspectResult(
64+
utilInspect(this.toPackedBits(), {
65+
depth,
66+
...options,
67+
// These arrays can be very large, so would prefer to use the default options instead.
68+
maxArrayLength: utilInspect.defaultOptions.maxArrayLength,
69+
})
70+
)})`;
71+
default:
72+
return binaryInspect.call(this, depth, options);
73+
}
74+
} satisfies CustomInspectFunction;
75+
2876
export const bsonStringifiers: Record<
2977
BSONClassKey | 'ObjectID',
30-
(this: any, depth: any, options: any) => string
78+
CustomInspectFunction
3179
> = {
3280
ObjectId: makeClasslessInspect('ObjectId'),
3381
ObjectID: makeClasslessInspect('ObjectId'),
@@ -44,38 +92,13 @@ export const bsonStringifiers: Record<
4492
BSONRegExp: makeClasslessInspect('BSONRegExp'),
4593
Binary: function (
4694
this: typeof BSON.Binary.prototype,
47-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
48-
...args: any[]
95+
...args: Parameters<CustomInspectFunction>
4996
): string {
5097
const hexString = this.toString('hex');
98+
5199
switch (this.sub_type) {
52100
case BSON.Binary.SUBTYPE_VECTOR:
53-
switch (this.buffer[0]) {
54-
case BSON.Binary.VECTOR_TYPE.Int8:
55-
return `Int8(${utilInspect(
56-
Array.from(this.toInt8Array()),
57-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
58-
...args
59-
)})`;
60-
case BSON.Binary.VECTOR_TYPE.Float32:
61-
return `Float32(${utilInspect(
62-
Array.from(this.toFloat32Array()),
63-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
64-
...args
65-
)})`;
66-
case BSON.Binary.VECTOR_TYPE.PackedBit:
67-
return `PackedBit(${utilInspect(
68-
hexString,
69-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
70-
...args
71-
)})`;
72-
default:
73-
return `Vector(${utilInspect(
74-
hexString,
75-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
76-
...args
77-
)})`;
78-
}
101+
return binaryVectorInspect.apply(this, args);
79102
case BSON.Binary.SUBTYPE_MD5:
80103
return `MD5('${hexString}')`;
81104
case BSON.Binary.SUBTYPE_UUID:
@@ -93,7 +116,7 @@ export const bsonStringifiers: Record<
93116
default:
94117
return binaryInspect.apply(this, args);
95118
}
96-
},
119+
} satisfies CustomInspectFunction,
97120
};
98121

99122
/**

0 commit comments

Comments
 (0)