Skip to content

Commit 41b1867

Browse files
committed
lib: distinguish webidl interfaces with the extended property "Exposed"
PR-URL: #46809 Refs: #42528 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent d0153ae commit 41b1867

File tree

6 files changed

+131
-81
lines changed

6 files changed

+131
-81
lines changed

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

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +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,
18+
exposeNamespace,
1519
} = require('internal/util');
1620
const config = internalBinding('config');
1721

@@ -35,36 +39,17 @@ exposeGetterAndSetter(globalThis,
3539
exposeInterface(globalThis, 'DOMException', value);
3640
});
3741

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-
42+
// https://dom.spec.whatwg.org/#interface-abortcontroller
4543
// Lazy ones.
46-
exposeLazyInterfaces(globalThis, 'internal/worker/io', ['BroadcastChannel']);
4744
exposeLazyInterfaces(globalThis, 'internal/abort_controller', [
4845
'AbortController', 'AbortSignal',
4946
]);
47+
// https://dom.spec.whatwg.org/#interface-eventtarget
5048
const {
5149
EventTarget, Event,
5250
} = require('internal/event_target');
5351
exposeInterface(globalThis, 'Event', Event);
5452
exposeInterface(globalThis, 'EventTarget', EventTarget);
55-
exposeLazyInterfaces(globalThis, 'internal/worker/io', [
56-
'MessageChannel', 'MessagePort', 'MessageEvent',
57-
]);
58-
defineLazyProperties(globalThis, 'buffer', ['atob', 'btoa']);
59-
// https://www.w3.org/TR/FileAPI/#dfn-Blob
60-
exposeLazyInterfaces(globalThis, 'internal/blob', ['Blob']);
61-
// https://www.w3.org/TR/hr-time-2/#the-performance-attribute
62-
exposeLazyInterfaces(globalThis, 'perf_hooks', [
63-
'Performance', 'PerformanceEntry', 'PerformanceMark', 'PerformanceMeasure',
64-
'PerformanceObserver', 'PerformanceObserverEntryList', 'PerformanceResourceTiming',
65-
]);
66-
67-
defineReplaceableLazyAttribute(globalThis, 'perf_hooks', ['performance']);
6853

6954
// https://encoding.spec.whatwg.org/#textencoder
7055
// https://encoding.spec.whatwg.org/#textdecoder
@@ -87,27 +72,6 @@ function createGlobalConsole() {
8772
return consoleFromNode;
8873
}
8974

90-
// https://heycam.github.io/webidl/#es-namespaces
91-
function exposeNamespace(target, name, namespaceObject) {
92-
ObjectDefineProperty(target, name, {
93-
__proto__: null,
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-
__proto__: null,
104-
enumerable: false,
105-
configurable: true,
106-
get: getter,
107-
set: setter,
108-
});
109-
}
110-
11175
// Web Streams API
11276
exposeLazyInterfaces(
11377
globalThis,
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
/**
4+
* This file exposes web interfaces that is defined with the WebIDL
5+
* Exposed=(Window,Worker) extended attribute or exposed in
6+
* WindowOrWorkerGlobalScope mixin.
7+
* See more details at https://webidl.spec.whatwg.org/#Exposed and
8+
* https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope.
9+
*/
10+
11+
const {
12+
globalThis,
13+
} = primordials;
14+
15+
const {
16+
defineOperation,
17+
defineLazyProperties,
18+
defineReplaceableLazyAttribute,
19+
exposeLazyInterfaces,
20+
} = require('internal/util');
21+
22+
// https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
23+
const timers = require('timers');
24+
defineOperation(globalThis, 'clearInterval', timers.clearInterval);
25+
defineOperation(globalThis, 'clearTimeout', timers.clearTimeout);
26+
defineOperation(globalThis, 'setInterval', timers.setInterval);
27+
defineOperation(globalThis, 'setTimeout', timers.setTimeout);
28+
29+
// https://html.spec.whatwg.org/multipage/web-messaging.html#broadcasting-to-other-browsing-contexts
30+
exposeLazyInterfaces(globalThis, 'internal/worker/io', ['BroadcastChannel']);
31+
exposeLazyInterfaces(globalThis, 'internal/worker/io', [
32+
'MessageChannel', 'MessagePort', 'MessageEvent',
33+
]);
34+
defineLazyProperties(globalThis, 'buffer', ['atob', 'btoa']);
35+
// https://www.w3.org/TR/FileAPI/#dfn-Blob
36+
exposeLazyInterfaces(globalThis, 'internal/blob', ['Blob']);
37+
// https://www.w3.org/TR/hr-time-2/#the-performance-attribute
38+
exposeLazyInterfaces(globalThis, 'perf_hooks', [
39+
'Performance', 'PerformanceEntry', 'PerformanceMark', 'PerformanceMeasure',
40+
'PerformanceObserver', 'PerformanceObserverEntryList', 'PerformanceResourceTiming',
41+
]);
42+
43+
defineReplaceableLazyAttribute(globalThis, 'perf_hooks', ['performance']);
44+
45+
// https://w3c.github.io/FileAPI/#creating-revoking
46+
const { installObjectURLMethods } = require('internal/url');
47+
installObjectURLMethods();

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: 45 additions & 30 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');
@@ -718,8 +713,33 @@ class URL {
718713
toJSON() {
719714
return this.#context.href;
720715
}
716+
}
717+
718+
ObjectDefineProperties(URL.prototype, {
719+
[SymbolToStringTag]: { __proto__: null, configurable: true, value: 'URL' },
720+
toString: kEnumerableProperty,
721+
href: kEnumerableProperty,
722+
origin: kEnumerableProperty,
723+
protocol: kEnumerableProperty,
724+
username: kEnumerableProperty,
725+
password: kEnumerableProperty,
726+
host: kEnumerableProperty,
727+
hostname: kEnumerableProperty,
728+
port: kEnumerableProperty,
729+
pathname: kEnumerableProperty,
730+
search: kEnumerableProperty,
731+
searchParams: kEnumerableProperty,
732+
hash: kEnumerableProperty,
733+
toJSON: kEnumerableProperty,
734+
});
735+
736+
function installObjectURLMethods() {
737+
const {
738+
storeDataObject,
739+
revokeDataObject,
740+
} = internalBinding('blob');
721741

722-
static createObjectURL(obj) {
742+
function createObjectURL(obj) {
723743
const cryptoRandom = lazyCryptoRandom();
724744
if (cryptoRandom === undefined)
725745
throw new ERR_NO_CRYPTO();
@@ -735,7 +755,7 @@ class URL {
735755
return `blob:nodedata:${id}`;
736756
}
737757

738-
static revokeObjectURL(url) {
758+
function revokeObjectURL(url) {
739759
url = `${url}`;
740760
try {
741761
// TODO(@anonrig): Remove this try/catch by calling `parse` directly.
@@ -747,30 +767,24 @@ class URL {
747767
// If there's an error, it's ignored.
748768
}
749769
}
750-
}
751-
752-
ObjectDefineProperties(URL.prototype, {
753-
[SymbolToStringTag]: { __proto__: null, configurable: true, value: 'URL' },
754-
toString: kEnumerableProperty,
755-
href: kEnumerableProperty,
756-
origin: kEnumerableProperty,
757-
protocol: kEnumerableProperty,
758-
username: kEnumerableProperty,
759-
password: kEnumerableProperty,
760-
host: kEnumerableProperty,
761-
hostname: kEnumerableProperty,
762-
port: kEnumerableProperty,
763-
pathname: kEnumerableProperty,
764-
search: kEnumerableProperty,
765-
searchParams: kEnumerableProperty,
766-
hash: kEnumerableProperty,
767-
toJSON: kEnumerableProperty,
768-
});
769770

770-
ObjectDefineProperties(URL, {
771-
createObjectURL: kEnumerableProperty,
772-
revokeObjectURL: kEnumerableProperty,
773-
});
771+
ObjectDefineProperties(URL, {
772+
createObjectURL: {
773+
__proto__: null,
774+
configurable: true,
775+
writable: true,
776+
enumerable: true,
777+
value: createObjectURL,
778+
},
779+
revokeObjectURL: {
780+
__proto__: null,
781+
configurable: true,
782+
writable: true,
783+
enumerable: true,
784+
value: revokeObjectURL,
785+
},
786+
});
787+
}
774788

775789
// application/x-www-form-urlencoded parser
776790
// Ref: https://url.spec.whatwg.org/#concept-urlencoded-parser
@@ -1251,6 +1265,7 @@ module.exports = {
12511265
fileURLToPath,
12521266
pathToFileURL,
12531267
toPathIfFileURL,
1268+
installObjectURLMethods,
12541269
URL,
12551270
URLSearchParams,
12561271
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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,10 @@ MaybeLocal<Value> Realm::BootstrapNode() {
218218
}
219219

220220
if (!env_->no_browser_globals()) {
221-
result = ExecuteBootstrapper("internal/bootstrap/browser");
222-
223-
if (result.IsEmpty()) {
221+
if (ExecuteBootstrapper("internal/bootstrap/web/exposed-wildcard")
222+
.IsEmpty() ||
223+
ExecuteBootstrapper("internal/bootstrap/web/exposed-window-or-worker")
224+
.IsEmpty()) {
224225
return MaybeLocal<Value>();
225226
}
226227
}

0 commit comments

Comments
 (0)