Skip to content

Commit 76ad2e6

Browse files
committed
lib: distinguish webidl interfaces with the extended property "Exposed"
1 parent 7a37829 commit 76ad2e6

File tree

6 files changed

+144
-99
lines changed

6 files changed

+144
-99
lines changed

lib/internal/bootstrap/browser.js renamed to lib/internal/bootstrap/web/exposed-wildcard.js

Lines changed: 9 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
'use strict';
22

3+
/**
4+
* This file exposes web interfaces that is defined with the WebIDL
5+
* [Exposed=*] extended attribute.
6+
* See more details at https://webidl.spec.whatwg.org/#Exposed.
7+
*/
8+
39
const {
4-
ObjectDefineProperty,
510
globalThis,
611
} = primordials;
712

813
const {
9-
defineOperation,
1014
exposeInterface,
1115
lazyDOMExceptionClass,
12-
defineLazyProperties,
13-
defineReplaceableLazyAttribute,
1416
exposeLazyInterfaces,
17+
exposeGetterAndSetter,
1518
} = require('internal/util');
16-
const config = internalBinding('config');
17-
18-
// https://console.spec.whatwg.org/#console-namespace
19-
exposeNamespace(globalThis, 'console',
20-
createGlobalConsole());
2119

2220
const { URL, URLSearchParams } = require('internal/url');
2321
// https://url.spec.whatwg.org/#url
@@ -35,78 +33,24 @@ exposeGetterAndSetter(globalThis,
3533
exposeInterface(globalThis, 'DOMException', value);
3634
});
3735

38-
// https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
39-
const timers = require('timers');
40-
defineOperation(globalThis, 'clearInterval', timers.clearInterval);
41-
defineOperation(globalThis, 'clearTimeout', timers.clearTimeout);
42-
defineOperation(globalThis, 'setInterval', timers.setInterval);
43-
defineOperation(globalThis, 'setTimeout', timers.setTimeout);
44-
36+
// https://dom.spec.whatwg.org/#interface-abortcontroller
4537
// Lazy ones.
4638
exposeLazyInterfaces(globalThis, 'internal/abort_controller', [
4739
'AbortController', 'AbortSignal',
4840
]);
41+
// https://dom.spec.whatwg.org/#interface-eventtarget
4942
const {
5043
EventTarget, Event,
5144
} = require('internal/event_target');
5245
exposeInterface(globalThis, 'Event', Event);
5346
exposeInterface(globalThis, 'EventTarget', EventTarget);
54-
exposeLazyInterfaces(globalThis, 'internal/worker/io', [
55-
'MessageChannel', 'MessagePort', 'MessageEvent',
56-
]);
57-
defineLazyProperties(globalThis, 'buffer', ['atob', 'btoa']);
58-
// https://www.w3.org/TR/FileAPI/#dfn-Blob
59-
exposeLazyInterfaces(globalThis, 'internal/blob', ['Blob']);
60-
// https://www.w3.org/TR/hr-time-2/#the-performance-attribute
61-
exposeLazyInterfaces(globalThis, 'perf_hooks', [
62-
'Performance', 'PerformanceEntry', 'PerformanceMark', 'PerformanceMeasure',
63-
'PerformanceObserver', 'PerformanceObserverEntryList', 'PerformanceResourceTiming',
64-
]);
65-
66-
defineReplaceableLazyAttribute(globalThis, 'perf_hooks', ['performance']);
6747

6848
// https://encoding.spec.whatwg.org/#textencoder
6949
// https://encoding.spec.whatwg.org/#textdecoder
7050
exposeLazyInterfaces(globalThis,
7151
'internal/encoding',
7252
['TextEncoder', 'TextDecoder']);
7353

74-
function createGlobalConsole() {
75-
const consoleFromNode =
76-
require('internal/console/global');
77-
if (config.hasInspector) {
78-
const inspector = require('internal/util/inspector');
79-
// TODO(joyeecheung): postpone this until the first time inspector
80-
// is activated.
81-
inspector.wrapConsole(consoleFromNode);
82-
const { setConsoleExtensionInstaller } = internalBinding('inspector');
83-
// Setup inspector command line API.
84-
setConsoleExtensionInstaller(inspector.installConsoleExtensions);
85-
}
86-
return consoleFromNode;
87-
}
88-
89-
// https://heycam.github.io/webidl/#es-namespaces
90-
function exposeNamespace(target, name, namespaceObject) {
91-
ObjectDefineProperty(target, name, {
92-
__proto__: null,
93-
writable: true,
94-
enumerable: false,
95-
configurable: true,
96-
value: namespaceObject
97-
});
98-
}
99-
100-
function exposeGetterAndSetter(target, name, getter, setter = undefined) {
101-
ObjectDefineProperty(target, name, {
102-
__proto__: null,
103-
enumerable: false,
104-
configurable: true,
105-
get: getter,
106-
set: setter,
107-
});
108-
}
109-
11054
// Web Streams API
11155
exposeLazyInterfaces(
11256
globalThis,
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
const {
4+
globalThis,
5+
} = primordials;
6+
7+
const {
8+
defineOperation,
9+
defineLazyProperties,
10+
defineReplaceableLazyAttribute,
11+
exposeLazyInterfaces,
12+
exposeNamespace,
13+
} = require('internal/util');
14+
const config = internalBinding('config');
15+
16+
// https://console.spec.whatwg.org/#console-namespace
17+
exposeNamespace(globalThis, 'console',
18+
createGlobalConsole());
19+
20+
// https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
21+
const timers = require('timers');
22+
defineOperation(globalThis, 'clearInterval', timers.clearInterval);
23+
defineOperation(globalThis, 'clearTimeout', timers.clearTimeout);
24+
defineOperation(globalThis, 'setInterval', timers.setInterval);
25+
defineOperation(globalThis, 'setTimeout', timers.setTimeout);
26+
27+
exposeLazyInterfaces(globalThis, 'internal/worker/io', [
28+
'MessageChannel', 'MessagePort', 'MessageEvent',
29+
]);
30+
defineLazyProperties(globalThis, 'buffer', ['atob', 'btoa']);
31+
// https://www.w3.org/TR/FileAPI/#dfn-Blob
32+
exposeLazyInterfaces(globalThis, 'internal/blob', ['Blob']);
33+
// https://www.w3.org/TR/hr-time-2/#the-performance-attribute
34+
exposeLazyInterfaces(globalThis, 'perf_hooks', [
35+
'Performance', 'PerformanceEntry', 'PerformanceMark', 'PerformanceMeasure',
36+
'PerformanceObserver', 'PerformanceObserverEntryList', 'PerformanceResourceTiming',
37+
]);
38+
39+
defineReplaceableLazyAttribute(globalThis, 'perf_hooks', ['performance']);
40+
41+
const { installObjectURLMethods } = require('internal/url');
42+
installObjectURLMethods();
43+
44+
function createGlobalConsole() {
45+
const consoleFromNode =
46+
require('internal/console/global');
47+
if (config.hasInspector) {
48+
const inspector = require('internal/util/inspector');
49+
// TODO(joyeecheung): postpone this until the first time inspector
50+
// is activated.
51+
inspector.wrapConsole(consoleFromNode);
52+
const { setConsoleExtensionInstaller } = internalBinding('inspector');
53+
// Setup inspector command line API.
54+
setConsoleExtensionInstaller(inspector.installConsoleExtensions);
55+
}
56+
return consoleFromNode;
57+
}

lib/internal/process/pre_execution.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ function setupFetch() {
268268
});
269269
}
270270

271-
// TODO(aduh95): move this to internal/bootstrap/browser when the CLI flag is
271+
// TODO(aduh95): move this to internal/bootstrap/web/* when the CLI flag is
272272
// removed.
273273
function setupWebCrypto() {
274274
if (process.config.variables.node_no_browser_globals ||
@@ -316,7 +316,7 @@ function setupCodeCoverage() {
316316
}
317317
}
318318

319-
// TODO(daeyeon): move this to internal/bootstrap/browser when the CLI flag is
319+
// TODO(daeyeon): move this to internal/bootstrap/web/* when the CLI flag is
320320
// removed.
321321
function setupCustomEvent() {
322322
if (process.config.variables.node_no_browser_globals ||

lib/internal/url.js

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,6 @@ const {
9191
updateUrl,
9292
} = internalBinding('url');
9393

94-
const {
95-
storeDataObject,
96-
revokeDataObject,
97-
} = internalBinding('blob');
98-
9994
const FORWARD_SLASH = /\//g;
10095

10196
const context = Symbol('context');
@@ -762,8 +757,34 @@ class URL {
762757
throw new ERR_INVALID_THIS('URL');
763758
return this[context].href;
764759
}
760+
}
765761

766-
static createObjectURL(obj) {
762+
ObjectDefineProperties(URL.prototype, {
763+
[kFormat]: { __proto__: null, configurable: false, writable: false },
764+
[SymbolToStringTag]: { __proto__: null, configurable: true, value: 'URL' },
765+
toString: kEnumerableProperty,
766+
href: kEnumerableProperty,
767+
origin: kEnumerableProperty,
768+
protocol: kEnumerableProperty,
769+
username: kEnumerableProperty,
770+
password: kEnumerableProperty,
771+
host: kEnumerableProperty,
772+
hostname: kEnumerableProperty,
773+
port: kEnumerableProperty,
774+
pathname: kEnumerableProperty,
775+
search: kEnumerableProperty,
776+
searchParams: kEnumerableProperty,
777+
hash: kEnumerableProperty,
778+
toJSON: kEnumerableProperty,
779+
});
780+
781+
function installObjectURLMethods() {
782+
const {
783+
storeDataObject,
784+
revokeDataObject,
785+
} = internalBinding('blob');
786+
787+
function createObjectURL(obj) {
767788
const cryptoRandom = lazyCryptoRandom();
768789
if (cryptoRandom === undefined)
769790
throw new ERR_NO_CRYPTO();
@@ -779,7 +800,7 @@ class URL {
779800
return `blob:nodedata:${id}`;
780801
}
781802

782-
static revokeObjectURL(url) {
803+
function revokeObjectURL(url) {
783804
url = `${url}`;
784805
try {
785806
// TODO(@anonrig): Remove this try/catch by calling `parse` directly.
@@ -791,31 +812,24 @@ class URL {
791812
// If there's an error, it's ignored.
792813
}
793814
}
794-
}
795-
796-
ObjectDefineProperties(URL.prototype, {
797-
[kFormat]: { __proto__: null, configurable: false, writable: false },
798-
[SymbolToStringTag]: { __proto__: null, configurable: true, value: 'URL' },
799-
toString: kEnumerableProperty,
800-
href: kEnumerableProperty,
801-
origin: kEnumerableProperty,
802-
protocol: kEnumerableProperty,
803-
username: kEnumerableProperty,
804-
password: kEnumerableProperty,
805-
host: kEnumerableProperty,
806-
hostname: kEnumerableProperty,
807-
port: kEnumerableProperty,
808-
pathname: kEnumerableProperty,
809-
search: kEnumerableProperty,
810-
searchParams: kEnumerableProperty,
811-
hash: kEnumerableProperty,
812-
toJSON: kEnumerableProperty,
813-
});
814815

815-
ObjectDefineProperties(URL, {
816-
createObjectURL: kEnumerableProperty,
817-
revokeObjectURL: kEnumerableProperty,
818-
});
816+
ObjectDefineProperties(URL, {
817+
createObjectURL: {
818+
__proto__: null,
819+
configurable: true,
820+
writable: true,
821+
enumerable: true,
822+
value: createObjectURL,
823+
},
824+
revokeObjectURL: {
825+
__proto__: null,
826+
configurable: true,
827+
writable: true,
828+
enumerable: true,
829+
value: revokeObjectURL,
830+
},
831+
});
832+
}
819833

820834
function initSearchParams(url, init) {
821835
if (!init) {
@@ -1310,6 +1324,7 @@ module.exports = {
13101324
pathToFileURL,
13111325
toPathIfFileURL,
13121326
isURLInstance,
1327+
installObjectURLMethods,
13131328
URL,
13141329
URLSearchParams,
13151330
domainToASCII,

lib/internal/util.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,27 @@ function exposeInterface(target, name, interfaceObject) {
511511
});
512512
}
513513

514+
// https://heycam.github.io/webidl/#es-namespaces
515+
function exposeNamespace(target, name, namespaceObject) {
516+
ObjectDefineProperty(target, name, {
517+
__proto__: null,
518+
writable: true,
519+
enumerable: false,
520+
configurable: true,
521+
value: namespaceObject,
522+
});
523+
}
524+
525+
function exposeGetterAndSetter(target, name, getter, setter = undefined) {
526+
ObjectDefineProperty(target, name, {
527+
__proto__: null,
528+
enumerable: false,
529+
configurable: true,
530+
get: getter,
531+
set: setter,
532+
});
533+
}
534+
514535
function defineLazyProperties(target, id, keys, enumerable = true) {
515536
const descriptors = { __proto__: null };
516537
let mod;
@@ -750,6 +771,8 @@ module.exports = {
750771
emitExperimentalWarning,
751772
exposeInterface,
752773
exposeLazyInterfaces,
774+
exposeNamespace,
775+
exposeGetterAndSetter,
753776
filterDuplicateStrings,
754777
filterOwnProperties,
755778
getConstructorOf,

src/node_realm.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,13 @@ MaybeLocal<Value> Realm::BootstrapNode() {
224224
}
225225

226226
if (!env_->no_browser_globals()) {
227-
result = ExecuteBootstrapper("internal/bootstrap/browser");
227+
result = ExecuteBootstrapper("internal/bootstrap/web/exposed-wildcard");
228+
229+
if (result.IsEmpty()) {
230+
return MaybeLocal<Value>();
231+
}
232+
233+
result = ExecuteBootstrapper("internal/bootstrap/web/exposed-window");
228234

229235
if (result.IsEmpty()) {
230236
return MaybeLocal<Value>();

0 commit comments

Comments
 (0)