diff options
author | ZYSzys <17367077526@163.com> | 2018-12-29 16:30:19 +0800 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-01-09 01:37:42 +0100 |
commit | 98ed23cfaae3a82a02826fe020776cc3bee4c181 (patch) | |
tree | dcafdb1a14d6a79740ab521fe40465a86e94ce6c /lib/internal | |
parent | 519a11b24fa453e5cefe13df10ab9696616b5b91 (diff) | |
download | android-node-v8-98ed23cfaae3a82a02826fe020776cc3bee4c181.tar.gz android-node-v8-98ed23cfaae3a82a02826fe020776cc3bee4c181.tar.bz2 android-node-v8-98ed23cfaae3a82a02826fe020776cc3bee4c181.zip |
fs: extract start and end check into checkPosition
PR-URL: https://github.com/nodejs/node/pull/25264
Reviewed-By: Weijia Wang <starkwang@126.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Diffstat (limited to 'lib/internal')
-rw-r--r-- | lib/internal/fs/streams.js | 43 |
1 files changed, 15 insertions, 28 deletions
diff --git a/lib/internal/fs/streams.js b/lib/internal/fs/streams.js index 98d256f696..501c9ee4d2 100644 --- a/lib/internal/fs/streams.js +++ b/lib/internal/fs/streams.js @@ -36,6 +36,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); @@ -64,23 +77,7 @@ 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; } @@ -88,17 +85,7 @@ function ReadStream(path, options) { 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( |