@@ -7,14 +7,22 @@ import {
77 frameName ,
88 frameTreeNodeId ,
99} from '../src/lib/profiler/trace-file-utils.js' ;
10- import type { TraceEvent , TraceEventContainer , TraceMetadata } from '../src/lib/profiler/trace-file.type' ;
10+ import type {
11+ TraceEvent ,
12+ TraceEventContainer ,
13+ TraceMetadata ,
14+ } from '../src/lib/profiler/trace-file.type' ;
1115
1216const BASE_TS = 1_700_000_005_000_000 ;
1317const FIXED_TIME = '2026-01-28T14:29:27.995Z' ;
1418
1519/* ───────────── IO ───────────── */
1620const read = ( p : string ) => fs . readFile ( p , 'utf8' ) . then ( s => s . trim ( ) ) ;
17- const parseJsonl = ( s : string ) => s . split ( '\n' ) . filter ( Boolean ) . map ( l => JSON . parse ( l ) ) ;
21+ const parseJsonl = ( s : string ) =>
22+ s
23+ . split ( '\n' )
24+ . filter ( Boolean )
25+ . map ( l => JSON . parse ( l ) ) ;
1826const parseDecodeJsonl = ( s : string ) => parseJsonl ( s ) . map ( decodeEvent ) ;
1927
2028/* ───────────── Metadata ───────────── */
@@ -24,21 +32,30 @@ const normMeta = (
2432) : TraceMetadata | undefined =>
2533 m
2634 ? ( {
27- ...( keepGen ? m : Object . fromEntries ( Object . entries ( m ) . filter ( ( [ k ] ) => k !== 'generatedAt' ) ) ) ,
28- startTime : FIXED_TIME ,
29- ...( keepGen && { generatedAt : FIXED_TIME } ) ,
30- } as TraceMetadata )
35+ ...( keepGen
36+ ? m
37+ : Object . fromEntries (
38+ Object . entries ( m ) . filter ( ( [ k ] ) => k !== 'generatedAt' ) ,
39+ ) ) ,
40+ startTime : FIXED_TIME ,
41+ ...( keepGen && { generatedAt : FIXED_TIME } ) ,
42+ } as TraceMetadata )
3143 : undefined ;
3244
3345/* ───────────── Detail ───────────── */
3446const normalizeDetail = ( d : unknown ) : unknown => {
3547 const o =
36- typeof d === 'string' ? JSON . parse ( d ) :
37- typeof d === 'object' && d ? d : null ;
48+ typeof d === 'string'
49+ ? JSON . parse ( d )
50+ : typeof d === 'object' && d
51+ ? d
52+ : null ;
3853 const props = o ?. devtools ?. properties ;
3954 if ( ! Array . isArray ( props ) ) return d ;
4055
41- const isTransition = props . some ( e => Array . isArray ( e ) && e [ 0 ] === 'Transition' ) ;
56+ const isTransition = props . some (
57+ e => Array . isArray ( e ) && e [ 0 ] === 'Transition' ,
58+ ) ;
4259
4360 return {
4461 ...o ,
@@ -66,39 +83,66 @@ const normalizeDetail = (d: unknown): unknown => {
6683} ;
6784
6885/* ───────────── Context ───────────── */
69- const uniq = < T > ( v : ( T | undefined ) [ ] ) => [ ...new Set ( v . filter ( Boolean ) as T [ ] ) ] ;
86+ const uniq = < T > ( v : ( T | undefined ) [ ] ) => [
87+ ...new Set ( v . filter ( Boolean ) as T [ ] ) ,
88+ ] ;
7089const ctx = ( e : TraceEvent [ ] , base = BASE_TS ) => ( {
71- pid : new Map ( uniq ( e . map ( x => x . pid ) ) . sort ( ) . map ( ( v , i ) => [ v , 10001 + i ] ) ) ,
72- tid : new Map ( uniq ( e . map ( x => x . tid ) ) . sort ( ) . map ( ( v , i ) => [ v , i + 1 ] ) ) ,
73- ts : new Map ( uniq ( e . map ( x => x . ts ) ) . sort ( ) . map ( ( v , i ) => [ v , base + i * 100 ] ) ) ,
74- id : new Map ( uniq ( e . map ( x => x . id2 ?. local ) ) . sort ( ) . map ( ( v , i ) => [ v , `0x${ ( i + 1 ) . toString ( 16 ) } ` ] ) ) ,
90+ pid : new Map (
91+ uniq ( e . map ( x => x . pid ) )
92+ . sort ( )
93+ . map ( ( v , i ) => [ v , 10001 + i ] ) ,
94+ ) ,
95+ tid : new Map (
96+ uniq ( e . map ( x => x . tid ) )
97+ . sort ( )
98+ . map ( ( v , i ) => [ v , i + 1 ] ) ,
99+ ) ,
100+ ts : new Map (
101+ uniq ( e . map ( x => x . ts ) )
102+ . sort ( )
103+ . map ( ( v , i ) => [ v , base + i * 100 ] ) ,
104+ ) ,
105+ id : new Map (
106+ uniq ( e . map ( x => x . id2 ?. local ) )
107+ . sort ( )
108+ . map ( ( v , i ) => [ v , `0x${ ( i + 1 ) . toString ( 16 ) } ` ] ) ,
109+ ) ,
75110} ) ;
76111
77112/* ───────────── Event normalization ───────────── */
78113const mapIf = < T , R > ( v : T | undefined , m : Map < T , R > , k : string ) =>
79114 v != null && m . has ( v ) ? { [ k ] : m . get ( v ) ! } : { } ;
80115
81- const normalizeEvent = ( e : TraceEvent , c : ReturnType < typeof ctx > ) : TraceEvent => {
116+ const normalizeEvent = (
117+ e : TraceEvent ,
118+ c : ReturnType < typeof ctx > ,
119+ ) : TraceEvent => {
82120 const pid = c . pid . get ( e . pid ) ?? e . pid ;
83121 const tid = c . tid . get ( e . tid ) ?? e . tid ;
84122
85123 const args = e . args && {
86124 ...e . args ,
87- ...( e . args . detail !== undefined && { detail : normalizeDetail ( e . args . detail ) } ) ,
125+ ...( e . args . detail !== undefined && {
126+ detail : normalizeDetail ( e . args . detail ) ,
127+ } ) ,
88128 ...( e . args . data &&
89129 typeof e . args . data === 'object' && {
90130 data : {
91131 ...( e . args . data as any ) ,
92- ...( pid && tid && 'frameTreeNodeId' in e . args . data && {
93- frameTreeNodeId : frameTreeNodeId ( pid , tid ) ,
94- } ) ,
95- ...( Array . isArray ( ( e . args . data as any ) . frames ) && pid && tid && {
96- frames : ( e . args . data as any ) . frames . map ( ( f : any ) => ( {
97- ...f ,
98- processId : pid ,
99- frame : frameName ( pid , tid ) ,
100- } ) ) ,
101- } ) ,
132+ ...( pid &&
133+ tid &&
134+ 'frameTreeNodeId' in e . args . data && {
135+ frameTreeNodeId : frameTreeNodeId ( pid , tid ) ,
136+ } ) ,
137+ ...( Array . isArray ( ( e . args . data as any ) . frames ) &&
138+ pid &&
139+ tid && {
140+ frames : ( e . args . data as any ) . frames . map ( ( f : any ) => ( {
141+ ...f ,
142+ processId : pid ,
143+ frame : frameName ( pid , tid ) ,
144+ } ) ) ,
145+ } ) ,
102146 } ,
103147 } ) ,
104148 } ;
@@ -108,9 +152,10 @@ const normalizeEvent = (e: TraceEvent, c: ReturnType<typeof ctx>): TraceEvent =>
108152 ...mapIf ( e . pid , c . pid , 'pid' ) ,
109153 ...mapIf ( e . tid , c . tid , 'tid' ) ,
110154 ...mapIf ( e . ts , c . ts , 'ts' ) ,
111- ...( e . id2 ?. local && c . id . has ( e . id2 . local ) && {
112- id2 : { ...e . id2 , local : c . id . get ( e . id2 . local ) ! } ,
113- } ) ,
155+ ...( e . id2 ?. local &&
156+ c . id . has ( e . id2 . local ) && {
157+ id2 : { ...e . id2 , local : c . id . get ( e . id2 . local ) ! } ,
158+ } ) ,
114159 ...( args && { args } ) ,
115160 } ;
116161} ;
@@ -133,9 +178,9 @@ export const normalizeAndFormatEvents = (
133178 typeof input === 'string'
134179 ? input . trim ( )
135180 ? normalizeTraceEvents ( parseJsonl ( input ) . map ( decodeEvent ) , opts )
136- . map ( encodeEvent )
137- . map ( o => JSON . stringify ( o ) )
138- . join ( '\n' ) + ( input . endsWith ( '\n' ) ? '\n' : '' )
181+ . map ( encodeEvent )
182+ . map ( o => JSON . stringify ( o ) )
183+ . join ( '\n' ) + ( input . endsWith ( '\n' ) ? '\n' : '' )
139184 : input
140185 : normalizeTraceEvents ( input , opts ) ;
141186
@@ -176,4 +221,6 @@ export const loadNormalizedTraceJson = async (
176221export const loadNormalizedTraceJsonl = async (
177222 p : `${string } .jsonl`,
178223) : Promise < TraceEventContainer > =>
179- createTraceFile ( { traceEvents : normalizeTraceEvents ( parseDecodeJsonl ( await read ( p ) ) ) } ) ;
224+ createTraceFile ( {
225+ traceEvents : normalizeTraceEvents ( parseDecodeJsonl ( await read ( p ) ) ) ,
226+ } ) ;
0 commit comments