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>
This commit is contained in:
Hamza Kargin authored and Antoine du Hamel committed 2026-11-23 12:34:29 +01:00
1 parent 3a8a88e1cc
commit ccd7a5d3aa
2 files changed
+54

No files matched your search

+3
View File
@@ -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);
@@ -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);
}));
}