stream: make Duplex inherits from DuplexBase

Add ability to subclass `stream.Duplex` without inheriting the
"no-half-open enforcer" regardless of the value of the `allowHalfOpen`
option.

PR-URL: https://github.com/nodejs/node/pull/18974
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chen Gang <gangc.cxy@foxmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Luigi Pinca 2018-03-05 11:12:08 +01:00
parent 42e9b483c0
commit df0716921e
3 changed files with 34 additions and 21 deletions

View File

@ -29,33 +29,15 @@
module.exports = Duplex;
const util = require('util');
const Readable = require('_stream_readable');
const Writable = require('_stream_writable');
const DuplexBase = require('internal/streams/duplex_base');
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];
}
}
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) {

View File

@ -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;

View File

@ -146,6 +146,7 @@
'lib/internal/streams/lazy_transform.js',
'lib/internal/streams/async_iterator.js',
'lib/internal/streams/BufferList.js',
'lib/internal/streams/duplex_base.js',
'lib/internal/streams/duplexpair.js',
'lib/internal/streams/legacy.js',
'lib/internal/streams/destroy.js',