From dc30ac38ca5903751a9839cf7b78e2c6480e2f09 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Fri, 23 Feb 2018 10:52:25 +0100 Subject: [PATCH] stream: add no-half-open enforcer only if needed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The listener does not do anything if `allowHalfOpen` is enabled. Add it only when `allowHalfOpen` is disabled. PR-URL: https://github.com/nodejs/node/pull/18953 Reviewed-By: Tobias Nießen Reviewed-By: Ruben Bridgewater Reviewed-By: Matteo Collina Reviewed-By: James M Snell --- lib/_stream_duplex.js | 11 +++++------ test/parallel/test-stream-duplex.js | 2 ++ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/_stream_duplex.js b/lib/_stream_duplex.js index 288b8b538c3219..59ce83292789b5 100644 --- a/lib/_stream_duplex.js +++ b/lib/_stream_duplex.js @@ -58,10 +58,10 @@ function Duplex(options) { this.writable = false; this.allowHalfOpen = true; - if (options && options.allowHalfOpen === false) + if (options && options.allowHalfOpen === false) { this.allowHalfOpen = false; - - this.once('end', onend); + this.once('end', onend); + } } Object.defineProperty(Duplex.prototype, 'writableHighWaterMark', { @@ -96,9 +96,8 @@ Object.defineProperty(Duplex.prototype, 'writableLength', { // the no-half-open enforcer function onend() { - // if we allow half-open state, or if the writable side ended, - // then we're ok. - if (this.allowHalfOpen || this._writableState.ended) + // If the writable side ended, then we're ok. + if (this._writableState.ended) return; // no more data can be written. diff --git a/test/parallel/test-stream-duplex.js b/test/parallel/test-stream-duplex.js index 1cc54db05206a3..2b2f9a9a0e6265 100644 --- a/test/parallel/test-stream-duplex.js +++ b/test/parallel/test-stream-duplex.js @@ -29,6 +29,8 @@ const stream = new Duplex({ objectMode: true }); assert(Duplex() instanceof Duplex); assert(stream._readableState.objectMode); assert(stream._writableState.objectMode); +assert(stream.allowHalfOpen); +assert.strictEqual(stream.listenerCount('end'), 0); let written; let read;