Skip to content

Commit 496d602

Browse files
committed
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. PR-URL: #19779 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent dca09a7 commit 496d602

File tree

4 files changed

+29
-46
lines changed

4 files changed

+29
-46
lines changed

lib/_stream_duplex.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,33 @@
2929
module.exports = Duplex;
3030

3131
const util = require('util');
32-
const DuplexBase = require('internal/streams/duplex_base');
33-
34-
util.inherits(Duplex, DuplexBase);
32+
const Readable = require('_stream_readable');
33+
const Writable = require('_stream_writable');
34+
35+
util.inherits(Duplex, Readable);
36+
37+
{
38+
// Allow the keys array to be GC'ed.
39+
const keys = Object.keys(Writable.prototype);
40+
for (var v = 0; v < keys.length; v++) {
41+
const method = keys[v];
42+
if (!Duplex.prototype[method])
43+
Duplex.prototype[method] = Writable.prototype[method];
44+
}
45+
}
3546

3647
function Duplex(options) {
3748
if (!(this instanceof Duplex))
3849
return new Duplex(options);
3950

40-
DuplexBase.call(this, options);
51+
Readable.call(this, options);
52+
Writable.call(this, options);
53+
54+
if (options && options.readable === false)
55+
this.readable = false;
56+
57+
if (options && options.writable === false)
58+
this.writable = false;
4159

4260
this.allowHalfOpen = true;
4361
if (options && options.allowHalfOpen === false) {

lib/internal/streams/duplex_base.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

lib/net.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ const {
7575
ERR_SOCKET_BAD_PORT,
7676
ERR_SOCKET_CLOSED
7777
} = errors.codes;
78-
const DuplexBase = require('internal/streams/duplex_base');
7978
const dns = require('dns');
8079

8180
const kLastWriteQueueSize = Symbol('lastWriteQueueSize');
@@ -242,19 +241,19 @@ function Socket(options) {
242241

243242
if (typeof options === 'number')
244243
options = { fd: options }; // Legacy interface.
245-
else if (options === undefined)
246-
options = {};
247244
else
248245
options = util._extend({}, options);
249246

247+
const allowHalfOpen = options.allowHalfOpen;
248+
249+
// Prevent the "no-half-open enforcer" from being inherited from `Duplex`.
250+
options.allowHalfOpen = true;
250251
// For backwards compat do not emit close on destroy.
251252
options.emitClose = false;
253+
stream.Duplex.call(this, options);
252254

253-
// `DuplexBase` is just a slimmed down constructor for `Duplex` which allow
254-
// us to not inherit the "no-half-open enforcer" as there is already one in
255-
// place. Instances of `Socket` are still instances of `Duplex`, that is,
256-
// `socket instanceof Duplex === true`.
257-
DuplexBase.call(this, options);
255+
// Default to *not* allowing half open sockets.
256+
this.allowHalfOpen = Boolean(allowHalfOpen);
258257

259258
if (options.handle) {
260259
this._handle = options.handle; // private
@@ -300,9 +299,6 @@ function Socket(options) {
300299
// handle strings directly
301300
this._writableState.decodeStrings = false;
302301

303-
// default to *not* allowing half open sockets
304-
this.allowHalfOpen = options.allowHalfOpen || false;
305-
306302
// if we have a handle, then start the flow of data into the
307303
// buffer. if not, then this will happen when we connect
308304
if (this._handle && options.readable !== false) {

node.gyp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@
150150
'lib/internal/streams/lazy_transform.js',
151151
'lib/internal/streams/async_iterator.js',
152152
'lib/internal/streams/buffer_list.js',
153-
'lib/internal/streams/duplex_base.js',
154153
'lib/internal/streams/duplexpair.js',
155154
'lib/internal/streams/legacy.js',
156155
'lib/internal/streams/destroy.js',

0 commit comments

Comments
 (0)