| 25 | } |
| 26 | |
| 27 | export async function parseRawTraceBuffer( |
| 28 | buffer: Uint8Array<ArrayBufferLike> | undefined, |
| 29 | metadata?: { |
| 30 | cpuThrottling?: number; |
| 31 | networkThrottling?: string; |
| 32 | }, |
| 33 | ): Promise<TraceResult | TraceParseError> { |
| 34 | engine.resetProcessor(); |
| 35 | if (!buffer) { |
| 36 | return { |
| 37 | error: 'No buffer was provided.', |
| 38 | }; |
| 39 | } |
| 40 | const asString = new TextDecoder().decode(buffer); |
| 41 | if (!asString) { |
| 42 | return { |
| 43 | error: 'Decoding the trace buffer returned an empty string.', |
| 44 | }; |
| 45 | } |
| 46 | try { |
| 47 | const data = JSON.parse(asString) as |
| 48 | | { |
| 49 | traceEvents: DevTools.TraceEngine.Types.Events.Event[]; |
| 50 | } |
| 51 | | DevTools.TraceEngine.Types.Events.Event[]; |
| 52 | |
| 53 | const events = Array.isArray(data) ? data : data.traceEvents; |
| 54 | await engine.parse(events, {metadata}); |
| 55 | const parsedTrace = engine.parsedTrace(); |
| 56 | if (!parsedTrace) { |
| 57 | return { |
| 58 | error: 'No parsed trace was returned from the trace engine.', |
| 59 | }; |
| 60 | } |
| 61 | |
| 62 | const insights = parsedTrace?.insights ?? null; |
| 63 | |
| 64 | return { |
| 65 | parsedTrace, |
| 66 | insights, |
| 67 | }; |
| 68 | } catch (e) { |
| 69 | const errorText = e instanceof Error ? e.message : JSON.stringify(e); |
| 70 | logger?.(`Unexpected error parsing trace: ${errorText}`); |
| 71 | return { |
| 72 | error: errorText, |
| 73 | }; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | const extraFormatDescriptions = `Information on performance traces may contain main thread activity represented as call frames and network requests. |
| 78 | |