diff options
author | Luigi Pinca <luigipinca@gmail.com> | 2018-03-05 11:12:08 +0100 |
---|---|---|
committer | Luigi Pinca <luigipinca@gmail.com> | 2018-03-07 16:05:17 +0100 |
commit | df0716921e9bfd99f8e115dbaeee6199a93cd8c5 (patch) | |
tree | 73e16be8e2a4b9eb9b78013aef748c1ccb091528 /lib | |
parent | 42e9b483c0d5a0ee49b91428415b4ccd8e94ebe4 (diff) | |
download | android-node-v8-df0716921e9bfd99f8e115dbaeee6199a93cd8c5.tar.gz android-node-v8-df0716921e9bfd99f8e115dbaeee6199a93cd8c5.tar.bz2 android-node-v8-df0716921e9bfd99f8e115dbaeee6199a93cd8c5.zip |
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>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/_stream_duplex.js | 26 | ||||
-rw-r--r-- | lib/internal/streams/duplex_base.js | 30 |
2 files changed, 34 insertions, 22 deletions
diff --git a/lib/_stream_duplex.js b/lib/_stream_duplex.js index 1ccb931260..522b94d660 100644 --- a/lib/_stream_duplex.js +++ b/lib/_stream_duplex.js @@ -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) { diff --git a/lib/internal/streams/duplex_base.js b/lib/internal/streams/duplex_base.js new file mode 100644 index 0000000000..df3c5c37a8 --- /dev/null +++ b/lib/internal/streams/duplex_base.js @@ -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; |