Skip to content

Commit a706342

Browse files
authored
src: add kNoBrowserGlobals flag for Environment
PR-URL: #40532 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent f34c0e0 commit a706342

File tree

9 files changed

+185
-142
lines changed

9 files changed

+185
-142
lines changed

configure.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,8 @@
703703
dest='no_browser_globals',
704704
default=None,
705705
help='do not export browser globals like setTimeout, console, etc. ' +
706-
'(This mode is not officially supported for regular applications)')
706+
'(This mode is deprecated and not officially supported for regular ' +
707+
'applications)')
707708

708709
parser.add_argument('--without-inspector',
709710
action='store_true',

lib/internal/bootstrap/browser.js

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
'use strict';
2+
3+
const {
4+
ObjectDefineProperty,
5+
globalThis,
6+
} = primordials;
7+
8+
const {
9+
defineOperation,
10+
exposeInterface,
11+
lazyDOMExceptionClass,
12+
} = require('internal/util');
13+
const config = internalBinding('config');
14+
15+
// Override global console from the one provided by the VM
16+
// to the one implemented by Node.js
17+
// https://console.spec.whatwg.org/#console-namespace
18+
exposeNamespace(globalThis, 'console',
19+
createGlobalConsole(globalThis.console));
20+
21+
const { URL, URLSearchParams } = require('internal/url');
22+
// https://url.spec.whatwg.org/#url
23+
exposeInterface(globalThis, 'URL', URL);
24+
// https://url.spec.whatwg.org/#urlsearchparams
25+
exposeInterface(globalThis, 'URLSearchParams', URLSearchParams);
26+
exposeGetterAndSetter(globalThis,
27+
'DOMException',
28+
lazyDOMExceptionClass,
29+
(value) => {
30+
exposeInterface(globalThis, 'DOMException', value);
31+
});
32+
33+
const {
34+
TextEncoder, TextDecoder
35+
} = require('internal/encoding');
36+
// https://encoding.spec.whatwg.org/#textencoder
37+
exposeInterface(globalThis, 'TextEncoder', TextEncoder);
38+
// https://encoding.spec.whatwg.org/#textdecoder
39+
exposeInterface(globalThis, 'TextDecoder', TextDecoder);
40+
41+
const {
42+
AbortController,
43+
AbortSignal,
44+
} = require('internal/abort_controller');
45+
exposeInterface(globalThis, 'AbortController', AbortController);
46+
exposeInterface(globalThis, 'AbortSignal', AbortSignal);
47+
48+
const {
49+
EventTarget,
50+
Event,
51+
} = require('internal/event_target');
52+
exposeInterface(globalThis, 'EventTarget', EventTarget);
53+
exposeInterface(globalThis, 'Event', Event);
54+
const {
55+
MessageChannel,
56+
MessagePort,
57+
MessageEvent,
58+
} = require('internal/worker/io');
59+
exposeInterface(globalThis, 'MessageChannel', MessageChannel);
60+
exposeInterface(globalThis, 'MessagePort', MessagePort);
61+
exposeInterface(globalThis, 'MessageEvent', MessageEvent);
62+
63+
// https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
64+
const timers = require('timers');
65+
defineOperation(globalThis, 'clearInterval', timers.clearInterval);
66+
defineOperation(globalThis, 'clearTimeout', timers.clearTimeout);
67+
defineOperation(globalThis, 'setInterval', timers.setInterval);
68+
defineOperation(globalThis, 'setTimeout', timers.setTimeout);
69+
70+
// https://www.w3.org/TR/hr-time-2/#the-performance-attribute
71+
defineReplacableAttribute(globalThis, 'performance',
72+
require('perf_hooks').performance);
73+
74+
function createGlobalConsole(consoleFromVM) {
75+
const consoleFromNode =
76+
require('internal/console/global');
77+
if (config.hasInspector) {
78+
const inspector = require('internal/util/inspector');
79+
// This will be exposed by `require('inspector').console` later.
80+
inspector.consoleFromVM = consoleFromVM;
81+
// TODO(joyeecheung): postpone this until the first time inspector
82+
// is activated.
83+
inspector.wrapConsole(consoleFromNode, consoleFromVM);
84+
const { setConsoleExtensionInstaller } = internalBinding('inspector');
85+
// Setup inspector command line API.
86+
setConsoleExtensionInstaller(inspector.installConsoleExtensions);
87+
}
88+
return consoleFromNode;
89+
}
90+
91+
// https://heycam.github.io/webidl/#es-namespaces
92+
function exposeNamespace(target, name, namespaceObject) {
93+
ObjectDefineProperty(target, name, {
94+
writable: true,
95+
enumerable: false,
96+
configurable: true,
97+
value: namespaceObject
98+
});
99+
}
100+
101+
function exposeGetterAndSetter(target, name, getter, setter = undefined) {
102+
ObjectDefineProperty(target, name, {
103+
enumerable: false,
104+
configurable: true,
105+
get: getter,
106+
set: setter,
107+
});
108+
}
109+
110+
// https://heycam.github.io/webidl/#Replaceable
111+
function defineReplacableAttribute(target, name, value) {
112+
ObjectDefineProperty(target, name, {
113+
writable: true,
114+
enumerable: true,
115+
configurable: true,
116+
value,
117+
});
118+
}

lib/internal/bootstrap/node.js

+16-137
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ const {
5656
} = primordials;
5757
const config = internalBinding('config');
5858
const internalTimers = require('internal/timers');
59-
const { deprecate, lazyDOMExceptionClass } = require('internal/util');
59+
const {
60+
defineOperation,
61+
deprecate,
62+
exposeInterface,
63+
} = require('internal/util');
6064

6165
setupProcessObject();
6266

@@ -205,79 +209,20 @@ const {
205209
queueMicrotask
206210
} = require('internal/process/task_queues');
207211

208-
if (!config.noBrowserGlobals) {
209-
// Override global console from the one provided by the VM
210-
// to the one implemented by Node.js
211-
// https://console.spec.whatwg.org/#console-namespace
212-
exposeNamespace(globalThis, 'console',
213-
createGlobalConsole(globalThis.console));
214-
215-
const { URL, URLSearchParams } = require('internal/url');
216-
// https://url.spec.whatwg.org/#url
217-
exposeInterface(globalThis, 'URL', URL);
218-
// https://url.spec.whatwg.org/#urlsearchparams
219-
exposeInterface(globalThis, 'URLSearchParams', URLSearchParams);
220-
exposeGetterAndSetter(globalThis,
221-
'DOMException',
222-
lazyDOMExceptionClass,
223-
(value) => {
224-
exposeInterface(globalThis, 'DOMException', value);
225-
});
212+
// Non-standard extensions:
213+
const { BroadcastChannel } = require('internal/worker/io');
214+
exposeInterface(globalThis, 'BroadcastChannel', BroadcastChannel);
226215

227-
const {
228-
TextEncoder, TextDecoder
229-
} = require('internal/encoding');
230-
// https://encoding.spec.whatwg.org/#textencoder
231-
exposeInterface(globalThis, 'TextEncoder', TextEncoder);
232-
// https://encoding.spec.whatwg.org/#textdecoder
233-
exposeInterface(globalThis, 'TextDecoder', TextDecoder);
216+
defineOperation(globalThis, 'queueMicrotask', queueMicrotask);
234217

235-
const {
236-
AbortController,
237-
AbortSignal,
238-
} = require('internal/abort_controller');
239-
exposeInterface(globalThis, 'AbortController', AbortController);
240-
exposeInterface(globalThis, 'AbortSignal', AbortSignal);
218+
const timers = require('timers');
219+
defineOperation(globalThis, 'clearImmediate', timers.clearImmediate);
220+
defineOperation(globalThis, 'setImmediate', timers.setImmediate);
241221

242-
const {
243-
EventTarget,
244-
Event,
245-
} = require('internal/event_target');
246-
exposeInterface(globalThis, 'EventTarget', EventTarget);
247-
exposeInterface(globalThis, 'Event', Event);
248-
const {
249-
MessageChannel,
250-
MessagePort,
251-
MessageEvent,
252-
BroadcastChannel,
253-
} = require('internal/worker/io');
254-
exposeInterface(globalThis, 'MessageChannel', MessageChannel);
255-
exposeInterface(globalThis, 'MessagePort', MessagePort);
256-
exposeInterface(globalThis, 'MessageEvent', MessageEvent);
257-
exposeInterface(globalThis, 'BroadcastChannel', BroadcastChannel);
258-
259-
// https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
260-
const timers = require('timers');
261-
defineOperation(globalThis, 'clearInterval', timers.clearInterval);
262-
defineOperation(globalThis, 'clearTimeout', timers.clearTimeout);
263-
defineOperation(globalThis, 'setInterval', timers.setInterval);
264-
defineOperation(globalThis, 'setTimeout', timers.setTimeout);
265-
266-
defineOperation(globalThis, 'queueMicrotask', queueMicrotask);
267-
268-
// https://www.w3.org/TR/hr-time-2/#the-performance-attribute
269-
defineReplacableAttribute(globalThis, 'performance',
270-
require('perf_hooks').performance);
271-
272-
// Non-standard extensions:
273-
defineOperation(globalThis, 'clearImmediate', timers.clearImmediate);
274-
defineOperation(globalThis, 'setImmediate', timers.setImmediate);
275-
276-
const {
277-
structuredClone,
278-
} = require('internal/structured_clone');
279-
defineOperation(globalThis, 'structuredClone', structuredClone);
280-
}
222+
const {
223+
structuredClone,
224+
} = require('internal/structured_clone');
225+
defineOperation(globalThis, 'structuredClone', structuredClone);
281226

282227
// Set the per-Environment callback that will be called
283228
// when the TrackingTraceStateObserver updates trace state.
@@ -483,69 +428,3 @@ function setupBuffer() {
483428
},
484429
});
485430
}
486-
487-
function createGlobalConsole(consoleFromVM) {
488-
const consoleFromNode =
489-
require('internal/console/global');
490-
if (config.hasInspector) {
491-
const inspector = require('internal/util/inspector');
492-
// This will be exposed by `require('inspector').console` later.
493-
inspector.consoleFromVM = consoleFromVM;
494-
// TODO(joyeecheung): postpone this until the first time inspector
495-
// is activated.
496-
inspector.wrapConsole(consoleFromNode, consoleFromVM);
497-
const { setConsoleExtensionInstaller } = internalBinding('inspector');
498-
// Setup inspector command line API.
499-
setConsoleExtensionInstaller(inspector.installConsoleExtensions);
500-
}
501-
return consoleFromNode;
502-
}
503-
504-
// https://heycam.github.io/webidl/#es-namespaces
505-
function exposeNamespace(target, name, namespaceObject) {
506-
ObjectDefineProperty(target, name, {
507-
writable: true,
508-
enumerable: false,
509-
configurable: true,
510-
value: namespaceObject
511-
});
512-
}
513-
514-
// https://heycam.github.io/webidl/#es-interfaces
515-
function exposeInterface(target, name, interfaceObject) {
516-
ObjectDefineProperty(target, name, {
517-
writable: true,
518-
enumerable: false,
519-
configurable: true,
520-
value: interfaceObject
521-
});
522-
}
523-
524-
function exposeGetterAndSetter(target, name, getter, setter = undefined) {
525-
ObjectDefineProperty(target, name, {
526-
enumerable: false,
527-
configurable: true,
528-
get: getter,
529-
set: setter,
530-
});
531-
}
532-
533-
// https://heycam.github.io/webidl/#define-the-operations
534-
function defineOperation(target, name, method) {
535-
ObjectDefineProperty(target, name, {
536-
writable: true,
537-
enumerable: true,
538-
configurable: true,
539-
value: method
540-
});
541-
}
542-
543-
// https://heycam.github.io/webidl/#Replaceable
544-
function defineReplacableAttribute(target, name, value) {
545-
ObjectDefineProperty(target, name, {
546-
writable: true,
547-
enumerable: true,
548-
configurable: true,
549-
value,
550-
});
551-
}

