11import type { Session } from 'node:inspector'
22import { mkdirSync , writeFileSync } from 'node:fs'
3- import { mkdir , writeFile } from 'node:fs/promises'
4- import process from 'node:process'
53import { colors } from 'consola/utils'
64import { join } from 'pathe'
75import { logger } from './logger'
@@ -10,8 +8,7 @@ let session: Session | undefined
108let profileCount = 0
119
1210export async function startCpuProfile ( ) : Promise < void > {
13- // Adopt session started in bin/nuxi.mjs
14- const cli = globalThis . __nuxt_cli__ as typeof globalThis . __nuxt_cli__ & { cpuProfileSession ?: import ( 'node:inspector' ) . Session }
11+ const cli = globalThis . __nuxt_cli__ as Record < string , any > | undefined
1512 if ( cli ?. cpuProfileSession ) {
1613 session = cli . cpuProfileSession
1714 delete cli . cpuProfileSession
@@ -26,38 +23,15 @@ export async function startCpuProfile(): Promise<void> {
2623 if ( err ) {
2724 rej ( err )
2825 }
29- else { res ( ) }
26+ else {
27+ res ( )
28+ }
3029 } )
3130 } )
3231 } )
3332}
3433
35- export function stopCpuProfile ( outDir : string ) : Promise < string | undefined > {
36- if ( ! session ) {
37- return Promise . resolve ( undefined )
38- }
39- const s = session
40- session = undefined
41- return new Promise ( ( res , rej ) => {
42- s . post ( 'Profiler.stop' , ( err , { profile } ) => {
43- if ( err ) {
44- return rej ( err )
45- }
46- const outPath = join ( outDir , `profile-${ profileCount ++ } .cpuprofile` )
47- mkdir ( outDir , { recursive : true } )
48- . then ( ( ) => writeFile ( outPath , JSON . stringify ( profile ) ) )
49- . then ( ( ) => {
50- logger . info ( `CPU profile written to ${ colors . cyan ( outPath ) } ` )
51- logger . info ( `Open it in ${ colors . cyan ( 'https://www.speedscope.app' ) } or Chrome DevTools` )
52- s . disconnect ( )
53- res ( outPath )
54- } )
55- . catch ( rej )
56- } )
57- } )
58- }
59-
60- function stopCpuProfileSync ( outDir : string ) : string | undefined {
34+ export function stopCpuProfile ( outDir : string ) : string | undefined {
6135 if ( ! session ) {
6236 return
6337 }
@@ -80,20 +54,3 @@ function stopCpuProfileSync(outDir: string): string | undefined {
8054 } )
8155 return outPath
8256}
83-
84- /**
85- * Install signal handlers that flush the CPU profile before exit.
86- * Returns a cleanup function to remove the handlers.
87- */
88- export function installSignalHandlers ( outDir : string ) : ( ) => void {
89- const onSignal = ( signal : NodeJS . Signals ) => {
90- stopCpuProfileSync ( outDir )
91- process . kill ( process . pid , signal )
92- }
93- process . once ( 'SIGINT' , onSignal )
94- process . once ( 'SIGTERM' , onSignal )
95- return ( ) => {
96- process . off ( 'SIGINT' , onSignal )
97- process . off ( 'SIGTERM' , onSignal )
98- }
99- }
0 commit comments