Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d915207

Browse filesBrowse files
joyeecheungrvagg
authored andcommitted
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 <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 7e70233 commit d915207
Copy full SHA for d915207

File tree

Expand file treeCollapse file tree

1 file changed

+59
-61
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+59
-61
lines changed
Open diff view settings
Collapse file

‎lib/internal/bootstrap/node.js‎

Copy file name to clipboardExpand all lines: lib/internal/bootstrap/node.js
+59-61Lines 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();
@@ -404,29 +427,9 @@ function setupBuffer() {
404427
global.Buffer = Buffer;
405428
}
406429

407-
function setupGlobalTimeouts() {
408-
const timers = NativeModule.require('timers');
409-
global.clearImmediate = timers.clearImmediate;
410-
global.clearInterval = timers.clearInterval;
411-
global.clearTimeout = timers.clearTimeout;
412-
global.setImmediate = timers.setImmediate;
413-
global.setInterval = timers.setInterval;
414-
global.setTimeout = timers.setTimeout;
415-
}
416-
417-
function setupGlobalConsole() {
418-
const consoleFromVM = global.console;
430+
function createGlobalConsole(consoleFromVM) {
419431
const consoleFromNode =
420432
NativeModule.require('internal/console/global');
421-
// Override global console from the one provided by the VM
422-
// to the one implemented by Node.js
423-
Object.defineProperty(global, 'console', {
424-
configurable: true,
425-
enumerable: false,
426-
value: consoleFromNode,
427-
writable: true
428-
});
429-
430433
if (config.hasInspector) {
431434
const inspector = NativeModule.require('internal/util/inspector');
432435
// This will be exposed by `require('inspector').console` later.
@@ -438,42 +441,7 @@ function setupGlobalConsole() {
438441
// Setup inspector command line API.
439442
setConsoleExtensionInstaller(inspector.installConsoleExtensions);
440443
}
441-
}
442-
443-
function setupGlobalURL() {
444-
const { URL, URLSearchParams } = NativeModule.require('internal/url');
445-
Object.defineProperties(global, {
446-
URL: {
447-
value: URL,
448-
writable: true,
449-
configurable: true,
450-
enumerable: false
451-
},
452-
URLSearchParams: {
453-
value: URLSearchParams,
454-
writable: true,
455-
configurable: true,
456-
enumerable: false
457-
}
458-
});
459-
}
460-
461-
function setupGlobalEncoding() {
462-
const { TextEncoder, TextDecoder } = NativeModule.require('util');
463-
Object.defineProperties(global, {
464-
TextEncoder: {
465-
value: TextEncoder,
466-
writable: true,
467-
configurable: true,
468-
enumerable: false
469-
},
470-
TextDecoder: {
471-
value: TextDecoder,
472-
writable: true,
473-
configurable: true,
474-
enumerable: false
475-
}
476-
});
444+
return consoleFromNode;
477445
}
478446

479447
function setupQueueMicrotask() {
@@ -513,3 +481,33 @@ function setupDOMException() {
513481
}
514482

515483
function noop() {}
484+
485+
// https://heycam.github.io/webidl/#es-namespaces
486+
function exposeNamespace(target, name, namespaceObject) {
487+
Object.defineProperty(target, name, {
488+
writable: true,
489+
enumerable: false,
490+
configurable: true,
491+
value: namespaceObject
492+
});
493+
}
494+
495+
// https://heycam.github.io/webidl/#es-interfaces
496+
function exposeInterface(target, name, interfaceObject) {
497+
Object.defineProperty(target, name, {
498+
writable: true,
499+
enumerable: false,
500+
configurable: true,
501+
value: interfaceObject
502+
});
503+
}
504+
505+
// https://heycam.github.io/webidl/#define-the-operations
506+
function defineOperation(target, name, method) {
507+
Object.defineProperty(target, name, {
508+
writable: true,
509+
enumerable: true,
510+
configurable: true,
511+
value: method
512+
});
513+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.