Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: Replace checkPosition with validateInteger #33277

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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