lib/internal/util.js

+22
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,26 @@ function createDeferredPromise() {
466466
return { promise, resolve, reject };
467467
}
468468

469+
// https://heycam.github.io/webidl/#define-the-operations
470+
function defineOperation(target, name, method) {
471+
ObjectDefineProperty(target, name, {
472+
writable: true,
473+
enumerable: true,
474+
configurable: true,
475+
value: method
476+
});
477+
}
478+
479+
// https://heycam.github.io/webidl/#es-interfaces
480+
function exposeInterface(target, name, interfaceObject) {
481+
ObjectDefineProperty(target, name, {
482+
writable: true,
483+
enumerable: false,
484+
configurable: true,
485+
value: interfaceObject
486+
});
487+
}
488+
469489
let _DOMException;
470490
const lazyDOMExceptionClass = () => {
471491
_DOMException ??= internalBinding('messaging').DOMException;
@@ -484,8 +504,10 @@ module.exports = {
484504
createClassWrapper,
485505
createDeferredPromise,
486506
decorateErrorStack,
507+
defineOperation,
487508
deprecate,
488509
emitExperimentalWarning,
510+
exposeInterface,
489511
filterDuplicateStrings,
490512
getConstructorOf,
491513
getSystemErrorMap,

src/env-inl.h

+9
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,15 @@ inline bool Environment::no_global_search_paths() const {
882882
!options_->global_search_paths;
883883
}
884884

885+
inline bool Environment::no_browser_globals() const {
886+
// configure --no-browser-globals
887+
#ifdef NODE_NO_BROWSER_GLOBALS
888+
return true;
889+
#else
890+
return flags_ & EnvironmentFlags::kNoBrowserGlobals;
891+
#endif
892+
}
893+
885894
bool Environment::filehandle_close_warning() const {
886895
return emit_filehandle_warning_;
887896
}

src/env.h

+1
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,7 @@ class Environment : public MemoryRetainer {
12121212
inline bool tracks_unmanaged_fds() const;
12131213
inline bool hide_console_windows() const;
12141214
inline bool no_global_search_paths() const;
1215+
inline bool no_browser_globals() const;
12151216
inline uint64_t thread_id() const;
12161217
inline worker::Worker* worker_context() const;
12171218
Environment* worker_parent_env() const;

0 commit comments

Comments
 (0)