/
parse_to_elements.ts
188 lines (167 loc) · 4.86 KB
/
parse_to_elements.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { BSONOffsetError } from '../../error';
import { NumberUtils } from '../../utils/number_utils';
/**
* @internal
*
* @remarks
* - This enum is const so the code we produce will inline the numbers
* - `minKey` is set to 255 so unsigned comparisons succeed
* - Modify with caution, double check the bundle contains literals
*/
const enum BSONElementType {
double = 1,
string = 2,
object = 3,
array = 4,
binData = 5,
undefined = 6,
objectId = 7,
bool = 8,
date = 9,
null = 10,
regex = 11,
dbPointer = 12,
javascript = 13,
symbol = 14,
javascriptWithScope = 15,
int = 16,
timestamp = 17,
long = 18,
decimal = 19,
minKey = 255,
maxKey = 127
}
/**
* @public
* @experimental
*/
export type BSONElement = [
type: number,
nameOffset: number,
nameLength: number,
offset: number,
length: number
];
function getSize(source: Uint8Array, offset: number) {
try {
return NumberUtils.getNonnegativeInt32LE(source, offset);
} catch (cause) {
throw new BSONOffsetError('BSON size cannot be negative', offset, { cause });
}
}
/**
* Searches for null terminator of a BSON element's value (Never the document null terminator)
* **Does not** bounds check since this should **ONLY** be used within parseToElements which has asserted that `bytes` ends with a `0x00`.
* So this will at most iterate to the document's terminator and error if that is the offset reached.
*/
function findNull(bytes: Uint8Array, offset: number): number {
let nullTerminatorOffset = offset;
for (; bytes[nullTerminatorOffset] !== 0x00; nullTerminatorOffset++);
if (nullTerminatorOffset === bytes.length - 1) {
// We reached the null terminator of the document, not a value's
throw new BSONOffsetError('Null terminator not found', offset);
}
return nullTerminatorOffset;
}
/**
* @public
* @experimental
*/
export function parseToElements(
bytes: Uint8Array,
startOffset: number | null = 0
): Iterable<BSONElement> {
startOffset ??= 0;
if (bytes.length < 5) {
throw new BSONOffsetError(
`Input must be at least 5 bytes, got ${bytes.length} bytes`,
startOffset
);
}
const documentSize = getSize(bytes, startOffset);
if (documentSize > bytes.length - startOffset) {
throw new BSONOffsetError(
`Parsed documentSize (${documentSize} bytes) does not match input length (${bytes.length} bytes)`,
startOffset
);
}
if (bytes[startOffset + documentSize - 1] !== 0x00) {
throw new BSONOffsetError('BSON documents must end in 0x00', startOffset + documentSize);
}
const elements: BSONElement[] = [];
let offset = startOffset + 4;
while (offset <= documentSize + startOffset) {
const type = bytes[offset];
offset += 1;
if (type === 0) {
if (offset - startOffset !== documentSize) {
throw new BSONOffsetError(`Invalid 0x00 type byte`, offset);
}
break;
}
const nameOffset = offset;
const nameLength = findNull(bytes, offset) - nameOffset;
offset += nameLength + 1;
let length: number;
if (
type === BSONElementType.double ||
type === BSONElementType.long ||
type === BSONElementType.date ||
type === BSONElementType.timestamp
) {
length = 8;
} else if (type === BSONElementType.int) {
length = 4;
} else if (type === BSONElementType.objectId) {
length = 12;
} else if (type === BSONElementType.decimal) {
length = 16;
} else if (type === BSONElementType.bool) {
length = 1;
} else if (
type === BSONElementType.null ||
type === BSONElementType.undefined ||
type === BSONElementType.maxKey ||
type === BSONElementType.minKey
) {
length = 0;
}
// Needs a size calculation
else if (type === BSONElementType.regex) {
length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
} else if (
type === BSONElementType.object ||
type === BSONElementType.array ||
type === BSONElementType.javascriptWithScope
) {
length = getSize(bytes, offset);
} else if (
type === BSONElementType.string ||
type === BSONElementType.binData ||
type === BSONElementType.dbPointer ||
type === BSONElementType.javascript ||
type === BSONElementType.symbol
) {
length = getSize(bytes, offset) + 4;
if (type === BSONElementType.binData) {
// binary subtype
length += 1;
}
if (type === BSONElementType.dbPointer) {
// dbPointer's objectId
length += 12;
}
} else {
throw new BSONOffsetError(
`Invalid 0x${type.toString(16).padStart(2, '0')} type byte`,
offset
);
}
if (length > documentSize) {
throw new BSONOffsetError('value reports length larger than document', offset);
}
elements.push([type, nameOffset, nameLength, offset, length]);
offset += length;
}
return elements;
}