1
1
import { bson as BSON } from './bson-export' ;
2
+ import type { InspectOptionsStylized , CustomInspectFunction } from 'util' ;
3
+ import { inspect as utilInspect } from 'util' ;
2
4
const inspectCustom = Symbol . for ( 'nodejs.util.inspect.custom' ) ;
3
5
type BSONClassKey = ( typeof BSON ) [ Exclude <
4
6
keyof typeof BSON ,
@@ -7,26 +9,73 @@ type BSONClassKey = (typeof BSON)[Exclude<
7
9
8
10
// Turn e.g. 'new Double(...)' into 'Double(...)' but preserve possible leading whitespace
9
11
function 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 * / , '' ) ;
11
19
}
12
20
13
21
// Create a Node.js-util-inspect() style custom inspect function that
14
22
// strips 'new ' from inspect results but otherwise uses the Node.js
15
23
// 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 {
17
27
const originalInspect = BSON [ className ] . prototype . inspect ;
18
28
return function (
19
29
this : ( typeof BSON ) [ typeof className ] [ 'prototype' ] ,
20
- ...args : any
30
+ ...args : Parameters < typeof originalInspect >
21
31
) {
22
- return removeNewFromInspectResult ( originalInspect . apply ( this , args as any ) ) ;
23
- } ;
32
+ return removeNewFromInspectResult ( originalInspect . apply ( this , args ) ) ;
33
+ } satisfies CustomInspectFunction ;
24
34
}
25
35
26
36
const 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
+
27
76
export const bsonStringifiers : Record <
28
77
BSONClassKey | 'ObjectID' ,
29
- ( this : any , depth : any , options : any ) => string
78
+ CustomInspectFunction
30
79
> = {
31
80
ObjectId : makeClasslessInspect ( 'ObjectId' ) ,
32
81
ObjectID : makeClasslessInspect ( 'ObjectId' ) ,
@@ -43,10 +92,13 @@ export const bsonStringifiers: Record<
43
92
BSONRegExp : makeClasslessInspect ( 'BSONRegExp' ) ,
44
93
Binary : function (
45
94
this : typeof BSON . Binary . prototype ,
46
- ...args : any [ ]
95
+ ...args : Parameters < CustomInspectFunction >
47
96
) : string {
48
97
const hexString = this . toString ( 'hex' ) ;
98
+
49
99
switch ( this . sub_type ) {
100
+ case BSON . Binary . SUBTYPE_VECTOR :
101
+ return binaryVectorInspect . apply ( this , args ) ;
50
102
case BSON . Binary . SUBTYPE_MD5 :
51
103
return `MD5('${ hexString } ')` ;
52
104
case BSON . Binary . SUBTYPE_UUID :
@@ -64,7 +116,7 @@ export const bsonStringifiers: Record<
64
116
default :
65
117
return binaryInspect . apply ( this , args ) ;
66
118
}
67
- } ,
119
+ } satisfies CustomInspectFunction ,
68
120
} ;
69
121
70
122
/**
0 commit comments