Skip to content

Commit

Permalink
prefer fail over close the websocket connection in error cases (#3651)
Browse files Browse the repository at this point in the history
* fail websocket connection in error cases

* fail websocket connection in error cases

* remove dupe error event emits
  • Loading branch information
KhafraDev authored Sep 27, 2024
1 parent bebe53c commit ae18f0e
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 20 deletions.
3 changes: 1 addition & 2 deletions lib/web/websocket/receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const {
isContinuationFrame
} = require('./util')
const { WebsocketFrameSend } = require('./frame')
const { closeWebSocketConnection } = require('./connection')
const { PerMessageDeflate } = require('./permessage-deflate')

// This code was influenced by ws released under the MIT license.
Expand Down Expand Up @@ -225,7 +224,7 @@ class ByteParser extends Writable {
} else {
this.#extensions.get('permessage-deflate').decompress(body, this.#info.fin, (error, data) => {
if (error) {
closeWebSocketConnection(this.#handler, 1007, error.message)
failWebsocketConnection(this.#handler, 1007, error.message)
return
}

Expand Down
2 changes: 1 addition & 1 deletion lib/web/websocket/stream/websocketstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class WebSocketStream {
onConnectionEstablished: (response, extensions) => this.#onConnectionEstablished(response, extensions),
onFail: (reason) => this.#onFail(reason),
onMessage: (opcode, data) => this.#onMessage(opcode, data),
onParserError: (err) => {}, // eslint-disable-line
onParserError: (err) => failWebsocketConnection(this.#handler, null, err.message),
onParserDrain: () => this.#handler.socket.resume(),
onSocketData: (chunk) => {
if (!this.#parser.write(chunk)) {
Expand Down
18 changes: 1 addition & 17 deletions lib/web/websocket/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class WebSocket extends EventTarget {
onConnectionEstablished: (response, extensions) => this.#onConnectionEstablished(response, extensions),
onFail: (code, reason) => this.#onFail(code, reason),
onMessage: (opcode, data) => this.#onMessage(opcode, data),
onParserError: (err) => this.#onParserError(err),
onParserError: (err) => failWebsocketConnection(this.#handler, null, err.message),
onParserDrain: () => this.#onParserDrain(),
onSocketData: (chunk) => {
if (!this.#parser.write(chunk)) {
Expand Down Expand Up @@ -529,22 +529,6 @@ class WebSocket extends EventTarget {
})
}

#onParserError (err) {
let message
let code

if (err instanceof CloseEvent) {
message = err.reason
code = err.code
} else {
message = err.message
}

fireEvent('error', this, () => new ErrorEvent('error', { error: err, message }))

closeWebSocketConnection(this.#handler, code, null)
}

#onParserDrain () {
this.#handler.socket.resume()
}
Expand Down

0 comments on commit ae18f0e

Please sign in to comment.