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:
parent
42e9b483c0
commit
df0716921e
@ -29,33 +29,15 @@
|
|||||||
module.exports = Duplex;
|
module.exports = Duplex;
|
||||||
|
|
||||||
const util = require('util');
|
const util = require('util');
|
||||||
const Readable = require('_stream_readable');
|
const DuplexBase = require('internal/streams/duplex_base');
|
||||||
const Writable = require('_stream_writable');
|
|
||||||
|
|
||||||
util.inherits(Duplex, Readable);
|
util.inherits(Duplex, DuplexBase);
|
||||||
|
|
||||||
{
|
|
||||||
// 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];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function Duplex(options) {
|
function Duplex(options) {
|
||||||
if (!(this instanceof Duplex))
|
if (!(this instanceof Duplex))
|
||||||
return new Duplex(options);
|
return new Duplex(options);
|
||||||
|
|
||||||
Readable.call(this, options);
|
DuplexBase.call(this, options);
|
||||||
Writable.call(this, options);
|
|
||||||
|
|
||||||
if (options && options.readable === false)
|
|
||||||
this.readable = false;
|
|
||||||
|
|
||||||
if (options && options.writable === false)
|
|
||||||
this.writable = false;
|
|
||||||
|
|
||||||
this.allowHalfOpen = true;
|
this.allowHalfOpen = true;
|
||||||
if (options && options.allowHalfOpen === false) {
|
if (options && options.allowHalfOpen === false) {
|
||||||
|
30
lib/internal/streams/duplex_base.js
Normal file
30
lib/internal/streams/duplex_base.js
Normal 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;
|
1
node.gyp
1
node.gyp
@ -146,6 +146,7 @@
|
|||||||
'lib/internal/streams/lazy_transform.js',
|
'lib/internal/streams/lazy_transform.js',
|
||||||
'lib/internal/streams/async_iterator.js',
|
'lib/internal/streams/async_iterator.js',
|
||||||
'lib/internal/streams/BufferList.js',
|
'lib/internal/streams/BufferList.js',
|
||||||
|
'lib/internal/streams/duplex_base.js',
|
||||||
'lib/internal/streams/duplexpair.js',
|
'lib/internal/streams/duplexpair.js',
|
||||||
'lib/internal/streams/legacy.js',
|
'lib/internal/streams/legacy.js',
|
||||||
'lib/internal/streams/destroy.js',
|
'lib/internal/streams/destroy.js',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user