Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -1191,9 +1191,20 @@ function receiverOnConclude(code, reason) {
const websocket = this[kWebSocket];

websocket._closeFrameReceived = true;
websocket._closeMessage = reason;
websocket._closeCode = code;

//
// The reason is a view into the buffer the socket delivered. Copy it so that
// the `WebSocket` does not keep that buffer alive. The copy is not pooled
// because a pooled one would keep the pool alive in the same way.
//
if (reason.length === 0) {
websocket._closeMessage = EMPTY_BUFFER;
} else {
websocket._closeMessage = Buffer.allocUnsafeSlow(reason.length);
websocket._closeMessage.set(reason);
}

if (websocket._socket[kWebSocket] === undefined) return;

websocket._socket.removeListener('data', socketOnData);
Expand Down
17 changes: 17 additions & 0 deletions test/websocket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5206,5 +5206,22 @@ describe('WebSocket', () => {
ws._socket.push(Buffer.concat(list));
});
});

it('does not retain the read buffer via the close reason', (done) => {
const wss = new WebSocket.Server({ port: 0 }, () => {
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);

ws.on('close', (code, reason) => {
assert.strictEqual(code, 1000);
assert.deepStrictEqual(reason, Buffer.from('bye'));
assert.strictEqual(reason.buffer.byteLength, reason.length);
wss.close(done);
});
});

wss.on('connection', (ws) => {
ws.close(1000, 'bye');
});
});
});
});
Loading