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
th37rose committed Jun 29, 2020
1 parent 282d3f6 commit 745634c
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
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
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 745634c

Please sign in to comment.