Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: make process.binding('util') return only type checkers #37819

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/internal/bootstrap/loaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -126,6 +130,9 @@ const runtimeDeprecatedList = new SafeSet([
'DeprecationWarning',
'DEP0111');
}
if (legacyWrapperList.has(module)) {
return nativeModuleRequire('internal/legacy/processbinding')[module]();
}
Comment on lines +133 to +135
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that due to the way this is currently implemented, process.binding("util") will now return a new object every time it’s called.


I’m adding wrapper caching in #38132.

return internalBinding(module);
}
// eslint-disable-next-line no-restricted-syntax
Expand Down
36 changes: 36 additions & 0 deletions lib/internal/legacy/processbinding.js
Original file line number Diff line number Diff line change
@@ -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);
})));
}
};
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
30 changes: 30 additions & 0 deletions test/parallel/test-process-binding-util.js
Original file line number Diff line number Diff line change
@@ -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]);
}