From 914d25e404a4117c4e57b2ca26e7f82d1d44ba45 Mon Sep 17 00:00:00 2001 From: jakecastelli <959672929@qq.com> Date: Mon, 3 Jun 2024 00:02:26 +0930 Subject: [PATCH] stream: prevent stream unexpected pause when highWaterMark set to 0 Co-authored-by: Robert Nagy --- 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 38f1cbadd39d1d..7847469e9e7336 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']); + })); +}