Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class Binary extends BSONValue {
throw new BSONError('Binary can only be constructed from Uint8Array or number[]');
}

this.sub_type = subType ?? Binary.BSON_BINARY_SUBTYPE_DEFAULT;
this.sub_type = (subType ?? Binary.BSON_BINARY_SUBTYPE_DEFAULT) & 0xff;

if (buffer == null) {
// create an empty binary buffer
Expand Down Expand Up @@ -279,7 +279,7 @@ export class Binary extends BSONValue {
}

throw new BSONError(
`Binary sub_type "${this.sub_type}" is not supported for converting to UUID. Only "${Binary.SUBTYPE_UUID}" is currently supported.`
`Binary sub_type "${this.sub_type}" (${typeof this.sub_type}) is not supported for converting to UUID. Only 0x${Binary.SUBTYPE_UUID.toString(16).padStart(2, '0')} is currently supported.`
);
}

Expand Down
22 changes: 22 additions & 0 deletions test/node/binary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ describe('class Binary', () => {
emptyZeroedArray.fill(0x00);
expect(binary.buffer).to.deep.equal(emptyZeroedArray);
});

const cases: { test: unknown; expected: number }[] = [
{ test: '4', expected: 4 },
{ test: { [Symbol.toPrimitive]: () => 4 }, expected: 4 },
{ test: { [Symbol.toPrimitive]: () => '4' }, expected: 4 },
{ test: { toString: () => '4' }, expected: 4 },
{ test: undefined, expected: Binary.SUBTYPE_DEFAULT },
{ test: null, expected: Binary.SUBTYPE_DEFAULT },
{ test: Infinity, expected: Binary.SUBTYPE_DEFAULT },
{ test: -Infinity, expected: Binary.SUBTYPE_DEFAULT },
{ test: -0, expected: Binary.SUBTYPE_DEFAULT },
{ test: 0.23, expected: Binary.SUBTYPE_DEFAULT },
{ test: NaN, expected: Binary.SUBTYPE_DEFAULT },
{ test: 0xff_ff, expected: 0xff }
];
for (const testCase of cases) {
it(`casts the subType input (${util.inspect(testCase.test, { compact: true, breakLength: 1000 })}) to a byte size number (${testCase.expected})`, () => {
//@ts-expect-error: checking strange inputs
const b = new Binary(new Uint8Array(0), testCase.test);
expect(b.sub_type).to.equal(testCase.expected);
});
}
});

context('createFromHexString()', () => {
Expand Down
15 changes: 15 additions & 0 deletions test/node/uuid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,21 @@ describe('UUID', () => {
expect(() => binV4.toUUID()).to.not.throw();
});

it('should throw message with type information when converted from an incompatible Binary instance', () => {
// @ts-expect-error: putting in a subtype that is not correct on purpose
const stringifiedSubType = Binary.createFromBase64(new UUID().toString('base64'), '4');
// @ts-expect-error: sub_type is a writable property
stringifiedSubType.sub_type = '4';
expect(() => stringifiedSubType.toUUID()).to.throw(/\(string\).+0x04/);

const fourish = { [Symbol.toPrimitive]: () => 4 };
// @ts-expect-error: putting in a subtype that is not correct on purpose
const objectSubtype = Binary.createFromBase64(new UUID().toString('base64'), fourish);
// @ts-expect-error: sub_type is a writable property
objectSubtype.sub_type = fourish;
expect(() => objectSubtype.toUUID()).to.throw(/\(object\).+0x04/);
});

it('should correctly allow for node.js inspect to work with UUID', () => {
const uuid = new UUID(UPPERCASE_DASH_SEPARATED_UUID_STRING);
expect(inspect(uuid)).to.equal(`new UUID('${LOWERCASE_DASH_SEPARATED_UUID_STRING}')`);
Expand Down
Loading