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
  • Loading branch information
addaleax committed Mar 19, 2021
1 parent eaf0f0f commit d77751e
Show file tree
Hide file tree
Showing 4 changed files with 49 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 @@ -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]();
}
return internalBinding(module);
}
// eslint-disable-next-line no-restricted-syntax
Expand Down
23 changes: 23 additions & 0 deletions lib/internal/legacy/processbinding.js
Original file line number Diff line number Diff line change
@@ -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);
}));
}
};
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
18 changes: 18 additions & 0 deletions test/parallel/test-process-binding-util.js
Original file line number Diff line number Diff line change
@@ -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]);
}

0 comments on commit d77751e

Please sign in to comment.