From 5a4c9e6cbbbb10ec8435029bbfe6e51eb7a0a910 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Mon, 5 Mar 2018 11:12:08 +0100 Subject: [PATCH] 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 Reviewed-By: Anna Henningsen Reviewed-By: Matteo Collina Reviewed-By: James M Snell Reviewed-By: Chen Gang Reviewed-By: Ruben Bridgewater --- lib/_stream_duplex.js | 26 ++++--------------------- lib/internal/streams/duplex_base.js | 30 +++++++++++++++++++++++++++++ node.gyp | 1 + 3 files changed, 35 insertions(+), 22 deletions(-) create mode 100644 lib/internal/streams/duplex_base.js diff --git a/lib/_stream_duplex.js b/lib/_stream_duplex.js index 59ce83292789b5..79ff582f459109 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 00000000000000..df3c5c37a80ea7 --- /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; diff --git a/node.gyp b/node.gyp index d85f6a491a9eea..d2cc59f10706cc 100644 --- a/node.gyp +++ b/node.gyp @@ -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',