Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NODE-5040): add color to BSON inspect #635

Merged
merged 17 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 additions & 11 deletions src/binary.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { isAnyArrayBuffer, isUint8Array } from './parser/utils';
import {
type InspectParameterFn,
getBasicInspectParameterFn,
isAnyArrayBuffer,
isUint8Array
} from './parser/utils';
import type { EJSONOptions } from './extended_json';
import { BSONError } from './error';
import { BSON_BINARY_SUBTYPE_UUID_NEW } from './constants';
Expand Down Expand Up @@ -264,13 +269,24 @@ export class Binary extends BSONValue {
}

/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
[Symbol.for('nodejs.util.inspect.custom')](
depth?: number,
options?: unknown,
inspect?: InspectParameterFn
): string {
return this.inspect(depth, options, inspect);
}
addaleax marked this conversation as resolved.
Show resolved Hide resolved

inspect(): string {
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
const addQuotes = inspect ? false : true;
inspect ??= getBasicInspectParameterFn();
const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position));
return `Binary.createFromBase64("${base64}", ${this.sub_type})`;
const base64Arg = inspect(base64, options);
const subTypeArg = inspect(this.sub_type, options);
if (addQuotes) {
return `Binary.createFromBase64('${base64Arg}', ${subTypeArg})`;
}
return `Binary.createFromBase64(${base64Arg}, ${subTypeArg})`;
}
}

Expand Down Expand Up @@ -465,11 +481,20 @@ export class UUID extends Binary {
* @returns return the 36 character hex string representation.
* @internal
*/
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}

inspect(): string {
return `new UUID("${this.toHexString()}")`;
[Symbol.for('nodejs.util.inspect.custom')](
depth?: number,
options?: unknown,
inspect?: InspectParameterFn
): string {
return this.inspect(depth, options, inspect);
}

inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
const addQuotes = inspect ? false : true;
inspect ??= getBasicInspectParameterFn();
if (addQuotes) {
return `new UUID('${inspect(this.toHexString(), options)}')`;
}
return `new UUID(${inspect(this.toHexString(), options)})`;
}
}
23 changes: 16 additions & 7 deletions src/code.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Document } from './bson';
import { BSONValue } from './bson_value';
import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils';

/** @public */
export interface CodeExtended {
Expand Down Expand Up @@ -56,14 +57,22 @@ export class Code extends BSONValue {
}

/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
[Symbol.for('nodejs.util.inspect.custom')](
depth?: number,
options?: unknown,
inspect?: InspectParameterFn
): string {
return this.inspect(depth, options, inspect);
}

inspect(): string {
const codeJson = this.toJSON();
return `new Code(${JSON.stringify(String(codeJson.code))}${
codeJson.scope != null ? `, ${JSON.stringify(codeJson.scope)}` : ''
})`;
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
inspect ??= getBasicInspectParameterFn();
let parametersString = inspect(this.code, options);
const multiLineFn = parametersString.includes('\n');
if (this.scope != null) {
parametersString += `,${multiLineFn ? '\n' : ' '}${inspect(this.scope, options)}`;
}
const endingNewline = multiLineFn && this.scope === null;
return `new Code(${multiLineFn ? '\n' : ''}${parametersString}${endingNewline ? '\n' : ''})`;
}
}
37 changes: 28 additions & 9 deletions src/db_ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Document } from './bson';
import { BSONValue } from './bson_value';
import type { EJSONOptions } from './extended_json';
import type { ObjectId } from './objectid';
import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils';

/** @public */
export interface DBRefLike {
Expand Down Expand Up @@ -112,16 +113,34 @@ export class DBRef extends BSONValue {
}

/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
[Symbol.for('nodejs.util.inspect.custom')](
depth?: number,
options?: unknown,
inspect?: InspectParameterFn
): string {
return this.inspect(depth, options, inspect);
}

inspect(): string {
// NOTE: if OID is an ObjectId class it will just print the oid string.
const oid =
this.oid === undefined || this.oid.toString === undefined ? this.oid : this.oid.toString();
addaleax marked this conversation as resolved.
Show resolved Hide resolved
return `new DBRef("${this.namespace}", new ObjectId("${String(oid)}")${
this.db ? `, "${this.db}"` : ''
})`;
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
const addQuotes = inspect ? false : true;
baileympearson marked this conversation as resolved.
Show resolved Hide resolved
inspect ??= getBasicInspectParameterFn();

const namespaceArg = addQuotes
? `'${inspect(this.namespace, options)}'`
: inspect(this.namespace, options);
const objectIDArg = addQuotes
? `new ObjectId('${inspect(this.oid, options)}')`
: inspect(this.oid, options);
const args = [namespaceArg, objectIDArg];

if (this.db) {
args.push(addQuotes ? `'${inspect(this.db, options)}'` : inspect(this.db, options));
}

if (Object.keys(this.fields).length > 0) {
args.push(inspect(this.fields, options));
}

return `new DBRef(${args.join(', ')})`;
}
}
20 changes: 15 additions & 5 deletions src/decimal128.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BSONValue } from './bson_value';
import { BSONError } from './error';
import { Long } from './long';
import { isUint8Array } from './parser/utils';
import { type InspectParameterFn, getBasicInspectParameterFn, isUint8Array } from './parser/utils';
import { ByteUtils } from './utils/byte_utils';

const PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/;
Expand Down Expand Up @@ -848,11 +848,21 @@ export class Decimal128 extends BSONValue {
}

/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
[Symbol.for('nodejs.util.inspect.custom')](
depth?: number,
options?: unknown,
inspect?: InspectParameterFn
): string {
return this.inspect(depth, options, inspect);
}

