Skip to content

Commit 112b2a1

Browse files
daeyeonaduh95
authored andcommitted
events: expose CustomEvent on global with CLI flag
Signed-off-by: Daeyeon Jeong [email protected] PR-URL: nodejs#43885 Fixes: nodejs#40678 Refs: nodejs#43514 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Mestery <[email protected]>
1 parent c2237dc commit 112b2a1

File tree

13 files changed

+68
-5
lines changed

13 files changed

+68
-5
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ module.exports = {
320320
'node-core/no-duplicate-requires': 'error',
321321
},
322322
globals: {
323+
CustomEvent: 'readable',
323324
Crypto: 'readable',
324325
CryptoKey: 'readable',
325326
fetch: 'readable',

doc/api/cli.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,14 @@ added: v16.15.0
280280

281281
Enable experimental support for the [Fetch API][].
282282

283+
### `--experimental-global-customevent`
284+
285+
<!-- YAML
286+
added: REPLACEME
287+
-->
288+
289+
Expose the [CustomEvent Web API][] on the global scope.
290+
283291
### `--experimental-global-webcrypto`
284292

285293
<!-- YAML
@@ -1619,6 +1627,7 @@ Node.js options that are allowed are:
16191627
* `--enable-source-maps`
16201628
* `--experimental-abortcontroller`
16211629
* `--experimental-fetch`
1630+
* `--experimental-global-customevent`
16221631
* `--experimental-global-webcrypto`
16231632
* `--experimental-import-meta-resolve`
16241633
* `--experimental-json-modules`
@@ -2041,6 +2050,7 @@ done
20412050
[#42511]: https://github.com/nodejs/node/issues/42511
20422051
[Chrome DevTools Protocol]: https://chromedevtools.github.io/devtools-protocol/
20432052
[CommonJS]: modules.md
2053+
[CustomEvent Web API]: https://dom.spec.whatwg.org/#customevent
20442054
[ECMAScript module loader]: esm.md#loaders
20452055
[Fetch API]: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
20462056
[Modules loaders]: packages.md#modules-loaders

doc/api/globals.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,19 @@ A browser-compatible implementation of {CryptoKey}. This global is available
298298
only if the Node.js binary was compiled with including support for the
299299
`node:crypto` module.
300300

301+
## `CustomEvent`
302+
303+
<!-- YAML
304+
added: REPLACEME
305+
-->
306+
307+
> Stability: 1 - Experimental. Enable this API with the
308+
> [`--experimental-global-customevent`][] CLI flag.
309+
310+
<!-- type=global -->
311+
312+
A browser-compatible implementation of the [`CustomEvent` Web API][].
313+
301314
## `Event`
302315

303316
<!-- YAML
@@ -599,8 +612,10 @@ The object that acts as the namespace for all W3C
599612

600613
[Web Crypto API]: webcrypto.md
601614
[`--experimental-fetch`]: cli.md#--experimental-fetch
615+
[`--experimental-global-customevent`]: cli.md#--experimental-global-customevent
602616
[`--experimental-global-webcrypto`]: cli.md#--experimental-global-webcrypto
603617
[`AbortController`]: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
618+
[`CustomEvent` Web API]: https://dom.spec.whatwg.org/#customevent
604619
[`EventTarget` and `Event` API]: events.md#eventtarget-and-event-api
605620
[`MessageChannel`]: worker_threads.md#class-messagechannel
606621
[`MessageEvent`]: https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/MessageEvent

doc/node.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ Enable Source Map V3 support for stack traces.
142142
.It Fl -experimental-fetch
143143
Enable experimental support for the Fetch API.
144144
.
145+
.It Fl -experimental-global-customevent
146+
Expose the CustomEvent on the global scope.
147+
.
145148
.It Fl -experimental-global-webcrypto
146149
Expose the Web Crypto API on the global scope.
147150
.

lib/.eslintrc.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ rules:
4141
message: Use `const { BroadcastChannel } = require('internal/worker/io');` instead of the global.
4242
- name: Buffer
4343
message: Use `const { Buffer } = require('buffer');` instead of the global.
44+
- name: CustomEvent
45+
message: Use `const { CustomEvent } = require('internal/event_target');` instead of the global.
4446
- name: Event
4547
message: Use `const { Event } = require('internal/event_target');` instead of the global.
4648
- name: EventTarget

lib/internal/bootstrap/pre_execution.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ function prepareMainThreadExecution(expandArgv1 = false,
4242
setupWarningHandler();
4343
setupFetch();
4444
setupWebCrypto();
45+
setupCustomEvent();
4546

4647
// Resolve the coverage directory to an absolute path, and
4748
// overwrite process.env so that the original path gets passed
@@ -229,6 +230,17 @@ function setupWebCrypto() {
229230
}
230231
}
231232

233+
// TODO(daeyeon): move this to internal/bootstrap/browser when the CLI flag is
234+
// removed.
235+
function setupCustomEvent() {
236+
if (process.config.variables.node_no_browser_globals ||
237+
!getOptionValue('--experimental-global-customevent')) {
238+
return;
239+
}
240+
const { CustomEvent } = require('internal/event_target');
241+
exposeInterface(globalThis, 'CustomEvent', CustomEvent);
242+
}
243+
232244
// Setup User-facing NODE_V8_COVERAGE environment variable that writes
233245
// ScriptCoverage to a specified file.
234246
function setupCoverageHooks(dir) {
@@ -576,6 +588,7 @@ module.exports = {
576588
setupWarningHandler,
577589
setupFetch,
578590
setupWebCrypto,
591+
setupCustomEvent,
579592
setupDebugEnv,
580593
setupPerfHooks,
581594
prepareMainThreadExecution,

lib/internal/main/worker_thread.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const {
1919
setupWarningHandler,
2020
setupFetch,
2121
setupWebCrypto,
22+
setupCustomEvent,
2223
setupDebugEnv,
2324
setupPerfHooks,
2425
initializeDeprecations,
@@ -71,6 +72,7 @@ setupDebugEnv();
7172
setupWarningHandler();
7273
setupFetch();
7374
setupWebCrypto();
75+
setupCustomEvent();
7476
initializeSourceMapsHandlers();
7577

7678
// Since worker threads cannot switch cwd, we do not need to

src/node_options.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
339339
&EnvironmentOptions::experimental_fetch,
340340
kAllowedInEnvironment);
341341
AddOption("--experimental-json-modules", "", NoOp{}, kAllowedInEnvironment);
342+
AddOption("--experimental-global-customevent",
343+
"expose experimental CustomEvent on the global scope",
344+
&EnvironmentOptions::experimental_global_customevent,
345+
kAllowedInEnvironment);
342346
AddOption("--experimental-global-webcrypto",
343347
"expose experimental Web Crypto API on the global scope",
344348
&EnvironmentOptions::experimental_global_web_crypto,

src/node_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class EnvironmentOptions : public Options {
109109
bool enable_source_maps = false;
110110
bool experimental_https_modules = false;
111111
bool experimental_fetch = false;
112+
bool experimental_global_customevent = false;
112113
bool experimental_global_web_crypto = false;
113114
std::string experimental_specifier_resolution;
114115
bool experimental_wasm_modules = false;

test/common/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@ if (hasCrypto && global.crypto) {
315315
knownGlobals.push(global.CryptoKey);
316316
knownGlobals.push(global.SubtleCrypto);
317317
}
318+
if (global.CustomEvent) {
319+
knownGlobals.push(global.CustomEvent);
320+
}
318321

319322
function allowGlobals(...allowlist) {
320323
knownGlobals = knownGlobals.concat(allowlist);

0 commit comments

Comments
 (0)