Skip to content

Commit fe73e4d

Browse files
committed
lib: make process.binding('util') return only type checkers
Ref: #37485 (review) Ref: #37787 PR-URL: #37819 Refs: #37787 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Anto Aravinth <[email protected]>
1 parent 924238d commit fe73e4d

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

Diff for: lib/internal/bootstrap/loaders.js

+7
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ const runtimeDeprecatedList = new SafeSet([
113113
'v8',
114114
]);
115115

116+
const legacyWrapperList = new SafeSet([
117+
'util',
118+
]);
119+
116120
// Set up process.binding() and process._linkedBinding().
117121
{
118122
const bindingObj = ObjectCreate(null);
@@ -129,6 +133,9 @@ const runtimeDeprecatedList = new SafeSet([
129133
'DeprecationWarning',
130134
'DEP0111');
131135
}
136+
if (legacyWrapperList.has(module)) {
137+
return nativeModuleRequire('internal/legacy/processbinding')[module]();
138+
}
132139
return internalBinding(module);
133140
}
134141
// eslint-disable-next-line no-restricted-syntax

Diff for: lib/internal/legacy/processbinding.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
const {
3+
ArrayPrototypeFilter,
4+
ArrayPrototypeIncludes,
5+
ObjectFromEntries,
6+
ObjectEntries,
7+
SafeArrayIterator,
8+
} = primordials;
9+
const { types } = require('util');
10+
11+
module.exports = {
12+
util() {
13+
return ObjectFromEntries(new SafeArrayIterator(ArrayPrototypeFilter(
14+
ObjectEntries(types),
15+
({ 0: key }) => {
16+
return ArrayPrototypeIncludes([
17+
'isArrayBuffer',
18+
'isArrayBufferView',
19+
'isAsyncFunction',
20+
'isDataView',
21+
'isDate',
22+
'isExternal',
23+
'isMap',
24+
'isMapIterator',
25+
'isNativeError',
26+
'isPromise',
27+
'isRegExp',
28+
'isSet',
29+
'isSetIterator',
30+
'isTypedArray',
31+
'isUint8Array',
32+
'isAnyArrayBuffer',
33+
], key);
34+
})));
35+
}
36+
};

Diff for: node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
'lib/internal/idna.js',
169169
'lib/internal/inspector_async_hook.js',
170170
'lib/internal/js_stream_socket.js',
171+
'lib/internal/legacy/processbinding.js',
171172
'lib/internal/linkedlist.js',
172173
'lib/internal/main/check_syntax.js',
173174
'lib/internal/main/eval_string.js',

Diff for: test/parallel/test-process-binding-util.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const util = require('util');
5+
6+
const utilBinding = process.binding('util');
7+
assert.deepStrictEqual(
8+
Object.keys(utilBinding).sort(),
9+
[
10+
'isAnyArrayBuffer',
11+
'isArrayBuffer',
12+
'isArrayBufferView',
13+
'isAsyncFunction',
14+
'isDataView',
15+
'isDate',
16+
'isExternal',
17+
'isMap',
18+
'isMapIterator',
19+
'isNativeError',
20+
'isPromise',
21+
'isRegExp',
22+
'isSet',
23+
'isSetIterator',
24+
'isTypedArray',
25+
'isUint8Array',
26+
]);
27+
28+
for (const k of Object.keys(utilBinding)) {
29+
assert.strictEqual(utilBinding[k], util.types[k]);
30+
}

0 commit comments

Comments
 (0)