From fe73e4d578f5e39c08e559c3cb5f031b84061933 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 19 Mar 2021 20:05:43 +0100 Subject: [PATCH] lib: make process.binding('util') return only type checkers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ref: https://github.com/nodejs/node/pull/37485#pullrequestreview-600060802 Ref: https://github.com/nodejs/node/pull/37787 PR-URL: https://github.com/nodejs/node/pull/37819 Refs: https://github.com/nodejs/node/pull/37787 Reviewed-By: James M Snell Reviewed-By: Benjamin Gruenbaum Reviewed-By: Colin Ihrig Reviewed-By: Rich Trott Reviewed-By: Antoine du Hamel Reviewed-By: Michaƫl Zasso Reviewed-By: Anto Aravinth --- lib/internal/bootstrap/loaders.js | 7 +++++ lib/internal/legacy/processbinding.js | 36 ++++++++++++++++++++++ node.gyp | 1 + test/parallel/test-process-binding-util.js | 30 ++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 lib/internal/legacy/processbinding.js create mode 100644 test/parallel/test-process-binding-util.js diff --git a/lib/internal/bootstrap/loaders.js b/lib/internal/bootstrap/loaders.js index ab589499d09722..4417a94181410b 100644 --- a/lib/internal/bootstrap/loaders.js +++ b/lib/internal/bootstrap/loaders.js @@ -113,6 +113,10 @@ const runtimeDeprecatedList = new SafeSet([ 'v8', ]); +const legacyWrapperList = new SafeSet([ + 'util', +]); + // Set up process.binding() and process._linkedBinding(). { const bindingObj = ObjectCreate(null); @@ -129,6 +133,9 @@ const runtimeDeprecatedList = new SafeSet([ 'DeprecationWarning', 'DEP0111'); } + if (legacyWrapperList.has(module)) { + return nativeModuleRequire('internal/legacy/processbinding')[module](); + } return internalBinding(module); } // eslint-disable-next-line no-restricted-syntax diff --git a/lib/internal/legacy/processbinding.js b/lib/internal/legacy/processbinding.js new file mode 100644 index 00000000000000..3d48e3c9882e58 --- /dev/null +++ b/lib/internal/legacy/processbinding.js @@ -0,0 +1,36 @@ +'use strict'; +const { + ArrayPrototypeFilter, + ArrayPrototypeIncludes, + ObjectFromEntries, + ObjectEntries, + SafeArrayIterator, +} = primordials; +const { types } = require('util'); + +module.exports = { + util() { + return ObjectFromEntries(new SafeArrayIterator(ArrayPrototypeFilter( + ObjectEntries(types), + ({ 0: key }) => { + return ArrayPrototypeIncludes([ + 'isArrayBuffer', + 'isArrayBufferView', + 'isAsyncFunction', + 'isDataView', + 'isDate', + 'isExternal', + 'isMap', + 'isMapIterator', + 'isNativeError', + 'isPromise', + 'isRegExp', + 'isSet', + 'isSetIterator', + 'isTypedArray', + 'isUint8Array', + 'isAnyArrayBuffer', + ], key); + }))); + } +}; diff --git a/node.gyp b/node.gyp index 5389383f8ddc9c..b22d8f515d5ec1 100644 --- a/node.gyp +++ b/node.gyp @@ -168,6 +168,7 @@ 'lib/internal/idna.js', 'lib/internal/inspector_async_hook.js', 'lib/internal/js_stream_socket.js', + 'lib/internal/legacy/processbinding.js', 'lib/internal/linkedlist.js', 'lib/internal/main/check_syntax.js', 'lib/internal/main/eval_string.js', diff --git a/test/parallel/test-process-binding-util.js b/test/parallel/test-process-binding-util.js new file mode 100644 index 00000000000000..d1e602de681cfa --- /dev/null +++ b/test/parallel/test-process-binding-util.js @@ -0,0 +1,30 @@ +'use strict'; +require('../common'); +const assert = require('assert'); +const util = require('util'); + +const utilBinding = process.binding('util'); +assert.deepStrictEqual( + Object.keys(utilBinding).sort(), + [ + 'isAnyArrayBuffer', + 'isArrayBuffer', + 'isArrayBufferView', + 'isAsyncFunction', + 'isDataView', + 'isDate', + 'isExternal', + 'isMap', + 'isMapIterator', + 'isNativeError', + 'isPromise', + 'isRegExp', + 'isSet', + 'isSetIterator', + 'isTypedArray', + 'isUint8Array', + ]); + +for (const k of Object.keys(utilBinding)) { + assert.strictEqual(utilBinding[k], util.types[k]); +}