Skip to content

Conversation

@lerouxb
Copy link
Contributor

@lerouxb lerouxb commented Nov 20, 2025

MONGOSH-1285

I built this on top of @addaleax's wip branch, so the types are still mostly from there.


The short version of what we're trying to do: For BSON that we get from the database we want to print all of it, untruncated, when it gets evaluated or otherwise inspected with util.inspect().


This solution wraps the ServiceProvider with another class that implements all the same methods. Then:

  • all methods that return cursors have the returned cursors intercepted and the relevant methods on there that return documents replaced with new methods that recursively install our inspect function.
  • all methods that return promises of bson have their results intercepted and we recursively install our inspect function on the result.
  • all other methods are just forwarded with their results unchanged.

I have been testing it with this document which should be affected by the inspectOptions depth, maxArrayLength and maxStringLength:

db.test.insertOne({
  array: Array.from(Array(1000), (_,i) => i),
  string: 'All work and no play makes Jack a dull boy. '.repeat(250),
  object: {
    foo: {
      bar: {
        baz: {
          qux: {
            quux: {
              corge: {
                grault: 'If you can read this, you are too close.'
              }
            }
          }
        }
      }
    }
  }
});

And this doc has every BSON type which is useful for testing that we're not messing up existing BSON formatting:

db.test.insertOne({
    double: Double(1.2),
    doubleThatIsAlsoAnInteger: Double(1),
    string: 'Hello, world!',
    binData: Binary(Buffer.from([1, 2, 3])),
    boolean: true,
    date: Date('2023-04-05T13:25:08.445Z'),
    null: null,
    regex: BSONRegExp('pattern', 'i'),
    javascript: Code('function() {}'),
    symbol: BSONSymbol('symbol'),
    javascriptWithScope: Code('function() {}', { foo: 1, bar: 'a' }),
    int: Int32(12345),
    timestamp: Timestamp(Long('7218556297505931265')),
    long: Long('123456789123456789'),
    decimal: Decimal128(
      Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
    ),
    minKey: MinKey(),
    maxKey: MaxKey(),

    binaries: {
      generic: Binary(Buffer.from([1, 2, 3]), 0),
      functionData: Binary(Buffer.from('//8='), 1),
      binaryOld: Binary(Buffer.from('//8='), 2),
      uuidOld: Binary(Buffer.from('c//SZESzTGmQ6OfR38A11A=='), 3),
      uuid: UUID('AAAAAAAA-AAAA-4AAA-AAAA-AAAAAAAAAAAA'),
      md5: Binary(Buffer.from('c//SZESzTGmQ6OfR38A11A=='), 5),
      encrypted: Binary(Buffer.from('c//SZESzTGmQ6OfR38A11A=='), 6),
      compressedTimeSeries: Binary(
        Buffer.from(
          'CQCKW/8XjAEAAIfx//////////H/////////AQAAAAAAAABfAAAAAAAAAAEAAAAAAAAAAgAAAAAAAAAHAAAAAAAAAA4AAAAAAAAAAA==',
          'base64'
        ),
      ),
      custom: Binary(Buffer.from('//8='), 128),
    },

    dbRef: DBRef('namespace', ObjectId('642d76b4b7ebfab15d3c4a78')),
  });

You can test that the find cursor's tryNext was replaced by running:

> db.test.find()

(Notice that the whole array, string and object all printed.)

This will exercise the inspect function on the top-level array that was returned.

You can test that it recursively installed it on the documents inside that array by running:

> db.test.find().toArray()[0]

or even

> config.set('inspectDepth', 2);
> f = { a: { b: { c: { d: 1 } } } }
{ a: { b: { c: [Object] } } } # notice that it truncated
> db.test.find().toArray()[0].object # this will print the whole object, not just 2 levels

This needs a lot of tests. There are almost certainly some cases left that I've missed. And I'm unsure about some details. Just opening to have a discussion. Oh and we might want a way for users to opt out of it.

// doesn't have readBufferedDocuments or toArray. We can try cast things to
// ServiceProviderAbstractCursor, but then that's not assignable to
// ServiceProviderAnyCursor. And that's why there's so much casting below.
const cursor = (this._sp[key] as any)(...args) as any;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just had the thought that one obvious way around this is to just have cursorMethod() for regular cursors and changeCursorMethod() for change stream cursors. And just replace ReturnType<ServiceProvider[K]> with something a lot more specific. Then we can probably have much better type safety.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, although I think either way is fine here

const customInspectSymbol = Symbol.for('nodejs.util.inspect.custom');

