diff --git a/lib/fs.js b/lib/fs.js index 0025e6a69de2ef..cf97eb236da02d 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1615,6 +1615,11 @@ function ReadStream(path, options) { if (!(this instanceof ReadStream)) return new ReadStream(path, options); + if (typeof options === 'string') + options = { encoding: options }; + else if (options && typeof options !== 'object') + throw new TypeError('Bad arguments'); + // a little bit bigger buffer and water marks by default options = Object.create(options || {}); if (options.highWaterMark === undefined) @@ -1781,6 +1786,11 @@ function WriteStream(path, options) { if (!(this instanceof WriteStream)) return new WriteStream(path, options); + if (typeof options === 'string') + options = { encoding: options }; + else if (options && typeof options !== 'object') + throw new TypeError('Bad arguments'); + options = options || {}; Writable.call(this, options); diff --git a/test/parallel/test-fs-read-stream-throw-type-error.js b/test/parallel/test-fs-read-stream-throw-type-error.js new file mode 100644 index 00000000000000..0bc7ccc3b2a3e1 --- /dev/null +++ b/test/parallel/test-fs-read-stream-throw-type-error.js @@ -0,0 +1,8 @@ +const common = require('../common'); +const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); + +const example = path.join(common.fixturesDir, 'x.txt'); +assert.throws(function() { fs.createReadStream(example, 123); }, TypeError); +assert.throws(function() { fs.createReadStream(example, true); }, TypeError); diff --git a/test/parallel/test-fs-write-stream-throw-type-error.js b/test/parallel/test-fs-write-stream-throw-type-error.js new file mode 100644 index 00000000000000..45500db17e6e57 --- /dev/null +++ b/test/parallel/test-fs-write-stream-throw-type-error.js @@ -0,0 +1,8 @@ +const common = require('../common'); +const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); + +const example = path.join(common.tmpDir, '/dummy'); +assert.throws(function() { fs.createReadStream(example, 123); }, TypeError); +assert.throws(function() { fs.createReadStream(example, true); }, TypeError);