1- /* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */
21import { BSONOffsetError } from '../../error' ;
32
43/**
@@ -9,7 +8,7 @@ import { BSONOffsetError } from '../../error';
98 * - `minKey` is set to 255 so unsigned comparisons succeed
109 * - Modify with caution, double check the bundle contains literals
1110 */
12- const enum t {
11+ const enum BSONElementType {
1312 double = 1 ,
1413 string = 2 ,
1514 object = 3 ,
@@ -45,8 +44,11 @@ export type BSONElement = [
4544 length : number
4645] ;
4746
48- /** Parses a int32 little-endian at offset, throws if it is negative */
49- function getSize ( source : Uint8Array , offset : number ) : number {
47+ /**
48+ * @internal
49+ * Parses a int32 little-endian at offset, throws if it is negative
50+ */
51+ export function getSize ( source : Uint8Array , offset : number ) : number {
5052 if ( source [ offset + 3 ] > 127 ) {
5153 throw new BSONOffsetError ( 'BSON size cannot be negative' , offset ) ;
5254 }
@@ -80,7 +82,12 @@ function findNull(bytes: Uint8Array, offset: number): number {
8082 * @public
8183 * @experimental
8284 */
83- export function parseToElements ( bytes : Uint8Array , startOffset = 0 ) : Iterable < BSONElement > {
85+ export function parseToElements (
86+ bytes : Uint8Array ,
87+ startOffset : number | null = 0
88+ ) : Iterable < BSONElement > {
89+ startOffset ??= 0 ;
90+
8491 if ( bytes . length < 5 ) {
8592 throw new BSONOffsetError (
8693 `Input must be at least 5 bytes, got ${ bytes . length } bytes` ,
@@ -121,37 +128,51 @@ export function parseToElements(bytes: Uint8Array, startOffset = 0): Iterable<BS
121128
122129 let length : number ;
123130
124- if ( type === t . double || type === t . long || type === t . date || type === t . timestamp ) {
131+ if (
132+ type === BSONElementType . double ||
133+ type === BSONElementType . long ||
134+ type === BSONElementType . date ||
135+ type === BSONElementType . timestamp
136+ ) {
125137 length = 8 ;
126- } else if ( type === t . int ) {
138+ } else if ( type === BSONElementType . int ) {
127139 length = 4 ;
128- } else if ( type === t . objectId ) {
140+ } else if ( type === BSONElementType . objectId ) {
129141 length = 12 ;
130- } else if ( type === t . decimal ) {
142+ } else if ( type === BSONElementType . decimal ) {
131143 length = 16 ;
132- } else if ( type === t . bool ) {
144+ } else if ( type === BSONElementType . bool ) {
133145 length = 1 ;
134- } else if ( type === t . null || type === t . undefined || type === t . maxKey || type === t . minKey ) {
146+ } else if (
147+ type === BSONElementType . null ||
148+ type === BSONElementType . undefined ||
149+ type === BSONElementType . maxKey ||
150+ type === BSONElementType . minKey
151+ ) {
135152 length = 0 ;
136153 }
137154 // Needs a size calculation
138- else if ( type === t . regex ) {
155+ else if ( type === BSONElementType . regex ) {
139156 length = findNull ( bytes , findNull ( bytes , offset ) + 1 ) + 1 - offset ;
140- } else if ( type === t . object || type === t . array || type === t . javascriptWithScope ) {
157+ } else if (
158+ type === BSONElementType . object ||
159+ type === BSONElementType . array ||
160+ type === BSONElementType . javascriptWithScope
161+ ) {
141162 length = getSize ( bytes , offset ) ;
142163 } else if (
143- type === t . string ||
144- type === t . binData ||
145- type === t . dbPointer ||
146- type === t . javascript ||
147- type === t . symbol
164+ type === BSONElementType . string ||
165+ type === BSONElementType . binData ||
166+ type === BSONElementType . dbPointer ||
167+ type === BSONElementType . javascript ||
168+ type === BSONElementType . symbol
148169 ) {
149170 length = getSize ( bytes , offset ) + 4 ;
150- if ( type === t . binData ) {
171+ if ( type === BSONElementType . binData ) {
151172 // binary subtype
152173 length += 1 ;
153174 }
154- if ( type === t . dbPointer ) {
175+ if ( type === BSONElementType . dbPointer ) {
155176 // dbPointer's objectId
156177 length += 12 ;
157178 }
0 commit comments