function cursorNext(
original: () => Promise<Document | null>
Copy link
Contributor Author

@lerouxb lerouxb Nov 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can probably use some typescript to automatically pick this type off abstract cursor. Something like that. Same for the other cursor methods below.

return async function (): Promise<Document | null> {
const result = await original();
if (result) {
replaceWithCustomInspect(result);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just suddenly had the realisation that this is a terrible function name. addCustomInspect() rather?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's that bad, but yes, addCustomInspect() sounds good 🙂

this: DeepInspectServiceProviderWrapper,
...args: Parameters<Required<ServiceProvider>[K]>
): // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore The returntype already contains a promise
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A way around all this is to just make one function similar to bsonMethod() for each unique return type, kinda like what I did for the cursor methods.

this: DeepInspectServiceProviderWrapper,
...args: Parameters<Required<ServiceProvider>[K]>
): ReturnType<Required<ServiceProvider>[K]> {
// not wrapping the result at all because forwardedMethod() is for simple
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the decision here to leave everything that's not definitely a bson document or array alone. Make sense?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! 🙂

maxStringLength: Infinity,
};

// reuse the standard inpect logic for an object without causing infinite
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels very hacky, but I haven't thought of a better way to do it yet. I'm trying to avoid having to write my own function that will format an array or object exactly the way util.inspect() already does.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We know it's BSON, so it should be fine to use a shallow copy of the document/array, right?

for (const item of obj) {
replaceWithCustomInspect(item);
}
} else if (obj && typeof obj === 'object' && obj !== null) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, again, I made the decision to only touch arrays and documents/sub-documents and leave simple values alone. BUT this currently also installs our inspect function on BSON types like long, decimal, etc. Which we probably don't want?

What's the best way to do that? Look for obj._bsontype?

Copy link
Contributor Author

@lerouxb lerouxb Nov 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok for some reason this code doesn't mess up BSON object formatting 🤔 I suppose the prototype still has the BSONValue one? https://github.com/mongodb/js-bson/blob/main/src/bson_value.ts#L38

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the best way to do that? Look for obj._bsontype?

We're pretty close to adopting BSON 7.x, where we have obj[bsonType] as a more reliable symbol property (but yeah, that's the best way to check for existing BSON objects)

undefined,
undefined,
initialServiceProvider
this.initialServiceProvider
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw it is VERY easy to accidentally pass initialServiceProvider (ie. the unwrapped value) to something in place of this.initialServiceProvider. Ask me how I know..

return result;
}

function replaceWithCustomInspect(obj: any) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something obvious to note here: I'm mutating the obj in place rather than creating new objects. I did it that way for simplicity and because I'm worried about the impact on performance making a gazillion new objects, but admittedly I haven't profiled that yet. Let me know if you think there's a better way.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this should be fine 👍

// doesn't have readBufferedDocuments or toArray. We can try cast things to
// ServiceProviderAbstractCursor, but then that's not assignable to
// ServiceProviderAnyCursor. And that's why there's so much casting below.
const cursor = (this._sp[key] as any)(...args) as any;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, although I think either way is fine here

// ServiceProviderAnyCursor. And that's why there's so much casting below.
const cursor = (this._sp[key] as any)(...args) as any;

cursor.next = cursorNext(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd consider returning a wrapped cursor instead of modifying the existing one, if that's possible – like the wrapping for the ServiceProvider itself, this would make sure that we actually wrap all methods that we should and aren't forgetting about any


return results;
};
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ All of these methods are essentially the same, no?

return async function (): Promise<Document | null> {
const result = await original();
if (result) {
replaceWithCustomInspect(result);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's that bad, but yes, addCustomInspect() sounds good 🙂

this: DeepInspectServiceProviderWrapper,
...args: Parameters<Required<ServiceProvider>[K]>
): ReturnType<Required<ServiceProvider>[K]> {
// not wrapping the result at all because forwardedMethod() is for simple
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! 🙂

maxStringLength: Infinity,
};

// reuse the standard inpect logic for an object without causing infinite
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We know it's BSON, so it should be fine to use a shallow copy of the document/array, right?

return result;
}

function replaceWithCustomInspect(obj: any) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this should be fine 👍


function replaceWithCustomInspect(obj: any) {
if (Array.isArray(obj)) {
(obj as any)[customInspectSymbol] = customDocumentInspect;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(obj as any)[customInspectSymbol] = customDocumentInspect;
(obj as any)[customInspectSymbol] ??= customDocumentInspect;

maybe unnecessarily defensive, but if something already has a custom inspect function for some reason, we can probably leave that in place

for (const item of obj) {
replaceWithCustomInspect(item);
}
} else if (obj && typeof obj === 'object' && obj !== null) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the best way to do that? Look for obj._bsontype?

We're pretty close to adopting BSON 7.x, where we have obj[bsonType] as a more reliable symbol property (but yeah, that's the best way to check for existing BSON objects)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants