mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
events: expose CustomEvent on global with CLI flag
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com PR-URL: https://github.com/nodejs/node/pull/43885 Backport-PR-URL: https://github.com/nodejs/node/pull/44082 Fixes: https://github.com/nodejs/node/issues/40678 Refs: https://github.com/nodejs/node/pull/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>
This commit is contained in:
1 parent
087adbb7a3
commit
7a5de2cc0c
13 files changed
+68
-5
No files matched your search
@@ -320,6 +320,7 @@ module.exports = {
|
||||
'node-core/no-duplicate-requires': 'error',
|
||||
},
|
||||
globals: {
|
||||
CustomEvent: 'readable',
|
||||
Crypto: 'readable',
|
||||
CryptoKey: 'readable',
|
||||
fetch: 'readable',
|
||||
|
||||
@@ -280,6 +280,14 @@ added: v16.15.0
|
||||
|
||||
Enable experimental support for the [Fetch API][].
|
||||
|
||||
### `--experimental-global-customevent`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
Expose the [CustomEvent Web API][] on the global scope.
|
||||
|
||||
### `--experimental-global-webcrypto`
|
||||
|
||||
<!-- YAML
|
||||
@@ -1619,6 +1627,7 @@ Node.js options that are allowed are:
|
||||
* `--enable-source-maps`
|
||||
* `--experimental-abortcontroller`
|
||||
* `--experimental-fetch`
|
||||
* `--experimental-global-customevent`
|
||||
* `--experimental-global-webcrypto`
|
||||
* `--experimental-import-meta-resolve`
|
||||
* `--experimental-json-modules`
|
||||
@@ -2041,6 +2050,7 @@ done
|
||||
[#42511]: https://github.com/nodejs/node/issues/42511
|
||||
[Chrome DevTools Protocol]: https://chromedevtools.github.io/devtools-protocol/
|
||||
[CommonJS]: modules.md
|
||||
[CustomEvent Web API]: https://dom.spec.whatwg.org/#customevent
|
||||
[ECMAScript module loader]: esm.md#loaders
|
||||
[Fetch API]: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
|
||||
[Modules loaders]: packages.md#modules-loaders
|
||||
|
||||
@@ -306,6 +306,19 @@ A browser-compatible implementation of {CryptoKey}. This global is available
|
||||
only if the Node.js binary was compiled with including support for the
|
||||
`node:crypto` module.
|
||||
|
||||
## `CustomEvent`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
> Stability: 1 - Experimental. Enable this API with the
|
||||
> [`--experimental-global-customevent`][] CLI flag.
|
||||
|
||||
<!-- type=global -->
|
||||
|
||||
A browser-compatible implementation of the [`CustomEvent` Web API][].
|
||||
|
||||
## `Event`
|
||||
|
||||
<!-- YAML
|
||||
@@ -607,8 +620,10 @@ The object that acts as the namespace for all W3C
|
||||
|
||||
[Web Crypto API]: webcrypto.md
|
||||
[`--experimental-fetch`]: cli.md#--experimental-fetch
|
||||
[`--experimental-global-customevent`]: cli.md#--experimental-global-customevent
|
||||
[`--experimental-global-webcrypto`]: cli.md#--experimental-global-webcrypto
|
||||
[`AbortController`]: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
|
||||
[`CustomEvent` Web API]: https://dom.spec.whatwg.org/#customevent
|
||||
[`EventTarget` and `Event` API]: events.md#eventtarget-and-event-api
|
||||
[`MessageChannel`]: worker_threads.md#class-messagechannel
|
||||
[`MessageEvent`]: https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/MessageEvent
|
||||
|
||||
@@ -142,6 +142,9 @@ Enable Source Map V3 support for stack traces.
|
||||
.It Fl -experimental-fetch
|
||||
Enable experimental support for the Fetch API.
|
||||
.
|
||||
.It Fl -experimental-global-customevent
|
||||
Expose the CustomEvent on the global scope.
|
||||
.
|
||||
.It Fl -experimental-global-webcrypto
|
||||
Expose the Web Crypto API on the global scope.
|
||||
.
|
||||
|
||||
@@ -41,6 +41,8 @@ rules:
|
||||
message: Use `const { BroadcastChannel } = require('internal/worker/io');` instead of the global.
|
||||
- name: Buffer
|
||||
message: Use `const { Buffer } = require('buffer');` instead of the global.
|
||||
- name: CustomEvent
|
||||
message: Use `const { CustomEvent } = require('internal/event_target');` instead of the global.
|
||||
- name: Event
|
||||
message: Use `const { Event } = require('internal/event_target');` instead of the global.
|
||||
- name: EventTarget
|
||||
|
||||
@@ -42,6 +42,7 @@ function prepareMainThreadExecution(expandArgv1 = false,
|
||||
setupWarningHandler();
|
||||
setupFetch();
|
||||
setupWebCrypto();
|
||||
setupCustomEvent();
|
||||
|
||||
// Resolve the coverage directory to an absolute path, and
|
||||
// overwrite process.env so that the original path gets passed
|
||||
@@ -232,6 +233,17 @@ function setupWebCrypto() {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(daeyeon): move this to internal/bootstrap/browser when the CLI flag is
|
||||
// removed.
|
||||
function setupCustomEvent() {
|
||||
if (process.config.variables.node_no_browser_globals ||
|
||||
!getOptionValue('--experimental-global-customevent')) {
|
||||
return;
|
||||
}
|
||||
const { CustomEvent } = require('internal/event_target');
|
||||
exposeInterface(globalThis, 'CustomEvent', CustomEvent);
|
||||
}
|
||||
|
||||
// Setup User-facing NODE_V8_COVERAGE environment variable that writes
|
||||
// ScriptCoverage to a specified file.
|
||||
function setupCoverageHooks(dir) {
|
||||
@@ -579,6 +591,7 @@ module.exports = {
|
||||
setupWarningHandler,
|
||||
setupFetch,
|
||||
setupWebCrypto,
|
||||
setupCustomEvent,
|
||||
setupDebugEnv,
|
||||
setupPerfHooks,
|
||||
prepareMainThreadExecution,
|
||||
|
||||
@@ -19,6 +19,7 @@ const {
|
||||
setupWarningHandler,
|
||||
setupFetch,
|
||||
setupWebCrypto,
|
||||
setupCustomEvent,
|
||||
setupDebugEnv,
|
||||
setupPerfHooks,
|
||||
initializeDeprecations,
|
||||
@@ -71,6 +72,7 @@ setupDebugEnv();
|
||||
setupWarningHandler();
|
||||
setupFetch();
|
||||
setupWebCrypto();
|
||||
setupCustomEvent();
|
||||
initializeSourceMapsHandlers();
|
||||
|
||||
// Since worker threads cannot switch cwd, we do not need to
|
||||
|
||||
@@ -339,6 +339,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
|
||||
&EnvironmentOptions::experimental_fetch,
|
||||
kAllowedInEnvironment);
|
||||
AddOption("--experimental-json-modules", "", NoOp{}, kAllowedInEnvironment);
|
||||
AddOption("--experimental-global-customevent",
|
||||
"expose experimental CustomEvent on the global scope",
|
||||
&EnvironmentOptions::experimental_global_customevent,
|
||||
kAllowedInEnvironment);
|
||||
AddOption("--experimental-global-webcrypto",
|
||||
"expose experimental Web Crypto API on the global scope",
|
||||
&EnvironmentOptions::experimental_global_web_crypto,
|
||||
|
||||
@@ -109,6 +109,7 @@ class EnvironmentOptions : public Options {
|
||||
bool enable_source_maps = false;
|
||||
bool experimental_https_modules = false;
|
||||
bool experimental_fetch = false;
|
||||
bool experimental_global_customevent = false;
|
||||
bool experimental_global_web_crypto = false;
|
||||
std::string experimental_specifier_resolution;
|
||||
bool experimental_wasm_modules = false;
|
||||
|
||||
@@ -315,6 +315,9 @@ if (hasCrypto && global.crypto) {
|
||||
knownGlobals.push(global.CryptoKey);
|
||||
knownGlobals.push(global.SubtleCrypto);
|
||||
}
|
||||
if (global.CustomEvent) {
|
||||
knownGlobals.push(global.CustomEvent);
|
||||
}
|
||||
|
||||
function allowGlobals(...allowlist) {
|
||||
knownGlobals = knownGlobals.concat(allowlist);
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
// Flags: --experimental-global-customevent --expose-internals
|
||||
'use strict';
|
||||
|
||||
require('../common');
|
||||
const { strictEqual, ok } = require('node:assert');
|
||||
const { CustomEvent: internalCustomEvent } = require('internal/event_target');
|
||||
|
||||
// Global
|
||||
ok(CustomEvent);
|
||||
|
||||
strictEqual(CustomEvent, internalCustomEvent);
|
||||
@@ -20,17 +20,13 @@
|
||||
"fail": {
|
||||
"unexpected": [
|
||||
"assert_true: expected true got false",
|
||||
"assert_array_equals: lengths differ, expected array [\"bubbles\", \"cancelable\"] length 2, got [\"cancelable\", \"bubbles\", \"sweet\"] length 3",
|
||||
"CustomEvent is not defined"
|
||||
"assert_array_equals: lengths differ, expected array [\"bubbles\", \"cancelable\"] length 2, got [\"cancelable\", \"bubbles\", \"sweet\"] length 3"
|
||||
]
|
||||
}
|
||||
},
|
||||
"EventListener-addEventListener.sub.window.js": {
|
||||
"fail": "document is not defined"
|
||||
},
|
||||
"EventTarget-constructible.any.js": {
|
||||
"fail": "CustomEvent is not defined"
|
||||
},
|
||||
"relatedTarget.window.js": {
|
||||
"fail": "document is not defined"
|
||||
},
|
||||
|
||||
@@ -4,4 +4,6 @@ const { WPTRunner } = require('../common/wpt');
|
||||
|
||||
const runner = new WPTRunner('dom/events');
|
||||
|
||||
runner.setFlags(['--experimental-global-customevent']);
|
||||
|
||||
runner.runJsTests();
|
||||
Reference in new issue
Block a user