Skip to content

Commit

Permalink
fs: extract start and end check into checkPosition
Browse files Browse the repository at this point in the history
PR-URL: #25264
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
ZYSzys authored and addaleax committed Jan 9, 2019
1 parent fc22df9 commit ca7adca
Showing 1 changed file with 15 additions and 28 deletions.
43 changes: 15 additions & 28 deletions lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ function allocNewPool(poolSize) {
pool.used = 0;
}

// Check the `this.start` and `this.end` of stream.
function checkPosition(pos, name) {
if (!Number.isSafeInteger(pos)) {
validateNumber(pos, name);
if (!Number.isInteger(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 ReadStream(path, options) {
if (!(this instanceof ReadStream))
return new ReadStream(path, options);
Expand Down Expand Up @@ -65,41 +78,15 @@ function ReadStream(path, options) {
this.closed = false;

if (this.start !== undefined) {
if (!Number.isSafeInteger(this.start)) {
validateNumber(this.start, 'start');
if (!Number.isInteger(this.start))
throw new ERR_OUT_OF_RANGE('start', 'an integer', this.start);
throw new ERR_OUT_OF_RANGE(
'start',
'>= 0 and <= 2 ** 53 - 1',
this.start
);
}
if (this.start < 0) {
throw new ERR_OUT_OF_RANGE(
'start',
'>= 0 and <= 2 ** 53 - 1',
this.start
);
}
checkPosition(this.start, 'start');

this.pos = this.start;
}

if (this.end === undefined) {
this.end = Infinity;
} else if (this.end !== Infinity) {
if (!Number.isSafeInteger(this.end)) {
if (typeof this.end !== 'number')
throw new ERR_INVALID_ARG_TYPE('end', 'number', this.end);
if (!Number.isInteger(this.end))
throw new ERR_OUT_OF_RANGE('end', 'an integer', this.end);
throw new ERR_OUT_OF_RANGE('end', '>= 0 and <= 2 ** 53 - 1', this.end);
}

if (this.end < 0) {
throw new ERR_OUT_OF_RANGE('end', '>= 0 and <= 2 ** 53 - 1', this.end);
}
checkPosition(this.end, 'end');

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

0 comments on commit ca7adca

Please sign in to comment.