1+ /* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */
12import { BSONOffsetError } from '../../error' ;
2- import { NumberUtils } from '../../utils/number_utils' ;
3+
4+ /**
5+ * @internal
6+ *
7+ * @remarks
8+ * - This enum is const so the code we produce will inline the numbers
9+ * - `minKey` is set to 255 so unsigned comparisons succeed
10+ * - Modify with caution, double check the bundle contains literals
11+ */
12+ const enum t {
13+ double = 1 ,
14+ string = 2 ,
15+ object = 3 ,
16+ array = 4 ,
17+ binData = 5 ,
18+ undefined = 6 ,
19+ objectId = 7 ,
20+ bool = 8 ,
21+ date = 9 ,
22+ null = 10 ,
23+ regex = 11 ,
24+ dbPointer = 12 ,
25+ javascript = 13 ,
26+ symbol = 14 ,
27+ javascriptWithScope = 15 ,
28+ int = 16 ,
29+ timestamp = 17 ,
30+ long = 18 ,
31+ decimal = 19 ,
32+ minKey = 255 ,
33+ maxKey = 127
34+ }
335
436/**
537 * @public
@@ -18,7 +50,12 @@ function getSize(source: Uint8Array, offset: number): number {
1850 if ( source [ offset + 3 ] > 127 ) {
1951 throw new BSONOffsetError ( 'BSON size cannot be negative' , offset ) ;
2052 }
21- return NumberUtils . getInt32LE ( source , offset ) ;
53+ return (
54+ source [ offset ] |
55+ ( source [ offset + 1 ] << 8 ) |
56+ ( source [ offset + 2 ] << 16 ) |
57+ ( source [ offset + 3 ] << 24 )
58+ ) ;
2259}
2360
2461/**
@@ -84,35 +121,31 @@ export function parseToElements(bytes: Uint8Array, startOffset = 0): Iterable<BS
84121
85122 let length : number ;
86123
87- // The following values are left as literals intentionally
88- if ( type === 1 || type === 18 || type === 9 || type === 17 ) {
89- // double, long, date, timestamp
124+ if ( type === t . double || type === t . long || type === t . date || type === t . timestamp ) {
90125 length = 8 ;
91- } else if ( type === 16 ) {
92- // int
126+ } else if ( type === t . int ) {
93127 length = 4 ;
94- } else if ( type === 7 ) {
95- // objectId
128+ } else if ( type === t . objectId ) {
96129 length = 12 ;
97- } else if ( type === 19 ) {
98- // decimal128
130+ } else if ( type === t . decimal ) {
99131 length = 16 ;
100- } else if ( type === 8 ) {
101- // boolean
132+ } else if ( type === t . bool ) {
102133 length = 1 ;
103- } else if ( type === 10 || type === 6 || type === 127 || type === 255 ) {
104- // null, undefined, maxKey, minKey
134+ } else if ( type === t . null || type === t . undefined || type === t . maxKey || type === t . minKey ) {
105135 length = 0 ;
106136 }
107137 // Needs a size calculation
108- else if ( type === 11 ) {
109- // regex
138+ else if ( type === t . regex ) {
110139 length = findNull ( bytes , findNull ( bytes , offset ) + 1 ) + 1 - offset ;
111- } else if ( type === 3 || type === 4 || type === 15 ) {
112- // object, array, code_w_scope
140+ } else if ( type === t . object || type === t . array || type === t . javascriptWithScope ) {
113141 length = getSize ( bytes , offset ) ;
114- } else if ( type === 2 || type === 5 || type === 12 || type === 13 || type === 14 ) {
115- // string, binary, dbpointer, code, symbol
142+ } else if (
143+ type === t . string ||
144+ type === t . binData ||
145+ type === t . dbPointer ||
146+ type === t . javascript ||
147+ type === t . symbol
148+ ) {
116149 length = getSize ( bytes , offset ) + 4 ;
117150 if ( type === 5 ) {
118151 // binary subtype
0 commit comments