inspect(): string {
return `new Decimal128("${this.toString()}")`;
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
const addQuotes = inspect ? false : true;
inspect ??= getBasicInspectParameterFn();
const d128string = inspect(this.toString(), options);
if (addQuotes) {
return `new Decimal128('${d128string}')`;
}
return `new Decimal128(${d128string})`;
}
}
15 changes: 10 additions & 5 deletions src/double.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BSONValue } from './bson_value';
import type { EJSONOptions } from './extended_json';
import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils';

/** @public */
export interface DoubleExtended {
Expand Down Expand Up @@ -72,12 +73,16 @@ export class Double extends BSONValue {
}

/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
[Symbol.for('nodejs.util.inspect.custom')](
depth?: number,
options?: unknown,
inspect?: InspectParameterFn
): string {
return this.inspect(depth, options, inspect);
}

inspect(): string {
const eJSON = this.toExtendedJSON() as DoubleExtended;
return `new Double(${eJSON.$numberDouble})`;
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
inspect ??= getBasicInspectParameterFn();
return `new Double(${inspect(this.valueOf(), options)})`;
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
}
}
14 changes: 10 additions & 4 deletions src/int_32.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BSONValue } from './bson_value';
import type { EJSONOptions } from './extended_json';
import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils';

/** @public */
export interface Int32Extended {
Expand Down Expand Up @@ -60,11 +61,16 @@ export class Int32 extends BSONValue {
}

/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
[Symbol.for('nodejs.util.inspect.custom')](
depth?: number,
options?: unknown,
inspect?: InspectParameterFn
): string {
return this.inspect(depth, options, inspect);
}

inspect(): string {
return `new Int32(${this.valueOf()})`;
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
inspect ??= getBasicInspectParameterFn();
return `new Int32(${inspect(this.value, options)})`;
}
}
20 changes: 14 additions & 6 deletions src/long.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BSONValue } from './bson_value';
import { BSONError } from './error';
import type { EJSONOptions } from './extended_json';
import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils';
import type { Timestamp } from './timestamp';

interface LongWASMHelpers {
Expand Down Expand Up @@ -1057,11 +1058,18 @@ export class Long extends BSONValue {
}

/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}

inspect(): string {
return `new Long("${this.toString()}"${this.unsigned ? ', true' : ''})`;
[Symbol.for('nodejs.util.inspect.custom')](
depth?: number,
options?: unknown,
inspect?: InspectParameterFn
): string {
return this.inspect(depth, options, inspect);
}

inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
inspect ??= getBasicInspectParameterFn();
const longVal = inspect(this.toString(), options);
const unsignedVal = this.unsigned ? `, ${inspect(this.unsigned, options)}` : '';
return `new Long(${longVal}${unsignedVal})`;
}
}
18 changes: 14 additions & 4 deletions src/objectid.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BSONValue } from './bson_value';
import { BSONError } from './error';
import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils';
import { BSONDataView, ByteUtils } from './utils/byte_utils';

// Regular expression that checks for hex value
Expand Down Expand Up @@ -296,11 +297,20 @@ export class ObjectId extends BSONValue {
* @returns return the 24 character hex string representation.
* @internal
*/
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
[Symbol.for('nodejs.util.inspect.custom')](
depth?: number,
options?: unknown,
inspect?: InspectParameterFn
): string {
return this.inspect(depth, options, inspect);
}

inspect(): string {
return `new ObjectId("${this.toHexString()}")`;
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
const addQuotes = inspect ? false : true;
inspect ??= getBasicInspectParameterFn();
if (addQuotes) {
return `new ObjectId('${inspect(this.toHexString(), options)}')`;
}
return `new ObjectId(${inspect(this.toHexString(), options)})`;
}
}
21 changes: 21 additions & 0 deletions src/parser/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,24 @@ export function isMap(d: unknown): d is Map<unknown, unknown> {
export function isDate(d: unknown): d is Date {
return Object.prototype.toString.call(d) === '[object Date]';
}

/** @internal */
export type StylizeFunction = (x: unknown, style: string) => string;
addaleax marked this conversation as resolved.
Show resolved Hide resolved
export type InspectParameterFn = (x: unknown, options: unknown) => string;
export function getBasicInspectParameterFn(): InspectParameterFn {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
return v => `${v}`;
}
aditi-khare-mongoDB marked this conversation as resolved.
Show resolved Hide resolved
export function getStylizeFunction(options?: unknown): StylizeFunction {
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
const stylizeExists =
options != null &&
typeof options === 'object' &&
'stylize' in options &&
typeof options.stylize === 'function';

if (stylizeExists) {
return options.stylize as (x: unknown, style: string) => string;
addaleax marked this conversation as resolved.
Show resolved Hide resolved
} else {
return getBasicInspectParameterFn();
}
}
12 changes: 8 additions & 4 deletions src/regexp.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BSONValue } from './bson_value';
import { BSONError } from './error';
import type { EJSONOptions } from './extended_json';
import { getStylizeFunction } from './parser/utils';

function alphabetize(str: string): string {
return str.split('').sort().join('');
Expand Down Expand Up @@ -104,11 +105,14 @@ export class BSONRegExp extends BSONValue {
}

/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
[Symbol.for('nodejs.util.inspect.custom')](depth?: number, options?: unknown): string {
return this.inspect(depth, options);
}

inspect(): string {
return `new BSONRegExp(${JSON.stringify(this.pattern)}, ${JSON.stringify(this.options)})`;
inspect(depth?: number, options?: unknown): string {
const stylize = getStylizeFunction(options);
const pattern = stylize(`'${this.pattern}'`, 'regexp');
const flags = stylize(`'${this.options}'`, 'regexp');
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
return `new BSONRegExp(${pattern}, ${flags})`;
}
}
Loading