Skip to content

Commit 6a6b036

Browse files
joyeecheungBridgeAR
authored andcommitted
lib: move internalBinding whitelisting into loaders.js
Instead of setting the internalBinding white list in node.js and wrapping process.binding twice, put it directly in loaders.js where the user land process.binding is defined. PR-URL: #24088 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent e4bcb97 commit 6a6b036

File tree

2 files changed

+48
-41
lines changed

2 files changed

+48
-41
lines changed

lib/internal/bootstrap/loaders.js

+48
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,52 @@
7070
writable: false
7171
});
7272

73+
// internalBindingWhitelist contains the name of internalBinding modules
74+
// that are whitelisted for access via process.binding()... this is used
75+
// to provide a transition path for modules that are being moved over to
76+
// internalBinding.
77+
const internalBindingWhitelist = [
78+
'cares_wrap',
79+
'fs_event_wrap',
80+
'icu',
81+
'udp_wrap',
82+
'uv',
83+
'pipe_wrap',
84+
'http_parser',
85+
'process_wrap',
86+
'v8',
87+
'tty_wrap',
88+
'stream_wrap',
89+
'signal_wrap',
90+
'crypto',
91+
'contextify',
92+
'tcp_wrap',
93+
'tls_wrap',
94+
'util',
95+
'async_wrap',
96+
'url',
97+
'spawn_sync',
98+
'js_stream',
99+
'zlib',
100+
'buffer',
101+
'natives',
102+
'constants'
103+
];
104+
// We will use a lazy loaded SafeSet in internalBindingWhitelistHas
105+
// for checking existence in this list.
106+
let internalBindingWhitelistSet;
107+
73108
// Set up process.binding() and process._linkedBinding()
74109
{
75110
const bindingObj = ObjectCreate(null);
76111

77112
process.binding = function binding(module) {
78113
module = String(module);
114+
// Deprecated specific process.binding() modules, but not all, allow
115+
// selective fallback to internalBinding for the deprecated ones.
116+
if (internalBindingWhitelistHas(module)) {
117+
return internalBinding(module);
118+
}
79119
let mod = bindingObj[module];
80120
if (typeof mod !== 'object') {
81121
mod = bindingObj[module] = getBinding(module);
@@ -376,6 +416,14 @@
376416
NativeModule.require('internal/process/coverage').setup();
377417
}
378418

419+
function internalBindingWhitelistHas(name) {
420+
if (!internalBindingWhitelistSet) {
421+
const { SafeSet } = NativeModule.require('internal/safe_globals');
422+
internalBindingWhitelistSet = new SafeSet(internalBindingWhitelist);
423+
}
424+
return internalBindingWhitelistSet.has(name);
425+
}
426+
379427
// This will be passed to the bootstrapNodeJSCore function in
380428
// bootstrap/node.js.
381429
return loaderExports;

lib/internal/bootstrap/node.js

-41
Original file line numberDiff line numberDiff line change
@@ -384,47 +384,6 @@
384384
for (var i = 0; i < arguments.length; i++)
385385
this.push(arguments[i]);
386386
}
387-
388-
// Deprecated specific process.binding() modules, but not all, allow
389-
// selective fallback to internalBinding for the deprecated ones.
390-
const { SafeSet } = NativeModule.require('internal/safe_globals');
391-
const processBinding = process.binding;
392-
// internalBindingWhitelist contains the name of internalBinding modules
393-
// that are whitelisted for access via process.binding()... this is used
394-
// to provide a transition path for modules that are being moved over to
395-
// internalBinding.
396-
const internalBindingWhitelist =
397-
new SafeSet([
398-
'cares_wrap',
399-
'fs_event_wrap',
400-
'icu',
401-
'udp_wrap',
402-
'uv',
403-
'pipe_wrap',
404-
'http_parser',
405-
'process_wrap',
406-
'v8',
407-
'tty_wrap',
408-
'stream_wrap',
409-
'signal_wrap',
410-
'crypto',
411-
'contextify',
412-
'tcp_wrap',
413-
'tls_wrap',
414-
'util',
415-
'async_wrap',
416-
'url',
417-
'spawn_sync',
418-
'js_stream',
419-
'zlib',
420-
'buffer',
421-
'natives',
422-
'constants']);
423-
process.binding = function binding(name) {
424-
return internalBindingWhitelist.has(name) ?
425-
internalBinding(name) :
426-
processBinding(name);
427-
};
428387
}
429388

430389
function setupGlobalVariables() {

0 commit comments

Comments
 (0)