11import { bson as BSON } from './bson-export' ;
2+ import type { InspectOptionsStylized , CustomInspectFunction } from 'util' ;
3+ import { inspect as utilInspect } from 'util' ;
24const inspectCustom = Symbol . for ( 'nodejs.util.inspect.custom' ) ;
35type BSONClassKey = ( typeof BSON ) [ Exclude <
46 keyof typeof BSON ,
@@ -7,26 +9,73 @@ type BSONClassKey = (typeof BSON)[Exclude<
79
810// Turn e.g. 'new Double(...)' into 'Double(...)' but preserve possible leading whitespace
911function removeNewFromInspectResult ( str : string ) : string {
10- return String ( str ) . replace ( / ^ ( \s * ) ( n e w ) / , '$1' ) ;
12+ return str . replace ( / ^ ( \s * ) ( n e w ) / , '$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 * / , '' ) ;
1119}
1220
1321// Create a Node.js-util-inspect() style custom inspect function that
1422// strips 'new ' from inspect results but otherwise uses the Node.js
1523// driver's bson library's inspect functions.
16- function makeClasslessInspect < K extends BSONClassKey > ( className : K ) {
24+ function makeClasslessInspect < K extends BSONClassKey > (
25+ className : K
26+ ) : CustomInspectFunction {
1727 const originalInspect = BSON [ className ] . prototype . inspect ;
1828 return function (
1929 this : ( typeof BSON ) [ typeof className ] [ 'prototype' ] ,
20- ...args : any
30+ ...args : Parameters < typeof originalInspect >
2131 ) {
22- return removeNewFromInspectResult ( originalInspect . apply ( this , args as any ) ) ;
23- } ;
32+ return removeNewFromInspectResult ( originalInspect . apply ( this , args ) ) ;
33+ } satisfies CustomInspectFunction ;
2434}
2535
2636const 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.fromPackedBits(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+
2776export const bsonStringifiers : Record <
2877 BSONClassKey | 'ObjectID' ,
29- ( this : any , depth : any , options : any ) => string
78+ CustomInspectFunction
3079> = {
3180 ObjectId : makeClasslessInspect ( 'ObjectId' ) ,
3281 ObjectID : makeClasslessInspect ( 'ObjectId' ) ,
@@ -43,10 +92,13 @@ export const bsonStringifiers: Record<
4392 BSONRegExp : makeClasslessInspect ( 'BSONRegExp' ) ,
4493 Binary : function (
4594 this : typeof BSON . Binary . prototype ,
46- ...args : any [ ]
95+ ...args : Parameters < CustomInspectFunction >
4796 ) : string {
4897 const hexString = this . toString ( 'hex' ) ;
98+
4999 switch ( this . sub_type ) {
100+ case BSON . Binary . SUBTYPE_VECTOR :
101+ return binaryVectorInspect . apply ( this , args ) ;
50102 case BSON . Binary . SUBTYPE_MD5 :
51103 return `MD5('${ hexString } ')` ;
52104 case BSON . Binary . SUBTYPE_UUID :
@@ -64,7 +116,7 @@ export const bsonStringifiers: Record<
64116 default :
65117 return binaryInspect . apply ( this , args ) ;
66118 }
67- } ,
119+ } satisfies CustomInspectFunction ,
68120} ;
69121
70122/**
0 commit comments