6565
6666const {
6767 ObjectSetPrototypeOf,
68- Symbol
68+ Symbol,
6969} = primordials ;
7070
7171module . exports = Transform ;
7272const {
7373 ERR_METHOD_NOT_IMPLEMENTED
7474} = require ( 'internal/errors' ) . codes ;
7575const Duplex = require ( 'internal/streams/duplex' ) ;
76+ const { getHighWaterMark } = require ( 'internal/streams/state' ) ;
7677ObjectSetPrototypeOf ( Transform . prototype , Duplex . prototype ) ;
7778ObjectSetPrototypeOf ( Transform , Duplex ) ;
7879
@@ -82,6 +83,26 @@ function Transform(options) {
8283 if ( ! ( this instanceof Transform ) )
8384 return new Transform ( options ) ;
8485
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+
85106 Duplex . call ( this , options ) ;
86107
87108 // We have implemented the _read method, and done the other things
@@ -164,9 +185,7 @@ Transform.prototype._write = function(chunk, encoding, callback) {
164185 if (
165186 wState . ended || // Backwards compat.
166187 length === rState . length || // Backwards compat.
167- rState . length < rState . highWaterMark ||
168- rState . highWaterMark === 0 ||
169- rState . length === 0
188+ rState . length < rState . highWaterMark
170189 ) {
171190 callback ( ) ;
172191 } else {
0 commit comments