Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer fail over close the websocket connection in error cases #3651

Merged
merged 3 commits into from
Sep 27, 2024
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
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
Loading