Skip to content

Commit 7dadc2e

Browse files
committed
chore: rename to getNonnegativeInt32LE
1 parent ca2ee96 commit 7dadc2e

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

src/parser/on_demand/parse_to_elements.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ export type BSONElement = [
4545
length: number
4646
];
4747

48-
const getSize = (source: Uint8Array, offset: number) => {
48+
function getSize(source: Uint8Array, offset: number) {
4949
try {
50-
return NumberUtils.getSize(source, offset);
50+
return NumberUtils.getNonnegativeInt32LE(source, offset);
5151
} catch (cause) {
5252
throw new BSONOffsetError('BSON size cannot be negative', offset, { cause });
5353
}
54-
};
54+
}
5555

5656
/**
5757
* Searches for null terminator of a BSON element's value (Never the document null terminator)

src/utils/number_utils.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ const isBigEndian = FLOAT_BYTES[7] === 0;
1414
*/
1515
export type NumberUtils = {
1616
/**
17-
* Parses a int32 little-endian at offset, throws if it is negative.
18-
* - size as in `size_t`
17+
* Parses a signed int32 at offset. Throws a `RangeError` if value is negative.
1918
*/
20-
getSize: (source: Uint8Array, offset: number) => number;
19+
getNonnegativeInt32LE: (source: Uint8Array, offset: number) => number;
2120
getInt32LE: (source: Uint8Array, offset: number) => number;
2221
getUint32LE: (source: Uint8Array, offset: number) => number;
2322
getUint32BE: (source: Uint8Array, offset: number) => number;
@@ -36,7 +35,7 @@ export type NumberUtils = {
3635
* @public
3736
*/
3837
export const NumberUtils: NumberUtils = {
39-
getSize(source: Uint8Array, offset: number): number {
38+
getNonnegativeInt32LE(source: Uint8Array, offset: number): number {
4039
if (source[offset + 3] > 127) {
4140
throw new RangeError(`Size cannot be negative at offset: ${offset}`);
4241
}

test/node/utils/number_utils.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ describe('NumberUtils', () => {
3030
/** Make a Uint8Array in a less verbose way */
3131
const b = (...values) => new Uint8Array(values);
3232

33+
context('getNonnegativeInt32LE()', () => {
34+
it('parses an int32 little endian', () => {
35+
expect(NumberUtils.getNonnegativeInt32LE(b(0, 0, 0, 1), 0)).to.equal(1 << 24);
36+
});
37+
38+
it('throws if int32 is negative', () => {
39+
expect(() => NumberUtils.getNonnegativeInt32LE(b(0, 0, 0, 128), 0)).to.throw(RangeError);
40+
});
41+
42+
it('parses an int32 little endian at offset', () => {
43+
expect(NumberUtils.getNonnegativeInt32LE(b(0, 0, 0, 0, 0, 1), 2)).to.equal(1 << 24);
44+
});
45+
46+
it('does not check bounds of offset', () => {
47+
expect(NumberUtils.getNonnegativeInt32LE(b(0, 0, 0, 1), 4)).to.be.NaN;
48+
});
49+
});
50+
3351
context('getInt32LE()', () => {
3452
it('parses an int32 little endian', () => {
3553
expect(NumberUtils.getInt32LE(b(0, 0, 0, 1), 0)).to.equal(1 << 24);

0 commit comments

Comments
 (0)