From 078a0c996f659a506ba5639cdcade8f675a8c6fe Mon Sep 17 00:00:00 2001 From: Rafael Gonzaga Date: Fri, 12 Nov 2021 18:48:34 -0300 Subject: [PATCH] async_hooks: expose async_wrap providers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit docs: add asyncWrapProviders api doc tests(async_hooks): use internalBinding for comparisson fix(test-async-wrap): lint error docs: use REPLACEME for asyncWrapProviders update: use freeze and copy for asyncWrapProviders update(async_hooks): use primordials on asyncWrapProviders fix: use common to expect error docs(asyncWrapProviders): rephrase return type fix: lint md fix: lint md docs(async_hooks): typo Co-authored-by: Stephen Belanger update(asyncWrapProviders): add __proto__ as null Co-authored-by: Simone Busoli Co-authored-by: Michaël Zasso test: adjust __proto__ assertion docs: add DEP0111 link PR-URL: https://github.com/nodejs/node/pull/40760 Reviewed-By: Matteo Collina Reviewed-By: Gerhard Stöbich Reviewed-By: Stephen Belanger --- doc/api/async_hooks.md | 13 +++++++++++++ lib/async_hooks.js | 3 +++ lib/internal/async_hooks.js | 3 +++ test/async-hooks/test-async-wrap-providers.js | 18 ++++++++++++++++++ 4 files changed, 37 insertions(+) create mode 100644 test/async-hooks/test-async-wrap-providers.js 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');