1'use strict'; 2require('../common'); 3const fixtures = require('../common/fixtures'); 4const assert = require('assert'); 5const fs = require('fs'); 6 7// This test ensures that appropriate TypeError is thrown by createReadStream 8// when an argument with invalid type is passed 9 10const example = fixtures.path('x.txt'); 11// Should not throw. 12fs.createReadStream(example, undefined); 13fs.createReadStream(example, null); 14fs.createReadStream(example, 'utf8'); 15fs.createReadStream(example, { encoding: 'utf8' }); 16 17const createReadStreamErr = (path, opt, error) => { 18 assert.throws(() => { 19 fs.createReadStream(path, opt); 20 }, error); 21}; 22 23const typeError = { 24 code: 'ERR_INVALID_ARG_TYPE', 25 name: 'TypeError' 26}; 27 28const rangeError = { 29 code: 'ERR_OUT_OF_RANGE', 30 name: 'RangeError' 31}; 32 33[123, 0, true, false].forEach((opts) => 34 createReadStreamErr(example, opts, typeError) 35); 36 37// Case 0: Should not throw if either start or end is undefined 38[{}, { start: 0 }, { end: Infinity }].forEach((opts) => 39 fs.createReadStream(example, opts) 40); 41 42// Case 1: Should throw TypeError if either start or end is not of type 'number' 43[ 44 { start: 'invalid' }, 45 { end: 'invalid' }, 46 { start: 'invalid', end: 'invalid' }, 47].forEach((opts) => createReadStreamErr(example, opts, typeError)); 48 49// Case 2: Should throw RangeError if either start or end is NaN 50[{ start: NaN }, { end: NaN }, { start: NaN, end: NaN }].forEach((opts) => 51 createReadStreamErr(example, opts, rangeError) 52); 53 54// Case 3: Should throw RangeError if either start or end is negative 55[{ start: -1 }, { end: -1 }, { start: -1, end: -1 }].forEach((opts) => 56 createReadStreamErr(example, opts, rangeError) 57); 58 59// Case 4: Should throw RangeError if either start or end is fractional 60[{ start: 0.1 }, { end: 0.1 }, { start: 0.1, end: 0.1 }].forEach((opts) => 61 createReadStreamErr(example, opts, rangeError) 62); 63 64// Case 5: Should not throw if both start and end are whole numbers 65fs.createReadStream(example, { start: 1, end: 5 }); 66 67// Case 6: Should throw RangeError if start is greater than end 68createReadStreamErr(example, { start: 5, end: 1 }, rangeError); 69 70// Case 7: Should throw RangeError if start or end is not safe integer 71const NOT_SAFE_INTEGER = 2 ** 53; 72[ 73 { start: NOT_SAFE_INTEGER, end: Infinity }, 74 { start: 0, end: NOT_SAFE_INTEGER }, 75].forEach((opts) => 76 createReadStreamErr(example, opts, rangeError) 77); 78