From 82b72c848f6585a11f12e0584d5a30df0111982b Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Tue, 3 Apr 2018 18:51:30 +0200 Subject: [PATCH 1/2] net,stream: remove DuplexBase `DuplexBase` was added to prevent the "no-half-open enforcer" from being inherited by `net.Socket`. The main reason to use it instead of `Duplex` was that it allowed to not copy the options object but since commit 5e3f516 the options object is copyed anyway so it is no longer useful. --- lib/_stream_duplex.js | 26 +++++++++++++++++++++---- lib/internal/streams/duplex_base.js | 30 ----------------------------- lib/net.js | 18 +++++++---------- node.gyp | 1 - 4 files changed, 29 insertions(+), 46 deletions(-) delete mode 100644 lib/internal/streams/duplex_base.js diff --git a/lib/_stream_duplex.js b/lib/_stream_duplex.js index 522b94d660c821..1ccb931260ddbd 100644 --- a/lib/_stream_duplex.js +++ b/lib/_stream_duplex.js @@ -29,15 +29,33 @@ module.exports = Duplex; const util = require('util'); -const DuplexBase = require('internal/streams/duplex_base'); - -util.inherits(Duplex, DuplexBase); +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]; + } +} function Duplex(options) { if (!(this instanceof Duplex)) return new Duplex(options); - DuplexBase.call(this, 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; this.allowHalfOpen = true; if (options && options.allowHalfOpen === false) { diff --git a/lib/internal/streams/duplex_base.js b/lib/internal/streams/duplex_base.js deleted file mode 100644 index df3c5c37a80ea7..00000000000000 --- a/lib/internal/streams/duplex_base.js +++ /dev/null @@ -1,30 +0,0 @@ -'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/lib/net.js b/lib/net.js index 5b460befa49374..9313a6dbc0c45c 100644 --- a/lib/net.js +++ b/lib/net.js @@ -75,7 +75,6 @@ const { ERR_SOCKET_BAD_PORT, ERR_SOCKET_CLOSED } = errors.codes; -const DuplexBase = require('internal/streams/duplex_base'); const dns = require('dns'); const kLastWriteQueueSize = Symbol('lastWriteQueueSize'); @@ -242,19 +241,19 @@ function Socket(options) { if (typeof options === 'number') options = { fd: options }; // Legacy interface. - else if (options === undefined) - options = {}; else options = util._extend({}, options); + const aho = options.allowHalfOpen; + + // Prevent the "no-half-open enforcer" from being inherited from `Duplex`. + options.allowHalfOpen = true; // For backwards compat do not emit close on destroy. options.emitClose = false; + stream.Duplex.call(this, options); - // `DuplexBase` is just a slimmed down constructor for `Duplex` which allow - // us to not inherit the "no-half-open enforcer" as there is already one in - // place. Instances of `Socket` are still instances of `Duplex`, that is, - // `socket instanceof Duplex === true`. - DuplexBase.call(this, options); + // Default to *not* allowing half open sockets. + this.allowHalfOpen = Boolean(aho); if (options.handle) { this._handle = options.handle; // private @@ -300,9 +299,6 @@ function Socket(options) { // handle strings directly this._writableState.decodeStrings = false; - // default to *not* allowing half open sockets - this.allowHalfOpen = options.allowHalfOpen || false; - // if we have a handle, then start the flow of data into the // buffer. if not, then this will happen when we connect if (this._handle && options.readable !== false) { diff --git a/node.gyp b/node.gyp index cb305b93e9ff4c..d87441fe3a17f6 100644 --- a/node.gyp +++ b/node.gyp @@ -149,7 +149,6 @@ 'lib/internal/streams/lazy_transform.js', 'lib/internal/streams/async_iterator.js', 'lib/internal/streams/buffer_list.js', - 'lib/internal/streams/duplex_base.js', 'lib/internal/streams/duplexpair.js', 'lib/internal/streams/legacy.js', 'lib/internal/streams/destroy.js', From 5c397d9cfffca7100f182ef2458f444ab3e99ba0 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Tue, 3 Apr 2018 21:27:13 +0200 Subject: [PATCH 2/2] fixup! net,stream: remove DuplexBase --- lib/_stream_duplex.js | 2 +- lib/net.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/_stream_duplex.js b/lib/_stream_duplex.js index 1ccb931260ddbd..b123cdcb4d776f 100644 --- a/lib/_stream_duplex.js +++ b/lib/_stream_duplex.js @@ -35,7 +35,7 @@ const Writable = require('_stream_writable'); util.inherits(Duplex, Readable); { - // avoid scope creep, the keys array can then be collected + // Allow the keys array to be GC'ed. const keys = Object.keys(Writable.prototype); for (var v = 0; v < keys.length; v++) { const method = keys[v]; diff --git a/lib/net.js b/lib/net.js index 9313a6dbc0c45c..68dde9cac3615e 100644 --- a/lib/net.js +++ b/lib/net.js @@ -244,7 +244,7 @@ function Socket(options) { else options = util._extend({}, options); - const aho = options.allowHalfOpen; + const allowHalfOpen = options.allowHalfOpen; // Prevent the "no-half-open enforcer" from being inherited from `Duplex`. options.allowHalfOpen = true; @@ -253,7 +253,7 @@ function Socket(options) { stream.Duplex.call(this, options); // Default to *not* allowing half open sockets. - this.allowHalfOpen = Boolean(aho); + this.allowHalfOpen = Boolean(allowHalfOpen); if (options.handle) { this._handle = options.handle; // private