Skip to content

Commit

Permalink
lib: make process.binding('util') return only type checkers
Browse files Browse the repository at this point in the history
Ref: #37485 (review)
Ref: #37787

PR-URL: #37819
Refs: #37787
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
  • Loading branch information
addaleax committed Mar 27, 2021
1 parent 924238d commit fe73e4d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/internal/bootstrap/loaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
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]);
}

0 comments on commit fe73e4d

Please sign in to comment.