65
65
66
66
const {
67
67
ObjectSetPrototypeOf,
68
- Symbol
68
+ Symbol,
69
69
} = primordials ;
70
70
71
71
module . exports = Transform ;
72
72
const {
73
73
ERR_METHOD_NOT_IMPLEMENTED
74
74
} = require ( 'internal/errors' ) . codes ;
75
75
const Duplex = require ( 'internal/streams/duplex' ) ;
76
+ const { getHighWaterMark } = require ( 'internal/streams/state' ) ;
76
77
ObjectSetPrototypeOf ( Transform . prototype , Duplex . prototype ) ;
77
78
ObjectSetPrototypeOf ( Transform , Duplex ) ;
78
79
@@ -82,6 +83,26 @@ function Transform(options) {
82
83
if ( ! ( this instanceof Transform ) )
83
84
return new Transform ( options ) ;
84
85
86
+ // TODO (ronag): This should preferably always be
87
+ // applied but would be semver-major. Or even better;
88
+ // make Transform a Readable with the Writable interface.
89
+ const readableHighWaterMark = options ? getHighWaterMark ( this , options , 'readableHighWaterMark' , true ) : null ;
90
+ if ( readableHighWaterMark === 0 ) {
91
+ // A Duplex will buffer both on the writable and readable side while
92
+ // a Transform just wants to buffer hwm number of elements. To avoid
93
+ // buffering twice we disable buffering on the writable side.
94
+ options = {
95
+ ...options ,
96
+ highWaterMark : null ,
97
+ readableHighWaterMark,
98
+ // TODO (ronag): 0 is not optimal since we have
99
+ // a "bug" where we check needDrain before calling _write and not after.
100
+ // Refs: https://github.com/nodejs/node/pull/32887
101
+ // Refs: https://github.com/nodejs/node/pull/35941
102
+ writableHighWaterMark : options . writableHighWaterMark || 0
103
+ } ;
104
+ }
105
+
85
106
Duplex . call ( this , options ) ;
86
107
87
108
// We have implemented the _read method, and done the other things
@@ -164,9 +185,7 @@ Transform.prototype._write = function(chunk, encoding, callback) {
164
185
if (
165
186
wState . ended || // Backwards compat.
166
187
length === rState . length || // Backwards compat.
167
- rState . length < rState . highWaterMark ||
168
- rState . highWaterMark === 0 ||
169
- rState . length === 0
188
+ rState . length < rState . highWaterMark
170
189
) {
171
190
callback ( ) ;
172
191
} else {
0 commit comments