From 29200f75b71a3960a3d170b1ac72e376d6f438dc Mon Sep 17 00:00:00 2001 From: steveluscher Date: Fri, 8 Sep 2023 00:07:22 +0000 Subject: [PATCH 1/2] test: assert that calling `close` in the `CONNECTING` state does not cause `onopen` to be called --- tests/functional/close-algorithm.test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/functional/close-algorithm.test.js b/tests/functional/close-algorithm.test.js index 2f840b46..62221849 100644 --- a/tests/functional/close-algorithm.test.js +++ b/tests/functional/close-algorithm.test.js @@ -88,6 +88,10 @@ test.cb('that if the readyState is CONNECTING we fail the connection and close', mockSocket.readyState = WebSocket.CONNECTING; + mockSocket.onopen = () => { + t.fail('open should not have been called'); + }; + mockSocket.onerror = () => { t.is(mockSocket.readyState, WebSocket.CLOSED); }; From 693b9bc9d28127b9f2a7e3a3a41f2d60ddb1a763 Mon Sep 17 00:00:00 2001 From: steveluscher Date: Fri, 8 Sep 2023 20:58:57 +0000 Subject: [PATCH 2/2] fix: bail from the `WebSocket` constructor's delayed setup if the `readyState` has changed from `CONNECTING` to something else --- src/websocket.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/websocket.js b/src/websocket.js index 5a9252aa..ecb3d2f3 100644 --- a/src/websocket.js +++ b/src/websocket.js @@ -51,6 +51,9 @@ class WebSocket extends EventTarget { * registered :-) */ delay(function delayCallback() { + if (this.readyState !== WebSocket.CONNECTING) { + return; + } if (server) { if ( server.options.verifyClient &&