Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed May 16, 2024
1 parent a92896e commit 36363c3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
4 changes: 1 addition & 3 deletions lib/web/websocket/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,7 @@ function closeWebSocketConnection (ws, code, reason, reasonByteLength) {
* @param {Buffer} chunk
*/
function onSocketData (chunk) {
const parser = this.ws[kByteParser]

if (parser.writable && !parser.write(chunk)) {
if (!this.ws[kByteParser].write(chunk)) {
this.pause()
}
}
Expand Down
15 changes: 14 additions & 1 deletion lib/web/websocket/permessage-deflate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ class PerMessageDeflate {
/** @type {import('node:zlib').InflateRaw} */
#inflate

#options = {}

constructor (extensions) {
this.#options.clientNoContextTakeover = extensions.has('client_no_context_takeover')
}

decompress (chunk, fin, callback) {
// An endpoint uses the following algorithm to decompress a message.
// 1. Append 4 octets of 0x00 0x00 0xff 0xff to the tail end of the
Expand All @@ -25,6 +31,8 @@ class PerMessageDeflate {
this.#inflate[kBuffer].push(data)
this.#inflate[kLength] += data.length
})

this.#inflate.on('error', (err) => callback(err))
}

this.#inflate.write(chunk)
Expand All @@ -34,10 +42,15 @@ class PerMessageDeflate {

this.#inflate.flush(() => {
const full = Buffer.concat(this.#inflate[kBuffer], this.#inflate[kLength])
callback(full)

this.#inflate[kBuffer].length = 0
this.#inflate[kLength] = 0

callback(null, full)

if (fin && this.#options.clientNoContextTakeover) {
this.#inflate.reset()
}
})
}
}
Expand Down
10 changes: 7 additions & 3 deletions lib/web/websocket/receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ByteParser extends Writable {
this.#extensions = extensions == null ? new Map() : extensions

if (this.#extensions.has('permessage-deflate')) {
this.#extensions.set('permessage-deflate', new PerMessageDeflate())
this.#extensions.set('permessage-deflate', new PerMessageDeflate(extensions))
}
}

Expand Down Expand Up @@ -218,7 +218,12 @@ class ByteParser extends Writable {
this.#fragments.length = 0
}
} else {
this.#extensions.get('permessage-deflate').decompress(body, this.#info.fin, (data) => {
this.#extensions.get('permessage-deflate').decompress(body, this.#info.fin, (error, data) => {
if (error) {
closeWebSocketConnection(this.ws, 1007, error.message, error.message.length)
return
}

websocketMessageReceived(this.ws, this.#info.binaryType, data)

this.#loop = true
Expand Down Expand Up @@ -363,7 +368,6 @@ class ByteParser extends Writable {
this.ws[kReadyState] = states.CLOSING
this.ws[kReceivedClose] = true

this.end()
return false
} else if (opcode === opcodes.PING) {
// Upon receipt of a Ping frame, an endpoint MUST send a Pong frame in
Expand Down

0 comments on commit 36363c3

Please sign in to comment.