Skip to content

Commit 63c33e2

Browse files
daeyeonaduh95
authored andcommittedAug 1, 2022
events: expose CustomEvent on global with CLI flag
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com PR-URL: nodejs#43885 Fixes: nodejs#40678 Refs: nodejs#43514 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com>
1 parent 3abc6d2 commit 63c33e2

13 files changed

+68
-5
lines changed
 

‎.eslintrc.js

+1
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

+10
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

+15
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,19 @@ A browser-compatible implementation of {CryptoKey}. This global is available
306306
only if the Node.js binary was compiled with including support for the
307307
`node:crypto` module.
308308

309+
## `CustomEvent`
310+
311+
<!-- YAML
312+
added: REPLACEME
313+
-->
314+
315+
> Stability: 1 - Experimental. Enable this API with the
316+
> [`--experimental-global-customevent`][] CLI flag.
317+
318+
<!-- type=global -->
319+
320+
A browser-compatible implementation of the [`CustomEvent` Web API][].
321+
309322
## `Event`
310323

311324
<!-- YAML
@@ -607,8 +620,10 @@ The object that acts as the namespace for all W3C
607620

608621
[Web Crypto API]: webcrypto.md
609622
[`--experimental-fetch`]: cli.md#--experimental-fetch
623+
[`--experimental-global-customevent`]: cli.md#--experimental-global-customevent
610624
[`--experimental-global-webcrypto`]: cli.md#--experimental-global-webcrypto
611625
[`AbortController`]: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
626+
[`CustomEvent` Web API]: https://dom.spec.whatwg.org/#customevent
612627
[`EventTarget` and `Event` API]: events.md#eventtarget-and-event-api
613628
[`MessageChannel`]: worker_threads.md#class-messagechannel
614629
[`MessageEvent`]: https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/MessageEvent

‎doc/node.1

+3
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

+2
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

+13
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

+2
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

+4
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

+1
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

+3
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);
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Flags: --experimental-global-customevent --expose-internals
2+
'use strict';
3+
4+
require('../common');
5+
const { strictEqual, ok } = require('node:assert');
6+
const { CustomEvent: internalCustomEvent } = require('internal/event_target');
7+
8+
// Global
9+
ok(CustomEvent);
10+
11+
strictEqual(CustomEvent, internalCustomEvent);

‎test/wpt/status/dom/events.json

+1-5
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,13 @@
2020
"fail": {
2121
"unexpected": [
2222
"assert_true: expected true got false",
23-
"assert_array_equals: lengths differ, expected array [\"bubbles\", \"cancelable\"] length 2, got [\"cancelable\", \"bubbles\", \"sweet\"] length 3",
24-
"CustomEvent is not defined"
23+
"assert_array_equals: lengths differ, expected array [\"bubbles\", \"cancelable\"] length 2, got [\"cancelable\", \"bubbles\", \"sweet\"] length 3"
2524
]
2625
}
2726
},
2827
"EventListener-addEventListener.sub.window.js": {
2928
"fail": "document is not defined"
3029
},
31-
"EventTarget-constructible.any.js": {
32-
"fail": "CustomEvent is not defined"
33-
},
3430
"relatedTarget.window.js": {
3531
"fail": "document is not defined"
3632
},

‎test/wpt/test-events.js

+2
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ const { WPTRunner } = require('../common/wpt');
44

55
const runner = new WPTRunner('dom/events');
66

7+
runner.setFlags(['--experimental-global-customevent']);
8+
79
runner.runJsTests();

0 commit comments

Comments
 (0)