Skip to content

Commit

Permalink
async_hooks: expose async_wrap providers
Browse files Browse the repository at this point in the history
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 <admin@stephenbelanger.com>

update(asyncWrapProviders): add __proto__ as null

Co-authored-by: Simone Busoli <simone.busoli@gmail.com>
Co-authored-by: Michaël Zasso <targos@protonmail.com>

test: adjust __proto__ assertion

docs: add DEP0111 link

PR-URL: #40760
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
  • Loading branch information
RafaelGSS authored and danielleadams committed Feb 1, 2022
1 parent cebf403 commit 078a0c9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
13 changes: 13 additions & 0 deletions doc/api/async_hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
<!-- YAML
added: REPLACEME
-->
* 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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
ObjectIs,
ReflectApply,
Symbol,
ObjectFreeze,
} = primordials;

const {
Expand All @@ -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
Expand Down Expand Up @@ -352,6 +354,7 @@ module.exports = {
executionAsyncId,
triggerAsyncId,
executionAsyncResource,
asyncWrapProviders: ObjectFreeze({ __proto__: null, ...asyncWrap.Providers }),
// Embedder API
AsyncResource,
};
3 changes: 3 additions & 0 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,5 +612,8 @@ module.exports = {
after: emitAfterNative,
destroy: emitDestroyNative,
promise_resolve: emitPromiseResolveNative
},
asyncWrap: {
Providers: async_wrap.Providers,
}
};
18 changes: 18 additions & 0 deletions test/async-hooks/test-async-wrap-providers.js
Original file line number Diff line number Diff line change
@@ -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');

0 comments on commit 078a0c9

Please sign in to comment.