Skip to content

Commit

Permalink
fix: PWSL continue frames
Browse files Browse the repository at this point in the history
This commit fixes the continue frames of PWSL.
  • Loading branch information
ThePedroo committed Jan 20, 2024
1 parent 6354be9 commit 63c8a43
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions src/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class WebSocket extends EventEmitter {
this.options = options
this.socket = null
this.cachedData = []
this.continuedData = -1

this.connect()

Expand Down Expand Up @@ -128,20 +129,23 @@ class WebSocket extends EventEmitter {
this.cachedData.push(headers.buffer)

if (headers.fin) {
this.emit('message', Buffer.concat(this.cachedData).toString())
this.emit('message', (this.continuedData === 1 ? this.cachedData.join('') : Buffer.concat(this.cachedData)))

this.cachedData = []
this.continuedData = -1
}

break
}
case 0x1: {
this.emit('message', headers.buffer.toString())

break
}
case 0x1:
case 0x2: {
this.emit('message', headers.buffer)
if (!headers.fin) {
this.continuedData = headers.opcode

this.cachedData.push(headers.buffer)
} else {
this.emit('message', headers.opcode === 0x1 ? headers.buffer.toString('utf8') : headers.buffer)
}

break
}
Expand All @@ -155,7 +159,7 @@ class WebSocket extends EventEmitter {
this.emit('close', code, reason)
}

socket.end()
this.cleanup()

break
}
Expand All @@ -177,7 +181,11 @@ class WebSocket extends EventEmitter {
this.socket.unshift(headers.buffer)
})

socket.on('close', () => this.emit('close'))
socket.on('close', () => {
this.emit('close')

this.cleanup()
})

this.socket = socket

Expand All @@ -187,6 +195,16 @@ class WebSocket extends EventEmitter {
request.end()
}

cleanup() {
this.socket.destroy()
this.socket = null

this.cachedData = []
this.continuedData = -1

return true
}

sendData(data, options) {
let payloadStartIndex = 2
let payloadLength = options.len
Expand Down

0 comments on commit 63c8a43

Please sign in to comment.