Skip to content

Commit

Permalink
chore: rename to getNonnegativeInt32LE
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Apr 1, 2024
1 parent ca2ee96 commit 7dadc2e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/parser/on_demand/parse_to_elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ export type BSONElement = [
length: number
];

const getSize = (source: Uint8Array, offset: number) => {
function getSize(source: Uint8Array, offset: number) {
try {
return NumberUtils.getSize(source, offset);
return NumberUtils.getNonnegativeInt32LE(source, offset);
} catch (cause) {
throw new BSONOffsetError('BSON size cannot be negative', offset, { cause });
}
};
}

/**
* Searches for null terminator of a BSON element's value (Never the document null terminator)
Expand Down
7 changes: 3 additions & 4 deletions src/utils/number_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ const isBigEndian = FLOAT_BYTES[7] === 0;
*/
export type NumberUtils = {
/**
* Parses a int32 little-endian at offset, throws if it is negative.
* - size as in `size_t`
* Parses a signed int32 at offset. Throws a `RangeError` if value is negative.
*/
getSize: (source: Uint8Array, offset: number) => number;
getNonnegativeInt32LE: (source: Uint8Array, offset: number) => number;
getInt32LE: (source: Uint8Array, offset: number) => number;
getUint32LE: (source: Uint8Array, offset: number) => number;
getUint32BE: (source: Uint8Array, offset: number) => number;
Expand All @@ -36,7 +35,7 @@ export type NumberUtils = {
* @public
*/
export const NumberUtils: NumberUtils = {
getSize(source: Uint8Array, offset: number): number {
getNonnegativeInt32LE(source: Uint8Array, offset: number): number {
if (source[offset + 3] > 127) {
throw new RangeError(`Size cannot be negative at offset: ${offset}`);
}
Expand Down
18 changes: 18 additions & 0 deletions test/node/utils/number_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ describe('NumberUtils', () => {
/** Make a Uint8Array in a less verbose way */
const b = (...values) => new Uint8Array(values);

context('getNonnegativeInt32LE()', () => {
it('parses an int32 little endian', () => {
expect(NumberUtils.getNonnegativeInt32LE(b(0, 0, 0, 1), 0)).to.equal(1 << 24);
});

it('throws if int32 is negative', () => {
expect(() => NumberUtils.getNonnegativeInt32LE(b(0, 0, 0, 128), 0)).to.throw(RangeError);
});

it('parses an int32 little endian at offset', () => {
expect(NumberUtils.getNonnegativeInt32LE(b(0, 0, 0, 0, 0, 1), 2)).to.equal(1 << 24);
});

it('does not check bounds of offset', () => {
expect(NumberUtils.getNonnegativeInt32LE(b(0, 0, 0, 1), 4)).to.be.NaN;
});
});

context('getInt32LE()', () => {
it('parses an int32 little endian', () => {
expect(NumberUtils.getInt32LE(b(0, 0, 0, 1), 0)).to.equal(1 << 24);
Expand Down

0 comments on commit 7dadc2e

Please sign in to comment.