Skip to content

Commit

Permalink
stream: the position of _read() is wrong
Browse files Browse the repository at this point in the history
Fixes: #33940
  • Loading branch information
helloyou2012 committed Apr 20, 2021
1 parent 13d0de5 commit 5faff30
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/internal/fs/streams.js
Expand Up @@ -255,6 +255,10 @@ ReadStream.prototype._read = function(n) {
if (er) {
errorOrDestroy(this, er);
} else if (bytesRead > 0) {
if (this.pos !== undefined) {
this.pos += bytesRead;
}

this.bytesRead += bytesRead;

if (bytesRead !== buf.length) {
Expand All @@ -271,10 +275,6 @@ ReadStream.prototype._read = function(n) {
this.push(null);
}
});

if (this.pos !== undefined) {
this.pos += n;
}
};

ReadStream.prototype._destroy = function(err, cb) {
Expand Down
61 changes: 61 additions & 0 deletions test/parallel/test-fs-read-stream-pos.js
@@ -0,0 +1,61 @@
'use strict';
const common = require('../common');
const tmpdir = require('../common/tmpdir');
const fs = require('fs');
const assert = require('assert');
const path = require('path');

tmpdir.refresh();

const file = path.join(tmpdir.path, '/read_stream_pos_test.txt');

fs.writeFileSync(file, '');

let counter = 0;

setInterval(() => {
counter = counter + 1;
const line = `hello at ${counter}\n`;
fs.writeFileSync(file, line, { flag: 'a' });
}, 1);

const hwm = 10;
let bufs = [];
let isLow = false;
let cur = 0;
let stream;

setInterval(() => {
if (stream) return;

stream = fs.createReadStream(file, {
highWaterMark: hwm,
start: cur
});
stream.on('data', common.mustCallAtLeast((chunk) => {
cur += chunk.length;
bufs.push(chunk);
if (isLow) {
const brokenLines = Buffer.concat(bufs).toString()
.split('\n')
.filter((line) => {
const s = 'hello at'.slice(0, line.length);
if (line && !line.startsWith(s)) {
return true;
}
return false;
});
assert.strictEqual(brokenLines.length, 0);
process.exit();
return;
}
if (chunk.length !== hwm) {
isLow = true;
}
}));
stream.on('end', () => {
stream = null;
isLow = false;
bufs = [];
});
}, 10);

0 comments on commit 5faff30

Please sign in to comment.