From e50513f9d61c31dddda1fe3e3cf65ea0fed26097 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hamza=20Karg=C4=B1n?= Date: Fri, 20 Nov 2026 22:27:39 +0400 Subject: [PATCH] fs: read data when highWaterMark is 0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- lib/internal/fs/streams.js | 3 ++ test/parallel/test-fs-read-stream-hwm-zero.js | 51 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 test/parallel/test-fs-read-stream-hwm-zero.js diff --git a/lib/internal/fs/streams.js b/lib/internal/fs/streams.js index 9bfbaa011255..e66a256f8118 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 000000000000..720a5fb1988d --- /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); + })); +}