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

File tree

1 file changed

+15
-28
lines changed

1 file changed

+15
-28
lines changed

lib/internal/fs/streams.js

+15-28
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+
function checkPosition(pos, name) {
42+
if (!Number.isSafeInteger(pos)) {
43+
validateNumber(pos, name);
44+
if (!Number.isInteger(pos))
45+
throw new ERR_OUT_OF_RANGE(name, 'an integer', pos);
46+
throw new ERR_OUT_OF_RANGE(name, '>= 0 and <= 2 ** 53 - 1', pos);
47+
}
48+
if (pos < 0) {
49+
throw new ERR_OUT_OF_RANGE(name, '>= 0 and <= 2 ** 53 - 1', pos);
50+
}
51+
}
52+
4053
function ReadStream(path, options) {
4154
if (!(this instanceof ReadStream))
4255
return new ReadStream(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-
throw new ERR_OUT_OF_RANGE('start', 'an integer', this.start);
72-
throw new ERR_OUT_OF_RANGE(
73-
'start',
74-
'>= 0 and <= 2 ** 53 - 1',
75-
this.start
76-
);
77-
}
78-
if (this.start < 0) {
79-
throw new ERR_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
} else if (this.end !== Infinity) {
92-
if (!Number.isSafeInteger(this.end)) {
93-
if (typeof this.end !== 'number')
94-
throw new ERR_INVALID_ARG_TYPE('end', 'number', this.end);
95-
if (!Number.isInteger(this.end))
96-
throw new ERR_OUT_OF_RANGE('end', 'an integer', this.end);
97-
throw new ERR_OUT_OF_RANGE('end', '>= 0 and <= 2 ** 53 - 1', this.end);
98-
}
99-
100-
if (this.end < 0) {
101-
throw new ERR_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
throw new ERR_OUT_OF_RANGE(

0 commit comments

Comments
 (0)