diff --git a/lib/_stream_duplex.js b/lib/_stream_duplex.js index 1ccb931260ddbd..522b94d660c821 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 3d5fd72958399c..ce8125ecc39d39 100644 --- a/node.gyp +++ b/node.gyp @@ -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',