diff --git a/src/parser/on_demand/parse_to_elements.ts b/src/parser/on_demand/parse_to_elements.ts index 47ed3c5e..0a778a92 100644 --- a/src/parser/on_demand/parse_to_elements.ts +++ b/src/parser/on_demand/parse_to_elements.ts @@ -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, @@ -82,8 +82,11 @@ function findNull(bytes: Uint8Array, offset: number): number { * @public * @experimental */ -export function parseToElements(bytes: Uint8Array, pOffset?: number | null): Iterable { - const startOffset = pOffset ?? 0; +export function parseToElements( + bytes: Uint8Array, + startOffset: number | null = 0 +): Iterable { + startOffset ??= 0; if (bytes.length < 5) { throw new BSONOffsetError( @@ -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; } diff --git a/src/parser/on_demand/parse_to_structure.ts b/src/parser/on_demand/parse_to_structure.ts index 157b102c..2924c725 100644 --- a/src/parser/on_demand/parse_to_structure.ts +++ b/src/parser/on_demand/parse_to_structure.ts @@ -94,13 +94,13 @@ 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 @@ -108,20 +108,22 @@ export function parseToStructure< 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;