MCPcopy Create free account
hub / github.com/ChromeDevTools/chrome-devtools-mcp / parseEventsArray

Function parseEventsArray

src/processors/ChunkedTraceParser.ts:203–326  ·  view source on GitHub ↗

* Scans trace events from a JSON array in bounded batches to limit memory consumption. * * @param buffer The raw byte buffer containing the events array. * @param startPos The index immediately after the opening array bracket. * @param config Configuration settings controlling batch sizes and te

(
  buffer: Uint8Array<ArrayBufferLike>,
  startPos: number,
  config: BatchConfig,
)

Source from the content-addressed store, hash-verified

201 * @throws {SyntaxError} If the event array contains malformed JSON, unbalanced delimiters, or unexpected tokens.
202 */
203function parseEventsArray(
204 buffer: Uint8Array<ArrayBufferLike>,
205 startPos: number,
206 config: BatchConfig,
207): {events: DevTools.TraceEngine.Types.Events.Event[]; endPos: number} {
208 const events: DevTools.TraceEngine.Types.Events.Event[] = [];
209 let batchStart = -1;
210 let lastEventEnd = -1;
211 let batchEventCount = 0;
212 let depth = 0;
213 let i = startPos;
214 let closed = false;
215 let state:
216 'expect_element_or_end' | 'expect_element' | 'expect_comma_or_end' =
217 'expect_element_or_end';
218
219 const flushBatch = (): void => {
220 if (batchStart !== -1 && lastEventEnd > batchStart) {
221 // Slicing between batchStart and lastEventEnd is safe for UTF-8 decoding.
222 // Both boundaries are ASCII structural braces ({ and }) that align with code unit boundaries.
223 const slice = buffer.subarray(batchStart, lastEventEnd);
224 const chunkText = config.decoder.decode(slice);
225 // Wrapping comma-delimited event objects in brackets creates a valid JSON array for the parser.
226 const parsed: DevTools.TraceEngine.Types.Events.Event[] = JSON.parse(
227 `[${chunkText}]`,
228 );
229 // Append items iteratively to avoid exceeding V8 call stack size limits on large batches.
230 for (const event of parsed) {
231 events.push(event);
232 }
233 batchStart = -1;
234 batchEventCount = 0;
235 }
236 };
237
238 while (i < buffer.length) {
239 const byte = buffer[i];
240
241 if (depth > 0) {
242 if (byte === BYTE_QUOTE) {
243 i = skipString(buffer, i);
244 continue;
245 }
246 if (byte === BYTE_OPEN_BRACE) {
247 depth++;
248 i++;
249 continue;
250 }
251 if (byte === BYTE_CLOSE_BRACE) {
252 depth--;
253 if (depth === 0) {
254 lastEventEnd = i + 1;
255 batchEventCount++;
256 state = 'expect_comma_or_end';
257
258 // Check thresholds after recording the completed event.
259 // Checking batchEventCount after incrementing ensures batches cap at exactly eventsPerBatch.
260 // Individual events are atomic JSON objects and cannot be split mid-event; when an event

Callers 1

Calls 2

skipStringFunction · 0.85
flushBatchFunction · 0.85

Tested by

no test coverage detected