33const {
44 Error,
55 FunctionPrototypeBind,
6- NumberIsSafeInteger,
76 ObjectDefineProperty,
87 Symbol,
98} = primordials ;
109
11- const {
12- ERR_ASYNC_TYPE ,
13- ERR_INVALID_ASYNC_ID
14- } = require ( 'internal/errors' ) . codes ;
15-
1610const async_wrap = internalBinding ( 'async_wrap' ) ;
1711/* async_hook_fields is a Uint32Array wrapping the uint32_t array of
1812 * Environment::AsyncHooks::fields_[]. Each index tracks the number of active
@@ -117,15 +111,6 @@ function fatalError(e) {
117111}
118112
119113
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-
129114// Emit From Native //
130115
131116// Used by C++ to call all init() callbacks. Because some state can be setup
@@ -314,6 +299,9 @@ function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
314299 }
315300}
316301
302+ function enabledHooksExist ( ) {
303+ return async_hook_fields [ kCheck ] > 0 ;
304+ }
317305
318306function initHooksExist ( ) {
319307 return async_hook_fields [ kInit ] > 0 ;
@@ -329,21 +317,11 @@ function destroyHooksExist() {
329317
330318
331319function 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-
340320 // Short circuit all checks for the common case. Which is that no hooks have
341321 // been set. Do this to remove performance impact for embedders (and core).
342322 if ( async_hook_fields [ kInit ] === 0 )
343323 return ;
344324
345- // This can run after the early return check b/c running this function
346- // manually means that the embedder must have used getDefaultTriggerAsyncId().
347325 if ( triggerAsyncId === null ) {
348326 triggerAsyncId = getDefaultTriggerAsyncId ( ) ;
349327 }
@@ -353,12 +331,6 @@ function emitInitScript(asyncId, type, triggerAsyncId, resource) {
353331
354332
355333function 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-
362334 pushAsyncIds ( asyncId , triggerAsyncId ) ;
363335
364336 if ( async_hook_fields [ kBefore ] > 0 )
@@ -367,8 +339,6 @@ function emitBeforeScript(asyncId, triggerAsyncId) {
367339
368340
369341function emitAfterScript ( asyncId ) {
370- validateAsyncId ( asyncId , 'asyncId' ) ;
371-
372342 if ( async_hook_fields [ kAfter ] > 0 )
373343 emitAfterNative ( asyncId ) ;
374344
@@ -377,8 +347,6 @@ function emitAfterScript(asyncId) {
377347
378348
379349function emitDestroyScript ( asyncId ) {
380- validateAsyncId ( asyncId , 'asyncId' ) ;
381-
382350 // Return early if there are no destroy callbacks, or invalid asyncId.
383351 if ( async_hook_fields [ kDestroy ] === 0 || asyncId <= 0 )
384352 return ;
@@ -418,8 +386,7 @@ function popAsyncIds(asyncId) {
418386 const stackLength = async_hook_fields [ kStackLength ] ;
419387 if ( stackLength === 0 ) return false ;
420388
421- if ( async_hook_fields [ kCheck ] > 0 &&
422- async_id_fields [ kExecutionAsyncId ] !== asyncId ) {
389+ if ( enabledHooksExist ( ) && async_id_fields [ kExecutionAsyncId ] !== asyncId ) {
423390 // Do the same thing as the native code (i.e. crash hard).
424391 return popAsyncIds_ ( asyncId ) ;
425392 }
@@ -464,6 +431,7 @@ module.exports = {
464431 getOrSetAsyncId,
465432 getDefaultTriggerAsyncId,
466433 defaultTriggerAsyncIdScope,
434+ enabledHooksExist,
467435 initHooksExist,
468436 afterHooksExist,
469437 destroyHooksExist,
0 commit comments