diff --git a/lib/internal/bootstrap/loaders.js b/lib/internal/bootstrap/loaders.js index 260e3b2d47983d..30d9edf01e021b 100644 --- a/lib/internal/bootstrap/loaders.js +++ b/lib/internal/bootstrap/loaders.js @@ -110,6 +110,10 @@ const runtimeDeprecatedList = new SafeSet([ 'async_wrap', ]); +const legacyWrapperList = new SafeSet([ + 'util', +]); + // Set up process.binding() and process._linkedBinding(). { const bindingObj = ObjectCreate(null); @@ -126,6 +130,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..e6073fc1ce2742 --- /dev/null +++ b/lib/internal/legacy/processbinding.js @@ -0,0 +1,23 @@ +'use strict'; +const { + ArrayPrototypeFilter, + ArrayPrototypeIncludes, + ObjectFromEntries, + ObjectEntries, +} = primordials; +const { types } = require('util'); + +module.exports = { + util() { + return ObjectFromEntries(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 bbb9c6d6cdaebf..db05ed3209dfad 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..ac3e0d8d2875f7 --- /dev/null +++ b/test/parallel/test-process-binding-util.js @@ -0,0 +1,18 @@ +'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]); +}