Skip to content

Commit 6a6ae04

Browse files
committed
[minor] Send the close status code only when necessary
1 parent 85919f2 commit 6a6ae04

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

lib/Sender.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const crypto = require('crypto');
1212
const PerMessageDeflate = require('./PerMessageDeflate');
1313
const bufferUtil = require('./BufferUtil');
1414
const ErrorCodes = require('./ErrorCodes');
15+
const constants = require('./Constants');
1516

1617
const Buffer = safeBuffer.Buffer;
1718

@@ -112,14 +113,26 @@ class Sender {
112113
* @public
113114
*/
114115
close (code, data, mask, cb) {
115-
if (code !== undefined && (typeof code !== 'number' || !ErrorCodes.isValidErrorCode(code))) {
116+
var buf;
117+
118+
if (code === undefined) {
119+
code = 1000;
120+
} else if (typeof code !== 'number' || !ErrorCodes.isValidErrorCode(code)) {
116121
throw new Error('first argument must be a valid error code number');
117122
}
118123

119-
const buf = Buffer.allocUnsafe(2 + (data ? Buffer.byteLength(data) : 0));
120-
121-
buf.writeUInt16BE(code || 1000, 0, true);
122-
if (buf.length > 2) buf.write(data, 2);
124+
if (data === undefined || data === '') {
125+
if (code === 1000) {
126+
buf = constants.EMPTY_BUFFER;
127+
} else {
128+
buf = Buffer.allocUnsafe(2);
129+
buf.writeUInt16BE(code, 0, true);
130+
}
131+
} else {
132+
buf = Buffer.allocUnsafe(2 + Buffer.byteLength(data));
133+
buf.writeUInt16BE(code, 0, true);
134+
buf.write(data, 2);
135+
}
123136

124137
if (this._deflating) {
125138
this.enqueue([this.doClose, buf, mask, cb]);

test/Sender.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ describe('Sender', function () {
267267
sender.send('bar', { compress: true, fin: true });
268268
sender.send('baz', { compress: true, fin: true });
269269

270-
sender.close(1000, null, false, () => {
270+
sender.close(1000, undefined, false, () => {
271271
assert.strictEqual(count, 4);
272272
done();
273273
});

test/WebSocket.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,34 @@ describe('WebSocket', function () {
12031203
});
12041204
});
12051205

1206+
it('sends the close status code only when necessary', function (done) {
1207+
let sent;
1208+
const wss = new WebSocket.Server({ port: 0 }, () => {
1209+
const port = wss._server.address().port;
1210+
const ws = new WebSocket(`ws://localhost:${port}`);
1211+
1212+
ws.on('open', () => {
1213+
ws._socket.once('data', (data) => {
1214+
sent = data;
1215+
});
1216+
});
1217+
});
1218+
1219+
wss.on('connection', (ws) => {
1220+
ws._socket.once('data', (received) => {
1221+
assert.ok(received.slice(0, 2).equals(Buffer.from([0x88, 0x80])));
1222+
assert.ok(sent.equals(Buffer.from([0x88, 0x00])));
1223+
1224+
ws.on('close', (code, reason) => {
1225+
assert.strictEqual(code, 1000);
1226+
assert.strictEqual(reason, '');
1227+
wss.close(done);
1228+
});
1229+
});
1230+
ws.close();
1231+
});
1232+
});
1233+
12061234
it('works when close reason is not specified', function (done) {
12071235
const wss = new WebSocket.Server({ port: 0 }, () => {
12081236
const port = wss._server.address().port;

0 commit comments

Comments
 (0)