Skip to content

Commit f2b9805

Browse files
lpincaapapirovski
authored andcommitted
stream: add no-half-open enforcer only if needed
The listener does not do anything if `allowHalfOpen` is enabled. Add it only when `allowHalfOpen` is disabled. PR-URL: nodejs#18953 Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 584cfc9 commit f2b9805

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

lib/_stream_duplex.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ function Duplex(options) {
5858
this.writable = false;
5959

6060
this.allowHalfOpen = true;
61-
if (options && options.allowHalfOpen === false)
61+
if (options && options.allowHalfOpen === false) {
6262
this.allowHalfOpen = false;
63-
64-
this.once('end', onend);
63+
this.once('end', onend);
64+
}
6565
}
6666

6767
Object.defineProperty(Duplex.prototype, 'writableHighWaterMark', {
@@ -96,9 +96,8 @@ Object.defineProperty(Duplex.prototype, 'writableLength', {
9696

9797
// the no-half-open enforcer
9898
function onend() {
99-
// if we allow half-open state, or if the writable side ended,
100-
// then we're ok.
101-
if (this.allowHalfOpen || this._writableState.ended)
99+
// If the writable side ended, then we're ok.
100+
if (this._writableState.ended)
102101
return;
103102

104103
// no more data can be written.

test/parallel/test-http-connect.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ server.on('connect', common.mustCall((req, socket, firstBodyChunk) => {
3434
assert.strictEqual(socket.listeners('close').length, 0);
3535
assert.strictEqual(socket.listeners('drain').length, 0);
3636
assert.strictEqual(socket.listeners('data').length, 0);
37-
assert.strictEqual(socket.listeners('end').length, 2);
37+
assert.strictEqual(socket.listeners('end').length, 1);
3838
assert.strictEqual(socket.listeners('error').length, 0);
3939

4040
socket.write('HTTP/1.1 200 Connection established\r\n\r\n');

test/parallel/test-stream-duplex.js

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const stream = new Duplex({ objectMode: true });
2929
assert(Duplex() instanceof Duplex);
3030
assert(stream._readableState.objectMode);
3131
assert(stream._writableState.objectMode);
32+
assert(stream.allowHalfOpen);
33+
assert.strictEqual(stream.listenerCount('end'), 0);
3234

3335
let written;
3436
let read;

0 commit comments

Comments
 (0)