11import { bson as BSON } from './bson-export' ;
2+ import type { InspectOptionsStylized , CustomInspectFunction } from 'util' ;
23import { inspect as utilInspect } from 'util' ;
34const inspectCustom = Symbol . for ( 'nodejs.util.inspect.custom' ) ;
45type BSONClassKey = ( typeof BSON ) [ Exclude <
@@ -8,26 +9,73 @@ type BSONClassKey = (typeof BSON)[Exclude<
89
910// Turn e.g. 'new Double(...)' into 'Double(...)' but preserve possible leading whitespace
1011function removeNewFromInspectResult ( str : string ) : string {
11- 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 * / , '' ) ;
1219}
1320
1421// Create a Node.js-util-inspect() style custom inspect function that
1522// strips 'new ' from inspect results but otherwise uses the Node.js
1623// driver's bson library's inspect functions.
17- function makeClasslessInspect < K extends BSONClassKey > ( className : K ) {
24+ function makeClasslessInspect < K extends BSONClassKey > (
25+ className : K
26+ ) : CustomInspectFunction {
1827 const originalInspect = BSON [ className ] . prototype . inspect ;
1928 return function (
20- this : ( typeof BSON ) [ typeof className ] [ 'prototype' ] ,
21- ...args : any
29+ this : typeof originalInspect ,
30+ ...args : Parameters < typeof originalInspect >
2231 ) {
2332 return removeNewFromInspectResult ( originalInspect . apply ( this , args ) ) ;
24- } ;
33+ } as CustomInspectFunction ;
2534}
2635
2736const 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.fromPackedBitArray(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+
2876export const bsonStringifiers : Record <
2977 BSONClassKey | 'ObjectID' ,
30- ( this : any , depth : any , options : any ) => string
78+ CustomInspectFunction
3179> = {
3280 ObjectId : makeClasslessInspect ( 'ObjectId' ) ,
3381 ObjectID : makeClasslessInspect ( 'ObjectId' ) ,
@@ -44,38 +92,13 @@ export const bsonStringifiers: Record<
4492 BSONRegExp : makeClasslessInspect ( 'BSONRegExp' ) ,
4593 Binary : function (
4694 this : typeof BSON . Binary . prototype ,
47- // eslint-disable-next-line @typescript-eslint/no-explicit-any
48- ...args : any [ ]
95+ ...args : Parameters < CustomInspectFunction >
4996 ) : string {
5097 const hexString = this . toString ( 'hex' ) ;
98+
5199 switch ( this . sub_type ) {
52100 case BSON . Binary . SUBTYPE_VECTOR :
53- switch ( this . buffer [ 0 ] ) {
54- case BSON . Binary . VECTOR_TYPE . Int8 :
55- return `Int8(${ utilInspect (
56- Array . from ( this . toInt8Array ( ) ) ,
57- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
58- ...args
59- ) } )`;
60- case BSON . Binary . VECTOR_TYPE . Float32 :
61- return `Float32(${ utilInspect (
62- Array . from ( this . toFloat32Array ( ) ) ,
63- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
64- ...args
65- ) } )`;
66- case BSON . Binary . VECTOR_TYPE . PackedBit :
67- return `PackedBit(${ utilInspect (
68- hexString ,
69- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
70- ...args
71- ) } )`;
72- default :
73- return `Vector(${ utilInspect (
74- hexString ,
75- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
76- ...args
77- ) } )`;
78- }
101+ return binaryVectorInspect . apply ( this , args ) ;
79102 case BSON . Binary . SUBTYPE_MD5 :
80103 return `MD5('${ hexString } ')` ;
81104 case BSON . Binary . SUBTYPE_UUID :
@@ -93,7 +116,7 @@ export const bsonStringifiers: Record<
93116 default :
94117 return binaryInspect . apply ( this , args ) ;
95118 }
96- } ,
119+ } satisfies CustomInspectFunction ,
97120} ;
98121
99122/**
0 commit comments