Skip to content

Commit

Permalink
stream: make Duplex inherits from DuplexBase
Browse files Browse the repository at this point in the history
Add ability to subclass `stream.Duplex` without inheriting the
"no-half-open enforcer" regardless of the value of the `allowHalfOpen`
option.

PR-URL: #18974
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Chen Gang <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
  • Loading branch information
lpinca authored and MylesBorins committed Mar 20, 2018
1 parent 27088cf commit 152c931
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
26 changes: 4 additions & 22 deletions lib/_stream_duplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,15 @@
module.exports = Duplex;

const util = require('util');
const Readable = require('_stream_readable');
const Writable = require('_stream_writable');

util.inherits(Duplex, Readable);

{
// avoid scope creep, the keys array can then be collected
const keys = Object.keys(Writable.prototype);
for (var v = 0; v < keys.length; v++) {
const method = keys[v];
if (!Duplex.prototype[method])
Duplex.prototype[method] = Writable.prototype[method];
}
}
const DuplexBase = require('internal/streams/duplex_base');

util.inherits(Duplex, DuplexBase);

function Duplex(options) {
if (!(this instanceof Duplex))
return new Duplex(options);

Readable.call(this, options);
Writable.call(this, options);

if (options && options.readable === false)
this.readable = false;

if (options && options.writable === false)
this.writable = false;
DuplexBase.call(this, options);

this.allowHalfOpen = true;
if (options && options.allowHalfOpen === false) {
Expand Down
30 changes: 30 additions & 0 deletions lib/internal/streams/duplex_base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const util = require('util');
const Readable = require('_stream_readable');
const Writable = require('_stream_writable');

function DuplexBase(options) {
Readable.call(this, options);
Writable.call(this, options);

if (options && options.readable === false)
this.readable = false;

if (options && options.writable === false)
this.writable = false;
}

util.inherits(DuplexBase, Readable);

{
// Avoid scope creep, the keys array can then be collected.
const keys = Object.keys(Writable.prototype);
for (var v = 0; v < keys.length; v++) {
const method = keys[v];
if (!DuplexBase.prototype[method])
DuplexBase.prototype[method] = Writable.prototype[method];
}
}

module.exports = DuplexBase;
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
'lib/internal/vm/Module.js',
'lib/internal/streams/lazy_transform.js',
'lib/internal/streams/BufferList.js',
'lib/internal/streams/duplex_base.js',
'lib/internal/streams/legacy.js',
'lib/internal/streams/destroy.js',
'lib/internal/wrap_js_stream.js',
Expand Down

0 comments on commit 152c931

Please sign in to comment.