Skip to content

feat(NODE-4819): add custom inspect support for other js runtimes #519

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions src/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { EJSONOptions } from './extended_json';
import { BSONError, BSONTypeError } from './error';
import { BSON_BINARY_SUBTYPE_UUID_NEW } from './constants';
import { ByteUtils } from './utils/byte_utils';
import { kInspect } from './utils/custom_inspect';

/** @public */
export type BinarySequence = Uint8Array | number[];
Expand Down Expand Up @@ -285,7 +286,7 @@ export class Binary {
}

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

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

Expand Down
3 changes: 2 additions & 1 deletion src/code.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Document } from './bson';
import { kInspect } from './utils/custom_inspect';

/** @public */
export interface CodeExtended {
Expand Down Expand Up @@ -46,7 +47,7 @@ export class Code {
}

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

Expand Down
3 changes: 2 additions & 1 deletion 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 type { EJSONOptions } from './extended_json';
import type { ObjectId } from './objectid';
import { isObjectLike } from './parser/utils';
import { kInspect } from './utils/custom_inspect';

/** @public */
export interface DBRefLike {
Expand Down Expand Up @@ -107,7 +108,7 @@ export class DBRef {
}

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

Expand Down
3 changes: 2 additions & 1 deletion src/decimal128.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BSONTypeError } from './error';
import { Long } from './long';
import { isUint8Array } from './parser/utils';
import { ByteUtils } from './utils/byte_utils';
import { kInspect } from './utils/custom_inspect';

const PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/;
const PARSE_INF_REGEXP = /^(\+|-)?(Infinity|inf)$/i;
Expand Down Expand Up @@ -765,7 +766,7 @@ export class Decimal128 {
}

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

Expand Down
3 changes: 2 additions & 1 deletion src/double.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { EJSONOptions } from './extended_json';
import { kInspect } from './utils/custom_inspect';

/** @public */
export interface DoubleExtended {
Expand Down Expand Up @@ -78,7 +79,7 @@ export class Double {
}

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

Expand Down
3 changes: 2 additions & 1 deletion src/int_32.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { EJSONOptions } from './extended_json';
import { kInspect } from './utils/custom_inspect';

/** @public */
export interface Int32Extended {
Expand Down Expand Up @@ -58,7 +59,7 @@ export class Int32 {
}

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

Expand Down
3 changes: 2 additions & 1 deletion src/long.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { EJSONOptions } from './extended_json';
import { isObjectLike } from './parser/utils';
import type { Timestamp } from './timestamp';
import { kInspect } from './utils/custom_inspect';

interface LongWASMHelpers {
/** Gets the high bits of the last operation performed */
Expand Down Expand Up @@ -1027,7 +1028,7 @@ export class Long {
}

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

Expand Down
4 changes: 3 additions & 1 deletion src/max_key.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { kInspect } from './utils/custom_inspect';

/** @public */
export interface MaxKeyExtended {
$maxKey: 1;
Expand Down Expand Up @@ -26,7 +28,7 @@ export class MaxKey {
}

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

Expand Down
4 changes: 3 additions & 1 deletion src/min_key.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { kInspect } from './utils/custom_inspect';

/** @public */
export interface MinKeyExtended {
$minKey: 1;
Expand Down Expand Up @@ -26,7 +28,7 @@ export class MinKey {
}

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

Expand Down
3 changes: 2 additions & 1 deletion src/objectid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BSONTypeError } from './error';
import { deprecate, isUint8Array, randomBytes } from './parser/utils';
import { BSONDataView, ByteUtils } from './utils/byte_utils';
import { kInspect } from './utils/custom_inspect';

// Regular expression that checks for hex value
const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
Expand Down Expand Up @@ -322,7 +323,7 @@ export class ObjectId {
* @returns return the 24 character hex string representation.
* @internal
*/
[Symbol.for('nodejs.util.inspect.custom')](): string {
[kInspect](): string {
return this.inspect();
}

Expand Down
4 changes: 3 additions & 1 deletion src/symbol.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { kInspect } from './utils/custom_inspect';

/** @public */
export interface BSONSymbolExtended {
$symbol: string;
Expand Down Expand Up @@ -50,7 +52,7 @@ export class BSONSymbol {
}

/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
[kInspect](): string {
return this.inspect();
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/timestamp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Long } from './long';
import { isObjectLike } from './parser/utils';
import { kInspect } from './utils/custom_inspect';

/** @public */
export type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect';
Expand Down Expand Up @@ -109,7 +110,7 @@ export class Timestamp extends LongWithoutOverridesClass {
}

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

Expand Down
12 changes: 12 additions & 0 deletions src/utils/custom_inspect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function getSymbol() {
if ('Deno' in globalThis) {
// Deno
return Symbol.for('Deno.customInspect');
}

// Node.js as Default
return Symbol.for('nodejs.util.inspect.custom');
}

/** @internal */
export const kInspect = getSymbol();