Expose length of buffered WebSocket data

Some encodings don't know how much data they need, rather they must
probe the data stream until they find an end marker. Expose how much
data is buffered in order to make this search efficient.
This commit is contained in:
Pierre Ossman
2025-09-03 11:24:34 +02:00
parent 23b7219a5d
commit d5b18a84ab
2 changed files with 18 additions and 0 deletions

View File

@@ -124,6 +124,10 @@ export default class Websock {
return res >>> 0;
}
rQlen() {
return this._rQlen - this._rQi;
}
rQshiftStr(len) {
let str = "";
// Handle large arrays in steps to avoid long strings on the stack

View File

@@ -47,6 +47,20 @@ describe('Websock', function () {
});
});
describe('rQlen())', function () {
it('should return the number of buffered bytes in the receive queue', function () {
websock._receiveData(new Uint8Array([0xab, 0xcd, 0x12, 0x34,
0x88, 0xee, 0x11, 0x33]));
expect(sock.rQlen()).to.equal(8);
sock.rQshift8();
expect(sock.rQlen()).to.equal(7);
sock.rQshift16();
expect(sock.rQlen()).to.equal(5);
sock.rQshift32();
expect(sock.rQlen()).to.equal(1);
});
});
describe('rQshiftStr', function () {
it('should shift the given number of bytes off of the receive queue and return a string', function () {
websock._receiveData(new Uint8Array([0xab, 0xcd, 0x12, 0x34,