Count send buffer size in UTF-8 bytes (#1116)

The send buffer limit in controller.max_send_buffer_size is set in MiB, but
buffered messages were measured with String.length, which counts UTF-16 code
units. Messages with non-ASCII text could go over the configured size in bytes
before the session was ended.

Measure each serialised message with Buffer.byteLength when it is buffered,
keep that size with the entry, and subtract the same size when it is
acknowledged. The web UI build already provides Buffer through the buffer
polyfill.

Fixes #1108

Co-authored-by: ColumbusLabs <287001685+ColumbusLabs@users.noreply.github.com>
This commit is contained in:
Columbus LabsandColumbusLabs authored and GitHub committed 2026-10-18 17:44:27 +01:00
1 parent 43243eab86
commit ee39945c83
2 files changed
+84 -9

No files matched your search

+8 -6
View File
@@ -171,10 +171,11 @@ export abstract class WebSocketBaseConnector<
_heartbeatId: ReturnType<typeof setInterval> | null = null;
_heartbeatInterval: number | null = null;
_lastReceivedSeq = undefined;
/** Serialised messages sent but not yet acknowledged by the other side */
_sendBuffer: { seq: number, text: string }[] = [];
/** Serialised messages sent but not yet acknowledged by the other side, with their UTF-8 size in bytes */
_sendBuffer: { seq: number, text: string, size: number }[] = [];
/** Total UTF-8 size in bytes of the messages in the send buffer */
_sendBufferSize = 0;
/** Session is ended if the unacknowledged messages exceed this many characters, 0 for no limit */
/** Session is ended if the unacknowledged messages exceed this many bytes, 0 for no limit */
maxSendBufferSize = 0;
_reset() {
@@ -225,7 +226,7 @@ export abstract class WebSocketBaseConnector<
for (let index = 0; index < this._sendBuffer.length; index++) {
if (this._sendBuffer[index].seq <= seq) {
dropCount += 1;
this._sendBufferSize -= this._sendBuffer[index].text.length;
this._sendBufferSize -= this._sendBuffer[index].size;
} else {
break;
}
@@ -334,8 +335,9 @@ export abstract class WebSocketBaseConnector<
}
const text = JSON.stringify(message);
this._sendBuffer.push({ seq: message.seq, text });
this._sendBufferSize += text.length;
const size = Buffer.byteLength(text, "utf8");
this._sendBuffer.push({ seq: message.seq, text, size });
this._sendBufferSize += size;
if (this.maxSendBufferSize && this._sendBufferSize > this.maxSendBufferSize) {
this._sendBufferOverflow();
return;