Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Mar 13, 2024
1 parent e3c5996 commit da05d20
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
53 changes: 35 additions & 18 deletions src/parser/on_demand/parse_to_elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { BSONOffsetError } from '../../error';
* - `minKey` is set to 255 so unsigned comparisons succeed
* - Modify with caution, double check the bundle contains literals
*/
const enum t {
const enum BSONElementType {
double = 1,
string = 2,
object = 3,
Expand Down Expand Up @@ -82,8 +82,11 @@ function findNull(bytes: Uint8Array, offset: number): number {
* @public
* @experimental
*/
export function parseToElements(bytes: Uint8Array, pOffset?: number | null): Iterable<BSONElement> {
const startOffset = pOffset ?? 0;
export function parseToElements(
bytes: Uint8Array,
startOffset: number | null = 0
): Iterable<BSONElement> {
startOffset ??= 0;

if (bytes.length < 5) {
throw new BSONOffsetError(
Expand Down Expand Up @@ -125,37 +128,51 @@ export function parseToElements(bytes: Uint8Array, pOffset?: number | null): Ite

let length: number;

if (type === t.double || type === t.long || type === t.date || type === t.timestamp) {
if (
type === BSONElementType.double ||
type === BSONElementType.long ||
type === BSONElementType.date ||
type === BSONElementType.timestamp
) {
length = 8;
} else if (type === t.int) {
} else if (type === BSONElementType.int) {
length = 4;
} else if (type === t.objectId) {
} else if (type === BSONElementType.objectId) {
length = 12;
} else if (type === t.decimal) {
} else if (type === BSONElementType.decimal) {
length = 16;
} else if (type === t.bool) {
} else if (type === BSONElementType.bool) {
length = 1;
} else if (type === t.null || type === t.undefined || type === t.maxKey || type === t.minKey) {
} else if (
type === BSONElementType.null ||
type === BSONElementType.undefined ||
type === BSONElementType.maxKey ||
type === BSONElementType.minKey
) {
length = 0;
}
// Needs a size calculation
else if (type === t.regex) {
else if (type === BSONElementType.regex) {
length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
} else if (type === t.object || type === t.array || type === t.javascriptWithScope) {
} else if (
type === BSONElementType.object ||
type === BSONElementType.array ||
type === BSONElementType.javascriptWithScope
) {
length = getSize(bytes, offset);
} else if (
type === t.string ||
type === t.binData ||
type === t.dbPointer ||
type === t.javascript ||
type === t.symbol
type === BSONElementType.string ||
type === BSONElementType.binData ||
type === BSONElementType.dbPointer ||
type === BSONElementType.javascript ||
type === BSONElementType.symbol
) {
length = getSize(bytes, offset) + 4;
if (type === t.binData) {
if (type === BSONElementType.binData) {
// binary subtype
length += 1;
}
if (type === t.dbPointer) {
if (type === BSONElementType.dbPointer) {
// dbPointer's objectId
length += 12;
}
Expand Down
22 changes: 12 additions & 10 deletions src/parser/on_demand/parse_to_structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,34 +94,36 @@ export function parseToStructure<
};

/** BSONElement offsets: type indicator and value offset */
const enum e {
const enum BSONElementOffset {
type = 0,
offset = 3
}

/** BSON Embedded types */
const enum t {
const enum BSONElementType {
object = 3,
array = 4,
javascriptWithScope = 15
}

embedded: while (ctx !== null) {
for (
let it: BSONElement | undefined = ctx.elements[ctx.elementOffset++];
it != null;
it = ctx.elements[ctx.elementOffset++]
let bsonElement: BSONElement | undefined = ctx.elements[ctx.elementOffset++];
bsonElement != null;
bsonElement = ctx.elements[ctx.elementOffset++]
) {
const type = it[e.type];
const offset = it[e.offset];
const type = bsonElement[BSONElementOffset.type];
const offset = bsonElement[BSONElementOffset.offset];

const container = reviver(bytes, ctx.container, it);
const container = reviver(bytes, ctx.container, bsonElement);
const isEmbeddedType =
type === t.object || type === t.array || type === t.javascriptWithScope;
type === BSONElementType.object ||
type === BSONElementType.array ||
type === BSONElementType.javascriptWithScope;

if (container != null && isEmbeddedType) {
const docOffset: number =
type !== t.javascriptWithScope
type !== BSONElementType.javascriptWithScope
? offset
: // value offset + codeSize + value int + code int
offset + getSize(bytes, offset + 4) + 4 + 4;
Expand Down

0 comments on commit da05d20

Please sign in to comment.