Skip to content

Commit

Permalink
events: expose CustomEvent on global with CLI flag
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
daeyeon authored and Fyko committed Sep 15, 2022
1 parent 46415d7 commit 0502d8f
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 5 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ module.exports = {
ByteLengthQueuingStrategy: 'readable',
CompressionStream: 'readable',
CountQueuingStrategy: 'readable',
CustomEvent: 'readable',
Crypto: 'readable',
CryptoKey: 'readable',
DecompressionStream: 'readable',
Expand Down
10 changes: 10 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@ effort to report stack traces relative to the original source file.
Overriding `Error.prepareStackTrace` prevents `--enable-source-maps` from
modifying the stack trace.

### `--experimental-global-customevent`

<!-- YAML
added: REPLACEME
-->

Expose the [CustomEvent Web API][] on the global scope.

### `--experimental-global-webcrypto`

<!-- YAML
Expand Down Expand Up @@ -1652,6 +1660,7 @@ Node.js options that are allowed are:
* `--enable-fips`
* `--enable-source-maps`
* `--experimental-abortcontroller`
* `--experimental-global-customevent`
* `--experimental-global-webcrypto`
* `--experimental-import-meta-resolve`
* `--experimental-json-modules`
Expand Down Expand Up @@ -2077,6 +2086,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
Expand Down
15 changes: 15 additions & 0 deletions doc/api/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,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][].

## Class: `DecompressionStream`

<!-- YAML
Expand Down Expand Up @@ -852,12 +865,14 @@ added: v18.0.0
A browser-compatible implementation of [`WritableStreamDefaultWriter`][].

[Web Crypto API]: webcrypto.md
[`--experimental-global-customevent`]: cli.md#--experimental-global-customevent
[`--experimental-global-webcrypto`]: cli.md#--experimental-global-webcrypto
[`--no-experimental-fetch`]: cli.md#--no-experimental-fetch
[`AbortController`]: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
[`ByteLengthQueuingStrategy`]: webstreams.md#class-bytelengthqueuingstrategy
[`CompressionStream`]: webstreams.md#class-compressionstream
[`CountQueuingStrategy`]: webstreams.md#class-countqueuingstrategy
[`CustomEvent` Web API]: https://dom.spec.whatwg.org/#customevent
[`DOMException`]: https://developer.mozilla.org/en-US/docs/Web/API/DOMException
[`DecompressionStream`]: webstreams.md#class-decompressionstream
[`EventTarget` and `Event` API]: events.md#eventtarget-and-event-api
Expand Down
3 changes: 3 additions & 0 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ Requires Node.js to be built with
.It Fl -enable-source-maps
Enable Source Map V3 support for stack traces.
.
.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.
.
Expand Down
2 changes: 2 additions & 0 deletions lib/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ rules:
message: Use `const { CompressionStream } = require('internal/webstreams/compression')` instead of the global.
- name: CountQueuingStrategy
message: Use `const { CountQueuingStrategy } = require('internal/webstreams/queuingstrategies')` instead of the global.
- name: CustomEvent
message: Use `const { CustomEvent } = require('internal/event_target');` instead of the global.
- name: DecompressionStream
message: Use `const { DecompressionStream } = require('internal/webstreams/compression')` instead of the global.
- name: DOMException
Expand Down
13 changes: 13 additions & 0 deletions lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,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
Expand Down Expand Up @@ -249,6 +250,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) {
Expand Down Expand Up @@ -567,6 +579,7 @@ module.exports = {
setupWarningHandler,
setupFetch,
setupWebCrypto,
setupCustomEvent,
setupDebugEnv,
setupPerfHooks,
prepareMainThreadExecution,
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const {
setupWarningHandler,
setupFetch,
setupWebCrypto,
setupCustomEvent,
setupDebugEnv,
setupPerfHooks,
initializeDeprecations,
Expand Down Expand Up @@ -71,6 +72,7 @@ setupDebugEnv();
setupWarningHandler();
setupFetch();
setupWebCrypto();
setupCustomEvent();
initializeSourceMapsHandlers();

// Since worker threads cannot switch cwd, we do not need to
Expand Down
4 changes: 4 additions & 0 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
&EnvironmentOptions::experimental_fetch,
kAllowedInEnvironment,
true);
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,
Expand Down
1 change: 1 addition & 0 deletions src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class EnvironmentOptions : public Options {
std::string dns_result_order;
bool enable_source_maps = false;
bool experimental_fetch = true;
bool experimental_global_customevent = false;
bool experimental_global_web_crypto = false;
bool experimental_https_modules = false;
std::string experimental_specifier_resolution;
Expand Down
3 changes: 3 additions & 0 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ if (hasCrypto && global.crypto) {
knownGlobals.push(global.CryptoKey);
knownGlobals.push(global.SubtleCrypto);
}
if (global.CustomEvent) {
knownGlobals.push(global.CustomEvent);
}
if (global.ReadableStream) {
knownGlobals.push(
global.ReadableStream,
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-global-customevent.js
Original file line number Diff line number Diff line change
@@ -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);
6 changes: 1 addition & 5 deletions test/wpt/status/dom/events.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 2 additions & 0 deletions test/wpt/test-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ const { WPTRunner } = require('../common/wpt');

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

runner.setFlags(['--experimental-global-customevent']);

runner.runJsTests();

0 comments on commit 0502d8f

Please sign in to comment.