Skip to content

Commit 152c931

Browse files
lpincaMylesBorins
authored andcommitted
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: #18974 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Chen Gang <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 27088cf commit 152c931

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

lib/_stream_duplex.js

+4-22
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,15 @@
2929
module.exports = Duplex;
3030

3131
const util = require('util');
32-
const Readable = require('_stream_readable');
33-
const Writable = require('_stream_writable');
34-
35-
util.inherits(Duplex, Readable);
36-
37-
{
38-
// avoid scope creep, the keys array can then be collected
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-
}
32+
const DuplexBase = require('internal/streams/duplex_base');
33+
34+
util.inherits(Duplex, DuplexBase);
4635

4736
function Duplex(options) {
4837
if (!(this instanceof Duplex))
4938
return new Duplex(options);
5039

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;
40+
DuplexBase.call(this, options);
5941

6042
this.allowHalfOpen = true;
6143
if (options && options.allowHalfOpen === false) {

lib/internal/streams/duplex_base.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
const util = require('util');
4+
const Readable = require('_stream_readable');
5+
const Writable = require('_stream_writable');
6+
7+
function DuplexBase(options) {
8+
Readable.call(this, options);
9+
Writable.call(this, options);
10+
11+
if (options && options.readable === false)
12+
this.readable = false;
13+
14+
if (options && options.writable === false)
15+
this.writable = false;
16+
}
17+
18+
util.inherits(DuplexBase, Readable);
19+
20+
{
21+
// Avoid scope creep, the keys array can then be collected.
22+
const keys = Object.keys(Writable.prototype);
23+
for (var v = 0; v < keys.length; v++) {
24+
const method = keys[v];
25+
if (!DuplexBase.prototype[method])
26+
DuplexBase.prototype[method] = Writable.prototype[method];
27+
}
28+
}
29+
30+
module.exports = DuplexBase;

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
'lib/internal/vm/Module.js',
143143
'lib/internal/streams/lazy_transform.js',
144144
'lib/internal/streams/BufferList.js',
145+
'lib/internal/streams/duplex_base.js',
145146
'lib/internal/streams/legacy.js',
146147
'lib/internal/streams/destroy.js',
147148
'lib/internal/wrap_js_stream.js',

0 commit comments

Comments
 (0)