Skip to content

Commit a31b1f6

Browse files
committedJan 5, 2018
[fix] Use 1005 status code if close frame payload length is 0
Fixes #1257
1 parent f941a40 commit a31b1f6

File tree

6 files changed

+17
-19
lines changed

6 files changed

+17
-19
lines changed
 

‎doc/ws.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ not yet transmitted to the network.
308308

309309
Received bytes count.
310310

311-
### websocket.close([code][, reason])
311+
### websocket.close([code[, reason]])
312312

313313
- `code` {Number} A numeric value indicating the status code explaining why
314314
the connection is being closed.

‎lib/Receiver.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ class Receiver {
402402
controlMessage (data) {
403403
if (this._opcode === 0x08) {
404404
if (data.length === 0) {
405-
this.onclose(1000, '');
405+
this.onclose(1005, '');
406406
this._loop = false;
407407
this.cleanup(this._cleanupCallback);
408408
} else if (data.length === 1) {

‎lib/Sender.js

+4-10
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,12 @@ class Sender {
116116
var buf;
117117

118118
if (code === undefined) {
119-
code = 1000;
119+
buf = constants.EMPTY_BUFFER;
120120
} else if (typeof code !== 'number' || !ErrorCodes.isValidErrorCode(code)) {
121121
throw new Error('first argument must be a valid error code number');
122-
}
123-
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-
}
122+
} else if (data === undefined || data === '') {
123+
buf = Buffer.allocUnsafe(2);
124+
buf.writeUInt16BE(code, 0, true);
131125
} else {
132126
buf = Buffer.allocUnsafe(2 + Buffer.byteLength(data));
133127
buf.writeUInt16BE(code, 0, true);

‎lib/WebSocket.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,11 @@ class WebSocket extends EventEmitter {
148148
this._closeFrameReceived = true;
149149
this._closeMessage = reason;
150150
this._closeCode = code;
151-
if (!this._finalized) this.close(code, reason);
151+
152+
if (this._finalized) return;
153+
154+
if (code === 1005) this.close();
155+
else this.close(code, reason);
152156
};
153157
this._receiver.onerror = (error, code) => {
154158
this._closeMessage = '';

‎test/Receiver.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('Receiver', function () {
2626
const p = new Receiver();
2727

2828
p.onclose = function (code, data) {
29-
assert.strictEqual(code, 1000);
29+
assert.strictEqual(code, 1005);
3030
assert.strictEqual(data, '');
3131
done();
3232
};
@@ -836,7 +836,7 @@ describe('Receiver', function () {
836836
assert.strictEqual(p._bufferedBytes, textFrame.length + closeFrame.length);
837837

838838
p.cleanup(() => {
839-
assert.deepStrictEqual(results, ['Hello', 'Hello', 1000, '']);
839+
assert.deepStrictEqual(results, ['Hello', 'Hello', 1005, '']);
840840
assert.strictEqual(p.onmessage, null);
841841
done();
842842
});

‎test/WebSocket.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@ describe('WebSocket', function () {
12221222
assert.ok(sent.equals(Buffer.from([0x88, 0x00])));
12231223

12241224
ws.on('close', (code, reason) => {
1225-
assert.strictEqual(code, 1000);
1225+
assert.strictEqual(code, 1005);
12261226
assert.strictEqual(reason, '');
12271227
wss.close(done);
12281228
});
@@ -1295,7 +1295,7 @@ describe('WebSocket', function () {
12951295

12961296
ws.on('message', (message) => messages.push(message));
12971297
ws.on('close', (code) => {
1298-
assert.strictEqual(code, 1000);
1298+
assert.strictEqual(code, 1005);
12991299
assert.deepStrictEqual(messages, ['foo', 'bar', 'baz']);
13001300
wss.close(done);
13011301
});
@@ -1330,7 +1330,7 @@ describe('WebSocket', function () {
13301330
const ws = new WebSocket(`ws://localhost:${port}`);
13311331

13321332
ws.on('close', (code) => {
1333-
assert.strictEqual(code, 1000);
1333+
assert.strictEqual(code, 1005);
13341334
assert.strictEqual(ws.readyState, WebSocket.CLOSED);
13351335
ws.close();
13361336
wss.close(done);
@@ -2180,7 +2180,7 @@ describe('WebSocket', function () {
21802180
ws.on('message', (message) => {
21812181
assert.strictEqual(message, 'hi');
21822182
ws.on('close', (code) => {
2183-
assert.strictEqual(code, 1000);
2183+
assert.strictEqual(code, 1005);
21842184
wss.close(done);
21852185
});
21862186
});

0 commit comments

Comments
 (0)
Please sign in to comment.