Skip to content

Commit dd40564

Browse files
committed
feat(service-provider-core): add BSON vector type stringifiers
1 parent c6279e6 commit dd40564

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

packages/service-provider-core/src/printable-bson.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { bson as BSON } from './bson-export';
2+
import { inspect as utilInspect } from 'util';
23
const inspectCustom = Symbol.for('nodejs.util.inspect.custom');
34
type BSONClassKey = (typeof BSON)[Exclude<
45
keyof typeof BSON,
@@ -19,7 +20,7 @@ function makeClasslessInspect<K extends BSONClassKey>(className: K) {
1920
this: (typeof BSON)[typeof className]['prototype'],
2021
...args: any
2122
) {
22-
return removeNewFromInspectResult(originalInspect.apply(this, args as any));
23+
return removeNewFromInspectResult(originalInspect.apply(this, args));
2324
};
2425
}
2526

@@ -43,10 +44,38 @@ export const bsonStringifiers: Record<
4344
BSONRegExp: makeClasslessInspect('BSONRegExp'),
4445
Binary: function (
4546
this: typeof BSON.Binary.prototype,
47+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4648
...args: any[]
4749
): string {
4850
const hexString = this.toString('hex');
4951
switch (this.sub_type) {
52+
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+
}
5079
case BSON.Binary.SUBTYPE_MD5:
5180
return `MD5('${hexString}')`;
5281
case BSON.Binary.SUBTYPE_UUID:

0 commit comments

Comments
 (0)