Skip to content

Commit e1349c0

Browse files
committed
[fix] Take into account the data that is being compressed
Improve `websocket.bufferedAmount` accuracy by taking into account the number of bytes of a message while it is being compressed.
1 parent 0954abc commit e1349c0

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

lib/sender.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ class Sender {
318318

319319
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
320320

321+
this._bufferedBytes += data.length;
321322
this._deflating = true;
322323
perMessageDeflate.compress(data, options.fin, (_, buf) => {
323324
if (this._socket.destroyed) {
@@ -336,6 +337,7 @@ class Sender {
336337
return;
337338
}
338339

340+
this._bufferedBytes -= data.length;
339341
this._deflating = false;
340342
options.readOnly = false;
341343
this.sendFrame(Sender.frame(buf, options), cb);

test/websocket.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,16 @@ describe('WebSocket', () => {
185185

186186
ws.on('open', () => {
187187
ws.send('foo');
188+
189+
assert.strictEqual(ws.bufferedAmount, 3);
190+
188191
ws.send('bar', (err) => {
189192
assert.ifError(err);
190193
assert.strictEqual(ws.bufferedAmount, 0);
191194
wss.close(done);
192195
});
193196

194-
assert.strictEqual(ws.bufferedAmount, 3);
197+
assert.strictEqual(ws.bufferedAmount, 6);
195198
});
196199
}
197200
);

0 commit comments

Comments
 (0)