Commit 45d1554464d for nodejs
commit 45d1554464dee09e2b6819c3cd1334dd612743cd
Author: Hamza Kargin <hamzakargin@outlook.com>
Date: Mon Nov 23 11:13:32 2026 +0400
fs: read data when highWaterMark is 0
ReadStream.prototype._read() passed the requested size straight to
the end-of-range check, so a size of 0 ended the stream before any
data was read. This happened for createReadStream() with
highWaterMark: 0, and since #66956 for every fs.ReadStream after
stream.setDefaultHighWaterMark(false, 0).
Read 64 KiB, the ReadStream default, when the requested size is 0.
Fixes: https://github.com/nodejs/node/issues/67667
Refs: https://github.com/nodejs/node/pull/66956
Assisted-by: AI coding agents
Signed-off-by: Hamza Kargın <hamzakargin@outlook.com>
PR-URL: https://github.com/nodejs/node/pull/67669
Reviewed-By: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
diff --git a/lib/internal/fs/streams.js b/lib/internal/fs/streams.js
index 9bfbaa01125..e66a256f811 100644
--- a/lib/internal/fs/streams.js
+++ b/lib/internal/fs/streams.js
@@ -275,6 +275,9 @@ ObjectDefineProperty(ReadStream.prototype, 'autoClose', {
ReadStream.prototype._construct = _construct;
ReadStream.prototype._read = function(n) {
+ if (n === 0)
+ n = 64 * 1024;
+
n = this.pos !== undefined ?
MathMin(this.end - this.pos + 1, n) :
MathMin(this.end - this.bytesRead + 1, n);
diff --git a/test/parallel/test-fs-read-stream-hwm-zero.js b/test/parallel/test-fs-read-stream-hwm-zero.js
new file mode 100644
index 00000000000..720a5fb1988
--- /dev/null
+++ b/test/parallel/test-fs-read-stream-hwm-zero.js
@@ -0,0 +1,51 @@
+'use strict';
+
+const common = require('../common');
+
+const assert = require('node:assert');
+const fs = require('node:fs');
+const { setDefaultHighWaterMark } = require('node:stream');
+
+const expected = fs.readFileSync(__filename);
+
+function readAll(stream, check) {
+ const chunks = [];
+ stream.on('data', (chunk) => chunks.push(chunk));
+ stream.on('end', () => check(Buffer.concat(chunks)));
+}
+
+{
+ const stream = fs.createReadStream(__filename, { highWaterMark: 0 });
+ assert.strictEqual(stream.readableHighWaterMark, 0);
+ readAll(stream, common.mustCall((data) => {
+ assert.deepStrictEqual(data, expected);
+ }));
+}
+
+{
+ const stream = fs.createReadStream(__filename, {
+ highWaterMark: 0,
+ start: 10,
+ end: 20,
+ });
+ readAll(stream, common.mustCall((data) => {
+ assert.deepStrictEqual(data, expected.subarray(10, 21));
+ }));
+}
+
+fs.promises.open(__filename).then(common.mustCall((fh) => {
+ const stream = fh.createReadStream({ highWaterMark: 0 });
+ readAll(stream, common.mustCall((data) => {
+ assert.deepStrictEqual(data, expected);
+ }));
+}));
+
+setDefaultHighWaterMark(false, 0);
+
+{
+ const stream = fs.createReadStream(__filename);
+ assert.strictEqual(stream.readableHighWaterMark, 0);
+ readAll(stream, common.mustCall((data) => {
+ assert.deepStrictEqual(data, expected);
+ }));
+}