Skip to content

Commit 98ed23c

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 519a11b commit 98ed23c

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
@@ -36,6 +36,19 @@ function allocNewPool(poolSize) {
3636
pool.used = 0;
3737
}
3838

39+
// Check the `this.start` and `this.end` of stream.
40+
function checkPosition(pos, name) {
41+
if (!Number.isSafeInteger(pos)) {
42+
validateNumber(pos, name);
43+
if (!Number.isInteger(pos))
44+
throw new ERR_OUT_OF_RANGE(name, 'an integer', pos);
45+
throw new ERR_OUT_OF_RANGE(name, '>= 0 and <= 2 ** 53 - 1', pos);
46+
}
47+
if (pos < 0) {
48+
throw new ERR_OUT_OF_RANGE(name, '>= 0 and <= 2 ** 53 - 1', pos);
49+
}
50+
}
51+
3952
function ReadStream(path, options) {
4053
if (!(this instanceof ReadStream))
4154
return new ReadStream(path, options);
@@ -64,41 +77,15 @@ function ReadStream(path, options) {
6477
this.closed = false;
6578

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

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

8885
if (this.end === undefined) {
8986
this.end = Infinity;
9087
} else if (this.end !== Infinity) {
91-
if (!Number.isSafeInteger(this.end)) {
92-
if (typeof this.end !== 'number')
93-
throw new ERR_INVALID_ARG_TYPE('end', 'number', this.end);
94-
if (!Number.isInteger(this.end))
95-
throw new ERR_OUT_OF_RANGE('end', 'an integer', this.end);
96-
throw new ERR_OUT_OF_RANGE('end', '>= 0 and <= 2 ** 53 - 1', this.end);
97-
}
98-
99-
if (this.end < 0) {
100-
throw new ERR_OUT_OF_RANGE('end', '>= 0 and <= 2 ** 53 - 1', this.end);
101-
}
88+
checkPosition(this.end, 'end');
10289

10390
if (this.start !== undefined && this.start > this.end) {
10491
throw new ERR_OUT_OF_RANGE(

0 commit comments

Comments
 (0)