From 0fac303d1cc2454d04da50facbc752b3ba0284bd Mon Sep 17 00:00:00 2001 From: jakecastelli <38635403+jakecastelli@users.noreply.github.com> Date: Sat, 8 Jun 2024 11:34:40 +0930 Subject: [PATCH] stream: prevent stream unexpected pause when highWaterMark set to 0 Co-authored-by: Robert Nagy PR-URL: https://github.com/nodejs/node/pull/53261 Fixes: https://github.com/nodejs/node/issues/51930 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Matteo Collina --- lib/internal/streams/writable.js | 2 +- test/parallel/test-streams-highwatermark.js | 24 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/internal/streams/writable.js b/lib/internal/streams/writable.js index 9f6fa4936dca62..268b54b964a835 100644 --- a/lib/internal/streams/writable.js +++ b/lib/internal/streams/writable.js @@ -565,7 +565,7 @@ function writeOrBuffer(stream, state, chunk, encoding, callback) { state[kState] &= ~kSync; } - const ret = state.length < state.highWaterMark; + const ret = state.length < state.highWaterMark || state.length === 0; if (!ret) { state[kState] |= kNeedDrain; diff --git a/test/parallel/test-streams-highwatermark.js b/test/parallel/test-streams-highwatermark.js index 7018c5bf29ce08..978a53913bd52f 100644 --- a/test/parallel/test-streams-highwatermark.js +++ b/test/parallel/test-streams-highwatermark.js @@ -85,3 +85,27 @@ const { inspect } = require('util'); hwm, })); } + +{ + const res = []; + const r = new stream.Readable({ + read() {}, + }); + const w = new stream.Writable({ + highWaterMark: 0, + write(chunk, encoding, callback) { + res.push(chunk.toString()); + callback(); + }, + }); + + r.pipe(w); + r.push('a'); + r.push('b'); + r.push('c'); + r.push(null); + + r.on('end', common.mustCall(() => { + assert.deepStrictEqual(res, ['a', 'b', 'c']); + })); +}