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
7 changes: 4 additions & 3 deletions lib/web/websocket/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function establishWebSocketConnection (url, protocols, client, handler, options)
// 1. If response is a network error or its status is not 101,
// fail the WebSocket connection.
if (response.type === 'error' || response.status !== 101) {
failWebsocketConnection(handler, 1002, 'Received network error or non-101 status code.')
failWebsocketConnection(handler, 1002, 'Received network error or non-101 status code.', response.error)
return
}

Expand Down Expand Up @@ -298,9 +298,10 @@ function closeWebSocketConnection (object, code, reason, validate = false) {
* @param {import('./websocket').Handler} handler
* @param {number} code
* @param {string|undefined} reason
* @param {unknown} cause
* @returns {void}
*/
function failWebsocketConnection (handler, code, reason) {
function failWebsocketConnection (handler, code, reason, cause) {
// If _The WebSocket Connection is Established_ prior to the point where
// the endpoint is required to _Fail the WebSocket Connection_, the
// endpoint SHOULD send a Close frame with an appropriate status code
Expand All @@ -315,7 +316,7 @@ function failWebsocketConnection (handler, code, reason) {
handler.socket.destroy()
}

handler.onFail(code, reason)
handler.onFail(code, reason, cause)
}

module.exports = {
Expand Down
6 changes: 3 additions & 3 deletions lib/web/websocket/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class WebSocket extends EventTarget {
/** @type {Handler} */
#handler = {
onConnectionEstablished: (response, extensions) => this.#onConnectionEstablished(response, extensions),
onFail: (code, reason) => this.#onFail(code, reason),
onFail: (code, reason, cause) => this.#onFail(code, reason, cause),
onMessage: (opcode, data) => this.#onMessage(opcode, data),
onParserError: (err) => failWebsocketConnection(this.#handler, null, err.message),
onParserDrain: () => this.#onParserDrain(),
Expand Down Expand Up @@ -462,11 +462,11 @@ class WebSocket extends EventTarget {
fireEvent('open', this)
}

#onFail (code, reason) {
#onFail (code, reason, cause) {
if (reason) {
// TODO: process.nextTick
fireEvent('error', this, (type, init) => new ErrorEvent(type, init), {
error: new Error(reason),
error: new Error(reason, cause ? { cause } : undefined),
message: reason
})
}
Expand Down
6 changes: 5 additions & 1 deletion test/types/websocket.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { ReadableStream, WritableStream } from 'stream/web'
import { expectType } from 'tsd'
import { WebSocketStream } from '../../types'
import { WebSocketStream, ErrorEvent } from '../../types'

declare const webSocketStream: WebSocketStream
const webSocketStreamOpened = await webSocketStream.opened

declare const errorEvent: ErrorEvent

// Test that the readable and writable streams are of identical types to ones from stream/web
expectType<WritableStream>(webSocketStreamOpened.writable)
expectType<ReadableStream>(webSocketStreamOpened.readable)

expectType<Error>(errorEvent.error)
17 changes: 17 additions & 0 deletions test/websocket/issue-4273.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

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

test('first error than close event is fired on failed connection', async (t) => {
const { completed, ok } = tspl(t, { plan: 1 })
const ws = new WebSocket('ws://localhost:1')

ws.addEventListener('error', (ev) => {
const { cause } = ev.error
ok(cause instanceof Error)
})

await completed
})
2 changes: 1 addition & 1 deletion types/websocket.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ interface ErrorEvent extends Event {
readonly filename: string
readonly lineno: number
readonly colno: number
readonly error: any
readonly error: Error
}

export declare const ErrorEvent: {
Expand Down
Loading