lib: expose global CloseEvent

This PR adds `CloseEvent` as a global, which can be disabled
via the --no-experimental-websocket flag.

```js
const ws = new WebSocket('...')

ws.addEventListener('close', (event) => {
  assert(event instanceof CloseEvent)
})

```

Fixes: https://github.com/nodejs/node/issues/50275
PR-URL: https://github.com/nodejs/node/pull/53355
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
pull/53366/head
Khafra 2024-06-07 13:54:28 -04:00 committed by GitHub
parent 0289e859ef
commit 6ed93b4d69
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 24 additions and 1 deletions

View File

@ -322,6 +322,19 @@ added: v0.0.1
[`clearTimeout`][] is described in the [timers][] section.
## `CloseEvent`
<!-- YAML
added: REPLACEME
-->
<!-- type=global -->
The `CloseEvent` class. See [`CloseEvent`][] for more details.
A browser-compatible implementation of [`CloseEvent`][]. Disable this API
with the [`--no-experimental-websocket`][] CLI flag.
## Class: `CompressionStream`
<!-- YAML
@ -1159,6 +1172,7 @@ A browser-compatible implementation of [`WritableStreamDefaultWriter`][].
[`--no-experimental-websocket`]: cli.md#--no-experimental-websocket
[`AbortController`]: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
[`ByteLengthQueuingStrategy`]: webstreams.md#class-bytelengthqueuingstrategy
[`CloseEvent`]: https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/CloseEvent
[`CompressionStream`]: webstreams.md#class-compressionstream
[`CountQueuingStrategy`]: webstreams.md#class-countqueuingstrategy
[`CustomEvent` Web API]: https://dom.spec.whatwg.org/#customevent

View File

@ -73,6 +73,10 @@ export default [
name: 'ByteLengthQueuingStrategy',
message: "Use `const { ByteLengthQueuingStrategy } = require('internal/webstreams/queuingstrategies')` instead of the global.",
},
{
name: 'CloseEvent',
message: "Use `const { CloseEvent } = require('internal/deps/undici/undici');` instead of the global.",
},
{
name: 'CompressionStream',
message: "Use `const { CompressionStream } = require('internal/webstreams/compression')` instead of the global.",

View File

@ -84,7 +84,7 @@ ObjectDefineProperty(globalThis, 'fetch', {
// https://fetch.spec.whatwg.org/#request-class
// https://fetch.spec.whatwg.org/#response-class
exposeLazyInterfaces(globalThis, 'internal/deps/undici/undici', [
'FormData', 'Headers', 'Request', 'Response', 'MessageEvent',
'FormData', 'Headers', 'Request', 'Response', 'MessageEvent', 'CloseEvent',
]);
// https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events.org/

View File

@ -304,6 +304,7 @@ function setupWarningHandler() {
function setupWebsocket() {
if (getOptionValue('--no-experimental-websocket')) {
delete globalThis.WebSocket;
delete globalThis.CloseEvent;
}
}

View File

@ -127,6 +127,7 @@ const webIdlExposedWindow = new Set([
'Response',
'WebSocket',
'EventSource',
'CloseEvent',
]);
const nodeGlobals = new Set([

View File

@ -13,6 +13,7 @@ export default [
languageOptions: {
globals: {
...globals.node,
CloseEvent: true,
},
},
rules: {

View File

@ -5,3 +5,4 @@ require('../common');
const assert = require('assert');
assert.strictEqual(typeof WebSocket, 'undefined');
assert.strictEqual(typeof CloseEvent, 'undefined');

View File

@ -4,3 +4,4 @@ require('../common');
const assert = require('assert');
assert.strictEqual(typeof WebSocket, 'function');
assert.strictEqual(typeof CloseEvent, 'function');