Skip to content

Commit

Permalink
fs: replace checkPosition with validateInteger
Browse files Browse the repository at this point in the history
PR-URL: #33277
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
  • Loading branch information
rickyes authored and codebytere committed Jul 8, 2020
1 parent 0837c2c commit 5fb1cc8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
23 changes: 4 additions & 19 deletions lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
const {
Array,
MathMin,
NumberIsInteger,
NumberIsSafeInteger,
ObjectDefineProperty,
ObjectSetPrototypeOf,
Symbol,
Expand All @@ -15,7 +13,7 @@ const {
ERR_OUT_OF_RANGE,
ERR_STREAM_DESTROYED
} = require('internal/errors').codes;
const { validateNumber } = require('internal/validators');
const { validateInteger } = require('internal/validators');
const fs = require('fs');
const { Buffer } = require('buffer');
const {
Expand Down Expand Up @@ -46,19 +44,6 @@ function allocNewPool(poolSize) {
pool.used = 0;
}

// Check the `this.start` and `this.end` of stream.
function checkPosition(pos, name) {
if (!NumberIsSafeInteger(pos)) {
validateNumber(pos, name);
if (!NumberIsInteger(pos))
throw new ERR_OUT_OF_RANGE(name, 'an integer', pos);
throw new ERR_OUT_OF_RANGE(name, '>= 0 and <= 2 ** 53 - 1', pos);
}
if (pos < 0) {
throw new ERR_OUT_OF_RANGE(name, '>= 0 and <= 2 ** 53 - 1', pos);
}
}

function roundUpToMultipleOf8(n) {
return (n + 7) & ~7; // Align to 8 byte boundary.
}
Expand Down Expand Up @@ -111,15 +96,15 @@ function ReadStream(path, options) {
this[kIsPerformingIO] = false;

if (this.start !== undefined) {
checkPosition(this.start, 'start');
validateInteger(this.start, 'start', 0);

this.pos = this.start;
}

if (this.end === undefined) {
this.end = Infinity;
} else if (this.end !== Infinity) {
checkPosition(this.end, 'end');
validateInteger(this.end, 'end', 0);

if (this.start !== undefined && this.start > this.end) {
throw new ERR_OUT_OF_RANGE(
Expand Down Expand Up @@ -339,7 +324,7 @@ function WriteStream(path, options) {
this[kIsPerformingIO] = false;

if (this.start !== undefined) {
checkPosition(this.start, 'start');
validateInteger(this.start, 'start', 0);

this.pos = this.start;
}
Expand Down
9 changes: 7 additions & 2 deletions test/parallel/test-file-write-stream3.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,12 @@ const run_test_4 = common.mustCall(function() {
const fn = () => {
fs.createWriteStream(filepath, { start: -5, flags: 'r+' });
};
// Verify the range of values using a common integer verifier.
// Limit Number.MAX_SAFE_INTEGER
const err = {
code: 'ERR_OUT_OF_RANGE',
message: 'The value of "start" is out of range. ' +
'It must be >= 0 and <= 2 ** 53 - 1. Received -5',
`It must be >= 0 && <= ${Number.MAX_SAFE_INTEGER}. Received -5`,
name: 'RangeError'
};
assert.throws(fn, err);
Expand All @@ -197,10 +199,13 @@ const run_test_5 = common.mustCall(function() {
const fn = () => {
fs.createWriteStream(filepath, { start: 2 ** 53, flags: 'r+' });
};
// Verify the range of values using a common integer verifier.
// Limit Number.MAX_SAFE_INTEGER
const err = {
code: 'ERR_OUT_OF_RANGE',
message: 'The value of "start" is out of range. It must be ' +
'>= 0 and <= 2 ** 53 - 1. Received 9_007_199_254_740_992',
`>= 0 && <= ${Number.MAX_SAFE_INTEGER}. ` +
'Received 9_007_199_254_740_992',
name: 'RangeError'
};
assert.throws(fn, err);
Expand Down

0 comments on commit 5fb1cc8

Please sign in to comment.