From b675ea0272f57acb1f19e4a53c900238724c271b 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 PR-URL: https://github.com/nodejs/node/pull/33277 Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell Reviewed-By: Yongsheng Zhang --- lib/internal/fs/streams.js | 23 ++++------------------- test/parallel/test-file-write-stream3.js | 9 +++++++-- 2 files changed, 11 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..b68fdbf3a1fbfc 100644 --- a/test/parallel/test-file-write-stream3.js +++ b/test/parallel/test-file-write-stream3.js @@ -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); @@ -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);