Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/web/websocket/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,8 @@ class WebSocket extends EventTarget {
}

if (!this.#handler.wasEverConnected) {
this.#handler.readyState = states.CLOSED

// If the WebSocket connection could not be established, it is also said
// that _The WebSocket Connection is Closed_, but not _cleanly_.
fireEvent('close', this, (type, init) => new CloseEvent(type, init), {
Expand Down
15 changes: 15 additions & 0 deletions test/websocket/issue-3697-2399493917.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

const { test } = require('node:test')
const { WebSocket } = require('../..')
const { tspl } = require('@matteo.collina/tspl')

// https://github.com/nodejs/undici/issues/3697#issuecomment-2399493917
test('closing before a connection is established changes readyState', async (t) => {
const { completed, strictEqual } = tspl(t, { plan: 1 })

const ws = new WebSocket('wss://example.com/non-existing-url')
ws.onclose = () => strictEqual(ws.readyState, WebSocket.CLOSED)

await completed
})