From be3205ae2482bf122c6e195467fc619f21498fb6 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Fri, 17 Nov 2023 19:16:55 +0100 Subject: [PATCH] lib: streamline process.binding() handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Make processBindingAllowList a separate list from runtimeDeprecatedList and legacyWrapperList instead of being an umbrella one, so it's easier to see the stages the bindings are in. - Cache process.binding() results so we don't need to mutate runtimeDeprecatedList. PR-URL: https://github.com/nodejs/node/pull/50773 Reviewed-By: Vinícius Lourenço Claro Cardoso Reviewed-By: James M Snell --- lib/internal/bootstrap/realm.js | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/lib/internal/bootstrap/realm.js b/lib/internal/bootstrap/realm.js index 6034af9a36003c..93a939cc7e4645 100644 --- a/lib/internal/bootstrap/realm.js +++ b/lib/internal/bootstrap/realm.js @@ -87,34 +87,26 @@ ObjectDefineProperty(process, 'moduleLoadList', { // more, we just implement them as legacy wrappers instead. See the // legacyWrapperList. const processBindingAllowList = new SafeSet([ - 'async_wrap', 'buffer', 'cares_wrap', 'config', 'constants', 'contextify', - 'crypto', 'fs', 'fs_event_wrap', - 'http_parser', 'icu', 'inspector', 'js_stream', - 'natives', 'os', 'pipe_wrap', 'process_wrap', - 'signal_wrap', 'spawn_sync', 'stream_wrap', 'tcp_wrap', 'tls_wrap', 'tty_wrap', 'udp_wrap', - 'url', - 'util', 'uv', - 'v8', 'zlib', ]); @@ -148,19 +140,23 @@ const experimentalModuleList = new SafeSet(); process.binding = function binding(module) { module = String(module); + const mod = bindingObj[module]; + if (typeof mod === 'object') { + return mod; + } // Deprecated specific process.binding() modules, but not all, allow // selective fallback to internalBinding for the deprecated ones. + if (runtimeDeprecatedList.has(module)) { + process.emitWarning( + `Access to process.binding('${module}') is deprecated.`, + 'DeprecationWarning', + 'DEP0111'); + return internalBinding(module); + } + if (legacyWrapperList.has(module)) { + return requireBuiltin('internal/legacy/processbinding')[module](); + } if (processBindingAllowList.has(module)) { - if (runtimeDeprecatedList.has(module)) { - runtimeDeprecatedList.delete(module); - process.emitWarning( - `Access to process.binding('${module}') is deprecated.`, - 'DeprecationWarning', - 'DEP0111'); - } - if (legacyWrapperList.has(module)) { - return requireBuiltin('internal/legacy/processbinding')[module](); - } return internalBinding(module); } // eslint-disable-next-line no-restricted-syntax