Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/web/websocket/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const parserStates = {
const emptyBuffer = Buffer.allocUnsafe(0)

const sendHints = {
string: 1,
text: 1,
typedArray: 2,
arrayBuffer: 3,
blob: 4
Expand Down
14 changes: 5 additions & 9 deletions lib/web/websocket/sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ const { WebsocketFrameSend } = require('./frame')
const { opcodes, sendHints } = require('./constants')
const FixedQueue = require('../../dispatcher/fixed-queue')

/** @type {typeof Uint8Array} */
const FastBuffer = Buffer[Symbol.species]

/**
* @typedef {object} SendQueueNode
* @property {Promise<void> | null} promise
Expand Down Expand Up @@ -86,18 +83,17 @@ class SendQueue {
}

function createFrame (data, hint) {
return new WebsocketFrameSend(toBuffer(data, hint)).createFrame(hint === sendHints.string ? opcodes.TEXT : opcodes.BINARY)
return new WebsocketFrameSend(toBuffer(data, hint)).createFrame(hint === sendHints.text ? opcodes.TEXT : opcodes.BINARY)
}

function toBuffer (data, hint) {
switch (hint) {
case sendHints.string:
return Buffer.from(data)
case sendHints.text:
case sendHints.typedArray:
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength)
case sendHints.arrayBuffer:
case sendHints.blob:
return new FastBuffer(data)
case sendHints.typedArray:
return new FastBuffer(data.buffer, data.byteOffset, data.byteLength)
return new Uint8Array(data)
}
}

Expand Down
10 changes: 5 additions & 5 deletions lib/web/websocket/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@ class WebSocket extends EventTarget {
// the bufferedAmount attribute by the number of bytes needed to
// express the argument as UTF-8.

const length = Buffer.byteLength(data)
const buffer = Buffer.from(data)

this.#bufferedAmount += length
this.#sendQueue.add(data, () => {
this.#bufferedAmount -= length
}, sendHints.string)
this.#bufferedAmount += buffer.byteLength
this.#sendQueue.add(buffer, () => {
this.#bufferedAmount -= buffer.byteLength
}, sendHints.text)
} else if (types.isArrayBuffer(data)) {
// If the WebSocket connection is established, and the WebSocket
// closing handshake has not yet started, then the user agent must
Expand Down