From 9250a5c680e281b25c45d7ab60bd31bb2eeeb8dc Mon Sep 17 00:00:00 2001 From: rickyes Date: Thu, 7 May 2020 15:15:23 +0800 Subject: [PATCH] fs: Replace checkPosition with validateInteger --- lib/internal/fs/streams.js | 23 ++++------------------- test/parallel/test-file-write-stream3.js | 4 ++-- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/lib/internal/fs/streams.js b/lib/internal/fs/streams.js index 343e577049d9b2..9ce21721faad80 100644 --- a/lib/internal/fs/streams.js +++ b/lib/internal/fs/streams.js @@ -3,8 +3,6 @@ const { Array, MathMin, - NumberIsInteger, - NumberIsSafeInteger, ObjectDefineProperty, ObjectSetPrototypeOf, Symbol, @@ -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 { @@ -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. } @@ -111,7 +96,7 @@ function ReadStream(path, options) { this[kIsPerformingIO] = false; if (this.start !== undefined) { - checkPosition(this.start, 'start'); + validateInteger(this.start, 'start', 0); this.pos = this.start; } @@ -119,7 +104,7 @@ function ReadStream(path, options) { 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( @@ -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; } diff --git a/test/parallel/test-file-write-stream3.js b/test/parallel/test-file-write-stream3.js index c5af446198d55c..5ee28d16a660dd 100644 --- a/test/parallel/test-file-write-stream3.js +++ b/test/parallel/test-file-write-stream3.js @@ -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); @@ -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);