* @see https://websockets.spec.whatwg.org/#dom-websocket-send * @param {NodeJS.TypedArray|ArrayBuffer|Blob|string} data
(data)
| 227 | * @param {NodeJS.TypedArray|ArrayBuffer|Blob|string} data |
| 228 | */ |
| 229 | send (data) { |
| 230 | webidl.brandCheck(this, WebSocket) |
| 231 | |
| 232 | const prefix = 'WebSocket.send' |
| 233 | webidl.argumentLengthCheck(arguments, 1, prefix) |
| 234 | |
| 235 | data = webidl.converters.WebSocketSendData(data, prefix, 'data') |
| 236 | |
| 237 | // 1. If this's ready state is CONNECTING, then throw an |
| 238 | // "InvalidStateError" DOMException. |
| 239 | if (isConnecting(this.#handler.readyState)) { |
| 240 | throw new DOMException('Sent before connected.', 'InvalidStateError') |
| 241 | } |
| 242 | |
| 243 | // 2. Run the appropriate set of steps from the following list: |
| 244 | // https://datatracker.ietf.org/doc/html/rfc6455#section-6.1 |
| 245 | // https://datatracker.ietf.org/doc/html/rfc6455#section-5.2 |
| 246 | |
| 247 | if (!isEstablished(this.#handler.readyState) || isClosing(this.#handler.readyState)) { |
| 248 | return |
| 249 | } |
| 250 | |
| 251 | // If data is a string |
| 252 | if (typeof data === 'string') { |
| 253 | // If the WebSocket connection is established and the WebSocket |
| 254 | // closing handshake has not yet started, then the user agent |
| 255 | // must send a WebSocket Message comprised of the data argument |
| 256 | // using a text frame opcode; if the data cannot be sent, e.g. |
| 257 | // because it would need to be buffered but the buffer is full, |
| 258 | // the user agent must flag the WebSocket as full and then close |
| 259 | // the WebSocket connection. Any invocation of this method with a |
| 260 | // string argument that does not throw an exception must increase |
| 261 | // the bufferedAmount attribute by the number of bytes needed to |
| 262 | // express the argument as UTF-8. |
| 263 | |
| 264 | const buffer = Buffer.from(data) |
| 265 | |
| 266 | this.#bufferedAmount += buffer.byteLength |
| 267 | this.#sendQueue.add(buffer, () => { |
| 268 | this.#bufferedAmount -= buffer.byteLength |
| 269 | }, sendHints.text) |
| 270 | } else if (isArrayBuffer(data)) { |
| 271 | // If the WebSocket connection is established, and the WebSocket |
| 272 | // closing handshake has not yet started, then the user agent must |
| 273 | // send a WebSocket Message comprised of data using a binary frame |
| 274 | // opcode; if the data cannot be sent, e.g. because it would need |
| 275 | // to be buffered but the buffer is full, the user agent must flag |
| 276 | // the WebSocket as full and then close the WebSocket connection. |
| 277 | // The data to be sent is the data stored in the buffer described |
| 278 | // by the ArrayBuffer object. Any invocation of this method with an |
| 279 | // ArrayBuffer argument that does not throw an exception must |
| 280 | // increase the bufferedAmount attribute by the length of the |
| 281 | // ArrayBuffer in bytes. |
| 282 | |
| 283 | this.#bufferedAmount += data.byteLength |
| 284 | this.#sendQueue.add(data, () => { |
| 285 | this.#bufferedAmount -= data.byteLength |
| 286 | }, sendHints.arrayBuffer) |
nothing calls this directly
no test coverage detected