From 9fafb0a09024dbfe434c01c413dbd1c7857a3dbe Mon Sep 17 00:00:00 2001 From: James M Snell Date: Thu, 2 Feb 2023 18:02:19 -0800 Subject: [PATCH] async_hooks: deprecate the AsyncResource.bind asyncResource property MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Runtime-deprecates the `asyncResource` property that is attached to the wrapper function returned by `asyncResource.bind()`. This property is not expected to align with the equivalent `asyncContext.wrap()` API in the proposed TC39 standard. PR-URL: https://github.com/nodejs/node/pull/46432 Reviewed-By: Chengzhong Wu Reviewed-By: Stephen Belanger Reviewed-By: Gerhard Stöbich Reviewed-By: Michaël Zasso Reviewed-By: Matteo Collina --- doc/api/async_context.md | 16 ++++++++++------ doc/api/deprecations.md | 14 ++++++++++++++ lib/async_hooks.js | 14 +++++++++++--- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/doc/api/async_context.md b/doc/api/async_context.md index 94af6b016b1e74..c3f287e690955c 100644 --- a/doc/api/async_context.md +++ b/doc/api/async_context.md @@ -455,6 +455,11 @@ added: - v14.8.0 - v12.19.0 changes: + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/46432 + description: The `asyncResource` property added to the bound function + has been deprecated and will be removed in a future + version. - version: - v17.8.0 - v16.15.0 @@ -473,9 +478,6 @@ changes: Binds the given function to the current execution context. -The returned function will have an `asyncResource` property referencing -the `AsyncResource` to which the function is bound. - ### `asyncResource.bind(fn[, thisArg])` + +Type: Runtime + +In a future version of Node.js, the `asyncResource` property will no longer +be added when a function is bound to an `AsyncResource`. + [NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf [RFC 6066]: https://tools.ietf.org/html/rfc6066#section-3 [RFC 8247 Section 2.4]: https://www.rfc-editor.org/rfc/rfc8247#section-2.4 diff --git a/lib/async_hooks.js b/lib/async_hooks.js index 525b69acbfc652..23efdfabdb8c6e 100644 --- a/lib/async_hooks.js +++ b/lib/async_hooks.js @@ -20,7 +20,10 @@ const { ERR_ASYNC_TYPE, ERR_INVALID_ASYNC_ID } = require('internal/errors').codes; -const { kEmptyObject } = require('internal/util'); +const { + deprecate, + kEmptyObject, +} = require('internal/util'); const { validateFunction, validateString, @@ -237,6 +240,7 @@ class AsyncResource { } else { bound = FunctionPrototypeBind(this.runInAsyncScope, this, fn, thisArg); } + let self = this; ObjectDefineProperties(bound, { 'length': { __proto__: null, @@ -249,8 +253,12 @@ class AsyncResource { __proto__: null, configurable: true, enumerable: true, - value: this, - writable: true, + get: deprecate(function() { + return self; + }, 'The asyncResource property on bound functions is deprecated', 'DEP0172'), + set: deprecate(function(val) { + self = val; + }, 'The asyncResource property on bound functions is deprecated', 'DEP0172'), } }); return bound;