Skip to content

Commit d5b18a8

Browse files
committed
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.
1 parent 23b7219 commit d5b18a8

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

core/websock.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ export default class Websock {
124124
return res >>> 0;
125125
}
126126

127+
rQlen() {
128+
return this._rQlen - this._rQi;
129+
}
130+
127131
rQshiftStr(len) {
128132
let str = "";
129133
// Handle large arrays in steps to avoid long strings on the stack

tests/test.websock.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ describe('Websock', function () {
4747
});
4848
});
4949

50+
describe('rQlen())', function () {
51+
it('should return the number of buffered bytes in the receive queue', function () {
52+
websock._receiveData(new Uint8Array([0xab, 0xcd, 0x12, 0x34,
53+
0x88, 0xee, 0x11, 0x33]));
54+
expect(sock.rQlen()).to.equal(8);
55+
sock.rQshift8();
56+
expect(sock.rQlen()).to.equal(7);
57+
sock.rQshift16();
58+
expect(sock.rQlen()).to.equal(5);
59+
sock.rQshift32();
60+
expect(sock.rQlen()).to.equal(1);
61+
});
62+
});
63+
5064
describe('rQshiftStr', function () {
5165
it('should shift the given number of bytes off of the receive queue and return a string', function () {
5266
websock._receiveData(new Uint8Array([0xab, 0xcd, 0x12, 0x34,

0 commit comments

Comments
 (0)