Skip to content

Commit

Permalink
fs: Replace checkPosition with validateInteger
Browse files Browse the repository at this point in the history
  • Loading branch information
rickyes committed May 21, 2020
1 parent cd4985c commit 9250a5c
Show file tree
Hide file tree
Showing 2 changed files with 6 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 @@ -16,7 +14,7 @@ const {
ERR_STREAM_DESTROYED
} = require('internal/errors').codes;
const { deprecate } = require('internal/util');
const { validateNumber } = require('internal/validators');
const { validateInteger } = require('internal/validators');
const fs = require('fs');
const { Buffer } = require('buffer');
const {
Expand Down Expand Up @@ -47,19 +45,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 @@ -346,7 +331,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
4 changes: 2 additions & 2 deletions test/parallel/test-file-write-stream3.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ const run_test_4 = common.mustCall(function() {
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 && <= 9007199254740991. Received -5',
name: 'RangeError'
};
assert.throws(fn, err);
Expand All @@ -200,7 +200,7 @@ const run_test_5 = common.mustCall(function() {
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 && <= 9007199254740991. Received 9_007_199_254_740_992',
name: 'RangeError'
};
assert.throws(fn, err);
Expand Down

0 comments on commit 9250a5c

Please sign in to comment.