From 6818acc06830a3119720bc8e97ae286ee0ebebb1 Mon Sep 17 00:00:00 2001 From: eXhumer Date: Sun, 8 Sep 2024 01:29:56 -0600 Subject: [PATCH] fix: fire close on failed WebSocket connection * see nodejs/undici#3546, nodejs/undici#3548 & nodejs/undici#3565 Signed-off-by: eXhumer --- lib/web/websocket/websocket.js | 8 ++++++++ test/websocket/issue-3546.js | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/web/websocket/websocket.js b/lib/web/websocket/websocket.js index 7e465f56e99..6263443dde7 100644 --- a/lib/web/websocket/websocket.js +++ b/lib/web/websocket/websocket.js @@ -537,6 +537,14 @@ class WebSocket extends EventTarget { message: reason }) } + + if (!this.#parser && !this.#handler.receivedClose) { + fireEvent('close', this, (type, init) => new CloseEvent(type, init), { + wasClean: false, + code: 1006, + reason + }) + } } #onMessage (type, data) { diff --git a/test/websocket/issue-3546.js b/test/websocket/issue-3546.js index 5d328902538..59a7bd0f093 100644 --- a/test/websocket/issue-3546.js +++ b/test/websocket/issue-3546.js @@ -5,7 +5,7 @@ const { WebSocket } = require('../..') const { tspl } = require('@matteo.collina/tspl') test('first error than close event is fired on failed connection', async (t) => { - const { completed, strictEqual } = tspl(t, { plan: 2 }) + const { completed, strictEqual } = tspl(t, { plan: 4 }) const ws = new WebSocket('ws://localhost:1') let orderOfEvents = 0