Skip to content

Commit

Permalink
fix(websocket): simplify typedarray copying (nodejs#1854)
Browse files Browse the repository at this point in the history
* fix(websocket): simplify typedarray copying

* fix: simplify even further

* and again
  • Loading branch information
KhafraDev authored and anonrig committed Apr 4, 2023
1 parent aa82d4d commit 3dfc641
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions lib/websocket/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,23 +309,14 @@ class WebSocket extends EventTarget {
// not throw an exception must increase the bufferedAmount attribute
// by the length of data’s buffer in bytes.

const ab = new ArrayBuffer(data.byteLength)
const ab = Buffer.from(data, data.byteOffset, data.byteLength)

if (Buffer.isBuffer(data)) {
// new Buffer signature is deprecated
Buffer.from(ab).set(data)
} else {
new data.constructor(ab).set(data)
}

const value = Buffer.from(ab)

const frame = new WebsocketFrameSend(value)
const frame = new WebsocketFrameSend(ab)
const buffer = frame.createFrame(opcodes.BINARY)

this.#bufferedAmount += value.byteLength
this.#bufferedAmount += ab.byteLength
socket.write(buffer, () => {
this.#bufferedAmount -= value.byteLength
this.#bufferedAmount -= ab.byteLength
})
} else if (isBlobLike(data)) {
// If the WebSocket connection is established, and the WebSocket
Expand Down

0 comments on commit 3dfc641

Please sign in to comment.