Skip to content

Commit ca7adca

Browse files
ZYSzysaddaleax
authored andcommitted
fs: extract start and end check into checkPosition
PR-URL: #25264 Reviewed-By: Weijia Wang <starkwang@126.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent fc22df9 commit ca7adca

1 file changed

Lines changed: 15 additions & 28 deletions

File tree

‎lib/internal/fs/streams.js‎

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ function allocNewPool(poolSize) {
3737
pool.used=0;
3838
}
3939

40+
// Check the `this.start` and `this.end` of stream.
41+
functioncheckPosition(pos,name){
42+
if(!Number.isSafeInteger(pos)){
43+
validateNumber(pos,name);
44+
if(!Number.isInteger(pos))
45+
thrownewERR_OUT_OF_RANGE(name,'an integer',pos);
46+
thrownewERR_OUT_OF_RANGE(name,'>= 0 and <= 2 ** 53 - 1',pos);
47+
}
48+
if(pos<0){
49+
thrownewERR_OUT_OF_RANGE(name,'>= 0 and <= 2 ** 53 - 1',pos);
50+
}
51+
}
52+
4053
functionReadStream(path,options){
4154
if(!(thisinstanceofReadStream))
4255
returnnewReadStream(path,options);
@@ -65,41 +78,15 @@ function ReadStream(path, options) {
6578
this.closed=false;
6679

6780
if(this.start!==undefined){
68-
if(!Number.isSafeInteger(this.start)){
69-
validateNumber(this.start,'start');
70-
if(!Number.isInteger(this.start))
71-
thrownewERR_OUT_OF_RANGE('start','an integer',this.start);
72-
thrownewERR_OUT_OF_RANGE(
73-
'start',
74-
'>= 0 and <= 2 ** 53 - 1',
75-
this.start
76-
);
77-
}
78-
if(this.start<0){
79-
thrownewERR_OUT_OF_RANGE(
80-
'start',
81-
'>= 0 and <= 2 ** 53 - 1',
82-
this.start
83-
);
84-
}
81+
checkPosition(this.start,'start');
8582

8683
this.pos=this.start;
8784
}
8885

8986
if(this.end===undefined){
9087
this.end=Infinity;
9188
}elseif(this.end!==Infinity){
92-
if(!Number.isSafeInteger(this.end)){
93-
if(typeofthis.end!=='number')
94-
thrownewERR_INVALID_ARG_TYPE('end','number',this.end);
95-
if(!Number.isInteger(this.end))
96-
thrownewERR_OUT_OF_RANGE('end','an integer',this.end);
97-
thrownewERR_OUT_OF_RANGE('end','>= 0 and <= 2 ** 53 - 1',this.end);
98-
}
99-
100-
if(this.end<0){
101-
thrownewERR_OUT_OF_RANGE('end','>= 0 and <= 2 ** 53 - 1',this.end);
102-
}
89+
checkPosition(this.end,'end');
10390

10491
if(this.start!==undefined&&this.start>this.end){
10592
thrownewERR_OUT_OF_RANGE(

0 commit comments

Comments
 (0)