Skip to content

Commit

Permalink
websocket: don't clone buffer if it's not needed. (#3214)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx committed May 7, 2024
1 parent 63b7794 commit 7150bed
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/web/websocket/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function websocketMessageReceived (ws, type, data) {
// -> type indicates that the data is Binary and binary type is "arraybuffer"
// a new ArrayBuffer object, created in the relevant Realm of the
// WebSocket object, whose contents are data
dataForEvent = new Uint8Array(data).buffer
dataForEvent = toArrayBuffer(data)
}
}

Expand All @@ -117,6 +117,13 @@ function websocketMessageReceived (ws, type, data) {
})
}

function toArrayBuffer (buffer) {
if (buffer.byteLength === buffer.buffer.byteLength) {
return buffer.buffer
}
return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength)
}

/**
* @see https://datatracker.ietf.org/doc/html/rfc6455
* @see https://datatracker.ietf.org/doc/html/rfc2616
Expand Down

0 comments on commit 7150bed

Please sign in to comment.