Skip to content

Commit bba27a1

Browse files
committed
process: use common operations to define browser globals
Extracts: - `exposeNamespace`: https://heycam.github.io/webidl/#es-namespaces - `exposeInterface`: https://heycam.github.io/webidl/#es-interfaces - `defineOperation`: https://heycam.github.io/webidl/#define-the-operations into functions to define browser globals. PR-URL: #26230 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 79a3348 commit bba27a1

File tree

1 file changed

+59
-61
lines changed

1 file changed

+59
-61
lines changed

lib/internal/bootstrap/node.js

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,34 @@ if (config.hasInspector) {
182182

183183
const browserGlobals = !process._noBrowserGlobals;
184184
if (browserGlobals) {
185-
setupGlobalTimeouts();
186-
setupGlobalConsole();
187-
setupGlobalURL();
188-
setupGlobalEncoding();
185+
// Override global console from the one provided by the VM
186+
// to the one implemented by Node.js
187+
// https://console.spec.whatwg.org/#console-namespace
188+
exposeNamespace(global, 'console', createGlobalConsole(global.console));
189+
190+
const { URL, URLSearchParams } = NativeModule.require('internal/url');
191+
// https://url.spec.whatwg.org/#url
192+
exposeInterface(global, 'URL', URL);
193+
// https://url.spec.whatwg.org/#urlsearchparams
194+
exposeInterface(global, 'URLSearchParams', URLSearchParams);
195+
196+
const { TextEncoder, TextDecoder } = NativeModule.require('util');
197+
// https://encoding.spec.whatwg.org/#textencoder
198+
exposeInterface(global, 'TextEncoder', TextEncoder);
199+
// https://encoding.spec.whatwg.org/#textdecoder
200+
exposeInterface(global, 'TextDecoder', TextDecoder);
201+
202+
// https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
203+
const timers = NativeModule.require('timers');
204+
defineOperation(global, 'clearInterval', timers.clearInterval);
205+
defineOperation(global, 'clearTimeout', timers.clearTimeout);
206+
defineOperation(global, 'setInterval', timers.setInterval);
207+
defineOperation(global, 'setTimeout', timers.setTimeout);
189208
setupQueueMicrotask();
209+
210+
// Non-standard extensions:
211+
defineOperation(global, 'clearImmediate', timers.clearImmediate);
212+
defineOperation(global, 'setImmediate', timers.setImmediate);
190213
}
191214

192215
setupDOMException();
@@ -376,29 +399,9 @@ function setupBuffer() {
376399
});
377400
}
378401

379-
function setupGlobalTimeouts() {
380-
const timers = NativeModule.require('timers');
381-
global.clearImmediate = timers.clearImmediate;
382-
global.clearInterval = timers.clearInterval;
383-
global.clearTimeout = timers.clearTimeout;
384-
global.setImmediate = timers.setImmediate;
385-
global.setInterval = timers.setInterval;
386-
global.setTimeout = timers.setTimeout;
387-
}
388-
389-
function setupGlobalConsole() {
390-
const consoleFromVM = global.console;
402+
function createGlobalConsole(consoleFromVM) {
391403
const consoleFromNode =
392404
NativeModule.require('internal/console/global');
393-
// Override global console from the one provided by the VM
394-
// to the one implemented by Node.js
395-
Object.defineProperty(global, 'console', {
396-
configurable: true,
397-
enumerable: false,
398-
value: consoleFromNode,
399-
writable: true
400-
});
401-
402405
if (config.hasInspector) {
403406
const inspector = NativeModule.require('internal/util/inspector');
404407
// This will be exposed by `require('inspector').console` later.
@@ -410,42 +413,7 @@ function setupGlobalConsole() {
410413
// Setup inspector command line API.
411414
setConsoleExtensionInstaller(inspector.installConsoleExtensions);
412415
}
413-
}
414-
415-
function setupGlobalURL() {
416-
const { URL, URLSearchParams } = NativeModule.require('internal/url');
417-
Object.defineProperties(global, {
418-
URL: {
419-
value: URL,
420-
writable: true,
421-
configurable: true,
422-
enumerable: false
423-
},
424-
URLSearchParams: {
425-
value: URLSearchParams,
426-
writable: true,
427-
configurable: true,
428-
enumerable: false
429-
}
430-
});
431-
}
432-
433-
function setupGlobalEncoding() {
434-
const { TextEncoder, TextDecoder } = NativeModule.require('util');
435-
Object.defineProperties(global, {
436-
TextEncoder: {
437-
value: TextEncoder,
438-
writable: true,
439-
configurable: true,
440-
enumerable: false
441-
},
442-
TextDecoder: {
443-
value: TextDecoder,
444-
writable: true,
445-
configurable: true,
446-
enumerable: false
447-
}
448-
});
416+
return consoleFromNode;
449417
}
450418

451419
function setupQueueMicrotask() {
@@ -483,3 +451,33 @@ function setupDOMException() {
483451
const { registerDOMException } = internalBinding('messaging');
484452
registerDOMException(DOMException);
485453
}
454+
455+
// https://heycam.github.io/webidl/#es-namespaces
456+
function exposeNamespace(target, name, namespaceObject) {
457+
Object.defineProperty(target, name, {
458+
writable: true,
459+
enumerable: false,
460+
configurable: true,
461+
value: namespaceObject
462+
});
463+
}
464+
465+
// https://heycam.github.io/webidl/#es-interfaces
466+
function exposeInterface(target, name, interfaceObject) {
467+
Object.defineProperty(target, name, {
468+
writable: true,
469+
enumerable: false,
470+
configurable: true,
471+
value: interfaceObject
472+
});
473+
}
474+
475+
// https://heycam.github.io/webidl/#define-the-operations
476+
function defineOperation(target, name, method) {
477+
Object.defineProperty(target, name, {
478+
writable: true,
479+
enumerable: true,
480+
configurable: true,
481+
value: method
482+
});
483+
}

0 commit comments

Comments
 (0)