11'use strict' ;
22
3- const { createHook } = require ( 'async_hooks' ) ;
43const inspector = process . binding ( 'inspector' ) ;
5- const config = process . binding ( 'config' ) ;
64
75if ( ! inspector || ! inspector . asyncTaskScheduled ) {
86 exports . setup = function ( ) { } ;
97 return ;
108}
119
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 ) {
1419 // It's difficult to tell which tasks will be recurring and which won't,
1520 // therefore we mark all tasks as recurring. Based on the discussion
1621 // in https://github.com/nodejs/node/pull/13870#discussion_r124515293,
1722 // 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+ } ,
2429
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+ } ,
3035
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+ } ,
3641
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+ } ) ;
4348
44- hook . promiseIds = new Set ( ) ;
49+ hook . promiseIds = new Set ( ) ;
50+ }
4551
4652function enable ( ) {
53+ if ( hook === undefined ) lazyHookCreation ( ) ;
4754 if ( config . bits < 64 ) {
4855 // V8 Inspector stores task ids as (void*) pointers.
4956 // async_hooks store ids as 64bit numbers.
@@ -61,6 +68,7 @@ function enable() {
6168}
6269
6370function disable ( ) {
71+ if ( hook === undefined ) lazyHookCreation ( ) ;
6472 hook . disable ( ) ;
6573}
6674
0 commit comments