Skip to content

Commit

Permalink
[minor] Remove unreachable code (#1224)
Browse files Browse the repository at this point in the history
`zlib.DeflateRaw` emits an `'error'` event only when an attempt to use
it is made after it has already been closed and this cannot happen in
our case.
  • Loading branch information
lpinca committed Oct 30, 2017
1 parent 5e0424c commit 8ea3739
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 49 deletions.
57 changes: 32 additions & 25 deletions lib/PerMessageDeflate.js
Expand Up @@ -394,47 +394,54 @@ class PerMessageDeflate {
flush: zlib.Z_SYNC_FLUSH,
windowBits
});
}
this._deflate.writeInProgress = true;

var totalLength = 0;
const buffers = [];

const onData = (data) => {
totalLength += data.length;
buffers.push(data);
};
this._deflate.totalLength = 0;
this._deflate.buffers = [];

const onError = (err) => {
cleanup();
callback(err);
};
//
// `zlib.DeflateRaw` emits an `'error'` event only when an attempt to use
// it is made after it has already been closed. This cannot happen here,
// so we only add a listener for the `'data'` event.
//
this._deflate.on('data', deflateOnData);
}

const cleanup = () => {
if (!this._deflate) return;
this._deflate.writeInProgress = true;

this._deflate.removeListener('error', onError);
this._deflate.removeListener('data', onData);
this._deflate.writeInProgress = false;
this._deflate.write(data);
this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
var data = bufferUtil.concat(
this._deflate.buffers,
this._deflate.totalLength
);
if (fin) data = data.slice(0, data.length - 4);

if (
(fin && this.params[`${endpoint}_no_context_takeover`]) ||
this._deflate.pendingClose
) {
this._deflate.close();
this._deflate = null;
} else {
this._deflate.writeInProgress = false;
this._deflate.totalLength = 0;
this._deflate.buffers = [];
}
};

this._deflate.on('error', onError).on('data', onData);
this._deflate.write(data);
this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
cleanup();
var data = bufferUtil.concat(buffers, totalLength);
if (fin) data = data.slice(0, data.length - 4);
callback(null, data);
});
}
}

module.exports = PerMessageDeflate;

/**
* The listener of the `zlib.DeflateRaw` stream `'data'` event.
*
* @param {Buffer} chunk A chunk of data
* @private
*/
function deflateOnData (chunk) {
this.buffers.push(chunk);
this.totalLength += chunk.length;
}
10 changes: 1 addition & 9 deletions lib/Sender.js
Expand Up @@ -35,8 +35,6 @@ class Sender {
this._bufferedBytes = 0;
this._deflating = false;
this._queue = [];

this.onerror = null;
}

/**
Expand Down Expand Up @@ -331,13 +329,7 @@ class Sender {
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];

this._deflating = true;
perMessageDeflate.compress(data, options.fin, (err, buf) => {
if (err) {
if (cb) cb(err);
else this.onerror(err);
return;
}

perMessageDeflate.compress(data, options.fin, (_, buf) => {
options.readOnly = false;
this.sendFrame(Sender.frame(buf, options), cb);
this._deflating = false;
Expand Down
19 changes: 4 additions & 15 deletions lib/WebSocket.js
Expand Up @@ -157,12 +157,6 @@ class WebSocket extends EventEmitter {
this.emit('error', error);
};

// sender event handlers
this._sender.onerror = (error) => {
this.close(1002, '');
this.emit('error', error);
};

this.readyState = WebSocket.OPEN;
this.emit('open');
}
Expand Down Expand Up @@ -198,17 +192,12 @@ class WebSocket extends EventEmitter {
if (!error) this._socket.end();
else this._socket.destroy();

this._socket = null;
this._ultron = null;
}

if (this._sender) {
this._sender = this._sender.onerror = null;
}

if (this._receiver) {
this._receiver.cleanup(() => this.emitClose());

this._receiver = null;
this._sender = null;
this._socket = null;
this._ultron = null;
} else {
this.emitClose();
}
Expand Down

0 comments on commit 8ea3739

Please sign in to comment.