Skip to content

Commit b675ea0

Browse files
rickyescodebytere
authored andcommitted
fs: replace checkPosition with validateInteger
PR-URL: #33277 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]>
1 parent 9018e92 commit b675ea0

File tree

2 files changed

+11
-21
lines changed

2 files changed

+11
-21
lines changed

lib/internal/fs/streams.js

+4-19
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
const {
44
Array,
55
MathMin,
6-
NumberIsInteger,
7-
NumberIsSafeInteger,
86
ObjectDefineProperty,
97
ObjectSetPrototypeOf,
108
Symbol,
@@ -16,7 +14,7 @@ const {
1614
ERR_STREAM_DESTROYED
1715
} = require('internal/errors').codes;
1816
const { deprecate } = require('internal/util');
19-
const { validateNumber } = require('internal/validators');
17+
const { validateInteger } = require('internal/validators');
2018
const fs = require('fs');
2119
const { Buffer } = require('buffer');
2220
const {
@@ -47,19 +45,6 @@ function allocNewPool(poolSize) {
4745
pool.used = 0;
4846
}
4947

50-
// Check the `this.start` and `this.end` of stream.
51-
function checkPosition(pos, name) {
52-
if (!NumberIsSafeInteger(pos)) {
53-
validateNumber(pos, name);
54-
if (!NumberIsInteger(pos))
55-
throw new ERR_OUT_OF_RANGE(name, 'an integer', pos);
56-
throw new ERR_OUT_OF_RANGE(name, '>= 0 and <= 2 ** 53 - 1', pos);
57-
}
58-
if (pos < 0) {
59-
throw new ERR_OUT_OF_RANGE(name, '>= 0 and <= 2 ** 53 - 1', pos);
60-
}
61-
}
62-
6348
function roundUpToMultipleOf8(n) {
6449
return (n + 7) & ~7; // Align to 8 byte boundary.
6550
}
@@ -111,15 +96,15 @@ function ReadStream(path, options) {
11196
this[kIsPerformingIO] = false;
11297

11398
if (this.start !== undefined) {
114-
checkPosition(this.start, 'start');
99+
validateInteger(this.start, 'start', 0);
115100

116101
this.pos = this.start;
117102
}
118103

119104
if (this.end === undefined) {
120105
this.end = Infinity;
121106
} else if (this.end !== Infinity) {
122-
checkPosition(this.end, 'end');
107+
validateInteger(this.end, 'end', 0);
123108

124109
if (this.start !== undefined && this.start > this.end) {
125110
throw new ERR_OUT_OF_RANGE(
@@ -346,7 +331,7 @@ function WriteStream(path, options) {
346331
this[kIsPerformingIO] = false;
347332

348333
if (this.start !== undefined) {
349-
checkPosition(this.start, 'start');
334+
validateInteger(this.start, 'start', 0);
350335

351336
this.pos = this.start;
352337
}

test/parallel/test-file-write-stream3.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,12 @@ const run_test_4 = common.mustCall(function() {
182182
const fn = () => {
183183
fs.createWriteStream(filepath, { start: -5, flags: 'r+' });
184184
};
185+
// Verify the range of values using a common integer verifier.
186+
// Limit Number.MAX_SAFE_INTEGER
185187
const err = {
186188
code: 'ERR_OUT_OF_RANGE',
187189
message: 'The value of "start" is out of range. ' +
188-
'It must be >= 0 and <= 2 ** 53 - 1. Received -5',
190+
`It must be >= 0 && <= ${Number.MAX_SAFE_INTEGER}. Received -5`,
189191
name: 'RangeError'
190192
};
191193
assert.throws(fn, err);
@@ -197,10 +199,13 @@ const run_test_5 = common.mustCall(function() {
197199
const fn = () => {
198200
fs.createWriteStream(filepath, { start: 2 ** 53, flags: 'r+' });
199201
};
202+
// Verify the range of values using a common integer verifier.
203+
// Limit Number.MAX_SAFE_INTEGER
200204
const err = {
201205
code: 'ERR_OUT_OF_RANGE',
202206
message: 'The value of "start" is out of range. It must be ' +
203-
'>= 0 and <= 2 ** 53 - 1. Received 9_007_199_254_740_992',
207+
`>= 0 && <= ${Number.MAX_SAFE_INTEGER}. ` +
208+
'Received 9_007_199_254_740_992',
204209
name: 'RangeError'
205210
};
206211
assert.throws(fn, err);

0 commit comments

Comments
 (0)