Skip to content

Commit

Permalink
lib: expose global CloseEvent
Browse files Browse the repository at this point in the history
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: #50275
PR-URL: #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>
  • Loading branch information
KhafraDev committed Jun 7, 2024
1 parent 0289e85 commit 6ed93b4
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 1 deletion.
14 changes: 14 additions & 0 deletions doc/api/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/eslint.config_partial.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/bootstrap/web/exposed-window-or-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
1 change: 1 addition & 0 deletions lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ function setupWarningHandler() {
function setupWebsocket() {
if (getOptionValue('--no-experimental-websocket')) {
delete globalThis.WebSocket;
delete globalThis.CloseEvent;
}
}

Expand Down
1 change: 1 addition & 0 deletions test/common/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ const webIdlExposedWindow = new Set([
'Response',
'WebSocket',
'EventSource',
'CloseEvent',
]);

const nodeGlobals = new Set([
Expand Down
1 change: 1 addition & 0 deletions test/eslint.config_partial.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default [
languageOptions: {
globals: {
...globals.node,
CloseEvent: true,
},
},
rules: {
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-websocket-disabled.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ require('../common');
const assert = require('assert');

assert.strictEqual(typeof WebSocket, 'undefined');
assert.strictEqual(typeof CloseEvent, 'undefined');
1 change: 1 addition & 0 deletions test/parallel/test-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ require('../common');
const assert = require('assert');

assert.strictEqual(typeof WebSocket, 'function');
assert.strictEqual(typeof CloseEvent, 'function');

0 comments on commit 6ed93b4

Please sign in to comment.