Skip to content

Commit

Permalink
set ws readyState if closed before connection could be established
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Oct 8, 2024
1 parent 40fb24d commit 2d9cda4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
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
})

0 comments on commit 2d9cda4

Please sign in to comment.