Skip to content

Commit f3a01d6

Browse files
committed
fix: comments
1 parent a078182 commit f3a01d6

File tree

3 files changed

+14
-37
lines changed

3 files changed

+14
-37
lines changed

src/parser/on_demand/parse_to_elements.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ export type BSONElement = [
1313
length: number
1414
];
1515

16+
/** Parses a int32 little-endian at offset, throws if it is negative */
17+
function getSize(source: Uint8Array, offset: number): number {
18+
if (source[offset + 3] > 127) {
19+
throw new BSONOffsetError('BSON size cannot be negative', offset);
20+
}
21+
return NumberUtils.getInt32LE(source, offset);
22+
}
23+
1624
/**
17-
* Searches for null terminator.
25+
* Searches for null terminator of a BSON element's value (Never the document null terminator)
1826
* **Does not** bounds check since this should **ONLY** be used within parseToElements which has asserted that `bytes` ends with a `0x00`.
1927
* So this will at most iterate to the document's terminator and error if that is the offset reached.
2028
*/
@@ -24,6 +32,7 @@ function findNull(bytes: Uint8Array, offset: number): number {
2432
for (; bytes[nullTerminatorOffset] !== 0x00; nullTerminatorOffset++);
2533

2634
if (nullTerminatorOffset === bytes.length - 1) {
35+
// We reached the null terminator of the document, not a value's
2736
throw new BSONOffsetError('Null terminator not found', offset);
2837
}
2938

@@ -42,7 +51,7 @@ export function parseToElements(bytes: Uint8Array, startOffset = 0): Iterable<BS
4251
);
4352
}
4453

45-
const documentSize = NumberUtils.getSize(bytes, startOffset);
54+
const documentSize = getSize(bytes, startOffset);
4655

4756
if (documentSize > bytes.length - startOffset) {
4857
throw new BSONOffsetError(
@@ -75,6 +84,7 @@ export function parseToElements(bytes: Uint8Array, startOffset = 0): Iterable<BS
7584

7685
let length: number;
7786

87+
// The following values are left as literals intentionally
7888
if (type === 1 || type === 18 || type === 9 || type === 17) {
7989
// double, long, date, timestamp
8090
length = 8;
@@ -100,10 +110,10 @@ export function parseToElements(bytes: Uint8Array, startOffset = 0): Iterable<BS
100110
length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
101111
} else if (type === 3 || type === 4 || type === 15) {
102112
// object, array, code_w_scope
103-
length = NumberUtils.getSize(bytes, offset);
113+
length = getSize(bytes, offset);
104114
} else if (type === 2 || type === 5 || type === 12 || type === 13 || type === 14) {
105115
// string, binary, dbpointer, code, symbol
106-
length = NumberUtils.getSize(bytes, offset) + 4;
116+
length = getSize(bytes, offset) + 4;
107117
if (type === 5) {
108118
// binary subtype
109119
length += 1;

src/utils/number_utils.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { BSONOffsetError } from '../error';
2-
31
const FLOAT = new Float64Array(1);
42
const FLOAT_BYTES = new Uint8Array(FLOAT.buffer, 0, 8);
53

@@ -9,13 +7,6 @@ const FLOAT_BYTES = new Uint8Array(FLOAT.buffer, 0, 8);
97
* @internal
108
*/
119
export const NumberUtils = {
12-
getSize(source: Uint8Array, offset: number): number {
13-
if (source[offset + 3] > 127) {
14-
throw new BSONOffsetError('BSON size cannot be negative', offset);
15-
}
16-
return NumberUtils.getInt32LE(source, offset);
17-
},
18-
1910
/** Reads a little-endian 32-bit integer from source */
2011
getInt32LE(source: Uint8Array, offset: number): number {
2112
return (

test/node/utils/number_utils.test.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,6 @@ describe('NumberUtils', () => {
55
/** Make a Uint8Array in a less verbose way */
66
const b = (...values) => new Uint8Array(values);
77

8-
context('getSize()', () => {
9-
it('parses an int32 little endian', () => {
10-
expect(NumberUtils.getSize(b(0, 0, 0, 1), 0)).to.equal(1 << 24);
11-
});
12-
13-
it('throws on a signed int32 little endian that is negative', () => {
14-
let thrownError: Error | undefined;
15-
try {
16-
NumberUtils.getSize(b(255, 255, 255, 255), 0);
17-
} catch (error) {
18-
thrownError = error;
19-
}
20-
expect(thrownError).to.match(/BSON size cannot be negative/i);
21-
});
22-
23-
it('parses an int32 little endian at offset', () => {
24-
expect(NumberUtils.getSize(b(0, 0, 0, 0, 0, 1), 2)).to.equal(1 << 24);
25-
});
26-
27-
it('does not check bounds of offset', () => {
28-
expect(NumberUtils.getSize(b(0, 0, 0, 1), 4)).to.equal(0);
29-
});
30-
});
31-
328
context('getInt32LE()', () => {
339
it('parses an int32 little endian', () => {
3410
expect(NumberUtils.getInt32LE(b(0, 0, 0, 1), 0)).to.equal(1 << 24);

0 commit comments

Comments
 (0)