Skip to content

Commit 9c0c0e6

Browse files
lpincaMylesBorins
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: #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 b01bd80 commit 9c0c0e6

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

‎lib/_stream_duplex.js‎

Lines changed: 5 additions & 6 deletions
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
functiononend(){
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-stream-duplex.js‎

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

3335
letwritten;
3436
letread;

0 commit comments

Comments
(0)