Skip to content

Commit

Permalink
optimize: PWSL parseFrameHeader
Browse files Browse the repository at this point in the history
This commit optimizes PWSL by removing unecessary code, as servers must not mask their data.
  • Loading branch information
ThePedroo committed Nov 22, 2023
1 parent 8b5700c commit 1ec0b0a
Showing 1 changed file with 1 addition and 21 deletions.
22 changes: 1 addition & 21 deletions src/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ function parseFrameHeader(buffer) {

const opcode = buffer[0] & 0b00001111
const fin = (buffer[0] & 0b10000000) == 0b10000000
const isMasked = (buffer[1] & 0x80) == 0x80
let payloadLength = buffer[1] & 0b01111111

if (payloadLength == 126) {
Expand All @@ -24,20 +23,7 @@ function parseFrameHeader(buffer) {
startIndex += 8
}

let mask = null

if (isMasked) {
mask = buffer.subarray(startIndex, startIndex + 4)
startIndex += 4

buffer = buffer.subarray(startIndex, startIndex + payloadLength)

for (let i = 0; i < buffer.length; i++) {
buffer[i] ^= mask[i & 3];
}
} else {
buffer = buffer.subarray(startIndex, startIndex + payloadLength)
}
buffer = buffer.subarray(startIndex, startIndex + payloadLength)

return {
opcode,
Expand Down Expand Up @@ -238,12 +224,6 @@ class WebSocket extends EventEmitter {
return true
}

send(data) {
const payload = Buffer.from(data, 'utf-8')

return this.sendData(payload, { len: payload.length, fin: true, opcode: 0x01, mask: true })
}

close(code, reason) {
const data = Buffer.allocUnsafe(2 + Buffer.byteLength(reason || 'normal close'))
data.writeUInt16BE(code || 1000)
Expand Down

0 comments on commit 1ec0b0a

Please sign in to comment.