Skip to content

Commit

Permalink
[fix] Take into account the data that is being compressed
Browse files Browse the repository at this point in the history
Improve `websocket.bufferedAmount` accuracy by taking into account the
number of bytes of a message while it is being compressed.
  • Loading branch information
lpinca committed Jun 24, 2020
1 parent 0954abc commit e1349c0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/sender.js
Expand Up @@ -318,6 +318,7 @@ class Sender {

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

this._bufferedBytes += data.length;
this._deflating = true;
perMessageDeflate.compress(data, options.fin, (_, buf) => {
if (this._socket.destroyed) {
Expand All @@ -336,6 +337,7 @@ class Sender {
return;
}

this._bufferedBytes -= data.length;
this._deflating = false;
options.readOnly = false;
this.sendFrame(Sender.frame(buf, options), cb);
Expand Down
5 changes: 4 additions & 1 deletion test/websocket.test.js
Expand Up @@ -185,13 +185,16 @@ describe('WebSocket', () => {

ws.on('open', () => {
ws.send('foo');

assert.strictEqual(ws.bufferedAmount, 3);

ws.send('bar', (err) => {
assert.ifError(err);
assert.strictEqual(ws.bufferedAmount, 0);
wss.close(done);
});

assert.strictEqual(ws.bufferedAmount, 3);
assert.strictEqual(ws.bufferedAmount, 6);
});
}
);
Expand Down

0 comments on commit e1349c0

Please sign in to comment.