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,70 @@ 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+ ...utilInspect . defaultOptions ,
50+ } )
51+ ) } )`;
52+ case BSON . Binary . VECTOR_TYPE . Float32 :
53+ return `Binary.fromFloat32Array(new Float32Array(${ removeTypedArrayPrefixFromInspectResult (
54+ utilInspect ( this . toFloat32Array ( ) , {
55+ depth,
56+ ...options ,
57+ ...utilInspect . defaultOptions ,
58+ } )
59+ ) } )`;
60+ case BSON . Binary . VECTOR_TYPE . PackedBit :
61+ return `Binary.fromPackedBitArray(new Uint8Array(${ removeTypedArrayPrefixFromInspectResult (
62+ utilInspect ( this . toPackedBits ( ) , {
63+ depth,
64+ ...options ,
65+ ...utilInspect . defaultOptions ,
66+ } )
67+ ) } )`;
68+ default :
69+ return binaryInspect . call ( this , depth , options ) ;
70+ }
71+ } satisfies CustomInspectFunction ;
72+
2873export const bsonStringifiers : Record <
2974 BSONClassKey | 'ObjectID' ,
30- ( this : any , depth : any , options : any ) => string
75+ CustomInspectFunction
3176> = {
3277 ObjectId : makeClasslessInspect ( 'ObjectId' ) ,
3378 ObjectID : makeClasslessInspect ( 'ObjectId' ) ,
@@ -44,38 +89,13 @@ export const bsonStringifiers: Record<
4489 BSONRegExp : makeClasslessInspect ( 'BSONRegExp' ) ,
4590 Binary : function (
4691 this : typeof BSON . Binary . prototype ,
47- // eslint-disable-next-line @typescript-eslint/no-explicit-any
48- ...args : any [ ]
92+ ...args : Parameters < CustomInspectFunction >
4993 ) : string {
5094 const hexString = this . toString ( 'hex' ) ;
95+
5196 switch ( this . sub_type ) {
5297 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- }
98+ return binaryVectorInspect . apply ( this , args ) ;
7999 case BSON . Binary . SUBTYPE_MD5 :
80100 return `MD5('${ hexString } ')` ;
81101 case BSON . Binary . SUBTYPE_UUID :
@@ -93,7 +113,7 @@ export const bsonStringifiers: Record<
93113 default :
94114 return binaryInspect . apply ( this , args ) ;
95115 }
96- } ,
116+ } satisfies CustomInspectFunction ,
97117} ;
98118
99119/**
0 commit comments