Skip to content

Commit 3b8e801

Browse files
committed
chore: add toBits validation
1 parent 266f9d0 commit 3b8e801

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/binary.ts

+2
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,8 @@ export class Binary extends BSONValue {
415415
throw new BSONError('Binary datatype field is not packed bit');
416416
}
417417

418+
validateBinaryVector(this);
419+
418420
const byteCount = this.length() - 2;
419421
const bitCount = byteCount * 8 - this.buffer[1];
420422
const bits = new Int8Array(bitCount);

test/node/bson_binary_vector.spec.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,22 @@ function testVectorInvalidBSONBytes(test: VectorTest, expectedErrorMessage: stri
185185
expect(thrownError?.message).to.match(new RegExp(expectedErrorMessage));
186186
});
187187

188+
if (toHelper === 'toPackedBits') {
189+
it(`Binary.toBits() throw a BSONError`, function () {
190+
let thrownError: Error | undefined;
191+
const bin = BSON.deserialize(Buffer.from(test.canonical_bson!, 'hex'));
192+
193+
try {
194+
bin.vector.toBits();
195+
} catch (error) {
196+
thrownError = error;
197+
}
198+
199+
expect(thrownError, thrownError?.stack).to.be.instanceOf(BSONError);
200+
expect(thrownError?.message).to.match(new RegExp(expectedErrorMessage));
201+
});
202+
}
203+
188204
it(`EJSON.stringify() throw a BSONError`, function () {
189205
let thrownError: Error | undefined;
190206
const bin = BSON.deserialize(Buffer.from(test.canonical_bson!, 'hex'));

0 commit comments

Comments
 (0)