Skip to content

Commit

Permalink
[fix] Use socket._writableState.length instead of socket.bufferSize
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Jun 29, 2020
1 parent 41b0f9b commit a162942
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
5 changes: 1 addition & 4 deletions lib/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,7 @@ class WebSocket extends EventEmitter {
get bufferedAmount() {
if (!this._socket) return this._bufferedAmount;

//
// `socket.bufferSize` is `undefined` if the socket is closed.
//
return (this._socket.bufferSize || 0) + this._sender._bufferedBytes;
return this._socket._writableState.length + this._sender._bufferedBytes;
}

/**
Expand Down
9 changes: 6 additions & 3 deletions test/websocket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,15 @@ describe('WebSocket', () => {
wss.on('connection', (ws) => {
const data = Buffer.alloc(1024, 61);

while (ws._socket.bufferSize === 0) {
while (ws.bufferedAmount === 0) {
ws.send(data);
}

assert.ok(ws._socket.bufferSize > 0);
assert.strictEqual(ws.bufferedAmount, ws._socket.bufferSize);
assert.ok(ws.bufferedAmount > 0);
assert.strictEqual(
ws.bufferedAmount,
ws._socket._writableState.length
);

ws.on('close', () => wss.close(done));
ws.close();
Expand Down

0 comments on commit a162942

Please sign in to comment.