From 11b554c103f4a173677748585b88e02d6cd5de4c Mon Sep 17 00:00:00 2001 From: Erfanium Date: Fri, 11 Nov 2022 19:50:21 +0330 Subject: [PATCH] feat: add custom inspect support for other js runtimes --- src/binary.ts | 5 +++-- src/code.ts | 3 ++- src/db_ref.ts | 3 ++- src/decimal128.ts | 3 ++- src/double.ts | 3 ++- src/int_32.ts | 3 ++- src/long.ts | 3 ++- src/max_key.ts | 4 +++- src/min_key.ts | 4 +++- src/objectid.ts | 3 ++- src/symbol.ts | 4 +++- src/timestamp.ts | 3 ++- src/utils/custom_inspect.ts | 12 ++++++++++++ 13 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 src/utils/custom_inspect.ts diff --git a/src/binary.ts b/src/binary.ts index eef19230..8e5fa039 100644 --- a/src/binary.ts +++ b/src/binary.ts @@ -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[]; @@ -285,7 +286,7 @@ export class Binary { } /** @internal */ - [Symbol.for('nodejs.util.inspect.custom')](): string { + [kInspect](): string { return this.inspect(); } @@ -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(); } diff --git a/src/code.ts b/src/code.ts index 86a4fb19..29867c8c 100644 --- a/src/code.ts +++ b/src/code.ts @@ -1,4 +1,5 @@ import type { Document } from './bson'; +import { kInspect } from './utils/custom_inspect'; /** @public */ export interface CodeExtended { @@ -46,7 +47,7 @@ export class Code { } /** @internal */ - [Symbol.for('nodejs.util.inspect.custom')](): string { + [kInspect](): string { return this.inspect(); } diff --git a/src/db_ref.ts b/src/db_ref.ts index 750c5be9..dd8addaa 100644 --- a/src/db_ref.ts +++ b/src/db_ref.ts @@ -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 { @@ -107,7 +108,7 @@ export class DBRef { } /** @internal */ - [Symbol.for('nodejs.util.inspect.custom')](): string { + [kInspect](): string { return this.inspect(); } diff --git a/src/decimal128.ts b/src/decimal128.ts index 86b664ef..32c13095 100644 --- a/src/decimal128.ts +++ b/src/decimal128.ts @@ -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; @@ -765,7 +766,7 @@ export class Decimal128 { } /** @internal */ - [Symbol.for('nodejs.util.inspect.custom')](): string { + [kInspect](): string { return this.inspect(); } diff --git a/src/double.ts b/src/double.ts index 51bbf22d..4e4f9c15 100644 --- a/src/double.ts +++ b/src/double.ts @@ -1,4 +1,5 @@ import type { EJSONOptions } from './extended_json'; +import { kInspect } from './utils/custom_inspect'; /** @public */ export interface DoubleExtended { @@ -78,7 +79,7 @@ export class Double { } /** @internal */ - [Symbol.for('nodejs.util.inspect.custom')](): string { + [kInspect](): string { return this.inspect(); } diff --git a/src/int_32.ts b/src/int_32.ts index b3b5760c..77efb57b 100644 --- a/src/int_32.ts +++ b/src/int_32.ts @@ -1,4 +1,5 @@ import type { EJSONOptions } from './extended_json'; +import { kInspect } from './utils/custom_inspect'; /** @public */ export interface Int32Extended { @@ -58,7 +59,7 @@ export class Int32 { } /** @internal */ - [Symbol.for('nodejs.util.inspect.custom')](): string { + [kInspect](): string { return this.inspect(); } diff --git a/src/long.ts b/src/long.ts index ed3f6e1b..655c8ed3 100644 --- a/src/long.ts +++ b/src/long.ts @@ -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 */ @@ -1027,7 +1028,7 @@ export class Long { } /** @internal */ - [Symbol.for('nodejs.util.inspect.custom')](): string { + [kInspect](): string { return this.inspect(); } diff --git a/src/max_key.ts b/src/max_key.ts index 0ff3d363..53a319cc 100644 --- a/src/max_key.ts +++ b/src/max_key.ts @@ -1,3 +1,5 @@ +import { kInspect } from './utils/custom_inspect'; + /** @public */ export interface MaxKeyExtended { $maxKey: 1; @@ -26,7 +28,7 @@ export class MaxKey { } /** @internal */ - [Symbol.for('nodejs.util.inspect.custom')](): string { + [kInspect](): string { return this.inspect(); } diff --git a/src/min_key.ts b/src/min_key.ts index f872b1eb..064e59c3 100644 --- a/src/min_key.ts +++ b/src/min_key.ts @@ -1,3 +1,5 @@ +import { kInspect } from './utils/custom_inspect'; + /** @public */ export interface MinKeyExtended { $minKey: 1; @@ -26,7 +28,7 @@ export class MinKey { } /** @internal */ - [Symbol.for('nodejs.util.inspect.custom')](): string { + [kInspect](): string { return this.inspect(); } diff --git a/src/objectid.ts b/src/objectid.ts index d0010d27..94029ff5 100644 --- a/src/objectid.ts +++ b/src/objectid.ts @@ -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}$'); @@ -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(); } diff --git a/src/symbol.ts b/src/symbol.ts index 1e82fc1c..2144147d 100644 --- a/src/symbol.ts +++ b/src/symbol.ts @@ -1,3 +1,5 @@ +import { kInspect } from './utils/custom_inspect'; + /** @public */ export interface BSONSymbolExtended { $symbol: string; @@ -50,7 +52,7 @@ export class BSONSymbol { } /** @internal */ - [Symbol.for('nodejs.util.inspect.custom')](): string { + [kInspect](): string { return this.inspect(); } } diff --git a/src/timestamp.ts b/src/timestamp.ts index 4c4b7e74..7e6834df 100644 --- a/src/timestamp.ts +++ b/src/timestamp.ts @@ -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'; @@ -109,7 +110,7 @@ export class Timestamp extends LongWithoutOverridesClass { } /** @internal */ - [Symbol.for('nodejs.util.inspect.custom')](): string { + [kInspect](): string { return this.inspect(); } diff --git a/src/utils/custom_inspect.ts b/src/utils/custom_inspect.ts new file mode 100644 index 00000000..0b6839b2 --- /dev/null +++ b/src/utils/custom_inspect.ts @@ -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();