1
1
'use strict' ;
2
2
3
- const { createHook } = require ( 'async_hooks' ) ;
4
3
const inspector = process . binding ( 'inspector' ) ;
5
- const config = process . binding ( 'config' ) ;
6
4
7
5
if ( ! inspector || ! inspector . asyncTaskScheduled ) {
8
6
exports . setup = function ( ) { } ;
9
7
return ;
10
8
}
11
9
12
- const hook = createHook ( {
13
- init ( asyncId , type , triggerAsyncId , resource ) {
10
+ let hook ;
11
+ let config ;
12
+
13
+ function lazyHookCreation ( ) {
14
+ const { createHook } = require ( 'async_hooks' ) ;
15
+ config = process . binding ( 'config' ) ;
16
+
17
+ hook = createHook ( {
18
+ init ( asyncId , type , triggerAsyncId , resource ) {
14
19
// It's difficult to tell which tasks will be recurring and which won't,
15
20
// therefore we mark all tasks as recurring. Based on the discussion
16
21
// in https://github.com/nodejs/node/pull/13870#discussion_r124515293,
17
22
// this should be fine as long as we call asyncTaskCanceled() too.
18
- const recurring = true ;
19
- if ( type === 'PROMISE' )
20
- this . promiseIds . add ( asyncId ) ;
21
- else
22
- inspector . asyncTaskScheduled ( type , asyncId , recurring ) ;
23
- } ,
23
+ const recurring = true ;
24
+ if ( type === 'PROMISE' )
25
+ this . promiseIds . add ( asyncId ) ;
26
+ else
27
+ inspector . asyncTaskScheduled ( type , asyncId , recurring ) ;
28
+ } ,
24
29
25
- before ( asyncId ) {
26
- if ( this . promiseIds . has ( asyncId ) )
27
- return ;
28
- inspector . asyncTaskStarted ( asyncId ) ;
29
- } ,
30
+ before ( asyncId ) {
31
+ if ( this . promiseIds . has ( asyncId ) )
32
+ return ;
33
+ inspector . asyncTaskStarted ( asyncId ) ;
34
+ } ,
30
35
31
- after ( asyncId ) {
32
- if ( this . promiseIds . has ( asyncId ) )
33
- return ;
34
- inspector . asyncTaskFinished ( asyncId ) ;
35
- } ,
36
+ after ( asyncId ) {
37
+ if ( this . promiseIds . has ( asyncId ) )
38
+ return ;
39
+ inspector . asyncTaskFinished ( asyncId ) ;
40
+ } ,
36
41
37
- destroy ( asyncId ) {
38
- if ( this . promiseIds . has ( asyncId ) )
39
- return this . promiseIds . delete ( asyncId ) ;
40
- inspector . asyncTaskCanceled ( asyncId ) ;
41
- } ,
42
- } ) ;
42
+ destroy ( asyncId ) {
43
+ if ( this . promiseIds . has ( asyncId ) )
44
+ return this . promiseIds . delete ( asyncId ) ;
45
+ inspector . asyncTaskCanceled ( asyncId ) ;
46
+ } ,
47
+ } ) ;
43
48
44
- hook . promiseIds = new Set ( ) ;
49
+ hook . promiseIds = new Set ( ) ;
50
+ }
45
51
46
52
function enable ( ) {
53
+ if ( hook === undefined ) lazyHookCreation ( ) ;
47
54
if ( config . bits < 64 ) {
48
55
// V8 Inspector stores task ids as (void*) pointers.
49
56
// async_hooks store ids as 64bit numbers.
@@ -61,6 +68,7 @@ function enable() {
61
68
}
62
69
63
70
function disable ( ) {
71
+ if ( hook === undefined ) lazyHookCreation ( ) ;
64
72
hook . disable ( ) ;
65
73
}
66
74
0 commit comments