3
3
const {
4
4
Error,
5
5
FunctionPrototypeBind,
6
- NumberIsSafeInteger,
7
6
ObjectDefineProperty,
8
7
Symbol,
9
8
} = primordials ;
10
9
11
- const {
12
- ERR_ASYNC_TYPE ,
13
- ERR_INVALID_ASYNC_ID
14
- } = require ( 'internal/errors' ) . codes ;
15
-
16
10
const async_wrap = internalBinding ( 'async_wrap' ) ;
17
11
/* async_hook_fields is a Uint32Array wrapping the uint32_t array of
18
12
* Environment::AsyncHooks::fields_[]. Each index tracks the number of active
@@ -117,15 +111,6 @@ function fatalError(e) {
117
111
}
118
112
119
113
120
- function validateAsyncId ( asyncId , type ) {
121
- // Skip validation when async_hooks is disabled
122
- if ( async_hook_fields [ kCheck ] <= 0 ) return ;
123
-
124
- if ( ! NumberIsSafeInteger ( asyncId ) || asyncId < - 1 ) {
125
- fatalError ( new ERR_INVALID_ASYNC_ID ( type , asyncId ) ) ;
126
- }
127
- }
128
-
129
114
// Emit From Native //
130
115
131
116
// Used by C++ to call all init() callbacks. Because some state can be setup
@@ -314,6 +299,9 @@ function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
314
299
}
315
300
}
316
301
302
+ function enabledHooksExist ( ) {
303
+ return async_hook_fields [ kCheck ] > 0 ;
304
+ }
317
305
318
306
function initHooksExist ( ) {
319
307
return async_hook_fields [ kInit ] > 0 ;
@@ -329,21 +317,11 @@ function destroyHooksExist() {
329
317
330
318
331
319
function emitInitScript ( asyncId , type , triggerAsyncId , resource ) {
332
- validateAsyncId ( asyncId , 'asyncId' ) ;
333
- if ( triggerAsyncId !== null )
334
- validateAsyncId ( triggerAsyncId , 'triggerAsyncId' ) ;
335
- if ( async_hook_fields [ kCheck ] > 0 &&
336
- ( typeof type !== 'string' || type . length <= 0 ) ) {
337
- throw new ERR_ASYNC_TYPE ( type ) ;
338
- }
339
-
340
320
// Short circuit all checks for the common case. Which is that no hooks have
341
321
// been set. Do this to remove performance impact for embedders (and core).
342
322
if ( async_hook_fields [ kInit ] === 0 )
343
323
return ;
344
324
345
- // This can run after the early return check b/c running this function
346
- // manually means that the embedder must have used getDefaultTriggerAsyncId().
347
325
if ( triggerAsyncId === null ) {
348
326
triggerAsyncId = getDefaultTriggerAsyncId ( ) ;
349
327
}
@@ -353,12 +331,6 @@ function emitInitScript(asyncId, type, triggerAsyncId, resource) {
353
331
354
332
355
333
function emitBeforeScript ( asyncId , triggerAsyncId ) {
356
- // Validate the ids. An id of -1 means it was never set and is visible on the
357
- // call graph. An id < -1 should never happen in any circumstance. Throw
358
- // on user calls because async state should still be recoverable.
359
- validateAsyncId ( asyncId , 'asyncId' ) ;
360
- validateAsyncId ( triggerAsyncId , 'triggerAsyncId' ) ;
361
-
362
334
pushAsyncIds ( asyncId , triggerAsyncId ) ;
363
335
364
336
if ( async_hook_fields [ kBefore ] > 0 )
@@ -367,8 +339,6 @@ function emitBeforeScript(asyncId, triggerAsyncId) {
367
339
368
340
369
341
function emitAfterScript ( asyncId ) {
370
- validateAsyncId ( asyncId , 'asyncId' ) ;
371
-
372
342
if ( async_hook_fields [ kAfter ] > 0 )
373
343
emitAfterNative ( asyncId ) ;
374
344
@@ -377,8 +347,6 @@ function emitAfterScript(asyncId) {
377
347
378
348
379
349
function emitDestroyScript ( asyncId ) {
380
- validateAsyncId ( asyncId , 'asyncId' ) ;
381
-
382
350
// Return early if there are no destroy callbacks, or invalid asyncId.
383
351
if ( async_hook_fields [ kDestroy ] === 0 || asyncId <= 0 )
384
352
return ;
@@ -418,8 +386,7 @@ function popAsyncIds(asyncId) {
418
386
const stackLength = async_hook_fields [ kStackLength ] ;
419
387
if ( stackLength === 0 ) return false ;
420
388
421
- if ( async_hook_fields [ kCheck ] > 0 &&
422
- async_id_fields [ kExecutionAsyncId ] !== asyncId ) {
389
+ if ( enabledHooksExist ( ) && async_id_fields [ kExecutionAsyncId ] !== asyncId ) {
423
390
// Do the same thing as the native code (i.e. crash hard).
424
391
return popAsyncIds_ ( asyncId ) ;
425
392
}
@@ -464,6 +431,7 @@ module.exports = {
464
431
getOrSetAsyncId,
465
432
getDefaultTriggerAsyncId,
466
433
defaultTriggerAsyncIdScope,
434
+ enabledHooksExist,
467
435
initHooksExist,
468
436
afterHooksExist,
469
437
destroyHooksExist,
0 commit comments