Skip to content

Commit f2ffaba

Browse files
helloyou2012targos
authored andcommitted
stream: the position of _read() is wrong
Fixes: #33940 PR-URL: #38292 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 16eb078 commit f2ffaba

2 files changed

Lines changed: 73 additions & 4 deletions

File tree

‎lib/internal/fs/streams.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ ReadStream.prototype._read = function(n) {
255255
if(er){
256256
errorOrDestroy(this,er);
257257
}elseif(bytesRead>0){
258+
if(this.pos!==undefined){
259+
this.pos+=bytesRead;
260+
}
261+
258262
this.bytesRead+=bytesRead;
259263

260264
if(bytesRead!==buf.length){
@@ -271,10 +275,6 @@ ReadStream.prototype._read = function(n) {
271275
this.push(null);
272276
}
273277
});
274-
275-
if(this.pos!==undefined){
276-
this.pos+=n;
277-
}
278278
};
279279

280280
ReadStream.prototype._destroy=function(err,cb){
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
3+
// Refs: https://github.com/nodejs/node/issues/33940
4+
5+
constcommon=require('../common');
6+
consttmpdir=require('../common/tmpdir');
7+
constfs=require('fs');
8+
constassert=require('assert');
9+
constpath=require('path');
10+
11+
tmpdir.refresh();
12+
13+
constfile=path.join(tmpdir.path,'/read_stream_pos_test.txt');
14+
15+
fs.writeFileSync(file,'');
16+
17+
letcounter=0;
18+
19+
setInterval(()=>{
20+
counter=counter+1;
21+
constline=`hello at ${counter}\n`;
22+
fs.writeFileSync(file,line,{flag: 'a'});
23+
},1);
24+
25+
consthwm=10;
26+
letbufs=[];
27+
letisLow=false;
28+
letcur=0;
29+
letstream;
30+
31+
setInterval(()=>{
32+
if(stream)return;
33+
34+
stream=fs.createReadStream(file,{
35+
highWaterMark: hwm,
36+
start: cur
37+
});
38+
stream.on('data',common.mustCallAtLeast((chunk)=>{
39+
cur+=chunk.length;
40+
bufs.push(chunk);
41+
if(isLow){
42+
constbrokenLines=Buffer.concat(bufs).toString()
43+
.split('\n')
44+
.filter((line)=>{
45+
consts='hello at'.slice(0,line.length);
46+
if(line&&!line.startsWith(s)){
47+
returntrue;
48+
}
49+
returnfalse;
50+
});
51+
assert.strictEqual(brokenLines.length,0);
52+
process.exit();
53+
return;
54+
}
55+
if(chunk.length!==hwm){
56+
isLow=true;
57+
}
58+
}));
59+
stream.on('end',()=>{
60+
stream=null;
61+
isLow=false;
62+
bufs=[];
63+
});
64+
},10);
65+
66+
// Time longer than 90 seconds to exit safely
67+
setTimeout(()=>{
68+
process.exit();
69+
},90000);

0 commit comments

Comments
 (0)