1
1
'use strict' ;
2
- const path = require ( 'path' ) ;
3
- const { mkdirSync, writeFileSync } = require ( 'fs' ) ;
4
- const hasInspector = process . config . variables . v8_enable_inspector === 1 ;
5
- let inspector = null ;
6
- if ( hasInspector ) inspector = require ( 'inspector' ) ;
7
-
8
- let session ;
2
+ let coverageConnection = null ;
3
+ let coverageDirectory ;
9
4
10
5
function writeCoverage ( ) {
11
- if ( ! session ) {
6
+ if ( ! coverageConnection && coverageDirectory ) {
12
7
return ;
13
8
}
14
9
10
+ const { join } = require ( 'path' ) ;
11
+ const { mkdirSync, writeFileSync } = require ( 'fs' ) ;
15
12
const { threadId } = require ( 'internal/worker' ) ;
16
13
17
14
const filename = `coverage-${ process . pid } -${ Date . now ( ) } -${ threadId } .json` ;
18
15
try {
19
- // TODO(bcoe): switch to mkdirp once #22302 is addressed.
20
- mkdirSync ( process . env . NODE_V8_COVERAGE ) ;
16
+ mkdirSync ( coverageDirectory , { recursive : true } ) ;
21
17
} catch ( err ) {
22
18
if ( err . code !== 'EEXIST' ) {
23
19
console . error ( err ) ;
24
20
return ;
25
21
}
26
22
}
27
23
28
- const target = path . join ( process . env . NODE_V8_COVERAGE , filename ) ;
29
-
24
+ const target = join ( coverageDirectory , filename ) ;
30
25
try {
31
- session . post ( 'Profiler.takePreciseCoverage' , ( err , coverageInfo ) => {
32
- if ( err ) return console . error ( err ) ;
33
- try {
34
- writeFileSync ( target , JSON . stringify ( coverageInfo ) ) ;
35
- } catch ( err ) {
36
- console . error ( err ) ;
37
- }
38
- } ) ;
26
+ disableAllAsyncHooks ( ) ;
27
+ let msg ;
28
+ coverageConnection . _coverageCallback = function ( _msg ) {
29
+ msg = _msg ;
30
+ } ;
31
+ coverageConnection . dispatch ( JSON . stringify ( {
32
+ id : 3 ,
33
+ method : 'Profiler.takePreciseCoverage'
34
+ } ) ) ;
35
+ const coverageInfo = JSON . parse ( msg ) . result ;
36
+ writeFileSync ( target , JSON . stringify ( coverageInfo ) ) ;
39
37
} catch ( err ) {
40
38
console . error ( err ) ;
41
39
} finally {
42
- session . disconnect ( ) ;
43
- session = null ;
40
+ coverageConnection . disconnect ( ) ;
41
+ coverageConnection = null ;
44
42
}
45
43
}
46
44
45
+ function disableAllAsyncHooks ( ) {
46
+ const { getHookArrays } = require ( 'internal/async_hooks' ) ;
47
+ const [ hooks_array ] = getHookArrays ( ) ;
48
+ hooks_array . forEach ( ( hook ) => { hook . disable ( ) ; } ) ;
49
+ }
50
+
47
51
exports . writeCoverage = writeCoverage ;
48
52
49
53
function setup ( ) {
50
- if ( ! hasInspector ) {
51
- console . warn ( 'coverage currently only supported in main thread' ) ;
54
+ const { Connection } = process . binding ( 'inspector' ) ;
55
+ if ( ! Connection ) {
56
+ console . warn ( 'inspector not enabled' ) ;
52
57
return ;
53
58
}
54
59
55
- session = new inspector . Session ( ) ;
56
- session . connect ( ) ;
57
- session . post ( 'Profiler.enable' ) ;
58
- session . post ( 'Profiler.startPreciseCoverage' , { callCount : true ,
59
- detailed : true } ) ;
60
+ coverageConnection = new Connection ( ( res ) => {
61
+ if ( coverageConnection . _coverageCallback ) {
62
+ coverageConnection . _coverageCallback ( res ) ;
63
+ }
64
+ } ) ;
65
+ coverageConnection . dispatch ( JSON . stringify ( {
66
+ id : 1 ,
67
+ method : 'Profiler.enable'
68
+ } ) ) ;
69
+ coverageConnection . dispatch ( JSON . stringify ( {
70
+ id : 2 ,
71
+ method : 'Profiler.startPreciseCoverage' ,
72
+ params : {
73
+ callCount : true ,
74
+ detailed : true
75
+ }
76
+ } ) ) ;
60
77
61
- const reallyReallyExit = process . reallyExit ;
78
+ try {
79
+ const { resolve } = require ( 'path' ) ;
80
+ coverageDirectory = process . env . NODE_V8_COVERAGE =
81
+ resolve ( process . env . NODE_V8_COVERAGE ) ;
82
+ } catch ( err ) {
83
+ console . error ( err ) ;
84
+ }
85
+ }
62
86
87
+ exports . setup = setup ;
88
+
89
+ function setupExitHooks ( ) {
90
+ const reallyReallyExit = process . reallyExit ;
63
91
process . reallyExit = function ( code ) {
64
92
writeCoverage ( ) ;
65
93
reallyReallyExit ( code ) ;
@@ -68,4 +96,4 @@ function setup() {
68
96
process . on ( 'exit' , writeCoverage ) ;
69
97
}
70
98
71
- exports . setup = setup ;
99
+ exports . setupExitHooks = setupExitHooks ;
0 commit comments