From 8f7787f81b185716406da752ff91f5ffa146c971 Mon Sep 17 00:00:00 2001 From: Charlie Bellini Date: Fri, 22 Nov 2024 00:04:47 +0100 Subject: [PATCH] fix(ext/websocket): don't throw exception when sending to closed socket (#26932) [The WebSocket specification for the `send` function](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send) says: > The browser will throw an exception if you call `send()` when the connection is in the `CONNECTING` state. If you call `send()` when the connection is in the `CLOSING` or `CLOSED` states, the browser will silently discard the data. and: > ### Exceptions > > `InvalidStateError` [`DOMException`](https://developer.mozilla.org/en-US/docs/Web/API/DOMException) > > Thrown if [`WebSocket.readyState`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState) is `CONNECTING`. This pull request fixes the current behavior to match the specification. Also, I believe it fixes #17586. --- ext/websocket/01_websocket.js | 6 +++++- tests/unit/websocket_test.ts | 15 +++++++++++++++ tests/wpt/runner/expectation.json | 4 ++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/ext/websocket/01_websocket.js b/ext/websocket/01_websocket.js index 468999b95d..78572f5f00 100644 --- a/ext/websocket/01_websocket.js +++ b/ext/websocket/01_websocket.js @@ -330,10 +330,14 @@ class WebSocket extends EventTarget { webidl.requiredArguments(arguments.length, 1, prefix); data = webidl.converters.WebSocketSend(data, prefix, "Argument 1"); - if (this[_readyState] !== OPEN) { + if (this[_readyState] === CONNECTING) { throw new DOMException("'readyState' not OPEN", "InvalidStateError"); } + if (this[_readyState] !== OPEN) { + return; + } + if (this[_sendQueue].length === 0) { // Fast path if the send queue is empty, for example when only synchronous // data is being sent. diff --git a/tests/unit/websocket_test.ts b/tests/unit/websocket_test.ts index 3aafe8da22..d9878828db 100644 --- a/tests/unit/websocket_test.ts +++ b/tests/unit/websocket_test.ts @@ -806,3 +806,18 @@ Deno.test("Close connection", async () => { await server.finished; conn.close(); }); + +Deno.test("send to a closed socket", async () => { + const { promise, resolve } = Promise.withResolvers(); + const ws = new WebSocket("ws://localhost:4242"); + const blob = new Blob(["foo"]); + ws.onerror = () => fail(); + ws.onopen = () => { + ws.close(); + ws.send(blob); + }; + ws.onclose = () => { + resolve(); + }; + await promise; +}); diff --git a/tests/wpt/runner/expectation.json b/tests/wpt/runner/expectation.json index 1a7cb8c560..6140f4379d 100644 --- a/tests/wpt/runner/expectation.json +++ b/tests/wpt/runner/expectation.json @@ -11303,10 +11303,10 @@ "006.html?default": true, "006.html?wpt_flags=h2": false, "006.html?wss": false, - "007.html?default": false, + "007.html?default": true, "007.html?wpt_flags=h2": false, "007.html?wss": false, - "008.html?default": false, + "008.html?default": true, "008.html?wss": false, "009.html?default": { "ignore": true