Repository: websockets/ws (js)

Issue:
Close frame spanning multiple packets is not handled correctly
- [x] I've searched for any related issues and avoided creating a duplicate
      issue.

#### Description
A close frame which has been split over multiple TCP packets, for example <header><payload> is not handled correctly.  The payload is never read and `conclude` event not emitted.

#### Reproducible in:

- version: 6.1.2
- Node.js version(s): 10.12
- OS version(s): Linux; Fedora 29

#### Steps to reproduce:

1. Establish a websocket session

2. Send a close frame from the server as two separate packets

#### Expected result:

1. Close frame is parsed correctly and `conclude` event is emitted from receiver

#### Actual result:

1. Close frame is not parsed and `conclude` is never emitted



Pull request description:
Handle close frame which spans multiple packets
Fixes #1493 

Patch:
diff --git a/lib/receiver.js b/lib/receiver.js
index b9102011..9d301235 100644
--- a/lib/receiver.js
+++ b/lib/receiver.js
@@ -62,7 +62,7 @@ class Receiver extends stream.Writable {
    * @param {Function} cb Callback
    */
   _write(chunk, encoding, cb) {
-    if (this._opcode === 0x08) return cb();
+    if (this._opcode === 0x08 && this._state == GET_INFO) return cb();
 
     this._bufferedBytes += chunk.length;
     this._buffers.push(chunk);
@@ -454,6 +454,7 @@ class Receiver extends stream.Writable {
         this.end();
       }
 
+      this._state = GET_INFO;
       return;
     }
 


Test patch:
diff --git a/test/receiver.test.js b/test/receiver.test.js
index 6211b31a..3bc68071 100644
--- a/test/receiver.test.js
+++ b/test/receiver.test.js
@@ -34,6 +34,19 @@ describe('Receiver', function() {
     receiver.write(Buffer.from('8800', 'hex'));
   });
 
+  it('parses a close message spanning multiple writes', function(done) {
+    const receiver = new Receiver();
+
+    receiver.on('conclude', (code, data) => {
+      assert.strictEqual(code, 1000);
+      assert.strictEqual(data, 'DONE');
+      done();
+    });
+
+    receiver.write(Buffer.from('8806', 'hex'));
+    receiver.write(Buffer.from('03e8444F4E45', 'hex'));
+  });
+
   it('parses a masked text message', function(done) {
     const receiver = new Receiver();