diff --git a/doc/api/async_hooks.md b/doc/api/async_hooks.md index c3170f8162f49b..f8d50f66d4c813 100644 --- a/doc/api/async_hooks.md +++ b/doc/api/async_hooks.md @@ -758,6 +758,18 @@ const server = net.createServer((conn) => { Promise contexts may not get valid `triggerAsyncId`s by default. See the section on [promise execution tracking][]. +### `async_hooks.asyncWrapProviders` + + + +* Returns: A map of provider types to the corresponding numeric id. + This map contains all the event types that might be emitted by the `async_hooks.init()` event. + +This feature suppresses the deprecated usage of `process.binding('async_wrap').Providers`. +See: [DEP0111][] + ## Promise execution tracking By default, promise executions are not assigned `asyncId`s due to the relatively @@ -841,6 +853,7 @@ The documentation for this class has moved [`AsyncResource`][]. The documentation for this class has moved [`AsyncLocalStorage`][]. +[DEP0111]: deprecations.md#dep0111-processbinding [Hook Callbacks]: #hook-callbacks [PromiseHooks]: https://docs.google.com/document/d/1rda3yKGHimKIhg5YeoAmCOtyURgsbTH_qaYR79FELlk/edit [`AsyncLocalStorage`]: async_context.md#class-asynclocalstorage diff --git a/lib/async_hooks.js b/lib/async_hooks.js index 04135ec41c223b..af5a37b749d94b 100644 --- a/lib/async_hooks.js +++ b/lib/async_hooks.js @@ -11,6 +11,7 @@ const { ObjectIs, ReflectApply, Symbol, + ObjectFreeze, } = primordials; const { @@ -29,6 +30,7 @@ const internal_async_hooks = require('internal/async_hooks'); // resource gets gced. const { registerDestroyHook } = internal_async_hooks; const { + asyncWrap, executionAsyncId, triggerAsyncId, // Private API @@ -352,6 +354,7 @@ module.exports = { executionAsyncId, triggerAsyncId, executionAsyncResource, + asyncWrapProviders: ObjectFreeze({ __proto__: null, ...asyncWrap.Providers }), // Embedder API AsyncResource, }; diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js index 390453ca7b8aa9..c25ce5a9ae13f8 100644 --- a/lib/internal/async_hooks.js +++ b/lib/internal/async_hooks.js @@ -612,5 +612,8 @@ module.exports = { after: emitAfterNative, destroy: emitDestroyNative, promise_resolve: emitPromiseResolveNative + }, + asyncWrap: { + Providers: async_wrap.Providers, } }; diff --git a/test/async-hooks/test-async-wrap-providers.js b/test/async-hooks/test-async-wrap-providers.js new file mode 100644 index 00000000000000..fe2eecee755c09 --- /dev/null +++ b/test/async-hooks/test-async-wrap-providers.js @@ -0,0 +1,18 @@ +// Flags: --expose-internals +'use strict'; + +const common = require('../common'); +const { internalBinding } = require('internal/test/binding'); +const providers = internalBinding('async_wrap').Providers; +const assert = require('assert'); +const { asyncWrapProviders } = require('async_hooks'); + +assert.ok(typeof asyncWrapProviders === 'object'); +assert.deepStrictEqual(asyncWrapProviders, { __proto__: null, ...providers }); + +const providerKeys = Object.keys(asyncWrapProviders); +assert.throws(() => { + asyncWrapProviders[providerKeys[0]] = 'another value'; +}, common.expectsError({ + name: 'TypeError', +}), 'should not allow modify asyncWrap providers');