Skip to content

Commit ccd7a5d

Browse files
hamzakarginaduh95
authored andcommitted
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: #67667 Refs: #66956 Assisted-by: AI coding agents Signed-off-by: Hamza Kargın <hamzakargin@outlook.com> PR-URL: #67669 Reviewed-By: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 3a8a88e commit ccd7a5d

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎lib/internal/fs/streams.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ ObjectDefineProperty(ReadStream.prototype, 'autoClose', {
275275
ReadStream.prototype._construct = _construct;
276276

277277
ReadStream.prototype._read = function(n) {
278+
if (n === 0)
279+
n = 64 * 1024;
280+
278281
n = this.pos !== undefined ?
279282
MathMin(this.end - this.pos + 1, n) :
280283
MathMin(this.end - this.bytesRead + 1, n);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
const assert = require('node:assert');
6+
const fs = require('node:fs');
7+
const { setDefaultHighWaterMark } = require('node:stream');
8+
9+
const expected = fs.readFileSync(__filename);
10+
11+
function readAll(stream, check) {
12+
const chunks = [];
13+
stream.on('data', (chunk) => chunks.push(chunk));
14+
stream.on('end', () => check(Buffer.concat(chunks)));
15+
}
16+
17+
{
18+
const stream = fs.createReadStream(__filename, { highWaterMark: 0 });
19+
assert.strictEqual(stream.readableHighWaterMark, 0);
20+
readAll(stream, common.mustCall((data) => {
21+
assert.deepStrictEqual(data, expected);
22+
}));
23+
}
24+
25+
{
26+
const stream = fs.createReadStream(__filename, {
27+
highWaterMark: 0,
28+
start: 10,
29+
end: 20,
30+
});
31+
readAll(stream, common.mustCall((data) => {
32+
assert.deepStrictEqual(data, expected.subarray(10, 21));
33+
}));
34+
}
35+
36+
fs.promises.open(__filename).then(common.mustCall((fh) => {
37+
const stream = fh.createReadStream({ highWaterMark: 0 });
38+
readAll(stream, common.mustCall((data) => {
39+
assert.deepStrictEqual(data, expected);
40+
}));
41+
}));
42+
43+
setDefaultHighWaterMark(false, 0);
44+
45+
{
46+
const stream = fs.createReadStream(__filename);
47+
assert.strictEqual(stream.readableHighWaterMark, 0);
48+
readAll(stream, common.mustCall((data) => {
49+
assert.deepStrictEqual(data, expected);
50+
}));
51+
}

0 commit comments

Comments
 (0)