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]); +}